App.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <template>
  2. <div>
  3. <keep-alive v-if="isInit" :include="cachedViews" :max="15">
  4. <router-view :key="key" />
  5. </keep-alive>
  6. <div v-else>
  7. <br />
  8. <br />
  9. <van-skeleton title :row="3" />
  10. <br />
  11. <br />
  12. <van-skeleton title :row="4" />
  13. <br />
  14. <br />
  15. <van-skeleton title :row="3" />
  16. <br />
  17. <br />
  18. <van-skeleton title :row="4" />
  19. <br />
  20. <br />
  21. </div>
  22. <wx-points-commit ref="wxPointsCommit"></wx-points-commit>
  23. </div>
  24. </template>
  25. <script>
  26. import loginMinix from '@/mixins/login';
  27. import { getIsMin, getIsWxh5, getUrlParams, initEnv, requestInit, wssInit, theCommunicationBetweenWechatAndH5IsNormal, isReloadBefore, getsTheCurrentTopic } from '@/utils/index.js';
  28. import uni from '@/utils/uniHooks';
  29. import { getAppIdByGroupIdAndMallId } from '@/utils/index.js';
  30. import sensorsFn from '@/plugins/sensors';
  31. export default {
  32. mixins: [loginMinix],
  33. async created() {
  34. await initEnv();
  35. await requestInit();
  36. await this.init();
  37. // await sensorsFn(); // 埋点初始化
  38. // 初始化环境变量
  39. },
  40. computed: {
  41. cachedViews() {
  42. return this.$store.state.cachedViews.cachedViews;
  43. },
  44. passLogin() {
  45. return this.$store.state.passLogin;
  46. },
  47. key() {
  48. return this.$route.path;
  49. },
  50. isInit() {
  51. return this.$store.state.isInit;
  52. },
  53. },
  54. watch: {
  55. cachedViews() {
  56. console.log('cachedViews(打印已经缓存的页面)', this.cachedViews);
  57. },
  58. $route: {
  59. immediate: true,
  60. handler(route) {
  61. const {
  62. name,
  63. meta: { keepAlive },
  64. } = route;
  65. if (name && keepAlive) {
  66. this.$store.commit('cachedViews/ADD_CACHED_VIEW', route);
  67. }
  68. },
  69. },
  70. },
  71. methods: {
  72. async init() {
  73. uni.setStorageSync('env', window.env);
  74. // 如果是微信小程序。初始化wss
  75. if (getIsMin()) {
  76. // 如果刷新之后没有与微信小程序建立链接则提示用户,关闭小程序重新进入
  77. const isReloadLoop = await isReloadBefore();
  78. if (isReloadLoop) {
  79. uni.removeStorageSync('isReload');
  80. const { theme } = getsTheCurrentTopic();
  81. this.$dialog({
  82. title: '温馨提示',
  83. message: '当前网络异常,请重新进入小程序',
  84. confirmButtonColor: this.$theme[theme].primaryColor,
  85. });
  86. return;
  87. /* const isReload = await theCommunicationBetweenWechatAndH5IsNormal();
  88. if (isReload) {
  89. uni.removeStorageSync('isReload');
  90. const { theme } = getsTheCurrentTopic();
  91. this.$dialog({
  92. title: '温馨提示',
  93. message: '当前网络异常,请重新进入小程序',
  94. confirmButtonColor: this.$theme[theme].primaryColor,
  95. });
  96. return;
  97. } */
  98. }
  99. if (window.location.href.indexOf('cryptojs') > -1) {
  100. return;
  101. }
  102. // 保留 carList
  103. const carList = uni.getStorageSync('carList');
  104. // 每次进入页面清空 缓存数据
  105. window.localStorage.clear();
  106. if (carList) {
  107. uni.setStorageSync('carList', JSON.parse(carList));
  108. }
  109. this.$store.commit('SET_IS_INIT', false);
  110. window.token = `${window.location.href}`.replace(/.*wx\/(.*)\/.*/g, '$1');
  111. try {
  112. const options = await wssInit();
  113. this.$store.dispatch('baseInit', {
  114. options,
  115. callback: () => {
  116. this.$store.commit('SET_IS_INIT', true);
  117. // 无感积分逻辑
  118. // this.wxEasyPointsCommitStatusInit();
  119. },
  120. });
  121. } catch (err) {
  122. console.log(err);
  123. }
  124. }
  125. // 如果是微信公众号
  126. if (getIsWxh5()) {
  127. // 判断用户是否登录
  128. this.micromessengerInit();
  129. }
  130. },
  131. micromessengerInit() {
  132. this.$store.commit('SET_IS_INIT', false);
  133. let path = '';
  134. let [groupId, mallId] = window.location.pathname.split('/').filter((elm) => elm);
  135. // return;
  136. // 如果groupId 是 tparking
  137. if (groupId === 'tparking') {
  138. [path, groupId, mallId] = window.location.pathname.split('/').filter((elm) => elm);
  139. }
  140. if (!groupId || !mallId) {
  141. groupId = uni.getStorageSync('groupId');
  142. mallId = uni.getStorageSync('mallId');
  143. }
  144. this.$store.commit('SET_GROUP_ID', groupId);
  145. this.$store.commit('SET_MALL_ID', mallId);
  146. // 设置appid
  147. const appid = getAppIdByGroupIdAndMallId({
  148. groupId,
  149. mallId,
  150. type: 'appid',
  151. });
  152. uni.setStorageSync('appid', appid);
  153. // 如果用户没有openid
  154. const openid = uni.getStorageSync('openid');
  155. const query = getUrlParams();
  156. // return;
  157. // 设置openid
  158. this.$nextTick(() => {
  159. this.$store.commit('SET_IS_INIT', true);
  160. if (openid && openid !== 'undefined') {
  161. this.$store.commit('SET_OPENID', openid);
  162. // 用户选择不登录
  163. if (this.isLogin !== '3') {
  164. this.$store.commit('SET_IS_INIT', false);
  165. setTimeout(() => {
  166. this.$store.commit('SET_IS_INIT', true);
  167. }, 700);
  168. // 判断用户是否需要登录
  169. this.checkIsLogin(() => {
  170. this.$store.commit('SET_IS_INIT', true);
  171. // window.location.reload();
  172. this.$store.dispatch('getUserDetail');
  173. this.$store.commit('SET_MEMBER', uni.getStorageSync('member'));
  174. // 无感积分逻辑
  175. this.wxEasyPointsCommitStatusInit();
  176. // uni.getStorageSync();
  177. });
  178. }
  179. } else {
  180. // 前往授权页面
  181. if (this.$route.path.indexOf('openWx') < 0) {
  182. let openWxPath = 'openWx';
  183. if (this.$route.fullPath === '/') {
  184. openWxPath = `/${groupId}/${mallId}/openWx`;
  185. }
  186. this.$router.push({
  187. path: openWxPath,
  188. query,
  189. });
  190. }
  191. }
  192. });
  193. },
  194. // 无感积分相关
  195. async wxEasyPointsCommitStatusInit() {
  196. return;
  197. this.$refs.wxPointsCommit.open();
  198. // 判断用户是否登陆
  199. if (uni.getStorageSync('openid') && uni.getStorageSync('member') && uni.getStorageSync('member') !== {}) {
  200. const easyPointsCommitStatus = await getWxEasyPointsCommitStatus();
  201. if (easyPointsCommitStatus) {
  202. this.$refs.wxPointsCommit.open();
  203. }
  204. }
  205. },
  206. },
  207. };
  208. </script>
  209. <style lang="less">
  210. /* 如果有其他引入,合并即可,该文件最好放第一行 */
  211. // @import '@kip/ui-mobile/theme/theme.css';
  212. @import '~@/kui/theme/theme.css';
  213. </style>
  214. <style>
  215. page {
  216. display: flex;
  217. flex-direction: column;
  218. height: 100%;
  219. }
  220. body {
  221. padding: 0;
  222. margin: 0;
  223. }
  224. </style>