保存mysql设置

pull/30/head
陶士涵 4 years ago
parent 208541b9b3
commit 7886dad432

@ -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)

@ -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": "操作成功",
})
}

@ -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()
//监听端口

@ -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;
}
}
});

@ -0,0 +1,38 @@
{{template "header" }}
<div id="app" style="width:100%">
<template>
<el-container v-loading.fullscreen.lock="fullscreenLoading">
<el-aside>
{{template "setting_left" .}}
</el-aside>
<el-main class="mainMain">
<el-form :model="mysql" :rules="rules" ref="mysql" label-width="120px">
<el-form-item label="服务地址" prop="server">
<el-input v-model="mysql.server"></el-input>
</el-form-item>
<el-form-item label="端口" prop="port">
<el-input v-model="mysql.port"></el-input>
</el-form-item>
<el-form-item label="数据库名" prop="database">
<el-input v-model="mysql.database"></el-input>
</el-form-item>
<el-form-item label="用户名" prop="username">
<el-input v-model="mysql.username"></el-input>
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input v-model="mysql.password"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="setMysql('mysql')">立即创建</el-button>
<el-button @click="resetForm('mysql')">取消</el-button>
</el-form-item>
</el-form>
</el-main>
</el-container>
</template>
</div>
</body>
{{template "setting_bottom" .}}
Loading…
Cancel
Save