|
- package db
-
- import (
- "applet/app/db/model"
- "applet/app/md"
- "applet/app/utils"
- "applet/app/utils/cache"
- "applet/app/utils/logx"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/syyongx/php2go"
- "github.com/tidwall/gjson"
- "regexp"
- "strings"
- "xorm.io/xorm"
-
- "github.com/gin-gonic/gin"
- )
-
- //处理多眼导航数据 激励广告过滤
- func MultiNavFormat(c *gin.Context, mod model.SysModule) model.SysModule {
- var data = mod.Data
- var listStyle = gjson.Get(data, "list_style").String()
-
- var multiNav []md.MultiNav
- if err := json.Unmarshal([]byte(listStyle), &multiNav); err != nil {
- return mod
- }
-
- userLevelWeight := c.GetString("user_level_weight")
-
- var list []md.MultiNav
- for _, v := range multiNav {
-
- if v.SkipIdentifier == "pub.flutter.incentive_ad" && v.Data.Conditions == "1" {
- var levelWeight = 0
- if v.Data.LevelWeight != "" && v.Data.LevelWeight != "0" {
- levelWeight = utils.StrToInt(v.Data.LevelWeight)
- }
- //如果用户等级大于等于设置等级 就显示
- if utils.StrToInt(userLevelWeight) >= levelWeight && c.GetString("user_login") == "1" {
- list = append(list, v)
- }
- } else {
- list = append(list, v)
- }
- }
- b, err := json.Marshal(list)
- if err != nil {
- return mod
- }
- if b != nil {
- mod.Data = strings.Replace(mod.Data, listStyle, string(b), -1)
- }
- return mod
- }
-
- func sysModFormat(c *gin.Context, m interface{}) (interface{}, error) {
- var mods []model.SysModule
- protocol := SysCfgGet(c, "file_bucket_scheme")
- domain := SysCfgGet(c, "file_bucket_host")
- //fmt.Println(protocol, domain)
- if protocol == "" || domain == "" {
- return nil, errors.New("System configuration error, object storage protocol and domain name not found")
- }
- modname_list := []string{"product", "search_result_taobao_item", "hot_rank_tab_view"}
- switch m.(type) {
- case *[]model.SysModule:
- ms := m.(*[]model.SysModule)
- for _, item := range *ms {
- replaceList := reformatPng(item.Data)
- l := removeDuplicateElement(replaceList)
- for _, s := range l {
- if strings.Contains(s, "http") {
- continue
- }
- ss := s
- s = strings.ReplaceAll(s, `"`, "")
- s = strings.ReplaceAll(s, `\`, "")
-
- newVal := fmt.Sprintf("%s://%s/%s", protocol, domain, php2go.Rawurlencode(s))
- item.Data = strings.ReplaceAll(item.Data, ss, `"`+newVal+`"`)
- }
- replaceList = reformatJPG(item.Data)
- l = removeDuplicateElement(replaceList)
- for _, s := range l {
- if strings.Contains(s, "http") {
- continue
- }
- ss := s
- s = strings.ReplaceAll(s, `"`, "")
- s = strings.ReplaceAll(s, `\`, "")
- newVal := fmt.Sprintf("%s://%s/%s", protocol, domain, php2go.Rawurlencode(s))
- item.Data = strings.Replace(item.Data, ss, `"`+newVal+`"`, -1)
- }
- replaceList = reformatGIF(item.Data)
- l = removeDuplicateElement(replaceList)
- for _, s := range l {
- if strings.Contains(s, "http") {
- continue
- }
- ss := s
- s = strings.ReplaceAll(s, `"`, "")
- s = strings.ReplaceAll(s, `\`, "")
- newVal := fmt.Sprintf("%s://%s/%s", protocol, domain, php2go.Rawurlencode(s))
- item.Data = strings.Replace(item.Data, ss, `"`+newVal+`"`, -1)
- }
- if strings.Contains(item.Data, "tmall") == false && item.ModName == "product_detail_title" {
- item.Data = strings.Replace(item.Data, "\"platform_css\":[", "\"platform_css\":[{\"name\":\"天猫\",\"type\":\"tmall\",\"text_color\":\"#FFFFFF\",\"bg_color\":\"#FF4242\"},", 1)
- }
- if strings.Contains(item.Data, "tmall") == false && utils.InArr(item.ModName, modname_list) {
- item.Data = strings.Replace(item.Data, "{\"index\":\"6\",\"type\":\"kaola\",\"platform_name\":\"考拉\",\"provider_name_color\":\"#FFFFFF\",\"provider_bg_color\":\"#FF4242\"}", "{\"index\":\"6\",\"type\":\"kaola\",\"platform_name\":\"考拉\",\"provider_name_color\":\"#FFFFFF\",\"provider_bg_color\":\"#FF4242\"},{\"index\":\"7\",\"type\":\"tmall\",\"platform_name\":\"天猫\",\"provider_name_color\":\"#FFFFFF\",\"provider_bg_color\":\"#FF4242\"}", 1)
- item.Data = strings.Replace(item.Data, "{\"type\":\"kaola\",\"provider_name_color\":\"#FFFFFF\",\"provider_bg_color\":\"#FF4242\"}", "{\"type\":\"kaola\",\"provider_name_color\":\"#FFFFFF\",\"provider_bg_color\":\"#FF4242\"},{\"type\":\"tmall\",\"provider_name_color\":\"#FFFFFF\",\"provider_bg_color\":\"#FF4242\"}", 1)
- }
-
- item = SysModDataByReplace(c, item)
- if item.ModName == "multi_nav" {
- item = MultiNavFormat(c, item)
- }
- mods = append(mods, item)
- }
- return &mods, nil
- case *model.SysModule:
- m := m.(*model.SysModule)
- repalceList := reformatPng(m.Data)
- l := removeDuplicateElement(repalceList)
- for _, s := range l {
- if strings.Contains(s, "http") {
- continue
- }
- if skipHTTPPng(s) {
- continue
- }
- ss := s
- s = strings.ReplaceAll(s, `"`, "")
- s = strings.ReplaceAll(s, `\`, "")
- new := fmt.Sprintf(`%s://%s/%s`, protocol, domain, php2go.Rawurlencode(s))
-
- m.Data = strings.Replace(m.Data, ss, `"`+new+`"`, -1)
-
- }
- repalceList = reformatJPG(m.Data)
- l = removeDuplicateElement(repalceList)
- for _, s := range l {
- if strings.Contains(s, "http") {
- continue
- }
- ss := s
- s = strings.ReplaceAll(s, `"`, "")
- s = strings.ReplaceAll(s, `\`, "")
- new := fmt.Sprintf("%s://%s/%s", protocol, domain, php2go.Rawurlencode(s))
- m.Data = strings.Replace(m.Data, ss, `"`+new+`"`, -1)
-
- }
- repalceList = reformatGIF(m.Data)
- l = removeDuplicateElement(repalceList)
- for _, s := range l {
- if strings.Contains(s, "http") {
- continue
- }
- ss := s
- s = strings.ReplaceAll(s, `"`, "")
- s = strings.ReplaceAll(s, `\`, "")
- new := fmt.Sprintf("%s://%s/%s", protocol, domain, php2go.Rawurlencode(s))
- m.Data = strings.Replace(m.Data, ss, `"`+new+`"`, -1)
-
- }
- m = SysModDataByReplaceSecond(c, m)
- return m, nil
- case []*model.UserLevel:
- ms := m.([]*model.UserLevel)
- for _, item := range ms {
- replaceList := reformatPng(item.CssSet)
- l := removeDuplicateElement(replaceList)
- for _, s := range l {
- // png
- if strings.Contains(s, "http") {
- continue
- }
- ss := s
- s = strings.ReplaceAll(s, `"`, "")
- s = strings.ReplaceAll(s, `\`, "")
- s = php2go.Rawurlencode(s)
- newVal := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
- item.CssSet = strings.Replace(item.CssSet, ss, `"`+newVal+`"`, -1)
- // jpg
- replaceList = reformatJPG(item.CssSet)
- l = removeDuplicateElement(replaceList)
- for _, s := range l {
- if strings.Contains(s, "http") {
- continue
- }
- ss := s
- s = strings.ReplaceAll(s, `"`, "")
- s = strings.ReplaceAll(s, `\`, "")
- s = php2go.Rawurlencode(s)
- newVal := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
- item.CssSet = strings.Replace(item.CssSet, ss, `"`+newVal+`"`, -1)
-
- }
- }
- }
- return ms, nil
- case []*model.SysPushUser:
- ms := m.([]*model.SysPushUser)
- for _, item := range ms {
- replaceList := reformatPng(item.SendData)
- l := removeDuplicateElement(replaceList)
- for _, s := range l {
- // png
- if strings.Contains(s, "http") {
- continue
- }
- ss := s
- s = strings.ReplaceAll(s, `"`, "")
- s = strings.ReplaceAll(s, `\`, "")
- s = php2go.Rawurlencode(s)
- newVal := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
- item.SendData = strings.Replace(item.SendData, ss, `"`+newVal+`"`, -1)
- // jpg
- replaceList = reformatJPG(item.SendData)
- l = removeDuplicateElement(replaceList)
- for _, s := range l {
- if strings.Contains(s, "http") {
- continue
- }
- ss := s
- s = strings.ReplaceAll(s, `"`, "")
- s = strings.ReplaceAll(s, `\`, "")
- s = php2go.Rawurlencode(s)
- newVal := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
- item.SendData = strings.Replace(item.SendData, ss, `"`+newVal+`"`, -1)
-
- }
- }
- }
- return ms, nil
- case *model.SysPushUser:
- m := m.(*model.SysPushUser)
- repalceList := reformatPng(m.SendData)
- l := removeDuplicateElement(repalceList)
- for _, s := range l {
- if strings.Contains(s, "http") {
- continue
- }
- ss := s
- s = strings.ReplaceAll(s, `"`, "")
- s = strings.ReplaceAll(s, `\`, "")
- s = php2go.Rawurlencode(s)
- new := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
- m.SendData = strings.Replace(m.SendData, ss, `"`+new+`"`, -1)
-
- }
- repalceList = reformatJPG(m.SendData)
- l = removeDuplicateElement(repalceList)
- for _, s := range l {
- if strings.Contains(s, "http") {
- continue
- }
- ss := s
- s = strings.ReplaceAll(s, `"`, "")
- s = strings.ReplaceAll(s, `\`, "")
- s = php2go.Rawurlencode(s)
- new := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
- m.SendData = strings.Replace(m.SendData, ss, `"`+new+`"`, -1)
-
- }
- repalceList = reformatGIF(m.SendData)
- l = removeDuplicateElement(repalceList)
- for _, s := range l {
- if strings.Contains(s, "http") {
- continue
- }
- ss := s
- s = strings.ReplaceAll(s, `"`, "")
- s = strings.ReplaceAll(s, `\`, "")
- s = php2go.Rawurlencode(s)
- new := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
- m.SendData = strings.Replace(m.SendData, ss, `"`+new+`"`, -1)
- }
- return m, nil
- default:
- return nil, nil
- }
- }
-
- func ReformatStr(str string, c *gin.Context) string {
- protocol := SysCfgGet(c, "file_bucket_scheme")
- domain := SysCfgGet(c, "file_bucket_host")
-
- // PNG
- replaceList := reformatPng(str)
- l := removeDuplicateElement(replaceList)
- for _, s := range l {
- if strings.Contains(s, "http") {
- continue
- }
- s = strings.ReplaceAll(s, `\`, "")
- s = php2go.Rawurlencode(s)
- new := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
- if skipHTTPPng(new) {
- continue
- }
- ss := s
- s = strings.ReplaceAll(s, `"`, "")
- str = strings.Replace(str, ss, `"`+new+`"`, -1)
- }
-
- // JPG
- replaceList = reformatJPG(str)
- l = removeDuplicateElement(replaceList)
- for _, s := range l {
- if strings.Contains(s, "http") {
- continue
- }
- s = strings.ReplaceAll(s, `\`, "")
- s = php2go.Rawurlencode(s)
- new := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
- if skipHTTPPng(new) {
- continue
- }
- ss := s
- s = strings.ReplaceAll(s, `"`, "")
- str = strings.Replace(str, ss, `"`+new+`"`, -1)
- }
-
- // GIF
- replaceList = reformatGIF(str)
- l = removeDuplicateElement(replaceList)
- for _, s := range l {
- if strings.Contains(s, "http") {
- continue
- }
- s = strings.ReplaceAll(s, `\`, "")
- s = php2go.Rawurlencode(s)
- new := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
- if skipHTTPPng(new) {
- continue
- }
- ss := s
- s = strings.ReplaceAll(s, `"`, "")
- str = strings.Replace(str, ss, `"`+new+`"`, -1)
- }
-
- return str
- }
-
- // 正则匹配
- func reformatPng(data string) []string {
- re, _ := regexp.Compile(`"([^\"]*.png")`)
- list := re.FindAllString(data, -1)
- return list
- }
-
- //
- func skipHTTPPng(data string) bool {
- re, _ := regexp.Compile(`(http|https):\/\/([^\"]*.png)`)
- return re.MatchString(data)
- }
-
- // 正则匹配
- func reformatJPG(data string) []string {
- re, _ := regexp.Compile(`"([^\"]*.jpg")`)
- list := re.FindAllString(data, -1)
- return list
- }
-
- // 正则匹配
- func reformatGIF(data string) []string {
- re, _ := regexp.Compile(`"([^\"]*.gif")`)
- list := re.FindAllString(data, -1)
- return list
- }
-
- func removeDuplicateElement(addrs []string) []string {
- result := make([]string, 0, len(addrs))
- temp := map[string]int{}
- i := 1
- for _, item := range addrs {
- if _, ok := temp[item]; !ok {
- temp[item] = i
- result = append(result, item)
- continue
- }
- temp[item] = temp[item] + 1
- }
- // fmt.Println(temp)
- return result
- }
-
- //单条记录获取
- func SysCfgGet(c *gin.Context, key string) string {
- res := SysCfgFind(c, key)
- //fmt.Println(res)
- if _, ok := res[key]; !ok {
- return ""
- }
- return res[key]
- }
-
- //单条记录获取DB
- func SysCfgGetWithDb(eg *xorm.Engine, masterId string, HKey string) string {
- cacheKey := fmt.Sprintf(md.AppCfgCacheKey, masterId, HKey[0:1])
- get, err := cache.HGetString(cacheKey, HKey)
- if err != nil || get == "" {
- cfg, err := SysCfgGetOne(eg, HKey)
- if err != nil || cfg == nil {
- _ = logx.Error(err)
- return ""
- }
-
- // key是否存在
- cacheKeyExist := false
- if cache.Exists(cacheKey) {
- cacheKeyExist = true
- }
-
- // 设置缓存
- _, err = cache.HSet(cacheKey, HKey, cfg.Val)
- if err != nil {
- _ = logx.Error(err)
- return ""
- }
- if !cacheKeyExist { // 如果是首次设置 设置过期时间
- _, err := cache.Expire(cacheKey, md.CfgCacheTime)
- if err != nil {
- _ = logx.Error(err)
- return ""
- }
- }
- return cfg.Val
- }
- return get
- }
-
- // 多条记录获取
- func SysCfgFind(c *gin.Context, keys ...string) map[string]string {
- res := map[string]string{}
- cacheKey := fmt.Sprintf(md.AppCfgCacheKey, c.GetString("mid"))
- err := cache.GetJson(cacheKey, &res)
- if err != nil || len(res) == 0 {
- cfgList, _ := SysCfgGetAll(DBs[c.GetString("mid")])
- 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
- }
-
- // 多条记录获取DB
- func SysCfgFindWithDb(eg *xorm.Engine, masterId string, keys ...string) map[string]string {
- res := map[string]string{}
- cacheKey := fmt.Sprintf(md.AppCfgCacheKey, masterId)
- err := cache.GetJson(cacheKey, &res)
- if err != nil || len(res) == 0 {
- cfgList, _ := SysCfgGetAll(eg)
- 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
- }
|