index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. import { createAxiosByinterceptors } from '@/api/request';
  2. import Stomp from "@/lib/stompjs";
  3. import { Decrypt,Encrypt } from "@/utils/crypto";
  4. import SockJS from "@/utils/sockjs";
  5. import qs from 'qs';
  6. import { lbsDictionary } from '@/common/js/BaseDictionary';
  7. import uni from './uniHooks';
  8. import {Dialog} from "vant";
  9. import kipTheme from "@/kui/theme/theme";
  10. import Vue from 'vue';
  11. export function getMobileOperatingSystem() {
  12. // #ifdef H5
  13. const userAgent = navigator.userAgent || navigator.vendor || window.opera;
  14. // Windows Phone must come first because its UA also contains "Android"
  15. if ( /windows phone/i.test(userAgent) ) {
  16. return 'Windows Phone';
  17. }
  18. if ( /android/i.test(userAgent) ) {
  19. return 'Android';
  20. }
  21. // iOS detection from: http://stackoverflow.com/a/9039885/177710
  22. if ( /iPad|iPhone|iPod/.test(userAgent) && !window.MSStream ) {
  23. return 'iOS';
  24. }
  25. return 'unknown';
  26. // #endif
  27. // #ifndef H5
  28. return 'unknown';
  29. // #endif
  30. }
  31. export function getQueryParam() {
  32. // let query: Record<string, string> = {};
  33. let query = {};
  34. query = location.search
  35. .slice(1)
  36. .split('&')
  37. .map(( p ) => p.split('='))
  38. // .reduce((obj: Record<string, string>, pair) => {
  39. .reduce(( obj,pair ) => {
  40. const [key,value] = pair.map(decodeURIComponent);
  41. obj[key] = value;
  42. return obj;
  43. },{});
  44. return query;
  45. }
  46. //
  47. export function isInWeixinH5() {
  48. // TODO: 发布前取消注释
  49. return navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1;
  50. // return true;
  51. }
  52. // 判断当前运行平台
  53. export function getPlatform() {
  54. const userAgent = navigator.userAgent.toLowerCase();
  55. // 微信小程序
  56. if ( /miniprogram/g.test(userAgent) ) {
  57. return 'miniprogram';
  58. }
  59. // 微信公众号
  60. if ( /micromessenger/g.test(userAgent) ) {
  61. return 'micromessenger';
  62. }
  63. return 'miniprogram'; // TODO: 上线前改为其他
  64. }
  65. // 是否是支付宝
  66. export function isAlipay() {
  67. const userAgent = navigator.userAgent.toLowerCase();
  68. return /alipayclient/g.test(userAgent)
  69. }
  70. // 是否在微信小程序中运行
  71. export function getIsMin() {
  72. const platform = getPlatform();
  73. // console.log('platform====>80', platform);
  74. return platform === 'miniprogram';
  75. }
  76. // 是否在微信公众号中运行
  77. export function getIsWxh5() {
  78. const platform = getPlatform();
  79. return platform === 'micromessenger';
  80. }
  81. // 获取appid
  82. export function getAppIdByGroupIdAndMallId( {groupId,mallId,type} ) {
  83. const platform = getPlatform();
  84. if ( platform === 'miniprogram' ) {
  85. return 'wx92c3e55fbef6b2af';
  86. }
  87. if ( platform === 'micromessenger' ) {
  88. // 后期在其他公众号上线H5应用,appid需要根据地址栏的 project 动态处理, 已预留入口
  89. // console.log(89);
  90. const env = window.env === 'qa' ? 'qa' : 'prod';
  91. let appInfo = {};
  92. Object.keys(lbsDictionary).forEach(( lbsId ) => {
  93. const elm = lbsDictionary[lbsId];
  94. // console.log(92, env, elm[env].groupId, groupId, elm[env].mallId, mallId);
  95. if ( elm[env].groupId === groupId && elm[env].mallId === mallId ) {
  96. appInfo = {
  97. appid: elm[env].appid,
  98. // secret: elm[env].secret,
  99. projectId: elm[env].projectId,
  100. };
  101. }
  102. });
  103. // console.log(101, appInfo);
  104. if ( JSON.stringify(appInfo) === '{}' ) {
  105. // groupId, mallId 错误
  106. return;
  107. }
  108. if ( type === 'appid' ) {
  109. return appInfo.appid;
  110. }
  111. if ( type === 'all' ) {
  112. return appInfo;
  113. }
  114. return 'wx907c27f16841a919';
  115. }
  116. return '';
  117. }
  118. export function getUrlParams( url = window.location.href ) {
  119. const str = `${ url }`.split('?')[1];
  120. if ( !str ) return {};
  121. return qs.parse(str);
  122. }
  123. // 根据不同环境和lsbid返回 groupId 和 mallId
  124. export function getGroupIdAndMallIdByLsbId( lbsId ) {
  125. // console.log(125,lbsId);
  126. const lbsObj = lbsDictionary[lbsId];
  127. if ( window.env === 'prod' ) {
  128. return lbsObj['prod'];
  129. }
  130. if ( window.env === 'dev' ) {
  131. return lbsObj['dev'];
  132. }
  133. return lbsObj['qa'];
  134. }
  135. // 微信小程序端登录之后的回调
  136. export function wxToLoginCallback( path,callback ) {
  137. const oldPath = uni.getStorageSync('oldPath');
  138. // 如果是在微信小程序内部运行的话
  139. if ( getIsMin() && oldPath !== path ) {
  140. uni.setStorageSync('oldPath',path);
  141. // 前往登录
  142. window.toWXSendMsg({
  143. type: 'toLogin',
  144. options: {
  145. path: path,
  146. },
  147. });
  148. window.subscribe('callback',( options ) => {
  149. // console.log('登录页面的回调',JSON.stringify(options));
  150. if ( options.isReload || Vue.prototype.isAlipayClient && options?.options?.isReload) {
  151. // console.log('刷新页面');
  152. window.location.reload();
  153. } else {
  154. // console.log('刷新页面:callback');
  155. callback && callback(options);
  156. uni.setStorageSync('oldPath','');
  157. }
  158. });
  159. return;
  160. }
  161. // 如果是在微信公众号环境运行的话
  162. /*if ( getIsWxh5() ) {
  163. return
  164. }*/
  165. }
  166. export function initEnv() {
  167. const origin = window.location.origin;
  168. console.log('当前页面的url地址 ',origin);
  169. if ( origin.indexOf('https://dev-') > -1 || origin.indexOf('http://127.0.0.1') > -1 || origin.indexOf('http://localhost:') > -1) {
  170. window.env = 'qa';
  171. window.profileApi = 'https://qa-apim.kerryplus.com/c/api';
  172. window.cmrApi = 'https://qa-crm.kerryplus.com/xcrm-api/api';
  173. window.api = 'qaApi';
  174. // window.env = 'dev';
  175. // window.profileApi = 'https://dev-gateway-kip.kerryonvip.com/api';
  176. // window.cmrApi = 'https://dev-crm.kerryplus.com/xcrm-api/api';
  177. // window.api = 'devApi';
  178. // window.env = 'prod';
  179. // window.profileApi = 'https://sl-apim.kerryplus.com/c/api';
  180. // window.cmrApi = 'https://sl-crm.kerryplus.com/xcrm-api/api';
  181. // window.api = 'api';
  182. window.wechatOfficialAccountId = 'wxb81a622ed6d60adf'
  183. return;
  184. }
  185. if ( origin.indexOf('https://dev-') > -1) {
  186. window.env = 'dev';
  187. window.profileApi = 'https://dev-gateway-kip.kerryonvip.com/api';
  188. window.cmrApi = 'https://dev-crm.kerryplus.com/xcrm-api/api';
  189. window.api = 'devApi';
  190. window.wechatOfficialAccountId = 'wxb81a622ed6d60adf'
  191. return;
  192. }
  193. if ( origin.indexOf('https://qa-') > -1 ) {
  194. window.env = 'qa';
  195. window.api = 'qaApi';
  196. window.profileApi = 'https://qa-apim.kerryplus.com/c/api';
  197. window.cmrApi = 'https://qa-crm.kerryplus.com/xcrm-api/api';
  198. window.wechatOfficialAccountId = 'wxb81a622ed6d60adf'
  199. return;
  200. }
  201. if ( origin.indexOf('https://sl-') > -1 ) {
  202. window.env = 'prod';
  203. window.profileApi = 'https://sl-apim.kerryplus.com/c/api';
  204. window.cmrApi = 'https://sl-crm.kerryplus.com/xcrm-api/api';
  205. window.api = 'api';
  206. window.wechatOfficialAccountId = 'wxb150c7d193e8662d'
  207. return;
  208. }
  209. if ( origin.indexOf('https://lt-') > -1 ) {
  210. window.env = 'lt';
  211. window.profileApi = 'https://lt-gateway-kip-internal.kerryplus.com/api';
  212. window.cmrApi = 'https://lt-crm.kerryplus.com/xcrm-api/api';
  213. window.api = 'api';
  214. window.wechatOfficialAccountId = 'wx2bd99ca94d6acd7e'
  215. return;
  216. }
  217. window.env = 'prod';
  218. window.profileApi = 'https://apim.kerryplus.com/c/api';
  219. window.cmrApi = 'https://crm.kerryplus.com/xcrm-api/api';
  220. window.api = 'api';
  221. window.wechatOfficialAccountId = 'wxb150c7d193e8662d'
  222. }
  223. export function requestInit() {
  224. let baseURL = window.profileApi + '/temporary-parking/v1';
  225. if (window.location.href.indexOf('parking.') < 0) {
  226. // baseURL = '/msApi';
  227. }
  228. window.requestms = createAxiosByinterceptors({
  229. // baseURL: `https://dev-kip-service-internal.kerryonvip.com/`,
  230. // baseURL: `http://tp.hht.test/`,
  231. // baseURL: window.profileApi, // TODO: 微服务发布到DEV环境之后取消注释
  232. baseURL,
  233. // baseURL: `/msApi`,
  234. });
  235. }
  236. // websocket 链接
  237. export function getUrl() {
  238. return `https://crm.kerryplus.com/xcrm-api`; // TODO: 临时更改websocket域名为prod
  239. // 如果 kerry+ 这边的访问环境是 sl 或者 lt,需要把 wss 指向 qa 环境。
  240. const href = `${ window.location.href }`;
  241. if ( /dev-|8080/.test(href) ) {
  242. return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  243. }
  244. if ( /qa-/.test(href) ) {
  245. return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  246. }
  247. // return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  248. return `https://crm.kerryplus.com/xcrm-api`;
  249. }
  250. export function windowSendInit() {
  251. const token = window.token;
  252. window.toWXSendMsg = function ( {type = '',funcName = '',options = {}} ) {
  253. if(Vue.prototype.isAlipayClient) {
  254. my.postMessage({
  255. name:'h5',
  256. body: {
  257. token,
  258. data: {
  259. type: type,
  260. funcName,
  261. options,
  262. }
  263. }
  264. });
  265. return
  266. }
  267. /**
  268. * 向小程序端发送消息
  269. */
  270. if ( !type ) return;
  271. window.stompClient.send(
  272. '/sendToWechat',
  273. {},
  274. JSON.stringify({
  275. token,
  276. data: Encrypt(
  277. JSON.stringify({
  278. type: type,
  279. funcName,
  280. options,
  281. })
  282. ),
  283. })
  284. );
  285. };
  286. // 主动订阅事件回调
  287. window.subscribe = function ( type,callback ) {
  288. if(Vue.prototype.isAlipayClient) {
  289. my.onMessage = function (response) {
  290. console.log('293my.onMessage', response)
  291. callback(response.body.data);
  292. }
  293. return
  294. }
  295. const subscribeId = window.stompClient.subscribe('/user/' + token + '/toH5',function ( response ) {
  296. try {
  297. let res = {
  298. token: '', // 微信小程序端 页面的传递过来的token
  299. data: '', // 微信小程序端 页面的传递过来的信息(已加密)
  300. };
  301. if ( response.body ) {
  302. res = JSON.parse(response.body);
  303. }
  304. // 检查 微信小程序端 发送过来的信息和token是否与当前页面的 token一致。并且 res.data 携带信息,在解密之后是 json 格式
  305. if ( res.token && res.token === token && res.data ) {
  306. const msgJson = JSON.parse(Decrypt(res.data));
  307. const reg = new RegExp(type);
  308. // 获取 projectId
  309. if ( reg.test(msgJson.type) ) {
  310. callback(msgJson.options,subscribeId);
  311. subscribeId.unsubscribe();
  312. return;
  313. }
  314. }
  315. } catch ( err ) {
  316. console.log('stomp error',err);
  317. }
  318. });
  319. };
  320. }
  321. export function wssInit() {
  322. return new Promise(( resolve,reject ) => {
  323. try {
  324. const socket = new SockJS(`${ getUrl() }/hafengWebsocket?token=${ window.token }`);
  325. window.stompClient = Stomp.over(socket);
  326. window.stompClient.debug = false;
  327. windowSendInit();
  328. window.stompClient.connect({},( frame ) => {
  329. // 请求 projectId
  330. window.toWXSendMsg({
  331. type: 'getProjectId',
  332. options: {},
  333. });
  334. window.subscribe('projectId',( options ) => {
  335. resolve(options);
  336. });
  337. });
  338. } catch ( err ) {
  339. reject(err);
  340. }
  341. });
  342. }
  343. // 获取当前的主题
  344. export function getsTheCurrentTopic() {
  345. let theme = 'theme-mall'
  346. let componentName = ''
  347. const source = uni.getStorageSync('source')
  348. const custTypeId = uni.getStorageSync('custTypeId')
  349. const com = ['purpleCom', 'blueCom', 'greenCom', 'officeBlueCom', 'purpleCom'];
  350. if (custTypeId < 3 || !custTypeId) {
  351. componentName = 'baseParkingFeeCom';
  352. theme = 'theme-mall'
  353. } else {
  354. componentName = com[custTypeId];
  355. theme = 'theme-office'
  356. }
  357. if (source === 'KIP') {
  358. theme = 'theme-office'
  359. }
  360. if (source === 'PUDONG') {
  361. theme = 'theme-pudong'
  362. }
  363. if (source === 'JINGAN') {
  364. theme = 'theme-jingan'
  365. }
  366. return { theme, componentName }
  367. }
  368. export function setToken() {
  369. window.token = `${window.location.href}`.replace(/.*wx\/(.*)\/.*/g, '$1');
  370. }
  371. // 如果用户的 vipcode 或者 userid 不存在提示用户返回首页重新进入
  372. export function backLbsHome() {
  373. const { theme } = getsTheCurrentTopic();
  374. const source = uni.getStorageSync('source')
  375. const dialog = Dialog.alert({
  376. title: '温馨提示',
  377. message: source === 'KIP' ? '当前网络异常,请返回首页重试' : '登录异常,请重新登录',
  378. confirmButtonColor: kipTheme[theme].primaryColor,
  379. beforeClose: async (action, done) => {
  380. try {
  381. if(window?.__wxjs_environment === 'miniprogram') {
  382. wx?.miniProgram?.reLaunch({
  383. url: source === 'KIP' ? '/pages/tabbar/home/home' : "/pages/package-parkingFee/parkingFeeWebViewLogin?needLogin=1" // 去 login 页面
  384. })
  385. }
  386. if (Vue.prototype.isAlipayClient) {
  387. my?.reLaunch({
  388. url: "/pages/package-parkingFee/parkingFeeWebViewLogin?needLogin=1" // 去 login 页面 1 去登录
  389. })
  390. }
  391. } catch (e) {
  392. console.log(e)
  393. }
  394. done()
  395. }
  396. });
  397. }
  398. export function toLogin() {
  399. if (window.__wxjs_environment === 'miniprogram') {
  400. wx.miniProgram.redirectTo({
  401. "url": "/pages/package-parkingFee/parkingFeeWebViewLogin?needLogin=1" // 去 login 页面 1 去登录
  402. })
  403. }
  404. }
  405. export * from './common/websocket.js'
  406. export * from './common/localStorage.js'
  407. export * from './common/function.js'
  408. export * from './common/lbsIdCommon.js'
  409. export * from './alipayClient/index.js'