crm_config.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_forum_admin/models"
  5. "eta/eta_forum_admin/models/system"
  6. "strings"
  7. )
  8. // CrmConfigController 基础配置
  9. type CrmConfigController struct {
  10. BaseAuthController
  11. }
  12. // Save
  13. // @Title 保存配置
  14. // @Description 保存配置
  15. // @Param request body map[string]interface{} true "type json string"
  16. // @Success 200 Ret=200 操作成功
  17. // @router /save [post]
  18. func (this *CrmConfigController) Save() {
  19. br := new(models.BaseResponse).Init()
  20. defer func() {
  21. if br.ErrMsg == "" {
  22. br.IsSendEmail = false
  23. }
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. sysUser := this.SysUser
  28. if sysUser == nil {
  29. br.Msg = "请登录"
  30. br.ErrMsg = "请登录,SysUser Is Empty"
  31. br.Ret = 408
  32. return
  33. }
  34. var req map[string]interface{}
  35. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  36. br.Msg = "参数解析异常!"
  37. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  38. return
  39. }
  40. // 获取配置信息
  41. list, e := system.GetCrmConfig()
  42. if e != nil {
  43. br.Msg = "保存失败"
  44. br.ErrMsg = "获取配置列表失败, Err: " + e.Error()
  45. return
  46. }
  47. confMap := make(map[string]*system.CrmConfig)
  48. for _, v := range list {
  49. if v.IsShow == 1 {
  50. confMap[v.ConfigCode] = v
  51. }
  52. }
  53. // 根据配置类型取值
  54. updates := make([]system.ConfUpdateItem, 0)
  55. for k, v := range req {
  56. // 过滤掉表中没有的key
  57. conf := confMap[k]
  58. if conf == nil {
  59. continue
  60. }
  61. str, ok := v.(string)
  62. if !ok {
  63. continue
  64. }
  65. str = strings.TrimSpace(str)
  66. if conf.Necessary == 1 && str == "" {
  67. br.Msg = conf.Remark + "不可为空"
  68. return
  69. }
  70. updates = append(updates, system.ConfUpdateItem{
  71. ConfigCode: k,
  72. ConfigValue: str,
  73. })
  74. }
  75. if len(updates) > 0 {
  76. if e = system.UpdateCrmConfigMulti(updates); e != nil {
  77. br.Msg = "保存失败"
  78. br.ErrMsg = "保存商家配置失败, Err: " + e.Error()
  79. return
  80. }
  81. }
  82. br.Ret = 200
  83. br.Success = true
  84. br.Msg = "操作成功"
  85. }
  86. // Fetch
  87. // @Title 获取配置
  88. // @Description 获取配置
  89. // @Success 200 Ret=200 获取成功
  90. // @router /fetch [get]
  91. func (this *CrmConfigController) Fetch() {
  92. br := new(models.BaseResponse).Init()
  93. defer func() {
  94. if br.ErrMsg == "" {
  95. br.IsSendEmail = false
  96. }
  97. this.Data["json"] = br
  98. this.ServeJSON()
  99. }()
  100. sysUser := this.SysUser
  101. if sysUser == nil {
  102. br.Msg = "请登录"
  103. br.ErrMsg = "请登录,SysUser Is Empty"
  104. br.Ret = 408
  105. return
  106. }
  107. // 获取配置信息
  108. tmpList, e := system.GetCrmConfig()
  109. if e != nil {
  110. br.Msg = "保存失败"
  111. br.ErrMsg = "获取配置列表失败, Err: " + e.Error()
  112. return
  113. }
  114. list := make(map[string]string)
  115. for _, v := range tmpList {
  116. if v.IsShow == 1 {
  117. list[v.ConfigCode] = v.ConfigValue
  118. }
  119. }
  120. br.Data = list
  121. br.Ret = 200
  122. br.Success = true
  123. br.Msg = "获取成功"
  124. }