App.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. const isReload = await theCommunicationBetweenWechatAndH5IsNormal();
  80. if (isReload) {
  81. const { theme } = getsTheCurrentTopic();
  82. this.$dialog({
  83. title: '温馨提示',
  84. message: '当前网络异常,请重新进入小程序',
  85. confirmButtonColor: this.$theme[theme].primaryColor,
  86. });
  87. return;
  88. }
  89. }
  90. if (window.location.href.indexOf('cryptojs') > -1) {
  91. return;
  92. }
  93. // 保留 carList
  94. const carList = uni.getStorageSync('carList');
  95. // 每次进入页面清空 缓存数据
  96. window.localStorage.clear();
  97. if (carList) {
  98. uni.setStorageSync('carList', JSON.parse(carList));
  99. }
  100. this.$store.commit('SET_IS_INIT', false);
  101. window.token = `${window.location.href}`.replace(/.*wx\/(.*)\/.*/g, '$1');
  102. try {
  103. const options = await wssInit();
  104. this.$store.dispatch('baseInit', {
  105. options,
  106. callback: () => {
  107. this.$store.commit('SET_IS_INIT', true);
  108. // 无感积分逻辑
  109. // this.wxEasyPointsCommitStatusInit();
  110. },
  111. });
  112. } catch (err) {
  113. console.log(err);
  114. }
  115. }
  116. // 如果是微信公众号
  117. if (getIsWxh5()) {
  118. // 判断用户是否登录
  119. this.micromessengerInit();
  120. }
  121. },
  122. micromessengerInit() {
  123. this.$store.commit('SET_IS_INIT', false);
  124. let path = '';
  125. let [groupId, mallId] = window.location.pathname.split('/').filter((elm) => elm);
  126. // return;
  127. // 如果groupId 是 tparking
  128. if (groupId === 'tparking') {
  129. [path, groupId, mallId] = window.location.pathname.split('/').filter((elm) => elm);
  130. }
  131. if (!groupId || !mallId) {
  132. groupId = uni.getStorageSync('groupId');
  133. mallId = uni.getStorageSync('mallId');
  134. }
  135. this.$store.commit('SET_GROUP_ID', groupId);
  136. this.$store.commit('SET_MALL_ID', mallId);
  137. // 设置appid
  138. const appid = getAppIdByGroupIdAndMallId({
  139. groupId,
  140. mallId,
  141. type: 'appid',
  142. });
  143. uni.setStorageSync('appid', appid);
  144. // 如果用户没有openid
  145. const openid = uni.getStorageSync('openid');
  146. const query = getUrlParams();
  147. // return;
  148. // 设置openid
  149. this.$nextTick(() => {
  150. this.$store.commit('SET_IS_INIT', true);
  151. if (openid && openid !== 'undefined') {
  152. this.$store.commit('SET_OPENID', openid);
  153. // 用户选择不登录
  154. if (this.isLogin !== '3') {
  155. this.$store.commit('SET_IS_INIT', false);
  156. setTimeout(() => {
  157. this.$store.commit('SET_IS_INIT', true);
  158. }, 700);
  159. // 判断用户是否需要登录
  160. this.checkIsLogin(() => {
  161. this.$store.commit('SET_IS_INIT', true);
  162. // window.location.reload();
  163. this.$store.dispatch('getUserDetail');
  164. this.$store.commit('SET_MEMBER', uni.getStorageSync('member'));
  165. // 无感积分逻辑
  166. this.wxEasyPointsCommitStatusInit();
  167. // uni.getStorageSync();
  168. });
  169. }
  170. } else {
  171. // 前往授权页面
  172. if (this.$route.path.indexOf('openWx') < 0) {
  173. let openWxPath = 'openWx';
  174. if (this.$route.fullPath === '/') {
  175. openWxPath = `/${groupId}/${mallId}/openWx`;
  176. }
  177. this.$router.push({
  178. path: openWxPath,
  179. query,
  180. });
  181. }
  182. }
  183. });
  184. },
  185. // 无感积分相关
  186. async wxEasyPointsCommitStatusInit() {
  187. return;
  188. this.$refs.wxPointsCommit.open();
  189. // 判断用户是否登陆
  190. if (uni.getStorageSync('openid') && uni.getStorageSync('member') && uni.getStorageSync('member') !== {}) {
  191. const easyPointsCommitStatus = await getWxEasyPointsCommitStatus();
  192. if (easyPointsCommitStatus) {
  193. this.$refs.wxPointsCommit.open();
  194. }
  195. }
  196. },
  197. },
  198. };
  199. </script>
  200. <style lang="less">
  201. /* 如果有其他引入,合并即可,该文件最好放第一行 */
  202. // @import '@kip/ui-mobile/theme/theme.css';
  203. @import '~@/kui/theme/theme.css';
  204. </style>
  205. <style>
  206. page {
  207. display: flex;
  208. flex-direction: column;
  209. height: 100%;
  210. }
  211. body {
  212. padding: 0;
  213. margin: 0;
  214. }
  215. </style>