App.vue 6.0 KB

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