面包店
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_sys_cfg_get.go 4.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package svc
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "strings"
  7. "xorm.io/xorm"
  8. "applet/app/cfg"
  9. "applet/app/db"
  10. "applet/app/md"
  11. "applet/app/utils"
  12. "applet/app/utils/cache"
  13. )
  14. // 单挑记录获取
  15. func SysCfgGet(c *gin.Context, key string) string {
  16. mid := c.GetString("mid")
  17. eg := db.DBs[mid]
  18. return db.SysCfgGetWithDb(eg, mid, key)
  19. }
  20. // 多条记录获取
  21. func SysCfgFind(c *gin.Context, keys ...string) map[string]string {
  22. e := db.DBs[c.GetString("mid")]
  23. res := map[string]string{}
  24. cacheKey := fmt.Sprintf(md.AppCfgCacheKey, c.GetString("mid"))
  25. err := cache.GetJson(cacheKey, &res)
  26. if err != nil || len(res) == 0 {
  27. cfgList, _ := db.SysCfgGetAll(e)
  28. if cfgList == nil {
  29. return nil
  30. }
  31. for _, v := range *cfgList {
  32. res[v.Key] = v.Val
  33. }
  34. // 先不设置缓存
  35. cache.SetJson(cacheKey, res, md.CfgCacheTime)
  36. }
  37. if len(keys) == 0 {
  38. return res
  39. }
  40. tmp := map[string]string{}
  41. for _, v := range keys {
  42. if val, ok := res[v]; ok {
  43. tmp[v] = val
  44. } else {
  45. tmp[v] = ""
  46. }
  47. }
  48. return tmp
  49. }
  50. // 多条记录获取
  51. func EgSysCfgFind(keys ...string) map[string]string {
  52. var e *xorm.Engine
  53. res := map[string]string{}
  54. if len(res) == 0 {
  55. cfgList, _ := db.SysCfgGetAll(e)
  56. if cfgList == nil {
  57. return nil
  58. }
  59. for _, v := range *cfgList {
  60. res[v.Key] = v.Val
  61. }
  62. // 先不设置缓存
  63. // cache.SetJson(md.KEY_SYS_CFG_CACHE, res, 60)
  64. }
  65. if len(keys) == 0 {
  66. return res
  67. }
  68. tmp := map[string]string{}
  69. for _, v := range keys {
  70. if val, ok := res[v]; ok {
  71. tmp[v] = val
  72. } else {
  73. tmp[v] = ""
  74. }
  75. }
  76. return tmp
  77. }
  78. // 清理系统配置信息
  79. func SysCfgCleanCache() {
  80. cache.Del(md.KEY_SYS_CFG_CACHE)
  81. }
  82. // 写入系统设置
  83. func SysCfgSet(c *gin.Context, key, val, memo string) bool {
  84. cfg, err := db.SysCfgGetOne(db.DBs[c.GetString("mid")], key)
  85. if err != nil || cfg == nil {
  86. return db.SysCfgInsert(db.DBs[c.GetString("mid")], key, val, memo)
  87. }
  88. if memo != "" && cfg.Memo != memo {
  89. cfg.Memo = memo
  90. }
  91. SysCfgCleanCache()
  92. return db.SysCfgUpdate(db.DBs[c.GetString("mid")], key, val, cfg.Memo)
  93. }
  94. // 多条记录获取
  95. func SysCfgFindByIds(eg *xorm.Engine, keys ...string) map[string]string {
  96. key := utils.Md5(eg.DataSourceName() + md.KEY_SYS_CFG_CACHE)
  97. res := map[string]string{}
  98. c, ok := cfg.MemCache.Get(key).(map[string]string)
  99. if !ok || len(c) == 0 {
  100. cfgList, _ := db.DbsSysCfgGetAll(eg)
  101. if cfgList == nil {
  102. return nil
  103. }
  104. for _, v := range *cfgList {
  105. res[v.Key] = v.Val
  106. }
  107. cfg.MemCache.Put(key, res, 10)
  108. } else {
  109. res = c
  110. }
  111. if len(keys) == 0 {
  112. return res
  113. }
  114. tmp := map[string]string{}
  115. for _, v := range keys {
  116. if val, ok := res[v]; ok {
  117. tmp[v] = val
  118. } else {
  119. tmp[v] = ""
  120. }
  121. }
  122. return tmp
  123. }
  124. // 支付配置
  125. func SysCfgFindPayment(c *gin.Context) ([]map[string]string, error) {
  126. platform := c.GetHeader("platform")
  127. payCfg := SysCfgFind(c, "pay_wx_pay_img", "pay_ali_pay_img", "pay_balance_img", "pay_type")
  128. if payCfg["pay_wx_pay_img"] == "" || payCfg["pay_ali_pay_img"] == "" || payCfg["pay_balance_img"] == "" || payCfg["pay_type"] == "" {
  129. return nil, errors.New("lack of payment config")
  130. }
  131. payCfg["pay_wx_pay_img"] = ImageFormat(c, payCfg["pay_wx_pay_img"])
  132. payCfg["pay_ali_pay_img"] = ImageFormat(c, payCfg["pay_ali_pay_img"])
  133. payCfg["pay_balance_img"] = ImageFormat(c, payCfg["pay_balance_img"])
  134. var result []map[string]string
  135. if strings.Contains(payCfg["pay_type"], "aliPay") && platform != md.PLATFORM_WX_APPLET {
  136. item := make(map[string]string)
  137. item["pay_channel"] = "alipay"
  138. item["img"] = payCfg["pay_ali_pay_img"]
  139. item["name"] = "支付宝支付"
  140. result = append(result, item)
  141. }
  142. if strings.Contains(payCfg["pay_type"], "wxPay") {
  143. item := make(map[string]string)
  144. item["pay_channel"] = "wx"
  145. item["img"] = payCfg["pay_wx_pay_img"]
  146. item["name"] = "微信支付"
  147. result = append(result, item)
  148. }
  149. if strings.Contains(payCfg["pay_type"], "walletPay") {
  150. item := make(map[string]string)
  151. item["pay_channel"] = "fin"
  152. item["img"] = payCfg["pay_balance_img"]
  153. item["name"] = "余额支付"
  154. result = append(result, item)
  155. }
  156. return result, nil
  157. }