App.vue 10 KB

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