From 2596e46821795b372fe80ef734c02f53def3ef38 Mon Sep 17 00:00:00 2001 From: taoshihan1991 <630892807@qq.com> Date: Fri, 25 Sep 2020 14:44:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE=E9=80=89?= =?UTF-8?q?=E9=A1=B9=E5=8F=82=E6=95=B0=E5=90=8E=E5=8F=B0=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/setting.go | 76 +++++++++++---------------------- models/configs.go | 7 ++- router/api.go | 2 + router/view.go | 1 + static/html/setting_bottom.html | 15 +++++++ static/html/setting_config.html | 36 ++++++++++++++++ static/html/setting_left.html | 1 + tmpl/setting.go | 11 +---- 8 files changed, 86 insertions(+), 63 deletions(-) create mode 100644 static/html/setting_config.html diff --git a/controller/setting.go b/controller/setting.go index 1ed7068..5dd559f 100644 --- a/controller/setting.go +++ b/controller/setting.go @@ -1,61 +1,33 @@ package controller import ( - "encoding/json" - "fmt" - "github.com/taoshihan1991/imaptool/config" - "github.com/taoshihan1991/imaptool/tmpl" - "github.com/taoshihan1991/imaptool/tools" - "net/http" - "os" + "github.com/gin-gonic/gin" + "github.com/taoshihan1991/imaptool/models" ) -func ActionSetting(w http.ResponseWriter, r *http.Request) { - render := tmpl.NewSettingHtml(w) - render.SetLeft("setting_left") - render.SetBottom("setting_bottom") - account := config.GetAccount() - render.Username = account["Username"] - render.Password = account["Password"] - render.Display("setting", render) -} -func SettingAccount(w http.ResponseWriter, r *http.Request) { - w.Header().Set("content-type", "text/json;charset=utf-8;") - - username := r.PostFormValue("username") - password := r.PostFormValue("password") - - isExist, _ := tools.IsFileExist(config.Dir) - if !isExist { - os.Mkdir(config.Dir, os.ModePerm) - } - fileConfig := config.AccountConf - file, _ := os.OpenFile(fileConfig, os.O_RDWR|os.O_CREATE, os.ModePerm) - - format := `{ - "Username":"%s", - "Password":"%s" -} -` - data := fmt.Sprintf(format, username, password) - file.WriteString(data) - - msg, _ := json.Marshal(tools.JsonResult{Code: 200, Msg: "操作成功!"}) - w.Write(msg) +func GetConfigs(c *gin.Context) { + configs:=models.FindConfigs() + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + "result":configs, + }) } -func SettingGetAccount(w http.ResponseWriter, r *http.Request) { - w.Header().Set("content-type", "text/json;charset=utf-8;") - mailServer := tools.GetMailServerFromCookie(r) - - if mailServer == nil { - msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"}) - w.Write(msg) +func PostConfig(c *gin.Context){ + key:=c.PostForm("key") + value:=c.PostForm("value") + if key==""||value==""{ + c.JSON(200, gin.H{ + "code": 400, + "msg": "error", + }) return } - result := config.GetAccount() - msg, _ := json.Marshal(tools.JsonListResult{ - JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"}, - Result: result, + models.UpdateConfig(key,value) + + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + "result":"", }) - w.Write(msg) -} +} \ No newline at end of file diff --git a/models/configs.go b/models/configs.go index a197154..d58e1f2 100644 --- a/models/configs.go +++ b/models/configs.go @@ -7,7 +7,12 @@ type Config struct{ ConfKey string `json:"conf_key"` ConfValue string `json:"conf_value"` } - +func UpdateConfig(key string,value string){ + c:=&Config{ + ConfValue: value, + } + DB.Model(c).Where("conf_key = ?",key).Update(c) +} func FindConfigs()[]Config{ var config []Config DB.Find(&config) diff --git a/router/api.go b/router/api.go index 533717a..bf157fc 100644 --- a/router/api.go +++ b/router/api.go @@ -54,6 +54,8 @@ func InitApiRouter(engine *gin.Engine){ engine.POST("/ipblack",middleware.JwtApiMiddleware,controller.PostIpblack) engine.DELETE("/ipblack",middleware.JwtApiMiddleware,controller.DelIpblack) engine.GET("/ipblacks_all",middleware.JwtApiMiddleware,controller.GetIpblacks) + engine.GET("/configs",middleware.JwtApiMiddleware,middleware.RbacAuth,controller.GetConfigs) + engine.POST("/config",middleware.JwtApiMiddleware,middleware.RbacAuth,controller.PostConfig) //微信接口 engine.GET("/micro_program",controller.GetCheckWeixinSign) } \ No newline at end of file diff --git a/router/view.go b/router/view.go index cdf38c5..788c46e 100644 --- a/router/view.go +++ b/router/view.go @@ -20,6 +20,7 @@ func InitViewRouter(engine *gin.Engine){ engine.GET("/setting_deploy", tmpl.PageSettingDeploy) engine.GET("/setting_kefu_list",tmpl.PageKefuList) engine.GET("/setting_ipblack",tmpl.PageIpblack) + engine.GET("/setting_config",tmpl.PageConfig) engine.GET("/mail_list", tmpl.PageMailList) engine.GET("/roles_list", tmpl.PageRoleList) engine.GET("/webjs", tmpl.PageWebJs) diff --git a/static/html/setting_bottom.html b/static/html/setting_bottom.html index 572340a..ee5a860 100644 --- a/static/html/setting_bottom.html +++ b/static/html/setting_bottom.html @@ -67,6 +67,7 @@ role_id:"", }, roleList:[], + configList:[], roleDialog:false, noticeList:[], welcomeDialog:false, @@ -200,6 +201,11 @@ _this.ipblackList=result.list; }); } + if(ACTION=="setting_config"){ + this.sendAjax("/configs","get",{},function(result){ + _this.configList=result; + }); + } }, sendAjax(url,method,params,callback){ let _this=this; @@ -327,6 +333,15 @@ this.roleForm.path=path this.roleDialog=true; }, + //设置配置项 + setConfigItem(key,value){ + let _this=this; + this.sendAjax("/config","POST",{key:key,value:value},function(result){ + _this.sendAjax("/configs","get",{},function(result){ + _this.configList=result; + }); + }); + }, //提交角色表单 submitRoleForm(formName){ let _this=this; diff --git a/static/html/setting_config.html b/static/html/setting_config.html new file mode 100644 index 0000000..7ec523a --- /dev/null +++ b/static/html/setting_config.html @@ -0,0 +1,36 @@ +{{template "header" }} +
+ + +
+ +{{template "setting_bottom" .}} diff --git a/static/html/setting_left.html b/static/html/setting_left.html index f8d6f2a..1d65d9c 100644 --- a/static/html/setting_left.html +++ b/static/html/setting_left.html @@ -35,6 +35,7 @@ 设置imap 设置登陆账号 设置mysql + 配置参数 IP黑名单 设置网页部署 diff --git a/tmpl/setting.go b/tmpl/setting.go index 4742c17..012b7f9 100644 --- a/tmpl/setting.go +++ b/tmpl/setting.go @@ -67,14 +67,5 @@ func PageConfig(c *gin.Context) { "action":"setting_config", }) } -type SettingHtml struct { - *CommonHtml - Username, Password string -} -func NewSettingHtml(w http.ResponseWriter) *SettingHtml { - obj := new(SettingHtml) - parent := NewRender(w) - obj.CommonHtml = parent - return obj -} +