|
- package svc
-
- import (
- "errors"
- "fmt"
- "github.com/gin-gonic/gin"
- "strings"
- "xorm.io/xorm"
-
- "applet/app/cfg"
- "applet/app/db"
- "applet/app/md"
- "applet/app/utils"
- "applet/app/utils/cache"
- )
-
- // 单挑记录获取
- func SysCfgGet(c *gin.Context, key string) string {
- mid := c.GetString("mid")
- eg := db.DBs[mid]
- return db.SysCfgGetWithDb(eg, mid, key)
- }
-
- // 多条记录获取
- func SysCfgFind(c *gin.Context, keys ...string) map[string]string {
- e := db.DBs[c.GetString("mid")]
- res := map[string]string{}
- cacheKey := fmt.Sprintf(md.AppCfgCacheKey, c.GetString("mid"))
- err := cache.GetJson(cacheKey, &res)
- if err != nil || len(res) == 0 {
- cfgList, _ := db.SysCfgGetAll(e)
- if cfgList == nil {
- return nil
- }
- for _, v := range *cfgList {
- res[v.Key] = v.Val
- }
- // 先不设置缓存
- cache.SetJson(cacheKey, res, md.CfgCacheTime)
- }
- if len(keys) == 0 {
- return res
- }
- tmp := map[string]string{}
- for _, v := range keys {
- if val, ok := res[v]; ok {
- tmp[v] = val
- } else {
- tmp[v] = ""
- }
- }
- return tmp
- }
-
- // 多条记录获取
- func EgSysCfgFind(keys ...string) map[string]string {
- var e *xorm.Engine
- res := map[string]string{}
- if len(res) == 0 {
- cfgList, _ := db.SysCfgGetAll(e)
- if cfgList == nil {
- return nil
- }
- for _, v := range *cfgList {
- res[v.Key] = v.Val
- }
- // 先不设置缓存
- // cache.SetJson(md.KEY_SYS_CFG_CACHE, res, 60)
- }
- if len(keys) == 0 {
- return res
- }
- tmp := map[string]string{}
- for _, v := range keys {
- if val, ok := res[v]; ok {
- tmp[v] = val
- } else {
- tmp[v] = ""
- }
- }
- return tmp
- }
-
- // 清理系统配置信息
- func SysCfgCleanCache() {
- cache.Del(md.KEY_SYS_CFG_CACHE)
- }
-
- // 写入系统设置
- func SysCfgSet(c *gin.Context, key, val, memo string) bool {
- cfg, err := db.SysCfgGetOne(db.DBs[c.GetString("mid")], key)
- if err != nil || cfg == nil {
- return db.SysCfgInsert(db.DBs[c.GetString("mid")], key, val, memo)
- }
- if memo != "" && cfg.Memo != memo {
- cfg.Memo = memo
- }
- SysCfgCleanCache()
- return db.SysCfgUpdate(db.DBs[c.GetString("mid")], key, val, cfg.Memo)
- }
-
- // 多条记录获取
- func SysCfgFindByIds(eg *xorm.Engine, keys ...string) map[string]string {
- key := utils.Md5(eg.DataSourceName() + md.KEY_SYS_CFG_CACHE)
- res := map[string]string{}
- c, ok := cfg.MemCache.Get(key).(map[string]string)
- if !ok || len(c) == 0 {
- cfgList, _ := db.DbsSysCfgGetAll(eg)
- if cfgList == nil {
- return nil
- }
- for _, v := range *cfgList {
- res[v.Key] = v.Val
- }
- cfg.MemCache.Put(key, res, 10)
- } else {
- res = c
- }
- if len(keys) == 0 {
- return res
- }
- tmp := map[string]string{}
- for _, v := range keys {
- if val, ok := res[v]; ok {
- tmp[v] = val
- } else {
- tmp[v] = ""
- }
- }
- return tmp
- }
-
- // 支付配置
- func SysCfgFindPayment(c *gin.Context) ([]map[string]string, error) {
- platform := c.GetHeader("platform")
- payCfg := SysCfgFind(c, "pay_wx_pay_img", "pay_ali_pay_img", "pay_balance_img", "pay_type")
- if payCfg["pay_wx_pay_img"] == "" || payCfg["pay_ali_pay_img"] == "" || payCfg["pay_balance_img"] == "" || payCfg["pay_type"] == "" {
- return nil, errors.New("lack of payment config")
- }
- payCfg["pay_wx_pay_img"] = ImageFormat(c, payCfg["pay_wx_pay_img"])
- payCfg["pay_ali_pay_img"] = ImageFormat(c, payCfg["pay_ali_pay_img"])
- payCfg["pay_balance_img"] = ImageFormat(c, payCfg["pay_balance_img"])
-
- var result []map[string]string
-
- if strings.Contains(payCfg["pay_type"], "aliPay") && platform != md.PLATFORM_WX_APPLET {
- item := make(map[string]string)
- item["pay_channel"] = "alipay"
- item["img"] = payCfg["pay_ali_pay_img"]
- item["name"] = "支付宝支付"
- result = append(result, item)
- }
-
- if strings.Contains(payCfg["pay_type"], "wxPay") {
- item := make(map[string]string)
- item["pay_channel"] = "wx"
- item["img"] = payCfg["pay_wx_pay_img"]
- item["name"] = "微信支付"
- result = append(result, item)
- }
-
- if strings.Contains(payCfg["pay_type"], "walletPay") {
- item := make(map[string]string)
- item["pay_channel"] = "fin"
- item["img"] = payCfg["pay_balance_img"]
- item["name"] = "余额支付"
- result = append(result, item)
- }
-
- return result, nil
- }
|