App.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. </div>
  23. </template>
  24. <script>
  25. import { KIP_API_CODE } from '@/common/js/config';
  26. import Stomp from '@/lib/stompjs';
  27. import loginMinix from '@/mixins/login';
  28. import {
  29. kipCheckToken,
  30. kipGetNewAccessTokenByRefreshToken,
  31. } from '@/utils/api-kip';
  32. import CacheTool from '@/utils/cache-tool';
  33. import { Decrypt, Encrypt } from '@/utils/crypto';
  34. import { getIsMin, getIsWxh5, getUrlParams } from '@/utils/index.js';
  35. import KipCacheTool from '@/utils/kip-cache-tool';
  36. import log from '@/utils/log';
  37. import SockJS from '@/utils/sockjs';
  38. import uni from '@/utils/uniHooks';
  39. import { getAppIdByGroupIdAndMallId } from '@/utils/index.js';
  40. import { Toast } from 'vant';
  41. import { pathToRegexp, parse, compile } from 'path-to-regexp';
  42. export default {
  43. mixins: [loginMinix],
  44. data() {
  45. return {
  46. ws: null,
  47. stompClient: null,
  48. isInit: true,
  49. Toastloading: null,
  50. };
  51. },
  52. created() {
  53. console.log('重新打包');
  54. this.initEnv();
  55. this.init();
  56. // 初始化环境变量
  57. },
  58. computed: {
  59. cachedViews() {
  60. return this.$store.state.cachedViews.cachedViews;
  61. },
  62. passLogin() {
  63. return this.$store.state.passLogin;
  64. },
  65. key() {
  66. return this.$route.path;
  67. },
  68. },
  69. watch: {
  70. cachedViews() {
  71. console.log(49, 'cachedViews', this.cachedViews);
  72. },
  73. $route: {
  74. immediate: true,
  75. handler(route) {
  76. const {
  77. name,
  78. meta: { keepAlive },
  79. } = route;
  80. if (name && keepAlive) {
  81. this.$store.commit('cachedViews/ADD_CACHED_VIEW', route);
  82. }
  83. },
  84. },
  85. },
  86. methods: {
  87. async init() {
  88. // 如果是微信小程序。初始化wss
  89. if (getIsMin()) {
  90. // this.Toastloading = Toast.loading({
  91. // message: '加载中',
  92. // duration: 0,
  93. // forbidClick: true,
  94. // });
  95. console.log('触发重新发布功能 8?');
  96. console.log('触发重新发布功能 8?');
  97. // 每次进入页面清空 缓存数据
  98. window.localStorage.clear();
  99. this.isInit = false;
  100. console.log(window.location.href);
  101. console.log(56, this.$route);
  102. console.log('触发重新发布功能 6?');
  103. window.token = `${window.location.href}`.replace(
  104. /.*wx\/(.*)\/.*/g,
  105. '$1'
  106. );
  107. try {
  108. const options = await this.wss();
  109. // console.log(8383, options);
  110. this.$store.dispatch('baseInit', {
  111. options,
  112. callback: () => {
  113. // this.Toastloading.clear();
  114. this.isInit = true;
  115. },
  116. });
  117. } catch (err) {
  118. console.log(err);
  119. }
  120. }
  121. // 如果是微信公众号
  122. if (getIsWxh5()) {
  123. // https://developers.weixin.qq.com/community/develop/doc/000ae2cb950808f90d8bc415551800
  124. window.H5_LAUNCH_URL = location.href;
  125. // 判断用户是否登录
  126. this.micromessengerInit();
  127. }
  128. },
  129. wss() {
  130. return new Promise((resolve, reject) => {
  131. try {
  132. const socket = new SockJS(
  133. `${this.getUrl()}/hafengWebsocket?token=${window.token}`
  134. );
  135. window.stompClient = Stomp.over(socket);
  136. window.stompClient.debug = null;
  137. this.windowSendInit();
  138. window.stompClient.connect({}, (frame) => {
  139. // 请求 projectId
  140. window.toWXSendMsg({
  141. type: 'getProjectId',
  142. options: {},
  143. });
  144. window.subscribe('projectId', (options) => {
  145. resolve(options);
  146. });
  147. });
  148. } catch (err) {
  149. // console.log(err);
  150. reject(err);
  151. // callback && callback();
  152. }
  153. });
  154. },
  155. windowSendInit() {
  156. const token = window.token;
  157. // console.log(118, token);
  158. window.toWXSendMsg = function ({
  159. type = '',
  160. funcName = '',
  161. options = {},
  162. }) {
  163. /**
  164. * 向小程序端发送消息
  165. */
  166. if (!type) return;
  167. // console.log(259, '微信支付的options', options);
  168. window.stompClient.send(
  169. '/sendToWechat',
  170. {},
  171. JSON.stringify({
  172. token,
  173. data: Encrypt(
  174. JSON.stringify({
  175. type: type,
  176. funcName,
  177. options,
  178. })
  179. ),
  180. })
  181. );
  182. };
  183. // 主动订阅事件回调
  184. window.subscribe = function (type, callback) {
  185. const subscribeId = window.stompClient.subscribe(
  186. '/user/' + token + '/toH5',
  187. function (response) {
  188. try {
  189. let res = {
  190. token: '', // 微信小程序端 页面的传递过来的token
  191. data: '', // 微信小程序端 页面的传递过来的信息(已加密)
  192. };
  193. if (response.body) {
  194. res = JSON.parse(response.body);
  195. }
  196. // 检查 微信小程序端 发送过来的信息和token是否与当前页面的 token一致。并且 res.data 携带信息,在解密之后是 json 格式
  197. if (res.token && res.token === token && res.data) {
  198. const msgJson = JSON.parse(Decrypt(res.data));
  199. const reg = new RegExp(type);
  200. // 获取 projectId
  201. if (reg.test(msgJson.type)) {
  202. callback(msgJson.options, subscribeId);
  203. subscribeId.unsubscribe();
  204. return;
  205. }
  206. }
  207. } catch (err) {
  208. console.log('stomp error', err);
  209. }
  210. }
  211. );
  212. };
  213. },
  214. initEnv() {
  215. CacheTool.init();
  216. const href = window.location.href;
  217. console.log('当前页面的url地址 ', href);
  218. if (/dev-|8080/.test(href)) {
  219. window.env = 'qa';
  220. window.profileApi = 'profileApi';
  221. window.api = 'qaApi';
  222. return;
  223. }
  224. if (/qa-/.test(href)) {
  225. window.env = 'qa';
  226. window.api = 'qaApi';
  227. window.profileApi = 'profileApi';
  228. return;
  229. }
  230. window.env = 'prod';
  231. window.profileApi = 'profileApiProd';
  232. window.api = 'api';
  233. },
  234. send() {
  235. const token = window.token;
  236. window.stompClient.send(
  237. '/sendToWechat',
  238. {},
  239. JSON.stringify({
  240. token: token,
  241. data: Encrypt(
  242. JSON.stringify({
  243. type: 'openWxPay',
  244. options: {},
  245. })
  246. ),
  247. })
  248. );
  249. },
  250. // websocket 链接
  251. getUrl() {
  252. // 如果 kerry+ 这边的访问环境是 sl 或者 lt,需要把 wss 指向 qa 环境。
  253. const href = `${window.location.href}`;
  254. if (/dev-|8080/.test(href)) {
  255. return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  256. }
  257. if (/qa-/.test(href)) {
  258. return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  259. }
  260. // return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  261. return `https://crm.kerryplus.com/xcrm-api`;
  262. },
  263. micromessengerInit() {
  264. this.isInit = false;
  265. let [groupId, mallId] = window.location.pathname
  266. .split('/')
  267. .filter((elm) => elm);
  268. console.log(265, groupId, mallId);
  269. // return;
  270. if (!groupId || !mallId) {
  271. groupId = uni.getStorageSync('groupId');
  272. mallId = uni.getStorageSync('mallId');
  273. }
  274. this.$store.commit('SET_GROUP_ID', groupId);
  275. this.$store.commit('SET_MALL_ID', mallId);
  276. // 设置appid
  277. const appid = getAppIdByGroupIdAndMallId({
  278. groupId,
  279. mallId,
  280. type: 'appid',
  281. });
  282. uni.setStorageSync('appid', appid);
  283. // 如果用户没有openid
  284. const openid = uni.getStorageSync('openid');
  285. const query = getUrlParams();
  286. console.log(284, query, groupId, mallId);
  287. // return;
  288. // 设置openid
  289. this.$nextTick(() => {
  290. this.isInit = true;
  291. if (openid && openid !== 'undefined') {
  292. this.$store.commit('SET_OPENID', openid);
  293. // 用户选择不登录
  294. if (this.isLogin !== '3') {
  295. // 判断用户是否需要登录
  296. this.checkIsLogin(() => {
  297. // window.location.reload();
  298. });
  299. }
  300. } else {
  301. // 前往授权页面
  302. if (this.$route.path.indexOf('openWx') < 0) {
  303. let openWxPath = 'openWx';
  304. if (this.$route.fullPath === '/') {
  305. openWxPath = `/${groupId}/${mallId}/openWx`;
  306. }
  307. this.$router.push({
  308. path: openWxPath,
  309. query,
  310. });
  311. }
  312. }
  313. });
  314. // 获取openid
  315. // openid 在本地存在的话,走登录验证
  316. // openid 不存在的话
  317. // window.location.pathname
  318. // this.$router.replace({
  319. // path: 'openWx',
  320. // query: query,
  321. // });
  322. // 初始化微信jsdk
  323. // 初始化appid,根据 lbsId、groupId、mallId
  324. /*console.log(109, { groupId, mallId });
  325. const kipAccessToken = uni.getStorageSync('kipAccessToken');
  326. // 设置 member、groupId 和 mallId
  327. this.$store.commit(
  328. 'SET_GROUP_ID',
  329. groupId || uni.getStorageSync('groupId')
  330. );
  331. this.$store.commit('SET_MALL_ID', mallId || uni.getStorageSync('mallId'));
  332. // 如果用户没有登录的话,我们需要让用户完成授权拿到 openid
  333. /!* const openid = uni.getStorageSync('openid');
  334. // console.log(949494, openid);
  335. // 设置openid
  336. if (openid && openid !== 'undefined') {
  337. this.$store.commit('SET_OPENID', openid);
  338. } *!/
  339. // 如果用户没有openid
  340. const openid = uni.getStorageSync('openid');
  341. // 如果 存在,清空缓存,然后先去openid
  342. // console.log(949494, openid);
  343. // 设置openid
  344. if (openid && openid !== 'undefined') {
  345. this.$store.commit('SET_OPENID', openid);
  346. }
  347. if (!openid) {
  348. // 当进入其他页面时,需要先打开路由展示
  349. this.isInit = true;
  350. // 前往授权页面
  351. this.$router.replace({
  352. path: '/openWx',
  353. query: query,
  354. });
  355. return;
  356. }
  357. /!* 如果用户选择不登录 *!/
  358. const passLogin = uni.getStorageSync('passLogin');
  359. if (passLogin && /true/.test(passLogin)) {
  360. return;
  361. }
  362. if (!openid) {
  363. // 当进入其他页面时,需要先打开路由展示
  364. this.isInit = true;
  365. // 前往授权页面
  366. this.$router.replace({
  367. path: 'openWx',
  368. query: query,
  369. });
  370. return;
  371. }
  372. if (!kipAccessToken) {
  373. // 如果用户不选择登录,则不走登录逻辑
  374. this.isInit = true;
  375. this.$router.replace({
  376. path: '/login',
  377. query: query,
  378. });
  379. return;
  380. }
  381. // 如果用已经登录
  382. // 验证 kipAccessToken 是否过期,过期则刷新
  383. const _this = this;
  384. kipCheckToken(kipAccessToken)
  385. .then((resp) => {
  386. // console.log(120, resp);
  387. if (resp && resp.statusCode == '401') {
  388. log.info(
  389. `access_token已过期,使用refresh_token刷新access_token,如果refresh_token过期则删除所有数据`
  390. );
  391. // token过期之后使用refresh_token刷新access_token,如果refresh_token过期则删除所有数据
  392. const kipRefreshToken = KipCacheTool.getKipRefreshToken();
  393. kipGetNewAccessTokenByRefreshToken(kipRefreshToken)
  394. .then((result) => {
  395. // const result = resp.data;
  396. if (
  397. result &&
  398. result.code === '000000' &&
  399. result.data.access_token
  400. ) {
  401. KipCacheTool.setKipToken(result.data.access_token);
  402. log.info(
  403. `refreshToken获取成功,这是新的token:`,
  404. result.data.access_token
  405. );
  406. } else {
  407. if (
  408. result &&
  409. result.code !== KIP_API_CODE.REFRESH_TOKEN_EXPIRED
  410. ) {
  411. uni.showToast({
  412. title: result.message,
  413. duration: 2000,
  414. icon: 'none',
  415. });
  416. }
  417. console.warn('===>清除所有缓存1');
  418. log.info(`清除所有缓存1`);
  419. _this.cleanAll();
  420. this.$router.replace({
  421. path: '/login',
  422. query: query,
  423. });
  424. }
  425. })
  426. .catch((err) => {
  427. console.warn('===>清除所有缓存2');
  428. log.info(`清除所有缓存2`);
  429. _this.cleanAll();
  430. this.$router.replace({
  431. path: '/login',
  432. query: query,
  433. });
  434. });
  435. }
  436. })
  437. .catch((err) => {
  438. console.error(err);
  439. console.warn('===>清除所有缓存3');
  440. log.info(`refresh_token获取失败,清除所有缓存3`, err);
  441. _this.cleanAll();
  442. this.$router.replace({
  443. path: '/login',
  444. query: query,
  445. });
  446. });
  447. // 初始化微信
  448. console.log('初始化');
  449. // initWxJsSdkConfig(['chooseWXPay', 'scanQRCode', 'checkJsApi'],['wx-open-launch-weapp']);
  450. // initWxJsSdkConfig(['chooseWXPay']);
  451. this.$router.push({
  452. path: '/home',
  453. query: query,
  454. });*/
  455. },
  456. },
  457. };
  458. </script>
  459. <style>
  460. page {
  461. display: flex;
  462. flex-direction: column;
  463. height: 100%;
  464. }
  465. body {
  466. padding: 0;
  467. margin: 0;
  468. }
  469. </style>