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

494 lines
13 KiB

  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/md"
  5. "applet/app/utils"
  6. "applet/app/utils/cache"
  7. "applet/app/utils/logx"
  8. "encoding/json"
  9. "errors"
  10. "fmt"
  11. "github.com/syyongx/php2go"
  12. "github.com/tidwall/gjson"
  13. "regexp"
  14. "strings"
  15. "xorm.io/xorm"
  16. "github.com/gin-gonic/gin"
  17. )
  18. //处理多眼导航数据 激励广告过滤
  19. func MultiNavFormat(c *gin.Context, mod model.SysModule) model.SysModule {
  20. var data = mod.Data
  21. var listStyle = gjson.Get(data, "list_style").String()
  22. var multiNav []md.MultiNav
  23. if err := json.Unmarshal([]byte(listStyle), &multiNav); err != nil {
  24. return mod
  25. }
  26. userLevelWeight := c.GetString("user_level_weight")
  27. var list []md.MultiNav
  28. for _, v := range multiNav {
  29. if v.SkipIdentifier == "pub.flutter.incentive_ad" && v.Data.Conditions == "1" {
  30. var levelWeight = 0
  31. if v.Data.LevelWeight != "" && v.Data.LevelWeight != "0" {
  32. levelWeight = utils.StrToInt(v.Data.LevelWeight)
  33. }
  34. //如果用户等级大于等于设置等级 就显示
  35. if utils.StrToInt(userLevelWeight) >= levelWeight && c.GetString("user_login") == "1" {
  36. list = append(list, v)
  37. }
  38. } else {
  39. list = append(list, v)
  40. }
  41. }
  42. b, err := json.Marshal(list)
  43. if err != nil {
  44. return mod
  45. }
  46. if b != nil {
  47. mod.Data = strings.Replace(mod.Data, listStyle, string(b), -1)
  48. }
  49. return mod
  50. }
  51. func sysModFormat(c *gin.Context, m interface{}) (interface{}, error) {
  52. var mods []model.SysModule
  53. protocol := SysCfgGet(c, "file_bucket_scheme")
  54. domain := SysCfgGet(c, "file_bucket_host")
  55. //fmt.Println(protocol, domain)
  56. if protocol == "" || domain == "" {
  57. return nil, errors.New("System configuration error, object storage protocol and domain name not found")
  58. }
  59. modname_list := []string{"product", "search_result_taobao_item", "hot_rank_tab_view"}
  60. switch m.(type) {
  61. case *[]model.SysModule:
  62. ms := m.(*[]model.SysModule)
  63. for _, item := range *ms {
  64. replaceList := reformatPng(item.Data)
  65. l := removeDuplicateElement(replaceList)
  66. for _, s := range l {
  67. if strings.Contains(s, "http") {
  68. continue
  69. }
  70. ss := s
  71. s = strings.ReplaceAll(s, `"`, "")
  72. s = strings.ReplaceAll(s, `\`, "")
  73. newVal := fmt.Sprintf("%s://%s/%s", protocol, domain, php2go.Rawurlencode(s))
  74. item.Data = strings.ReplaceAll(item.Data, ss, `"`+newVal+`"`)
  75. }
  76. replaceList = reformatJPG(item.Data)
  77. l = removeDuplicateElement(replaceList)
  78. for _, s := range l {
  79. if strings.Contains(s, "http") {
  80. continue
  81. }
  82. ss := s
  83. s = strings.ReplaceAll(s, `"`, "")
  84. s = strings.ReplaceAll(s, `\`, "")
  85. newVal := fmt.Sprintf("%s://%s/%s", protocol, domain, php2go.Rawurlencode(s))
  86. item.Data = strings.Replace(item.Data, ss, `"`+newVal+`"`, -1)
  87. }
  88. replaceList = reformatGIF(item.Data)
  89. l = removeDuplicateElement(replaceList)
  90. for _, s := range l {
  91. if strings.Contains(s, "http") {
  92. continue
  93. }
  94. ss := s
  95. s = strings.ReplaceAll(s, `"`, "")
  96. s = strings.ReplaceAll(s, `\`, "")
  97. newVal := fmt.Sprintf("%s://%s/%s", protocol, domain, php2go.Rawurlencode(s))
  98. item.Data = strings.Replace(item.Data, ss, `"`+newVal+`"`, -1)
  99. }
  100. if strings.Contains(item.Data, "tmall") == false && item.ModName == "product_detail_title" {
  101. item.Data = strings.Replace(item.Data, "\"platform_css\":[", "\"platform_css\":[{\"name\":\"天猫\",\"type\":\"tmall\",\"text_color\":\"#FFFFFF\",\"bg_color\":\"#FF4242\"},", 1)
  102. }
  103. if strings.Contains(item.Data, "tmall") == false && utils.InArr(item.ModName, modname_list) {
  104. 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)
  105. 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)
  106. }
  107. item = SysModDataByReplace(c, item)
  108. if item.ModName == "multi_nav" {
  109. item = MultiNavFormat(c, item)
  110. }
  111. mods = append(mods, item)
  112. }
  113. return &mods, nil
  114. case *model.SysModule:
  115. m := m.(*model.SysModule)
  116. repalceList := reformatPng(m.Data)
  117. l := removeDuplicateElement(repalceList)
  118. for _, s := range l {
  119. if strings.Contains(s, "http") {
  120. continue
  121. }
  122. if skipHTTPPng(s) {
  123. continue
  124. }
  125. ss := s
  126. s = strings.ReplaceAll(s, `"`, "")
  127. s = strings.ReplaceAll(s, `\`, "")
  128. new := fmt.Sprintf(`%s://%s/%s`, protocol, domain, php2go.Rawurlencode(s))
  129. m.Data = strings.Replace(m.Data, ss, `"`+new+`"`, -1)
  130. }
  131. repalceList = reformatJPG(m.Data)
  132. l = removeDuplicateElement(repalceList)
  133. for _, s := range l {
  134. if strings.Contains(s, "http") {
  135. continue
  136. }
  137. ss := s
  138. s = strings.ReplaceAll(s, `"`, "")
  139. s = strings.ReplaceAll(s, `\`, "")
  140. new := fmt.Sprintf("%s://%s/%s", protocol, domain, php2go.Rawurlencode(s))
  141. m.Data = strings.Replace(m.Data, ss, `"`+new+`"`, -1)
  142. }
  143. repalceList = reformatGIF(m.Data)
  144. l = removeDuplicateElement(repalceList)
  145. for _, s := range l {
  146. if strings.Contains(s, "http") {
  147. continue
  148. }
  149. ss := s
  150. s = strings.ReplaceAll(s, `"`, "")
  151. s = strings.ReplaceAll(s, `\`, "")
  152. new := fmt.Sprintf("%s://%s/%s", protocol, domain, php2go.Rawurlencode(s))
  153. m.Data = strings.Replace(m.Data, ss, `"`+new+`"`, -1)
  154. }
  155. m = SysModDataByReplaceSecond(c, m)
  156. return m, nil
  157. case []*model.UserLevel:
  158. ms := m.([]*model.UserLevel)
  159. for _, item := range ms {
  160. replaceList := reformatPng(item.CssSet)
  161. l := removeDuplicateElement(replaceList)
  162. for _, s := range l {
  163. // png
  164. if strings.Contains(s, "http") {
  165. continue
  166. }
  167. ss := s
  168. s = strings.ReplaceAll(s, `"`, "")
  169. s = strings.ReplaceAll(s, `\`, "")
  170. s = php2go.Rawurlencode(s)
  171. newVal := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
  172. item.CssSet = strings.Replace(item.CssSet, ss, `"`+newVal+`"`, -1)
  173. // jpg
  174. replaceList = reformatJPG(item.CssSet)
  175. l = removeDuplicateElement(replaceList)
  176. for _, s := range l {
  177. if strings.Contains(s, "http") {
  178. continue
  179. }
  180. ss := s
  181. s = strings.ReplaceAll(s, `"`, "")
  182. s = strings.ReplaceAll(s, `\`, "")
  183. s = php2go.Rawurlencode(s)
  184. newVal := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
  185. item.CssSet = strings.Replace(item.CssSet, ss, `"`+newVal+`"`, -1)
  186. }
  187. }
  188. }
  189. return ms, nil
  190. case []*model.SysPushUser:
  191. ms := m.([]*model.SysPushUser)
  192. for _, item := range ms {
  193. replaceList := reformatPng(item.SendData)
  194. l := removeDuplicateElement(replaceList)
  195. for _, s := range l {
  196. // png
  197. if strings.Contains(s, "http") {
  198. continue
  199. }
  200. ss := s
  201. s = strings.ReplaceAll(s, `"`, "")
  202. s = strings.ReplaceAll(s, `\`, "")
  203. s = php2go.Rawurlencode(s)
  204. newVal := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
  205. item.SendData = strings.Replace(item.SendData, ss, `"`+newVal+`"`, -1)
  206. // jpg
  207. replaceList = reformatJPG(item.SendData)
  208. l = removeDuplicateElement(replaceList)
  209. for _, s := range l {
  210. if strings.Contains(s, "http") {
  211. continue
  212. }
  213. ss := s
  214. s = strings.ReplaceAll(s, `"`, "")
  215. s = strings.ReplaceAll(s, `\`, "")
  216. s = php2go.Rawurlencode(s)
  217. newVal := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
  218. item.SendData = strings.Replace(item.SendData, ss, `"`+newVal+`"`, -1)
  219. }
  220. }
  221. }
  222. return ms, nil
  223. case *model.SysPushUser:
  224. m := m.(*model.SysPushUser)
  225. repalceList := reformatPng(m.SendData)
  226. l := removeDuplicateElement(repalceList)
  227. for _, s := range l {
  228. if strings.Contains(s, "http") {
  229. continue
  230. }
  231. ss := s
  232. s = strings.ReplaceAll(s, `"`, "")
  233. s = strings.ReplaceAll(s, `\`, "")
  234. s = php2go.Rawurlencode(s)
  235. new := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
  236. m.SendData = strings.Replace(m.SendData, ss, `"`+new+`"`, -1)
  237. }
  238. repalceList = reformatJPG(m.SendData)
  239. l = removeDuplicateElement(repalceList)
  240. for _, s := range l {
  241. if strings.Contains(s, "http") {
  242. continue
  243. }
  244. ss := s
  245. s = strings.ReplaceAll(s, `"`, "")
  246. s = strings.ReplaceAll(s, `\`, "")
  247. s = php2go.Rawurlencode(s)
  248. new := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
  249. m.SendData = strings.Replace(m.SendData, ss, `"`+new+`"`, -1)
  250. }
  251. repalceList = reformatGIF(m.SendData)
  252. l = removeDuplicateElement(repalceList)
  253. for _, s := range l {
  254. if strings.Contains(s, "http") {
  255. continue
  256. }
  257. ss := s
  258. s = strings.ReplaceAll(s, `"`, "")
  259. s = strings.ReplaceAll(s, `\`, "")
  260. s = php2go.Rawurlencode(s)
  261. new := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
  262. m.SendData = strings.Replace(m.SendData, ss, `"`+new+`"`, -1)
  263. }
  264. return m, nil
  265. default:
  266. return nil, nil
  267. }
  268. }
  269. func ReformatStr(str string, c *gin.Context) string {
  270. protocol := SysCfgGet(c, "file_bucket_scheme")
  271. domain := SysCfgGet(c, "file_bucket_host")
  272. // PNG
  273. replaceList := reformatPng(str)
  274. l := removeDuplicateElement(replaceList)
  275. for _, s := range l {
  276. if strings.Contains(s, "http") {
  277. continue
  278. }
  279. s = strings.ReplaceAll(s, `\`, "")
  280. s = php2go.Rawurlencode(s)
  281. new := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
  282. if skipHTTPPng(new) {
  283. continue
  284. }
  285. ss := s
  286. s = strings.ReplaceAll(s, `"`, "")
  287. str = strings.Replace(str, ss, `"`+new+`"`, -1)
  288. }
  289. // JPG
  290. replaceList = reformatJPG(str)
  291. l = removeDuplicateElement(replaceList)
  292. for _, s := range l {
  293. if strings.Contains(s, "http") {
  294. continue
  295. }
  296. s = strings.ReplaceAll(s, `\`, "")
  297. s = php2go.Rawurlencode(s)
  298. new := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
  299. if skipHTTPPng(new) {
  300. continue
  301. }
  302. ss := s
  303. s = strings.ReplaceAll(s, `"`, "")
  304. str = strings.Replace(str, ss, `"`+new+`"`, -1)
  305. }
  306. // GIF
  307. replaceList = reformatGIF(str)
  308. l = removeDuplicateElement(replaceList)
  309. for _, s := range l {
  310. if strings.Contains(s, "http") {
  311. continue
  312. }
  313. s = strings.ReplaceAll(s, `\`, "")
  314. s = php2go.Rawurlencode(s)
  315. new := fmt.Sprintf("%s://%s/%s", protocol, domain, s)
  316. if skipHTTPPng(new) {
  317. continue
  318. }
  319. ss := s
  320. s = strings.ReplaceAll(s, `"`, "")
  321. str = strings.Replace(str, ss, `"`+new+`"`, -1)
  322. }
  323. return str
  324. }
  325. // 正则匹配
  326. func reformatPng(data string) []string {
  327. re, _ := regexp.Compile(`"([^\"]*.png")`)
  328. list := re.FindAllString(data, -1)
  329. return list
  330. }
  331. //
  332. func skipHTTPPng(data string) bool {
  333. re, _ := regexp.Compile(`(http|https):\/\/([^\"]*.png)`)
  334. return re.MatchString(data)
  335. }
  336. // 正则匹配
  337. func reformatJPG(data string) []string {
  338. re, _ := regexp.Compile(`"([^\"]*.jpg")`)
  339. list := re.FindAllString(data, -1)
  340. return list
  341. }
  342. // 正则匹配
  343. func reformatGIF(data string) []string {
  344. re, _ := regexp.Compile(`"([^\"]*.gif")`)
  345. list := re.FindAllString(data, -1)
  346. return list
  347. }
  348. func removeDuplicateElement(addrs []string) []string {
  349. result := make([]string, 0, len(addrs))
  350. temp := map[string]int{}
  351. i := 1
  352. for _, item := range addrs {
  353. if _, ok := temp[item]; !ok {
  354. temp[item] = i
  355. result = append(result, item)
  356. continue
  357. }
  358. temp[item] = temp[item] + 1
  359. }
  360. // fmt.Println(temp)
  361. return result
  362. }
  363. //单条记录获取
  364. func SysCfgGet(c *gin.Context, key string) string {
  365. res := SysCfgFind(c, key)
  366. //fmt.Println(res)
  367. if _, ok := res[key]; !ok {
  368. return ""
  369. }
  370. return res[key]
  371. }
  372. //单条记录获取DB
  373. func SysCfgGetWithDb(eg *xorm.Engine, masterId string, HKey string) string {
  374. cacheKey := fmt.Sprintf(md.AppCfgCacheKey, masterId, HKey[0:1])
  375. get, err := cache.HGetString(cacheKey, HKey)
  376. if err != nil || get == "" {
  377. cfg, err := SysCfgGetOne(eg, HKey)
  378. if err != nil || cfg == nil {
  379. _ = logx.Error(err)
  380. return ""
  381. }
  382. // key是否存在
  383. cacheKeyExist := false
  384. if cache.Exists(cacheKey) {
  385. cacheKeyExist = true
  386. }
  387. // 设置缓存
  388. _, err = cache.HSet(cacheKey, HKey, cfg.Val)
  389. if err != nil {
  390. _ = logx.Error(err)
  391. return ""
  392. }
  393. if !cacheKeyExist { // 如果是首次设置 设置过期时间
  394. _, err := cache.Expire(cacheKey, md.CfgCacheTime)
  395. if err != nil {
  396. _ = logx.Error(err)
  397. return ""
  398. }
  399. }
  400. return cfg.Val
  401. }
  402. return get
  403. }
  404. // 多条记录获取
  405. func SysCfgFind(c *gin.Context, keys ...string) map[string]string {
  406. res := map[string]string{}
  407. cacheKey := fmt.Sprintf(md.AppCfgCacheKey, c.GetString("mid"))
  408. err := cache.GetJson(cacheKey, &res)
  409. if err != nil || len(res) == 0 {
  410. cfgList, _ := SysCfgGetAll(DBs[c.GetString("mid")])
  411. if cfgList == nil {
  412. return nil
  413. }
  414. for _, v := range *cfgList {
  415. res[v.Key] = v.Val
  416. }
  417. cache.SetJson(cacheKey, res, md.CfgCacheTime)
  418. }
  419. if len(keys) == 0 {
  420. return res
  421. }
  422. tmp := map[string]string{}
  423. for _, v := range keys {
  424. if val, ok := res[v]; ok {
  425. tmp[v] = val
  426. } else {
  427. tmp[v] = ""
  428. }
  429. }
  430. return tmp
  431. }
  432. // 多条记录获取DB
  433. func SysCfgFindWithDb(eg *xorm.Engine, masterId string, keys ...string) map[string]string {
  434. res := map[string]string{}
  435. cacheKey := fmt.Sprintf(md.AppCfgCacheKey, masterId)
  436. err := cache.GetJson(cacheKey, &res)
  437. if err != nil || len(res) == 0 {
  438. cfgList, _ := SysCfgGetAll(eg)
  439. if cfgList == nil {
  440. return nil
  441. }
  442. for _, v := range *cfgList {
  443. res[v.Key] = v.Val
  444. }
  445. cache.SetJson(cacheKey, res, md.CfgCacheTime)
  446. }
  447. if len(keys) == 0 {
  448. return res
  449. }
  450. tmp := map[string]string{}
  451. for _, v := range keys {
  452. if val, ok := res[v]; ok {
  453. tmp[v] = val
  454. } else {
  455. tmp[v] = ""
  456. }
  457. }
  458. return tmp
  459. }