Browse Source

配置管理 后台功能

pixel 5 years ago
parent
commit
326699ab38

+ 28 - 23
QMPlusServer/config/config.go

@@ -7,46 +7,47 @@ import (
 )
 
 type Config struct {
-	MysqlAdmin   MysqlAdmin
-	Qiniu        Qiniu
-	CasbinConfig CasbinConfig
-	RedisAdmin   RedisAdmin
-	System       System
-	JWT          JWT
+	MysqlAdmin   MysqlAdmin `json:"mysqlAdmin"`
+	Qiniu        Qiniu      `json:"qiniu"`
+	CasbinConfig CasbinConfig  `json:"casbinConfig"`
+	RedisAdmin   RedisAdmin  `json:"redisAdmin"`
+	System       System  `json:"system"`
+	JWT          JWT  `json:"jwt"`
 }
 
-type System struct {
-	UseMultipoint bool
-	Env           string
+type System struct {    // 系统配置
+	UseMultipoint bool   `json:"useMultipoint"`
+	Env           string  `json:"env"`
 }
 
-type JWT struct {
-	SigningKey string
+type JWT struct {   // jwt签名
+	SigningKey string  `json:"signingKey"`
 }
 
-type CasbinConfig struct {
-	ModelPath string // casbin model地址配置
+type CasbinConfig struct {  //casbin配置
+	ModelPath string  `json:"modelPath"` // casbin model地址配置
 }
 
 type MysqlAdmin struct { // mysql admin 数据库配置
-	Username string
-	Password string
-	Path     string
-	Dbname   string
-	Config   string
+	Username string    `json:"username"`
+	Password string    `json:"password"`
+	Path     string    `json:"path"`
+	Dbname   string    `json:"dbname"`
+	Config   string    `json:"config"`
 }
 
 type RedisAdmin struct { // Redis admin 数据库配置
-	Addr     string
-	Password string
-	DB       int
+	Addr     string     `json:"addr"`
+	Password string     `json:"password"`
+	DB       int        `json:"db"`
 }
 type Qiniu struct { // 七牛 密钥配置
-	AccessKey string
-	SecretKey string
+	AccessKey string    `json:"accessKey"`
+	SecretKey string    `json:"secretKey"`
 }
 
 var GinVueAdminconfig Config
+var VTool *viper.Viper
 
 func init() {
 	v := viper.New()
@@ -60,8 +61,12 @@ func init() {
 	v.WatchConfig()
 	v.OnConfigChange(func(e fsnotify.Event) {
 		fmt.Println("Config file changed:", e.Name)
+		if err := v.Unmarshal(&GinVueAdminconfig); err != nil {
+			fmt.Println(err)
+		}
 	})
 	if err := v.Unmarshal(&GinVueAdminconfig); err != nil {
 		fmt.Println(err)
 	}
+	VTool = v
 }

+ 60 - 0
QMPlusServer/controller/api/sys_system.go

@@ -1 +1,61 @@
 package api
+
+import (
+	"fmt"
+	"gin-vue-admin/controller/servers"
+	"gin-vue-admin/model/sysModel"
+	"github.com/gin-gonic/gin"
+)
+
+// @Tags system
+// @Summary 获取配置文件内容
+// @Security ApiKeyAuth
+// @Produce  application/json
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
+// @Router /system/getSystemConfig [post]
+func GetSystemConfig(c *gin.Context) {
+	err, config := new(sysModel.System).GetSystemConfig()
+	if err != nil {
+		servers.ReportFormat(c, false, fmt.Sprintf("获取失败:%v", err), gin.H{})
+	} else {
+		servers.ReportFormat(c, true, "获取成功", gin.H{"config": config})
+	}
+}
+
+
+// @Tags system
+// @Summary 设置配置文件内容
+// @Security ApiKeyAuth
+// @Produce  application/json
+// @Param data body sysModel.System true
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
+// @Router /system/setSystemConfig [post]
+func SetSystemConfig(c *gin.Context) {
+	var sys sysModel.System
+	_ = c.ShouldBind(&sys)
+	err := sys.SetSystemConfig()
+	if err != nil {
+		servers.ReportFormat(c, false, fmt.Sprintf("设置失败:%v", err), gin.H{})
+	} else {
+		servers.ReportFormat(c, true, "设置成功", gin.H{})
+	}
+}
+
+
+// @Tags system
+// @Summary 设置配置文件内容
+// @Security ApiKeyAuth
+// @Produce  application/json
+// @Param data body sysModel.System true
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
+// @Router /system/ReloadSystem [post]
+func ReloadSystem(c *gin.Context) {
+	var sys sysModel.System
+	_ = c.ShouldBind(&sys)
+	err := sys.SetSystemConfig()
+	if err != nil {
+		servers.ReportFormat(c, false, fmt.Sprintf("设置失败:%v", err), gin.H{})
+	} else {
+		servers.ReportFormat(c, true, "设置成功", gin.H{})
+	}
+}

+ 1 - 0
QMPlusServer/init/initRouter/init_router.go

@@ -26,5 +26,6 @@ func InitRouter() *gin.Engine {
 	router.InitWorkflowRouter(ApiGroup)              // 工作流相关路由
 	router.InitCasbinRouter(ApiGroup)                // 权限相关路由
 	router.InitJwtRouter(ApiGroup)                   // jwt相关路由
+	router.InitSystemRouter(ApiGroup)				// system相关路由
 	return Router
 }

+ 16 - 4
QMPlusServer/model/sysModel/sys_system.go

@@ -1,11 +1,23 @@
 package sysModel
 
-import "gin-vue-admin/config"
+import (
+	"gin-vue-admin/config"
+	"gin-vue-admin/tools"
+)
 
 type System struct {
-	config config.Config
+	Config config.Config
 }
 
-func test(){
-
+func (s *System)GetSystemConfig()(err error,conf config.Config){
+	return nil,config.GinVueAdminconfig
 }
+
+func (s *System)SetSystemConfig()(err error){
+	confs:= tools.StructToMap(s.Config)
+	for k,v:= range confs {
+		config.VTool.Set(k,v)
+	}
+	err = config.VTool.WriteConfig()
+	return err
+}

+ 14 - 0
QMPlusServer/router/sys_system.go

@@ -1 +1,15 @@
 package router
+
+import (
+	"gin-vue-admin/controller/api"
+	"gin-vue-admin/middleware"
+	"github.com/gin-gonic/gin"
+)
+
+func InitSystemRouter(Router *gin.RouterGroup) {
+	UserRouter := Router.Group("system").Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
+	{
+		UserRouter.POST("getSystemConfig", api.GetSystemConfig)     // 获取配置文件内容
+		UserRouter.POST("setSystemConfig", api.SetSystemConfig)     // 设置配置文件内容
+	}
+}

+ 26 - 26
QMPlusServer/static/config/config.json

@@ -1,28 +1,28 @@
 {
-    "system": {
-        "useMultipoint": false,
-        "env": "develop"
-    },
-    "jwt": {
-        "signingKey": "qmPlus"
-    },
-    "mysqlAdmin": {
-        "username": "root",
-        "password": "Aa@6447985",
-        "path": "127.0.0.1:3306",
-        "dbname": "qmplus",
-        "config": "charset=utf8&parseTime=True&loc=Local"
-    },
-    "redisAdmin": {
-        "addr": "127.0.0.1:6379",
-        "password": "",
-        "DB": 0
-    },
-    "qiniu": {
-        "accessKey": "25j8dYBZ2wuiy0yhwShytjZDTX662b8xiFguwxzZ",
-        "secretKey": "pgdbqEsf7ooZh7W3xokP833h3dZ_VecFXPDeG5JY"
-    },
-    "casbinConfig": {
-        "modelPath": "./static/rbacmodel/rbac_model.conf"
-    }
+  "casbinconfig": {
+    "modelPath": "./static/rbacmodel/rbac_model.conf"
+  },
+  "jwt": {
+    "signingKey": "qmPlus"
+  },
+  "mysqladmin": {
+    "username": "root",
+    "password": "Aa@6447985",
+    "path": "127.0.0.1:3306",
+    "dbname": "qmplus",
+    "config": "charset=utf8\u0026parseTime=True\u0026loc=Local"
+  },
+  "qiniu": {
+    "accessKey": "25j8dYBZ2wuiy0yhwShytjZDTX662b8xiFguwxzZ",
+    "secretKey": "pgdbqEsf7ooZh7W3xokP833h3dZ_VecFXPDeG5JY"
+  },
+  "redisadmin": {
+    "addr": "127.0.0.1:6379",
+    "password": "",
+    "db": 0
+  },
+  "system": {
+    "useMultipoint": false,
+    "env": "develop"
+  }
 }

+ 14 - 0
QMPlusServer/tools/struct_to_map.go

@@ -1 +1,15 @@
 package tools
+
+import "reflect"
+
+// 利用反射将结构体转化为map
+func StructToMap(obj interface{}) map[string]interface{}{
+	obj1 := reflect.TypeOf(obj)
+	obj2 := reflect.ValueOf(obj)
+
+	var data = make(map[string]interface{})
+	for i := 0; i < obj1.NumField(); i++ {
+		data[obj1.Field(i).Name] = obj2.Field(i).Interface()
+	}
+	return data
+}