App.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <template>
  2. <div>
  3. <!-- <div @click="send">send</div>-->
  4. <router-view></router-view>
  5. </div>
  6. </template>
  7. <script>
  8. import { isMini } from '@/utils/common';
  9. // import { crmQueryMemberInfo } from '@/utils/api-crm-member';
  10. // import Ws from '@/utils/websocket';
  11. import SockJS from '@/utils/sockjs';
  12. import Stomp from '@/lib/stompjs/index';
  13. // import uni from '@/utils/uniHooks';
  14. import { Encrypt, Decrypt } from '@/utils/crypto';
  15. import CacheTool from '@/utils/cache-tool';
  16. import uni from '@/utils/uniHooks';
  17. import { mapGetters } from 'vuex';
  18. import { initWxJsSdkConfig } from '@/utils/login.js';
  19. import { getUrlParams, getPlatform } from '@/utils/index.js';
  20. // import qs from 'qs';
  21. export default {
  22. data() {
  23. return {
  24. ws: null,
  25. msg: '',
  26. isInit: false,
  27. stompClient: null,
  28. host: 'https://dev-crm-kpl.kerryprops.com.cn/xcrm-api',
  29. token: '1234',
  30. };
  31. },
  32. created() {
  33. this.init();
  34. },
  35. mounted() {
  36. // if(!this.isInit) {
  37. // this.init();
  38. // }
  39. },
  40. computed: {
  41. disabledBtn() {
  42. return this.numArr.findIndex((val) => !val) !== -1;
  43. },
  44. ...mapGetters({
  45. groupId: (state) => state.groupId,
  46. openid: (state) => state.openid,
  47. mallId: (state) => state.mallId,
  48. kipUserId: (state) => state.kipUserId,
  49. userInfo: (state) => state.userInfo,
  50. }),
  51. },
  52. methods: {
  53. async init() {
  54. // 初始化环境变量
  55. CacheTool.init();
  56. if (this.isInit) return;
  57. this.isInit = false;
  58. // 当前页面是否是在微信公众号运行:start
  59. const kipAccessToken = uni.getStorageSync('kipAccessToken');
  60. const query = getUrlParams();
  61. const platform = getPlatform();
  62. this.$store.commit('SET_GROUP_ID', query.groupId);
  63. this.$store.commit('SET_MALL_ID', query.mallid);
  64. console.log(65, kipAccessToken);
  65. // TODO true 需要取消
  66. if (platform === 'micromessenger' && !kipAccessToken || true) {
  67. // 如果用户没有登录的话,我们需要让用户完成授权拿到 openid
  68. const openid = uni.getStorageSync('openid');
  69. if (!openid && false) {
  70. // 当进入其他页面时,需要先打开路由展示
  71. this.isInit = true;
  72. // 前往授权页面
  73. this.$router.replace({
  74. path: '/openWx',
  75. query: query,
  76. });
  77. return;
  78. }
  79. // 初始化微信
  80. initWxJsSdkConfig(['chooseWXPay']);
  81. // 如果用户不选择登录,则不走登录逻辑
  82. this.isInit = true;
  83. this.$router.replace({
  84. path: '/login',
  85. query: query,
  86. });
  87. return;
  88. }
  89. // 如果用已经登录
  90. if (platform === 'micromessenger' && kipAccessToken) {
  91. this.$router.replace({
  92. path: '',
  93. query: query,
  94. });
  95. this.isInit = true;
  96. return;
  97. }
  98. // 当前页面是否是在微信公众号运行:end
  99. /* 微信小程序 webview 配置 */
  100. if (!query?.pageId?.length || !query?.token?.length) {
  101. return;
  102. }
  103. window.token = query?.token;
  104. // this.getTicket()
  105. this.wss(() => {
  106. this.$store.dispatch('baseInit', {
  107. pageId: window.location.href.replace(/.*pageId=([0-9A-Z]*).*/g, '$1'),
  108. // callback: this.wss,
  109. callback: () => {
  110. console.log(838383, this.isInit);
  111. this.isInit = true;
  112. // 当所有的数据都初始化之后再次执行
  113. this.getTicket();
  114. },
  115. });
  116. });
  117. },
  118. send() {
  119. window.stompClient.send(
  120. '/sendToWechat',
  121. {},
  122. JSON.stringify({
  123. token: window.token,
  124. data: Encrypt(
  125. JSON.stringify({
  126. type: 'openWxPay',
  127. options: {},
  128. })
  129. ),
  130. })
  131. );
  132. },
  133. wss(callback) {
  134. try {
  135. console.log(
  136. 107107,
  137. this.host + '/hafengWebsocket' + '?token=' + window.token
  138. );
  139. const socket = new SockJS(
  140. this.host + '/hafengWebsocket' + '?token=' + window.token
  141. );
  142. window.stompClient = Stomp.over(socket);
  143. window.stompClient.debug = null;
  144. const self = this;
  145. this.windowSendInit();
  146. window.stompClient.connect({}, (frame) => {
  147. callback && callback();
  148. window.subscribe('projectId', (options) => {
  149. self.$store.commit('SET_PROJECT_ID', options.projectId);
  150. self.$store.commit('SET_ACCESS_TOKEN', options.accessToken);
  151. });
  152. });
  153. } catch (err) {
  154. console.log(err);
  155. callback && callback();
  156. }
  157. },
  158. disconnect() {
  159. if (window.stompClient != null) {
  160. window.stompClient.disconnect();
  161. }
  162. },
  163. windowSendInit() {
  164. // uni.showToast({title: '哈哈哈哈'})
  165. // return
  166. window.toWXSendMsg = function ({
  167. type = '',
  168. funcName = '',
  169. options = {},
  170. }) {
  171. /**
  172. * 向小程序端发送消息
  173. */
  174. if (!type) return;
  175. window.stompClient.send(
  176. '/sendToWechat',
  177. {},
  178. JSON.stringify({
  179. token: window.token,
  180. data: Encrypt(
  181. JSON.stringify({
  182. type: type,
  183. funcName,
  184. options,
  185. })
  186. ),
  187. })
  188. );
  189. };
  190. // 主动订阅事件回调
  191. window.subscribe = function (type, callback) {
  192. const subscribeId = window.stompClient.subscribe(
  193. '/user/' + window.token + '/toH5',
  194. function (response) {
  195. try {
  196. let res = {
  197. token: '', // 微信小程序端 页面的传递过来的token
  198. data: '', // 微信小程序端 页面的传递过来的信息(已加密)
  199. };
  200. if (response.body) {
  201. res = JSON.parse(response.body);
  202. }
  203. // 检查 微信小程序端 发送过来的信息和token是否与当前页面的 token一致。并且 res.data 携带信息,在解密之后是 json 格式
  204. if (res.token && res.token === self.token && res.data) {
  205. const msgJson = JSON.parse(Decrypt(res.data));
  206. const reg = new RegExp(type);
  207. // 获取 projectId
  208. if (reg.test(msgJson.type)) {
  209. callback(msgJson.options, subscribeId);
  210. subscribeId.unsubscribe();
  211. return;
  212. }
  213. }
  214. } catch (err) {
  215. console.log('stomp error', err);
  216. }
  217. }
  218. );
  219. };
  220. },
  221. // 环境变量初始化
  222. envInit() {
  223. const href = `${window.location.href}`;
  224. // dev
  225. if (/8080|dev-/g.test(href)) {
  226. CacheTool.setEnv('qa');
  227. return;
  228. }
  229. // qa
  230. if (/qa-/g.test(href)) {
  231. CacheTool.setEnv('dev');
  232. return;
  233. }
  234. // prod
  235. CacheTool.setEnv('prod');
  236. },
  237. // h5获取配置---公众号支付
  238. getTicket() {
  239. console.log(173);
  240. let self = this;
  241. var datas = {
  242. groupId: this.groupId,
  243. mallid: this.mallId,
  244. url: window.location.href.split('#')[0],
  245. };
  246. self.$md(datas);
  247. console.log(199, datas);
  248. uni.request({
  249. // url: self.$baseURL + "api/1.0/login/getTicket",
  250. // url: 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api/' + "api/1.0/login/getTicket",
  251. method: 'POST',
  252. data: datas,
  253. header: JSON.parse(uni.getStorageSync('handleUser')),
  254. success: (res) => {
  255. console.log(206, res);
  256. if (res.data.code == 0) {
  257. self.$wx.config({
  258. debug: false, // 开启调试模式
  259. appId: res.data.data.appId, // 必填,公众号的唯一标识
  260. timestamp: res.data.data.timestamp, // 必填,生成签名的时间戳
  261. nonceStr: res.data.data.nonceStr, // 必填,生成签名的随机串
  262. signature: res.data.data.signature, // 必填,签名
  263. jsApiList: ['chooseWXPay'], // 必填,需要使用的JS接口列表
  264. });
  265. self.$wx.ready(function () {
  266. self.$wx.checkJsApi({
  267. jsApiList: ['chooseWXPay'],
  268. success: (res) => {
  269. console.log('checked api:', res);
  270. },
  271. fail: (err) => {
  272. console.log('check api fail:', err);
  273. },
  274. });
  275. });
  276. self.$wx.error(function (res) {
  277. console.log('err', res);
  278. });
  279. } else {
  280. uni.showToast({
  281. title: res.data.msg,
  282. duration: 2000,
  283. icon: 'none',
  284. });
  285. }
  286. },
  287. });
  288. },
  289. },
  290. };
  291. </script>
  292. <style>
  293. page {
  294. display: flex;
  295. flex-direction: column;
  296. height: 100%;
  297. }
  298. body {
  299. padding: 0;
  300. margin: 0;
  301. }
  302. </style>