business_conf.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "html"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // BusinessConfController 商家配置
  13. type BusinessConfController struct {
  14. BaseAuthController
  15. }
  16. type BusinessConfOpenController struct {
  17. BaseCommonController
  18. }
  19. // Save
  20. // @Title 保存配置
  21. // @Description 保存配置
  22. // @Param request body map[string]interface{} true "type json string"
  23. // @Success 200 Ret=200 操作成功
  24. // @router /save [post]
  25. func (this *BusinessConfController) Save() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. if br.ErrMsg == "" {
  29. br.IsSendEmail = false
  30. }
  31. this.Data["json"] = br
  32. this.ServeJSON()
  33. }()
  34. sysUser := this.SysUser
  35. if sysUser == nil {
  36. br.Msg = "请登录"
  37. br.ErrMsg = "请登录,SysUser Is Empty"
  38. br.Ret = 408
  39. return
  40. }
  41. var req map[string]interface{}
  42. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  43. br.Msg = "参数解析异常!"
  44. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  45. return
  46. }
  47. // 获取配置信息
  48. confOb := new(models.BusinessConf)
  49. list, e := confOb.GetItemsByCondition("", make([]interface{}, 0), []string{}, "")
  50. if e != nil {
  51. br.Msg = "保存失败"
  52. br.ErrMsg = "获取配置列表失败, Err: " + e.Error()
  53. return
  54. }
  55. confMap := make(map[string]*models.BusinessConf)
  56. for _, c := range list {
  57. confMap[c.ConfKey] = c
  58. }
  59. // 根据配置类型取值
  60. updates := make([]models.BusinessConfUpdate, 0)
  61. for k, v := range req {
  62. // 过滤掉表中没有的key
  63. conf := confMap[k]
  64. if conf == nil {
  65. continue
  66. }
  67. switch conf.ValType {
  68. case 1: // 字符串
  69. str, ok := v.(string)
  70. if !ok {
  71. continue
  72. }
  73. str = strings.TrimSpace(str)
  74. if conf.Necessary == 1 && str == "" {
  75. br.Msg = conf.Remark + "不可为空"
  76. return
  77. }
  78. updates = append(updates, models.BusinessConfUpdate{
  79. ConfKey: k,
  80. ConfVal: str,
  81. })
  82. case 2: // 数值
  83. num, ok := v.(float64)
  84. if !ok {
  85. continue
  86. }
  87. if conf.Necessary == 1 && num <= 0 {
  88. br.Msg = conf.Remark + "不可为空"
  89. return
  90. }
  91. val := strconv.FormatFloat(num, 'f', 0, 64)
  92. updates = append(updates, models.BusinessConfUpdate{
  93. ConfKey: k,
  94. ConfVal: val,
  95. })
  96. case 3: // 字符串数组
  97. arr, ok := v.([]interface{})
  98. if !ok {
  99. continue
  100. }
  101. if conf.Necessary == 1 && len(arr) == 0 {
  102. br.Msg = conf.Remark + "不可为空"
  103. return
  104. }
  105. strArr := make([]string, 0)
  106. for _, a := range arr {
  107. if s, ok2 := a.(string); ok2 {
  108. strArr = append(strArr, s)
  109. }
  110. }
  111. val := strings.Join(strArr, ",")
  112. updates = append(updates, models.BusinessConfUpdate{
  113. ConfKey: k,
  114. ConfVal: val,
  115. })
  116. case 4: // 富文本
  117. content, ok := v.(string)
  118. if !ok {
  119. continue
  120. }
  121. content = strings.TrimSpace(content)
  122. if conf.Necessary == 1 && content == "" {
  123. br.Msg = conf.Remark + "不可为空"
  124. return
  125. }
  126. content = html.EscapeString(content)
  127. updates = append(updates, models.BusinessConfUpdate{
  128. ConfKey: k,
  129. ConfVal: content,
  130. })
  131. }
  132. }
  133. if len(updates) > 0 {
  134. if e = models.UpdateBusinessConfMulti(updates); e != nil {
  135. br.Msg = "保存失败"
  136. br.ErrMsg = "保存商家配置失败, Err: " + e.Error()
  137. return
  138. }
  139. }
  140. // 操作日志
  141. go func() {
  142. b, e := json.Marshal(req)
  143. if e != nil {
  144. return
  145. }
  146. recordOb := new(models.BusinessConfOperationRecord)
  147. recordOb.SysUserId = sysUser.AdminId
  148. recordOb.SysRealName = sysUser.RealName
  149. recordOb.Content = string(b)
  150. recordOb.CreateTime = time.Now().Local()
  151. _ = recordOb.Create()
  152. }()
  153. br.Ret = 200
  154. br.Success = true
  155. br.Msg = "操作成功"
  156. }
  157. // Fetch
  158. // @Title 获取配置
  159. // @Description 获取配置
  160. // @Success 200 Ret=200 获取成功
  161. // @router /fetch [get]
  162. func (this *BusinessConfController) Fetch() {
  163. br := new(models.BaseResponse).Init()
  164. defer func() {
  165. if br.ErrMsg == "" {
  166. br.IsSendEmail = false
  167. }
  168. this.Data["json"] = br
  169. this.ServeJSON()
  170. }()
  171. sysUser := this.SysUser
  172. if sysUser == nil {
  173. br.Msg = "请登录"
  174. br.ErrMsg = "请登录,SysUser Is Empty"
  175. br.Ret = 408
  176. return
  177. }
  178. list, e := models.GetBusinessConf()
  179. if e != nil {
  180. br.Msg = "获取失败"
  181. br.ErrMsg = "获取商家配置失败, Err: " + e.Error()
  182. return
  183. }
  184. br.Data = list
  185. br.Ret = 200
  186. br.Success = true
  187. br.Msg = "获取成功"
  188. }
  189. // CodeEncrypt
  190. // @Title 商家编码加密
  191. // @Description 商家编码加密
  192. // @Success 200 Ret=200 获取成功
  193. // @router /code_encrypt [get]
  194. func (this *BusinessConfOpenController) CodeEncrypt() {
  195. br := new(models.BaseResponse).Init()
  196. defer func() {
  197. if br.ErrMsg == "" {
  198. br.IsSendEmail = false
  199. }
  200. this.Data["json"] = br
  201. this.ServeJSON()
  202. }()
  203. res := ""
  204. if utils.BusinessCode != "" {
  205. res = utils.MD5(fmt.Sprintf("%s%s", utils.BusinessCode, utils.BusinessCodeSalt))
  206. }
  207. br.Data = res
  208. br.Ret = 200
  209. br.Success = true
  210. br.Msg = "获取成功"
  211. }