App.vue 7.0 KB

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