From 70998c424d4eadfce177fe2c3a5a84bee68176a5 Mon Sep 17 00:00:00 2001 From: wenjianzhang Date: Wed, 12 May 2021 18:47:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor=F0=9F=8E=A8=20=E4=BC=98=E5=8C=96Contex?= =?UTF-8?q?t=E8=AE=BE=E7=BD=AE=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/apis/sys_config.go | 4 +- app/admin/apis/tools/gen.go | 130 +++++++++++++------------- app/jobs/apis/sys_job.go | 4 +- app/other/apis/file.go | 4 +- app/other/apis/sys_china_area_data.go | 10 +- common/apis/api.go | 12 +-- 6 files changed, 79 insertions(+), 85 deletions(-) diff --git a/app/admin/apis/sys_config.go b/app/admin/apis/sys_config.go index fdb1d30..3b0466d 100644 --- a/app/admin/apis/sys_config.go +++ b/app/admin/apis/sys_config.go @@ -18,7 +18,7 @@ type SysConfig struct { } func (e SysConfig) GetSysConfigList(c *gin.Context) { - e.SetContext(c) //Context = c + e.MakeContext(c) log := e.GetLogger() d := new(dto.SysConfigSearch) db, err := e.GetOrm() @@ -272,4 +272,4 @@ func (e SysConfig) GetSysConfigByKEYForService(c *gin.Context) { return } e.OK(v, s.Msg) -} +} \ No newline at end of file diff --git a/app/admin/apis/tools/gen.go b/app/admin/apis/tools/gen.go index 2f4c52a..5292111 100644 --- a/app/admin/apis/tools/gen.go +++ b/app/admin/apis/tools/gen.go @@ -490,70 +490,70 @@ func (e Gen) GenMenuAndApi(c *gin.Context) { Amenu.UpdatedAt = timeNow Amenu.MenuId, err = Amenu.Create(db) - AList := models.SysApi{} - AList.Name = "" - AList.Title = "分页获取" + tab.TableComment - AList.Path = "/api/v1/" + tab.ModuleFrontName - AList.Action = "GET" - AList.ParentId = Amenu.MenuId - AList.Sort = 0 - AList.CreateBy = 1 - AList.UpdateBy = 1 - AList.CreatedAt = timeNow - AList.UpdatedAt = timeNow - AList.Id, err = AList.Create(db) - - AGet := models.SysApi{} - AList.Name = "" - AGet.Title = "根据id获取" + tab.TableComment - AGet.Path = "/api/v1/" + tab.ModuleFrontName + "/:id" - AGet.Action = "GET" - AGet.ParentId = Amenu.MenuId - AGet.Sort = 0 - AGet.CreateBy = 1 - AGet.UpdateBy = 1 - AGet.CreatedAt = timeNow - AGet.UpdatedAt = timeNow - AGet.Id, err = AGet.Create(db) - - ACreate := models.SysApi{} - AList.Name = "" - ACreate.Title = "创建" + tab.TableComment - ACreate.Path = "/api/v1/" + tab.ModuleFrontName - ACreate.Action = "POST" - ACreate.ParentId = Amenu.MenuId - ACreate.Sort = 0 - ACreate.CreateBy = 1 - ACreate.UpdateBy = 1 - ACreate.CreatedAt = timeNow - ACreate.UpdatedAt = timeNow - ACreate.Id, err = ACreate.Create(db) - - AUpdate := models.SysApi{} - AList.Name = "" - AUpdate.Title = "修改" + tab.TableComment - AUpdate.Path = "/api/v1/" + tab.ModuleFrontName + "/:id" - AUpdate.Action = "PUT" - AUpdate.ParentId = Amenu.MenuId - AUpdate.Sort = 0 - AUpdate.CreateBy = 1 - AUpdate.UpdateBy = 1 - AUpdate.CreatedAt = timeNow - AUpdate.UpdatedAt = timeNow - AUpdate.Id, err = AUpdate.Create(db) - - ADelete := models.SysApi{} - AList.Name = "" - ADelete.Title = "删除" + tab.TableComment - ADelete.Path = "/api/v1/" + tab.ModuleFrontName - ADelete.Action = "DELETE" - ADelete.ParentId = Amenu.MenuId - ADelete.Sort = 0 - ADelete.CreateBy = 1 - ADelete.UpdateBy = 1 - ADelete.CreatedAt = timeNow - ADelete.UpdatedAt = timeNow - ADelete.Id, err = ADelete.Create(db) + //AList := models.SysApi{} + //AList.Name = "" + //AList.Title = "分页获取" + tab.TableComment + //AList.Path = "/api/v1/" + tab.ModuleFrontName + //AList.Action = "GET" + //AList.ParentId = Amenu.MenuId + //AList.Sort = 0 + //AList.CreateBy = 1 + //AList.UpdateBy = 1 + //AList.CreatedAt = timeNow + //AList.UpdatedAt = timeNow + //AList.Id, err = AList.Create(db) + // + //AGet := models.SysApi{} + //AList.Name = "" + //AGet.Title = "根据id获取" + tab.TableComment + //AGet.Path = "/api/v1/" + tab.ModuleFrontName + "/:id" + //AGet.Action = "GET" + //AGet.ParentId = Amenu.MenuId + //AGet.Sort = 0 + //AGet.CreateBy = 1 + //AGet.UpdateBy = 1 + //AGet.CreatedAt = timeNow + //AGet.UpdatedAt = timeNow + //AGet.Id, err = AGet.Create(db) + // + //ACreate := models.SysApi{} + //AList.Name = "" + //ACreate.Title = "创建" + tab.TableComment + //ACreate.Path = "/api/v1/" + tab.ModuleFrontName + //ACreate.Action = "POST" + //ACreate.ParentId = Amenu.MenuId + //ACreate.Sort = 0 + //ACreate.CreateBy = 1 + //ACreate.UpdateBy = 1 + //ACreate.CreatedAt = timeNow + //ACreate.UpdatedAt = timeNow + //ACreate.Id, err = ACreate.Create(db) + // + //AUpdate := models.SysApi{} + //AList.Name = "" + //AUpdate.Title = "修改" + tab.TableComment + //AUpdate.Path = "/api/v1/" + tab.ModuleFrontName + "/:id" + //AUpdate.Action = "PUT" + //AUpdate.ParentId = Amenu.MenuId + //AUpdate.Sort = 0 + //AUpdate.CreateBy = 1 + //AUpdate.UpdateBy = 1 + //AUpdate.CreatedAt = timeNow + //AUpdate.UpdatedAt = timeNow + //AUpdate.Id, err = AUpdate.Create(db) + // + //ADelete := models.SysApi{} + //AList.Name = "" + //ADelete.Title = "删除" + tab.TableComment + //ADelete.Path = "/api/v1/" + tab.ModuleFrontName + //ADelete.Action = "DELETE" + //ADelete.ParentId = Amenu.MenuId + //ADelete.Sort = 0 + //ADelete.CreateBy = 1 + //ADelete.UpdateBy = 1 + //ADelete.CreatedAt = timeNow + //ADelete.UpdatedAt = timeNow + //ADelete.Id, err = ADelete.Create(db) e.OK("", "数据生成成功!") -} +} \ No newline at end of file diff --git a/app/jobs/apis/sys_job.go b/app/jobs/apis/sys_job.go index 6f22db3..6069905 100644 --- a/app/jobs/apis/sys_job.go +++ b/app/jobs/apis/sys_job.go @@ -46,7 +46,7 @@ func (e SysJob) RemoveJobForService(c *gin.Context) { // StartJobForService 启动job service实现 func (e SysJob) StartJobForService(c *gin.Context) { - e.SetContext(c) + e.MakeContext(c) log := e.GetLogger() db, err := e.GetOrm() if err != nil { @@ -71,4 +71,4 @@ func (e SysJob) StartJobForService(c *gin.Context) { return } e.OK(nil, s.Msg) -} +} \ No newline at end of file diff --git a/app/other/apis/file.go b/app/other/apis/file.go index 76e40e2..3848964 100644 --- a/app/other/apis/file.go +++ b/app/other/apis/file.go @@ -41,7 +41,7 @@ type File struct { // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /api/v1/public/uploadFile [post] func (e File) UploadFile(c *gin.Context) { - e.SetContext(c) + e.MakeContext(c) tag, _ := c.GetPostForm("type") urlPrefix := fmt.Sprintf("http://%s/", c.Request.Host) var fileResponse FileResponse @@ -195,4 +195,4 @@ func ossUpload(name string, path string) error { func qiniuUpload(name string, path string) error { oss := file_store.ALiYunOSS{} return oss.UpLoad(name, path) -} +} \ No newline at end of file diff --git a/app/other/apis/sys_china_area_data.go b/app/other/apis/sys_china_area_data.go index eb56e61..6df8461 100644 --- a/app/other/apis/sys_china_area_data.go +++ b/app/other/apis/sys_china_area_data.go @@ -18,7 +18,7 @@ type SysChinaAreaData struct { } func (e SysChinaAreaData) GetSysChinaAreaDataList(c *gin.Context) { - e.SetContext(c) + e.MakeContext(c) log := e.GetLogger() d := new(dto2.SysChinaAreaDataSearch) db, err := e.GetOrm() @@ -52,7 +52,7 @@ func (e SysChinaAreaData) GetSysChinaAreaDataList(c *gin.Context) { } func (e SysChinaAreaData) GetSysChinaAreaData(c *gin.Context) { - e.SetContext(c) + e.MakeContext(c) log := e.GetLogger() control := new(dto2.SysChinaAreaDataById) db, err := e.GetOrm() @@ -85,7 +85,7 @@ func (e SysChinaAreaData) GetSysChinaAreaData(c *gin.Context) { } func (e SysChinaAreaData) InsertSysChinaAreaData(c *gin.Context) { - e.SetContext(c) + e.MakeContext(c) log := e.GetLogger() control := new(dto2.SysChinaAreaDataControl) db, err := e.GetOrm() @@ -122,7 +122,7 @@ func (e SysChinaAreaData) InsertSysChinaAreaData(c *gin.Context) { } func (e SysChinaAreaData) UpdateSysChinaAreaData(c *gin.Context) { - e.SetContext(c) + e.MakeContext(c) log := e.GetLogger() control := new(dto2.SysChinaAreaDataControl) db, err := e.GetOrm() @@ -159,7 +159,7 @@ func (e SysChinaAreaData) UpdateSysChinaAreaData(c *gin.Context) { } func (e SysChinaAreaData) DeleteSysChinaAreaData(c *gin.Context) { - e.SetContext(c) + e.MakeContext(c) log := e.GetLogger() control := new(dto2.SysChinaAreaDataById) db, err := e.GetOrm() diff --git a/common/apis/api.go b/common/apis/api.go index ba93ecd..b4d977a 100644 --- a/common/apis/api.go +++ b/common/apis/api.go @@ -18,7 +18,8 @@ type Api struct { Logger *logger.Logger } -func (e Api) SetContext(c *gin.Context) { +// MakeContext 设置http上下文 +func (e *Api) MakeContext(c *gin.Context) { fmt.Println(&c) e.Context = c fmt.Println(&e.Context) @@ -30,13 +31,6 @@ func (e Api) GetLogger() *logger.Logger { return api.GetRequestLogger(e.Context) } -//func (e Api) GetLogger() (*logger.Logger, error) { -// if e.Context == nil { -// return nil, errors.New("API context cannot be nil") -// } -// return api.GetRequestLogger(e.Context), nil -//} - func (e Api) Bind(d interface{}, bindings ...binding.Binding) error { var err error for i := range bindings { @@ -99,4 +93,4 @@ func (e Api) PageOK(result interface{}, count int, pageIndex int, pageSize int, // Custom 兼容函数 func (e Api) Custom(data gin.H) { response.Custum(e.Context, data) -} +} \ No newline at end of file