Browse Source

增加系统配置接口

kobe6258 4 months ago
parent
commit
e7dfd4ab36
4 changed files with 138 additions and 0 deletions
  1. 91 0
      controllers/sys_config.go
  2. 32 0
      models/sys_config.go
  3. 9 0
      routers/commentsRouter.go
  4. 6 0
      routers/router.go

+ 91 - 0
controllers/sys_config.go

@@ -0,0 +1,91 @@
+package controllers
+
+import (
+	"encoding/json"
+	"eta/eta_mini_crm_ht/models"
+	"fmt"
+)
+
+type SysConfigController struct {
+	BaseAuthController
+}
+
+type Config struct {
+	ConfigId   int
+	ConfigType string
+	Json       bool
+}
+
+const (
+	ConfigTypeInt  = "int"
+	ConfigTypeStr  = "string"
+	ConfigTypeByte = "byte"
+)
+const (
+	// configCode
+	PaymentWay = "paymentWay"
+)
+
+// SysConfigMap 用于存储错误码和错误信息的映射
+var SysConfigMap = map[string]*Config{
+	PaymentWay: {ConfigId: 1003, ConfigType: ConfigTypeStr, Json: true},
+}
+
+func GetConfig(code string) *Config {
+	return SysConfigMap[code]
+}
+
+// GetConfig
+// @Title 系统用户详情信息
+// @Description 用户详情信息
+// @Param   SysUserId   query   int  true       "系统用户id"
+// @Success 200 {object} models.LoginResp
+// @router /config [get]
+func (this *SysConfigController) GetConfig() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	ConfigKey := this.GetString("ConfigKey")
+
+	if ConfigKey == "" {
+		br.Msg = "系统参数错误"
+		br.ErrMsg = fmt.Sprintf("系统参数错误 <%d>", ConfigKey)
+		return
+	}
+	configMap := SysConfigMap[ConfigKey]
+	configId := configMap.ConfigId
+	if configId == 0 {
+		br.Msg = "系统参数错误"
+		br.ErrMsg = fmt.Sprintf("系统参数错误,系统配置不存在")
+		return
+	}
+	config, err := models.GetConfig(configId)
+	if err != nil {
+		br.Msg = "系统参数获取失败"
+		br.ErrMsg = "系统参数获取失败,Err" + err.Error()
+		return
+	}
+	var value interface{}
+	switch config.ConfigType {
+	case ConfigTypeInt:
+		value = config.ByteValue
+	case ConfigTypeByte:
+		value = config.IntValue
+	case ConfigTypeStr:
+		if configMap.Json {
+			var jsonMap map[string]interface{}
+			err = json.Unmarshal([]byte(config.StrValue), &jsonMap)
+			value = jsonMap
+		} else {
+			value = config.StrValue
+		}
+
+	}
+
+	br.Data = value
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "获取成功"
+}

+ 32 - 0
models/sys_config.go

@@ -0,0 +1,32 @@
+package models
+
+import (
+	"github.com/beego/beego/v2/client/orm"
+	"time"
+)
+
+// SysConfig 表示 sys_config 表的结构体
+type SysConfig struct {
+	ID          int       `gorm:"primaryKey;autoIncrement" json:"id"`
+	ConfigID    int       `gorm:"column:config_id" json:"config_id"`
+	ConfigName  string    `gorm:"column:config_name" json:"config_name"`
+	ConfigType  string    `gorm:"column:config_type;type:enum('string','int','byte')" json:"config_type"`
+	Deleted     int       `gorm:"column:deleted" json:"deleted"`
+	StrValue    string    `gorm:"column:str_value" json:"str_value"`
+	IntValue    int       `gorm:"column:int_value" json:"int_value"`
+	ByteValue   string    `gorm:"column:byte_value" json:"byte_value"`
+	CreatedTime time.Time `gorm:"column:created_time" json:"created_time"`
+	UpdatedTime time.Time `gorm:"column:updated_time;default:CURRENT_TIMESTAMP;autoUpdateTime" json:"updated_time"`
+}
+
+func (SysConfig) TableName() string {
+	return "sys_config"
+
+}
+
+func GetConfig(configId int) (config SysConfig, err error) {
+	o := orm.NewOrm()
+	sql := `select * from sys_config where config_id = ? and deleted=0`
+	err = o.Raw(sql, configId).QueryRow(&config)
+	return
+}

+ 9 - 0
routers/commentsRouter.go

@@ -477,6 +477,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_mini_crm_ht/controllers:SysConfigController"] = append(beego.GlobalControllerRouter["eta/eta_mini_crm_ht/controllers:SysConfigController"],
+        beego.ControllerComments{
+            Method: "GetConfig",
+            Router: `/config`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_mini_crm_ht/controllers:SysDepartmentController"] = append(beego.GlobalControllerRouter["eta/eta_mini_crm_ht/controllers:SysDepartmentController"],
         beego.ControllerComments{
             Method: "Add",

+ 6 - 0
routers/router.go

@@ -21,6 +21,12 @@ func init() {
 				&controllers.SysUserController{},
 			),
 		),
+		beego.NSNamespace("/sys",
+			beego.NSInclude(
+				&controllers.SysConfigController{},
+			),
+		),
+
 		beego.NSNamespace("/department",
 			beego.NSInclude(
 				&controllers.SysDepartmentController{},