cache-tool.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. let 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. return window.profileApi
  51. }
  52. if (curEnvConst?.constants) {
  53. curEnvConst.constants = {
  54. ...curEnvConst?.constants,
  55. ...window?.injectConfig
  56. }
  57. } else {
  58. curEnvConst = {
  59. ...curEnvConst,
  60. constants: {
  61. ...window?.injectConfig
  62. }
  63. }
  64. }
  65. }
  66. if (key) {
  67. return curEnvConst?.constants[key];
  68. } else {
  69. return curEnvConst?.constants;
  70. }
  71. },
  72. setMiniAppOptions(options) {
  73. if (!options) {
  74. return;
  75. }
  76. uni.setStorageSync('options', options);
  77. const query = options.query
  78. if (!query.scene) {
  79. this.setMiniAppOptionsQuery(query)
  80. }
  81. },
  82. setMiniAppOptionsQuery(query) {
  83. if (!query || JSON.stringify(query) == '{}') {
  84. // 直接进入的utm_lbs是空的
  85. // const mallid = uni.getStorageSync("mallid")
  86. const defaultOptionsQuery = {
  87. channel: APPID,
  88. utm_channel: APPID,
  89. utm_method: 'dr',
  90. // utm_lbs: mallid,
  91. }
  92. query = defaultOptionsQuery;
  93. }
  94. // channel 肯定是当前小程序的appid
  95. query.channel = APPID;
  96. uni.setStorageSync("options_query", query);
  97. },
  98. getLogoByMallid(mallid) {
  99. console.log(90, mallid);
  100. const defaultLogo = isKerryOnAppid() ? LOGO_KO_DEFAULT : LOGO_DEFAULT
  101. if (!mallid) {
  102. console.error("=>[getLogoByMallid]mallid is null")
  103. return defaultLogo
  104. }
  105. const marketList = this.getMarketList()
  106. if (!marketList) {
  107. console.error("=>[setMarketList]marketList is null")
  108. return defaultLogo;
  109. }
  110. const market = marketList.find(item => item.mallid == mallid)
  111. if (!market) {
  112. console.error("=>[setMarketList]market is null")
  113. return defaultLogo;
  114. }
  115. return market?.logoPicture || defaultLogo;
  116. },
  117. getMarketList() {
  118. const marketList = uni.getStorageSync("marketList");
  119. if (!marketList || marketList.length === 0) {
  120. console.error("=>[getMarketList]marketList is empty")
  121. return []
  122. }
  123. return marketList;
  124. },
  125. }
  126. function isKerryOnAppid() {
  127. if (APPID == APPID_MAP.kerryMall) {
  128. return true;
  129. } else {
  130. return false;
  131. }
  132. }