index.js 12 KB

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