App.vue 7.7 KB

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