附近小店
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

219 строки
5.0 KiB

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