diff --git a/config/config.go b/config/config.go index acb09b7..a1807d4 100644 --- a/config/config.go +++ b/config/config.go @@ -10,7 +10,21 @@ import ( const Dir = "config/" const AccountConf = Dir + "account.json" +const MysqlConf = Dir + "mysql.json" +func GetMysql() map[string]string { + var mysql map[string]string + isExist, _ := tools.IsFileExist(MysqlConf) + if !isExist { + return mysql + } + info, err := ioutil.ReadFile(MysqlConf) + if err != nil { + return mysql + } + err = json.Unmarshal(info, &mysql) + return mysql +} func GetAccount() map[string]string { var account map[string]string isExist, _ := tools.IsFileExist(AccountConf) diff --git a/controller/mysql.go b/controller/mysql.go index 0661fef..afd1593 100644 --- a/controller/mysql.go +++ b/controller/mysql.go @@ -1,20 +1,47 @@ package controller import ( + "fmt" "github.com/gin-gonic/gin" - "github.com/taoshihan1991/imaptool/tmpl" - "net/http" + "github.com/taoshihan1991/imaptool/config" + "github.com/taoshihan1991/imaptool/tools" + "os" ) func MysqlGetConf(c *gin.Context) { - + mysqlInfo:=config.GetMysql() c.JSON(200, gin.H{ "code": 200, "msg": "验证成功", + "result":mysqlInfo, }) } -func ActionMysqlSet(w http.ResponseWriter, r *http.Request) { - render := tmpl.NewSettingHtml(w) - render.SetLeft("setting_left") - render.SetBottom("setting_bottom") - render.Display("mysql_setting", render) +func MysqlSetConf(c *gin.Context) { + + mysqlServer:=c.PostForm("server") + mysqlPort:=c.PostForm("port") + mysqlDb:=c.PostForm("database") + mysqlUsername:=c.PostForm("username") + mysqlPassword:=c.PostForm("password") + isExist, _ := tools.IsFileExist(config.Dir) + if !isExist { + os.Mkdir(config.Dir, os.ModePerm) + } + fileConfig := config.MysqlConf + file, _ := os.OpenFile(fileConfig, os.O_RDWR|os.O_CREATE, os.ModePerm) + + format := `{ + "Server":"%s", + "Port":"%s", + "Database":"%s", + "Username":"%s", + "Password":"%s" } +` + data := fmt.Sprintf(format, mysqlServer,mysqlPort,mysqlDb,mysqlUsername,mysqlPassword) + file.WriteString(data) + + c.JSON(200, gin.H{ + "code": 200, + "msg": "操作成功", + }) +} \ No newline at end of file diff --git a/server.go b/server.go index c19eef3..396c4ce 100644 --- a/server.go +++ b/server.go @@ -35,7 +35,8 @@ func main() { engine.GET("/setting", tmpl.PageSetting) //设置mysql engine.GET("/setting_mysql", tmpl.PageSettingMysql) - engine.GET("/get_setting_mysql",middleware.JwtApiMiddleware, controller.MysqlGetConf) + engine.GET("/mysql",middleware.JwtApiMiddleware, controller.MysqlGetConf) + engine.POST("/mysql",middleware.JwtApiMiddleware, controller.MysqlSetConf) //------------------old code----------------------------- mux := &http.ServeMux{} //根路径 @@ -74,8 +75,6 @@ func main() { //mux.Handle("/chat_server", websocket.Handler(controller.ChatServer)) //获取在线用户 mux.HandleFunc("/chat_users", controller.ChatUsers) - //设置mysql - mux.HandleFunc("/setting_mysql", controller.ActionMysqlSet) //后台任务 controller.TimerSessFile() //监听端口 diff --git a/static/html/setting_bottom.html b/static/html/setting_bottom.html index ee0d270..5184a49 100644 --- a/static/html/setting_bottom.html +++ b/static/html/setting_bottom.html @@ -7,8 +7,8 @@ fullscreenLoading:true, openIndex:[1], account: { - username: Username, - password: Password, + username: "", + password: "", }, mysql: { server: "", @@ -65,17 +65,26 @@ let _this=this; this.$refs[formName].validate((valid) => { if (valid) { - $.post("/setting_mysql",_this.account,function(data){ - if(data.code==200){ - _this.$message({ - message: data.msg, - type: 'success' - }); - }else{ - _this.$message({ - message: data.msg, - type: 'error' - }); + let _this=this; + $.ajax({ + type:"POST", + url:"/mysql", + data:this.mysql, + headers:{ + "token":localStorage.getItem("token") + }, + success: function(data) { + if(data.code==200){ + _this.$message({ + message: data.msg, + type: 'success' + }); + }else{ + _this.$message({ + message: data.msg, + type: 'error' + }); + } } }); } else { @@ -108,11 +117,18 @@ let _this=this; $.ajax({ type:"get", - url:"/get_setting_mysql", + url:"/mysql", headers:{ "token":localStorage.getItem("token") }, success: function(data) { + if(data.result!=null){ + _this.mysql.username=data.result.Username; + _this.mysql.password=data.result.Password; + _this.mysql.database=data.result.Database; + _this.mysql.server=data.result.Server; + _this.mysql.port=data.result.Port; + } } }); diff --git a/static/html/setting_mysql.html b/static/html/setting_mysql.html new file mode 100644 index 0000000..e870f52 --- /dev/null +++ b/static/html/setting_mysql.html @@ -0,0 +1,38 @@ +{{template "header" }} +
+ + +
+ +{{template "setting_bottom" .}}