App.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <template>
  2. <div>
  3. <!-- <div @click="scanQRCode">scanQRCode</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 {
  29. getIsMin, getIsWxh5, getUrlParams, initEnv, requestInit,
  30. wssInit, theCommunicationBetweenWechatAndH5IsNormal,
  31. isReloadBefore, getsTheCurrentTopic, alipayClientInit,
  32. localStorageInit, setToken, windowSendInit, getQuery
  33. } from '@/utils/index.js';
  34. import uni from '@/utils/uniHooks';
  35. import { getAppIdByGroupIdAndMallId } from '@/utils/index.js';
  36. import sensorsFn from '@/plugins/sensors';
  37. import {getTokenById} from '@/utils/api-kip'
  38. import { initWxJsSdkConfig } from '@/utils/login';
  39. export default {
  40. mixins: [loginMinix],
  41. async created() {
  42. await initEnv();
  43. await requestInit();
  44. await this.init();
  45. // await sensorsFn(); // 埋点初始化
  46. // 初始化环境变量
  47. },
  48. computed: {
  49. cachedViews() {
  50. return this.$store.state.cachedViews.cachedViews;
  51. },
  52. passLogin() {
  53. return this.$store.state.passLogin;
  54. },
  55. key() {
  56. return this.$route.path;
  57. },
  58. isInit() {
  59. return this.$store.state.isInit;
  60. },
  61. },
  62. watch: {
  63. cachedViews() {
  64. console.log('cachedViews(打印已经缓存的页面)', this.cachedViews);
  65. },
  66. $route: {
  67. immediate: true,
  68. handler(route) {
  69. const {
  70. name,
  71. meta: { keepAlive },
  72. } = route;
  73. if (name && keepAlive) {
  74. this.$store.commit('cachedViews/ADD_CACHED_VIEW', route);
  75. }
  76. },
  77. },
  78. },
  79. methods: {
  80. scanQRCode() {
  81. try {
  82. // 验证 jsdk
  83. this.$wx.scanQRCode({
  84. desc: 'scanQRCode desc',
  85. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  86. // scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码,默认二者都有
  87. success: ( res ) => {
  88. console.log(237,res);
  89. // runScanFn(res);∑
  90. // this.formMsg.deviceCode = res.resultStr;
  91. },
  92. error: ( res ) => {
  93. console.log(242,res);
  94. },
  95. });
  96. } catch (err) {
  97. console.log(99, err);
  98. }
  99. },
  100. async init() {
  101. window.isWxV2 = false
  102. // this.$store.commit('SET_IS_INIT', true);
  103. // return
  104. // 微信重构改造 第二版本
  105. if(window.location.href.indexOf('tparking/wxV2') > -1) {
  106. localStorageInit();
  107. window.isWxV2 = true
  108. uni.showLoading({
  109. title: '加载中',
  110. });
  111. console.log(window.location.search);
  112. const options = getQuery(window.location.href)
  113. // 获取 token
  114. let accessToken = ''
  115. if(options.sessionId) {
  116. accessToken = await getTokenById({
  117. tokenId: options.sessionId
  118. })
  119. }
  120. this.$store.dispatch('baseInitV2', {
  121. options: {
  122. ...options,
  123. accessToken: accessToken
  124. },
  125. callback: () => {
  126. console.log(108, this.$route);
  127. this.$store.commit('SET_IS_INIT', true);
  128. // 缓存
  129. // this.$store.commit('cachedViews/ADD_CACHED_VIEW', {
  130. // name: 'wxV2'
  131. // });
  132. // 无感积分逻辑
  133. // this.wxEasyPointsCommitStatusInit();
  134. },
  135. });
  136. return
  137. }
  138. uni.setStorageSync('env', window.env);
  139. // 如果是支付宝小程序:初始化
  140. if (isAlipayClient) {
  141. setToken()
  142. localStorageInit();
  143. windowSendInit()
  144. try {
  145. this.$store.commit('SET_IS_INIT', false);
  146. const options = await alipayClientInit();
  147. await this.$store.dispatch('baseInit', {
  148. options,
  149. callback: () => {
  150. this.$store.commit('SET_IS_INIT', true);
  151. // 无感积分逻辑
  152. // this.wxEasyPointsCommitStatusInit();
  153. },
  154. });
  155. } catch (err) {
  156. console.log(err);
  157. }
  158. return
  159. }
  160. // 如果是微信小程序。初始化wss
  161. if (getIsMin()) {
  162. // 如果刷新之后没有与微信小程序建立链接则提示用户,关闭小程序重新进入
  163. const isReloadLoop = await isReloadBefore();
  164. if (isReloadLoop) {
  165. uni.removeStorageSync('isReload');
  166. const { theme } = getsTheCurrentTopic();
  167. this.$dialog({
  168. title: '温馨提示',
  169. message: '当前网络异常,请重新进入小程序',
  170. confirmButtonColor: this.$theme[theme].primaryColor,
  171. });
  172. return;
  173. /* const isReload = await theCommunicationBetweenWechatAndH5IsNormal();
  174. if (isReload) {
  175. uni.removeStorageSync('isReload');
  176. const { theme } = getsTheCurrentTopic();
  177. this.$dialog({
  178. title: '温馨提示',
  179. message: '当前网络异常,请重新进入小程序',
  180. confirmButtonColor: this.$theme[theme].primaryColor,
  181. });
  182. return;
  183. } */
  184. }
  185. if (window.location.href.indexOf('cryptojs') > -1) {
  186. return;
  187. }
  188. // // 保留 carList
  189. // const carList = uni.getStorageSync('carList');
  190. // // 保留 buildingId
  191. // const buildingId = window.localStorage.getItem('buildingId')
  192. // // 每次进入页面清空 缓存数据
  193. // window.localStorage.clear();
  194. // if (carList) {
  195. // uni.setStorageSync('carList', JSON.parse(carList));
  196. // }
  197. // if (buildingId) {
  198. // window.localStorage.setItem('buildingId', buildingId)
  199. // }
  200. setToken();
  201. localStorageInit();
  202. this.$store.commit('SET_IS_INIT', false);
  203. try {
  204. const options = await wssInit();
  205. this.$store.dispatch('baseInit', {
  206. options,
  207. callback: () => {
  208. this.$store.commit('SET_IS_INIT', true);
  209. // 无感积分逻辑
  210. // this.wxEasyPointsCommitStatusInit();
  211. },
  212. });
  213. } catch (err) {
  214. console.log(err);
  215. }
  216. }
  217. // 如果是微信公众号
  218. if (getIsWxh5()) {
  219. // 判断用户是否登录
  220. this.micromessengerInit();
  221. }
  222. },
  223. micromessengerInit() {
  224. this.$store.commit('SET_IS_INIT', false);
  225. let path = '';
  226. let [groupId, mallId] = window.location.pathname.split('/').filter((elm) => elm);
  227. // return;
  228. // 如果groupId 是 tparking
  229. if (groupId === 'tparking') {
  230. [path, groupId, mallId] = window.location.pathname.split('/').filter((elm) => elm);
  231. }
  232. if (!groupId || !mallId) {
  233. groupId = uni.getStorageSync('groupId');
  234. mallId = uni.getStorageSync('mallId');
  235. }
  236. this.$store.commit('SET_GROUP_ID', groupId);
  237. this.$store.commit('SET_MALL_ID', mallId);
  238. // 设置appid
  239. const appid = getAppIdByGroupIdAndMallId({
  240. groupId,
  241. mallId,
  242. type: 'appid',
  243. });
  244. uni.setStorageSync('appid', appid);
  245. // 如果用户没有openid
  246. const openid = uni.getStorageSync('openid');
  247. const query = getUrlParams();
  248. // return;
  249. // 设置openid
  250. this.$nextTick(() => {
  251. this.$store.commit('SET_IS_INIT', true);
  252. if (openid && openid !== 'undefined') {
  253. this.$store.commit('SET_OPENID', openid);
  254. // 用户选择不登录
  255. if (this.isLogin !== 'loginDenied') {
  256. this.$store.commit('SET_IS_INIT', false);
  257. setTimeout(() => {
  258. this.$store.commit('SET_IS_INIT', true);
  259. }, 700);
  260. // 判断用户是否需要登录
  261. this.checkIsLogin(() => {
  262. this.$store.commit('SET_IS_INIT', true);
  263. // window.location.reload();
  264. this.$store.dispatch('getUserDetail');
  265. this.$store.commit('SET_MEMBER', uni.getStorageSync('member'));
  266. // 无感积分逻辑
  267. this.wxEasyPointsCommitStatusInit();
  268. // uni.getStorageSync();
  269. });
  270. }
  271. } else {
  272. // 前往授权页面
  273. if (this.$route.path.indexOf('openWx') < 0) {
  274. let openWxPath = 'openWx';
  275. if (this.$route.fullPath === '/') {
  276. openWxPath = `/${groupId}/${mallId}/openWx`;
  277. }
  278. this.$router.push({
  279. path: openWxPath,
  280. query,
  281. });
  282. }
  283. }
  284. });
  285. },
  286. // 无感积分相关
  287. async wxEasyPointsCommitStatusInit() {
  288. return;
  289. this.$refs.wxPointsCommit.open();
  290. // 判断用户是否登陆
  291. if (uni.getStorageSync('openid') && uni.getStorageSync('member') && uni.getStorageSync('member') !== {}) {
  292. const easyPointsCommitStatus = await getWxEasyPointsCommitStatus();
  293. if (easyPointsCommitStatus) {
  294. this.$refs.wxPointsCommit.open();
  295. }
  296. }
  297. },
  298. },
  299. };
  300. </script>
  301. <style lang="less">
  302. /* 如果有其他引入,合并即可,该文件最好放第一行 */
  303. // @import '@kip/ui-mobile/theme/theme.css';
  304. @import '~@/kui/theme/theme.css';
  305. </style>
  306. <style>
  307. page {
  308. display: flex;
  309. flex-direction: column;
  310. height: 100%;
  311. }
  312. body {
  313. padding: 0;
  314. margin: 0;
  315. }
  316. </style>