sys_config.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_crm_ht/models"
  5. "fmt"
  6. )
  7. type SysConfigController struct {
  8. BaseAuthController
  9. }
  10. type Config struct {
  11. ConfigId int
  12. ConfigType string
  13. Json bool
  14. }
  15. const (
  16. ConfigTypeInt = "int"
  17. ConfigTypeStr = "string"
  18. ConfigTypeByte = "byte"
  19. )
  20. const (
  21. // configCode
  22. PaymentWayCode string = "paymentWay"
  23. )
  24. // SysConfigMap 用于存储错误码和错误信息的映射
  25. var SysConfigMap = map[string]*Config{
  26. PaymentWayCode: {ConfigId: 1003, ConfigType: ConfigTypeStr, Json: true},
  27. }
  28. func GetConfig(code string) *Config {
  29. return SysConfigMap[code]
  30. }
  31. // GetConfig
  32. // @Title 系统用户详情信息
  33. // @Description 用户详情信息
  34. // @Param SysUserId query int true "系统用户id"
  35. // @Success 200 {object} models.LoginResp
  36. // @router /config [get]
  37. func (this *SysConfigController) GetConfig() {
  38. br := new(models.BaseResponse).Init()
  39. defer func() {
  40. this.Data["json"] = br
  41. this.ServeJSON()
  42. }()
  43. ConfigKey := this.GetString("ConfigKey")
  44. if ConfigKey == "" {
  45. br.Msg = "系统参数错误"
  46. br.ErrMsg = fmt.Sprintf("系统参数错误 <%d>", ConfigKey)
  47. return
  48. }
  49. configMap := SysConfigMap[ConfigKey]
  50. configId := configMap.ConfigId
  51. if configId == 0 {
  52. br.Msg = "系统参数错误"
  53. br.ErrMsg = fmt.Sprintf("系统参数错误,系统配置不存在")
  54. return
  55. }
  56. config, err := models.GetConfig(configId)
  57. if err != nil {
  58. br.Msg = "系统参数获取失败"
  59. br.ErrMsg = "系统参数获取失败,Err" + err.Error()
  60. return
  61. }
  62. var value interface{}
  63. switch config.ConfigType {
  64. case ConfigTypeInt:
  65. value = config.ByteValue
  66. case ConfigTypeByte:
  67. value = config.IntValue
  68. case ConfigTypeStr:
  69. if configMap.Json {
  70. var jsonMap map[string]interface{}
  71. err = json.Unmarshal([]byte(config.StrValue), &jsonMap)
  72. value = jsonMap
  73. } else {
  74. value = config.StrValue
  75. }
  76. }
  77. br.Data = value
  78. br.Ret = 200
  79. br.Success = true
  80. br.Msg = "获取成功"
  81. }