cache-tool.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import {
  2. ENV_LIST,
  3. APPID,
  4. LOGO_DEFAULT,
  5. APPID_MAP,
  6. LOGO_KO_DEFAULT,
  7. } from '@/constants.js'
  8. import uni from './uniHooks'
  9. export default {
  10. init() {
  11. const href = `${window.location.href}`
  12. // dev
  13. if (/8080|dev-tparking/g.test(href)) {
  14. this.setEnv('qa')
  15. return
  16. }
  17. // qa
  18. if (/qa-tparking/g.test(href)) {
  19. this.setEnv('qa')
  20. return
  21. }
  22. // prod
  23. this.setEnv('prod')
  24. },
  25. setEnv(env) {
  26. // console.log(2020202020, env)
  27. uni.setStorageSync("env", env);
  28. },
  29. getEnv() {
  30. const env = uni.getStorageSync("env");
  31. if (!env) {
  32. const env = ENV_LIST[1];
  33. uni.setStorageSync("env", env.name);
  34. return env.name
  35. }
  36. return env;
  37. },
  38. cleanAllCache() {
  39. uni.clearStorageSync()
  40. },
  41. getCurEnvConst(key) {
  42. const curEnvName = this.getEnv();
  43. const curEnvConst = ENV_LIST.find(item => item.name == curEnvName);
  44. // 如果是来自 window.injectConfig
  45. if (window?.injectConfig) {
  46. console.log(464646, window?.injectConfig);
  47. console.log(474747, key);
  48. if (key === 'KIP_API') {
  49. return window?.injectConfig?.profileApi
  50. }
  51. curEnvConst.constants = {
  52. ...curEnvConst?.constants,
  53. ...window?.injectConfig
  54. }
  55. }
  56. if (key) {
  57. return curEnvConst?.constants[key];
  58. } else {
  59. return curEnvConst?.constants;
  60. }
  61. },
  62. setMiniAppOptions(options) {
  63. if (!options) {
  64. return;
  65. }
  66. uni.setStorageSync('options', options);
  67. const query = options.query
  68. if (!query.scene) {
  69. this.setMiniAppOptionsQuery(query)
  70. }
  71. },
  72. setMiniAppOptionsQuery(query) {
  73. if (!query || JSON.stringify(query) == '{}') {
  74. // 直接进入的utm_lbs是空的
  75. // const mallid = uni.getStorageSync("mallid")
  76. const defaultOptionsQuery = {
  77. channel: APPID,
  78. utm_channel: APPID,
  79. utm_method: 'dr',
  80. // utm_lbs: mallid,
  81. }
  82. query = defaultOptionsQuery;
  83. }
  84. // channel 肯定是当前小程序的appid
  85. query.channel = APPID;
  86. uni.setStorageSync("options_query", query);
  87. },
  88. getLogoByMallid(mallid) {
  89. console.log(90, mallid);
  90. const defaultLogo = isKerryOnAppid() ? LOGO_KO_DEFAULT : LOGO_DEFAULT
  91. if (!mallid) {
  92. console.error("=>[getLogoByMallid]mallid is null")
  93. return defaultLogo
  94. }
  95. const marketList = this.getMarketList()
  96. if (!marketList) {
  97. console.error("=>[setMarketList]marketList is null")
  98. return defaultLogo;
  99. }
  100. const market = marketList.find(item => item.mallid == mallid)
  101. if (!market) {
  102. console.error("=>[setMarketList]market is null")
  103. return defaultLogo;
  104. }
  105. return market?.logoPicture || defaultLogo;
  106. },
  107. getMarketList() {
  108. const marketList = uni.getStorageSync("marketList");
  109. if (!marketList || marketList.length === 0) {
  110. console.error("=>[getMarketList]marketList is empty")
  111. return []
  112. }
  113. return marketList;
  114. },
  115. }
  116. function isKerryOnAppid() {
  117. if (APPID == APPID_MAP.kerryMall) {
  118. return true;
  119. } else {
  120. return false;
  121. }
  122. }