附近小店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

db_sys_mod_format_img.go 6.1 KiB

3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils"
  5. "errors"
  6. "fmt"
  7. "github.com/gin-gonic/gin"
  8. "github.com/syyongx/php2go"
  9. "regexp"
  10. "strings"
  11. )
  12. func sysModFormat(c *gin.Context, m interface{}) (interface{}, error) {
  13. var mods []model.SysModule
  14. protocol := SysCfgGet(c, "file_bucket_scheme")
  15. domain := SysCfgGet(c, "file_bucket_host")
  16. //fmt.Println(protocol, domain)
  17. if protocol == "" || domain == "" {
  18. return nil, errors.New("System configuration error, object storage protocol and domain name not found")
  19. }
  20. modname_list := []string{"product", "search_result_taobao_item", "hot_rank_tab_view"}
  21. switch m.(type) {
  22. case *[]model.SysModule:
  23. ms := m.(*[]model.SysModule)
  24. for _, item := range *ms {
  25. item.Data = ReformatStr(protocol, domain, item.Data, c)
  26. if item.ModName == "product_detail_title" {
  27. if strings.Contains(item.Data, "tmall") == false {
  28. item.Data = strings.Replace(item.Data, "\"platform_css\":[", "\"platform_css\":[{\"name\":\"天猫\",\"type\":\"tmall\",\"text_color\":\"#FFFFFF\",\"bg_color\":\"#FF4242\"},", 1)
  29. }
  30. if strings.Contains(item.Data, "kuaishou") == false {
  31. item.Data = strings.Replace(item.Data, "\"platform_css\":[", "\"platform_css\":[{\"name\":\"快手\",\"type\":\"kuaishou\",\"text_color\":\"#FFFFFF\",\"bg_color\":\"#FF4242\"},", 1)
  32. }
  33. if strings.Contains(item.Data, "tikTok") == false {
  34. item.Data = strings.Replace(item.Data, "\"platform_css\":[", "\"platform_css\":[{\"name\":\"抖音\",\"type\":\"tikTok\",\"text_color\":\"#FFFFFF\",\"bg_color\":\"#FF4242\"},", 1)
  35. }
  36. }
  37. if strings.Contains(item.Data, "tmall") == false && utils.InArr(item.ModName, modname_list) {
  38. 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)
  39. 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)
  40. }
  41. if strings.Contains(item.Data, "\"virtual_coin_pre_fix_name\":") == false && item.ModName == "product" {
  42. item.Data = strings.ReplaceAll(item.Data, "\"virtual_coin_name\":", "\"virtual_coin_pre_fix_name\":\"返\",\"virtual_coin_name\":")
  43. }
  44. item = SysModDataByReplace(c, item)
  45. mods = append(mods, item)
  46. }
  47. return &mods, nil
  48. case *model.SysModule:
  49. m := m.(*model.SysModule)
  50. m.Data = ReformatStr(protocol, domain, m.Data, c)
  51. m = SysModDataByReplaceSecond(c, m)
  52. return m, nil
  53. case []*model.UserLevel:
  54. ms := m.([]*model.UserLevel)
  55. for _, item := range ms {
  56. item.CssSet = ReformatStr(protocol, domain, item.CssSet, c)
  57. }
  58. return ms, nil
  59. case []*model.SysPushUser:
  60. ms := m.([]*model.SysPushUser)
  61. for _, item := range ms {
  62. item.SendData = ReformatStr(protocol, domain, item.SendData, c)
  63. }
  64. return ms, nil
  65. case *model.SysPushUser:
  66. m := m.(*model.SysPushUser)
  67. m.SendData = ReformatStr(protocol, domain, m.SendData, c)
  68. return m, nil
  69. default:
  70. return nil, nil
  71. }
  72. }
  73. func ReformatComm(str, protocol, domain string) string {
  74. // PNG
  75. replaceList := reformatImg(str)
  76. l := removeDuplicateElement(replaceList)
  77. for _, s := range l {
  78. if strings.Contains(s, "http") {
  79. continue
  80. }
  81. ss := s
  82. s = strings.ReplaceAll(s, `\`, "")
  83. s = strings.ReplaceAll(s, `"`, "")
  84. s = php2go.Rawurlencode(s)
  85. new := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
  86. //if skipHTTPPng(new) {
  87. // continue
  88. //}
  89. str = strings.Replace(str, ss, `"`+new+`"`, -1)
  90. }
  91. return str
  92. }
  93. func ReformatStr(protocol, domain, str string, c *gin.Context) string {
  94. //protocol := SysCfgGet(c, "file_bucket_scheme")
  95. //domain := SysCfgGet(c, "file_bucket_host")
  96. // PNG
  97. str = ReformatComm(str, protocol, domain)
  98. return str
  99. }
  100. // 正则匹配
  101. func reformatPng(data string) []string {
  102. re, _ := regexp.Compile(`"([^\"]*.png")`)
  103. list := re.FindAllString(data, -1)
  104. return list
  105. }
  106. func reformatJpeg(data string) []string {
  107. re, _ := regexp.Compile(`"([^\"]*.jpeg")`)
  108. list := re.FindAllString(data, -1)
  109. return list
  110. }
  111. func reformatMp4(data string) []string {
  112. re, _ := regexp.Compile(`"([^\"]*.mp4")`)
  113. list := re.FindAllString(data, -1)
  114. return list
  115. }
  116. func skipHTTPPng(data string) bool {
  117. re, _ := regexp.Compile(`(http|https):\/\/([^\"]*.png)`)
  118. return re.MatchString(data)
  119. }
  120. // 正则匹配
  121. func reformatJPG(data string) []string {
  122. re, _ := regexp.Compile(`"([^\"]*.jpg")`)
  123. list := re.FindAllString(data, -1)
  124. return list
  125. }
  126. // 正则匹配
  127. func reformatGIF(data string) []string {
  128. re, _ := regexp.Compile(`"([^\"]*.gif")`)
  129. list := re.FindAllString(data, -1)
  130. return list
  131. }
  132. func reformatImg(data string) []string {
  133. re, _ := regexp.Compile(`"([^\"]*.(png|jpg|jpeg|gif|mp4)")`)
  134. list := re.FindAllString(data, -1)
  135. return list
  136. }
  137. func removeDuplicateElement(addrs []string) []string {
  138. result := make([]string, 0, len(addrs))
  139. temp := map[string]int{}
  140. i := 1
  141. for _, item := range addrs {
  142. if _, ok := temp[item]; !ok {
  143. temp[item] = i
  144. result = append(result, item)
  145. continue
  146. }
  147. temp[item] = temp[item] + 1
  148. }
  149. // fmt.Println(temp)
  150. return result
  151. }
  152. // 单条记录获取DB
  153. func SysCfgGet(c *gin.Context, key string) string {
  154. res := SysCfgFind(c, key)
  155. //fmt.Println(res)
  156. if _, ok := res[key]; !ok {
  157. return ""
  158. }
  159. return res[key]
  160. }
  161. // 多条记录获取
  162. func SysCfgFind(c *gin.Context, keys ...string) map[string]string {
  163. eg := DBs[c.GetString("mid")]
  164. masterId := c.GetString("mid")
  165. res := map[string]string{}
  166. //TODO::判断keys长度(大于10个直接查数据库)
  167. if len(keys) > 10 {
  168. cfgList, _ := SysCfgGetAll(eg)
  169. if cfgList == nil {
  170. return nil
  171. }
  172. for _, v := range *cfgList {
  173. res[v.Key] = v.Val
  174. }
  175. } else {
  176. for _, key := range keys {
  177. res[key] = SysCfgGetWithDb(eg, masterId, key)
  178. }
  179. }
  180. return res
  181. }