App.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 { createAxiosByinterceptors } from '@/api/request';
  27. import Stomp from '@/lib/stompjs';
  28. import loginMinix from '@/mixins/login';
  29. import { Decrypt, Encrypt } from '@/utils/crypto';
  30. import { getIsMin, getIsWxh5, getUrlParams } from '@/utils/index.js';
  31. import SockJS from '@/utils/sockjs';
  32. import uni from '@/utils/uniHooks';
  33. import { getAppIdByGroupIdAndMallId } from '@/utils/index.js';
  34. export default {
  35. mixins: [loginMinix],
  36. async created() {
  37. await this.initEnv();
  38. await this.requestInit();
  39. await this.init();
  40. // 初始化环境变量
  41. },
  42. computed: {
  43. cachedViews() {
  44. return this.$store.state.cachedViews.cachedViews;
  45. },
  46. passLogin() {
  47. return this.$store.state.passLogin;
  48. },
  49. key() {
  50. return this.$route.path;
  51. },
  52. isInit() {
  53. return this.$store.state.isInit;
  54. },
  55. },
  56. watch: {
  57. cachedViews() {
  58. console.log('cachedViews(打印已经缓存的页面)', this.cachedViews);
  59. },
  60. $route: {
  61. immediate: true,
  62. handler(route) {
  63. const {
  64. name,
  65. meta: { keepAlive },
  66. } = route;
  67. if (name && keepAlive) {
  68. this.$store.commit('cachedViews/ADD_CACHED_VIEW', route);
  69. }
  70. },
  71. },
  72. },
  73. methods: {
  74. async init() {
  75. uni.setStorageSync('env', window.env);
  76. // 如果是微信小程序。初始化wss
  77. if (getIsMin()) {
  78. // 保留 carList
  79. const carList = uni.getStorageSync('carList');
  80. // 每次进入页面清空 缓存数据
  81. window.localStorage.clear();
  82. if (carList) {
  83. uni.setStorageSync('carList', JSON.parse(carList));
  84. }
  85. this.$store.commit('SET_IS_INIT', false);
  86. window.token = `${window.location.href}`.replace(/.*wx\/(.*)\/.*/g, '$1');
  87. try {
  88. const options = await this.wss();
  89. this.$store.dispatch('baseInit', {
  90. options,
  91. callback: () => {
  92. this.$store.commit('SET_IS_INIT', true);
  93. // 无感积分逻辑
  94. this.wxEasyPointsCommitStatusInit();
  95. },
  96. });
  97. } catch (err) {
  98. console.log(err);
  99. }
  100. }
  101. // 如果是微信公众号
  102. if (getIsWxh5()) {
  103. // 判断用户是否登录
  104. this.micromessengerInit();
  105. }
  106. },
  107. wss() {
  108. return new Promise((resolve, reject) => {
  109. try {
  110. const socket = new SockJS(`${this.getUrl()}/hafengWebsocket?token=${window.token}`);
  111. window.stompClient = Stomp.over(socket);
  112. window.stompClient.debug = null;
  113. this.windowSendInit();
  114. window.stompClient.connect({}, (frame) => {
  115. // 请求 projectId
  116. window.toWXSendMsg({
  117. type: 'getProjectId',
  118. options: {},
  119. });
  120. window.subscribe('projectId', (options) => {
  121. resolve(options);
  122. });
  123. });
  124. } catch (err) {
  125. // console.log(err);
  126. reject(err);
  127. // callback && callback();
  128. }
  129. });
  130. },
  131. windowSendInit() {
  132. const token = window.token;
  133. // console.log(118, token);
  134. window.toWXSendMsg = function ({ type = '', funcName = '', options = {} }) {
  135. /**
  136. * 向小程序端发送消息
  137. */
  138. if (!type) return;
  139. // console.log(259, '微信支付的options', options);
  140. window.stompClient.send(
  141. '/sendToWechat',
  142. {},
  143. JSON.stringify({
  144. token,
  145. data: Encrypt(
  146. JSON.stringify({
  147. type: type,
  148. funcName,
  149. options,
  150. })
  151. ),
  152. })
  153. );
  154. };
  155. // 主动订阅事件回调
  156. window.subscribe = function (type, callback) {
  157. const subscribeId = window.stompClient.subscribe('/user/' + token + '/toH5', function (response) {
  158. try {
  159. let res = {
  160. token: '', // 微信小程序端 页面的传递过来的token
  161. data: '', // 微信小程序端 页面的传递过来的信息(已加密)
  162. };
  163. if (response.body) {
  164. res = JSON.parse(response.body);
  165. }
  166. // 检查 微信小程序端 发送过来的信息和token是否与当前页面的 token一致。并且 res.data 携带信息,在解密之后是 json 格式
  167. if (res.token && res.token === token && res.data) {
  168. const msgJson = JSON.parse(Decrypt(res.data));
  169. const reg = new RegExp(type);
  170. // 获取 projectId
  171. if (reg.test(msgJson.type)) {
  172. callback(msgJson.options, subscribeId);
  173. subscribeId.unsubscribe();
  174. return;
  175. }
  176. }
  177. } catch (err) {
  178. console.log('stomp error', err);
  179. }
  180. });
  181. };
  182. },
  183. initEnv() {
  184. const href = window.location.href;
  185. console.log('当前页面的url地址 ', href);
  186. if (/dev-|8080/.test(href)) {
  187. // window.env = 'qa';
  188. // window.profileApi = 'https://qa-apim.kerryplus.com/c/api';
  189. // window.api = 'qaApi';
  190. window.env = 'dev';
  191. window.profileApi = 'https://dev-gateway-kip.kerryonvip.com/api';
  192. window.api = 'devApi';
  193. return;
  194. }
  195. if (/qa-/.test(href)) {
  196. window.env = 'qa';
  197. window.api = 'qaApi';
  198. window.profileApi = 'https://qa-apim.kerryplus.com/c/api';
  199. return;
  200. }
  201. window.env = 'prod';
  202. window.profileApi = 'https://apim.kerryplus.com/c/api';
  203. window.api = 'api';
  204. },
  205. requestInit() {
  206. window.requestms = createAxiosByinterceptors({
  207. // baseURL: `https://dev-kip-service-internal.kerryonvip.com/`,
  208. // baseURL: `http://tp.hht.test/`,
  209. // baseURL: window.profileApi, // TODO: 微服务发布到DEV环境之后取消注释
  210. baseURL: `/msApi`,
  211. });
  212. },
  213. // websocket 链接
  214. getUrl() {
  215. // 如果 kerry+ 这边的访问环境是 sl 或者 lt,需要把 wss 指向 qa 环境。
  216. const href = `${window.location.href}`;
  217. if (/dev-|8080/.test(href)) {
  218. return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  219. }
  220. if (/qa-/.test(href)) {
  221. return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  222. }
  223. // return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  224. return `https://crm.kerryplus.com/xcrm-api`;
  225. },
  226. micromessengerInit() {
  227. this.$store.commit('SET_IS_INIT', false);
  228. let path = '';
  229. let [groupId, mallId] = window.location.pathname.split('/').filter((elm) => elm);
  230. console.log(265, groupId, mallId);
  231. // return;
  232. // 如果groupId 是 tparking
  233. if (groupId === 'tparking') {
  234. [path, groupId, mallId] = window.location.pathname.split('/').filter((elm) => elm);
  235. }
  236. if (!groupId || !mallId) {
  237. groupId = uni.getStorageSync('groupId');
  238. mallId = uni.getStorageSync('mallId');
  239. }
  240. this.$store.commit('SET_GROUP_ID', groupId);
  241. this.$store.commit('SET_MALL_ID', mallId);
  242. // 设置appid
  243. const appid = getAppIdByGroupIdAndMallId({
  244. groupId,
  245. mallId,
  246. type: 'appid',
  247. });
  248. uni.setStorageSync('appid', appid);
  249. // 如果用户没有openid
  250. const openid = uni.getStorageSync('openid');
  251. const query = getUrlParams();
  252. console.log(284, query, groupId, mallId);
  253. // return;
  254. // 设置openid
  255. this.$nextTick(() => {
  256. this.$store.commit('SET_IS_INIT', true);
  257. if (openid && openid !== 'undefined') {
  258. this.$store.commit('SET_OPENID', openid);
  259. // 用户选择不登录
  260. if (this.isLogin !== '3') {
  261. this.$store.commit('SET_IS_INIT', false);
  262. setTimeout(() => {
  263. this.$store.commit('SET_IS_INIT', true);
  264. }, 700);
  265. // 判断用户是否需要登录
  266. this.checkIsLogin(() => {
  267. this.$store.commit('SET_IS_INIT', true);
  268. // window.location.reload();
  269. this.$store.dispatch('getUserDetail');
  270. this.$store.commit('SET_MEMBER', uni.getStorageSync('member'));
  271. // 无感积分逻辑
  272. this.wxEasyPointsCommitStatusInit();
  273. // uni.getStorageSync();
  274. });
  275. }
  276. } else {
  277. // 前往授权页面
  278. if (this.$route.path.indexOf('openWx') < 0) {
  279. let openWxPath = 'openWx';
  280. if (this.$route.fullPath === '/') {
  281. openWxPath = `/${groupId}/${mallId}/openWx`;
  282. }
  283. this.$router.push({
  284. path: openWxPath,
  285. query,
  286. });
  287. }
  288. }
  289. });
  290. },
  291. // 无感积分相关
  292. async wxEasyPointsCommitStatusInit() {
  293. return
  294. this.$refs.wxPointsCommit.open();
  295. // 判断用户是否登陆
  296. if (uni.getStorageSync('openid') && uni.getStorageSync('member') && uni.getStorageSync('member') !== {}) {
  297. const easyPointsCommitStatus = await getWxEasyPointsCommitStatus()
  298. if (easyPointsCommitStatus) {
  299. this.$refs.wxPointsCommit.open();
  300. }
  301. }
  302. }
  303. },
  304. };
  305. </script>
  306. <style lang="less">
  307. /* 如果有其他引入,合并即可,该文件最好放第一行 */
  308. // @import '@kip/ui-mobile/theme/theme.css';
  309. @import '~@/kui/theme/theme.css';
  310. </style>
  311. <style>
  312. page {
  313. display: flex;
  314. flex-direction: column;
  315. height: 100%;
  316. }
  317. body {
  318. padding: 0;
  319. margin: 0;
  320. }
  321. </style>