index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. return;
  182. }
  183. if ( /qa-/.test(href) ) {
  184. window.env = 'qa';
  185. window.api = 'qaApi';
  186. window.profileApi = 'https://qa-apim.kerryplus.com/c/api';
  187. window.cmrApi = 'https://qa-crm.kerryplus.com/xcrm-api/api';
  188. return;
  189. }
  190. if ( /sl-/.test(href) ) {
  191. window.env = 'prod';
  192. window.profileApi = 'https://sl-apim.kerryplus.com/c/api';
  193. window.cmrApi = 'https://sl-crm.kerryplus.com/xcrm-api/api';
  194. window.api = 'api';
  195. return;
  196. }
  197. window.env = 'prod';
  198. window.profileApi = 'https://apim.kerryplus.com/c/api';
  199. window.cmrApi = 'https://crm.kerryplus.com/xcrm-api/api';
  200. window.api = 'api';
  201. }
  202. export function requestInit() {
  203. let baseURL = window.profileApi + '/temporary-parking/v1';
  204. if (window.location.href.indexOf('parking.') < 0) {
  205. // baseURL = '/msApi';
  206. }
  207. window.requestms = createAxiosByinterceptors({
  208. // baseURL: `https://dev-kip-service-internal.kerryonvip.com/`,
  209. // baseURL: `http://tp.hht.test/`,
  210. // baseURL: window.profileApi, // TODO: 微服务发布到DEV环境之后取消注释
  211. baseURL,
  212. // baseURL: `/msApi`,
  213. });
  214. }
  215. // websocket 链接
  216. export function getUrl() {
  217. return `https://crm.kerryplus.com/xcrm-api`; // TODO: 临时更改websocket域名为prod
  218. // 如果 kerry+ 这边的访问环境是 sl 或者 lt,需要把 wss 指向 qa 环境。
  219. const href = `${ window.location.href }`;
  220. if ( /dev-|8080/.test(href) ) {
  221. return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  222. }
  223. if ( /qa-/.test(href) ) {
  224. return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  225. }
  226. // return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  227. return `https://crm.kerryplus.com/xcrm-api`;
  228. }
  229. export function windowSendInit() {
  230. const token = window.token;
  231. window.toWXSendMsg = function ( {type = '',funcName = '',options = {}} ) {
  232. if(isAlipayClient) {
  233. my.postMessage({
  234. name:'h5',
  235. body: {
  236. token,
  237. data: {
  238. type: type,
  239. funcName,
  240. options,
  241. }
  242. }
  243. });
  244. return
  245. }
  246. /**
  247. * 向小程序端发送消息
  248. */
  249. if ( !type ) return;
  250. window.stompClient.send(
  251. '/sendToWechat',
  252. {},
  253. JSON.stringify({
  254. token,
  255. data: Encrypt(
  256. JSON.stringify({
  257. type: type,
  258. funcName,
  259. options,
  260. })
  261. ),
  262. })
  263. );
  264. };
  265. // 主动订阅事件回调
  266. window.subscribe = function ( type,callback ) {
  267. if(isAlipayClient) {
  268. my.onMessage = function (response) {
  269. console.log('293my.onMessage', response)
  270. callback(response.body.data);
  271. }
  272. return
  273. }
  274. const subscribeId = window.stompClient.subscribe('/user/' + token + '/toH5',function ( response ) {
  275. try {
  276. let res = {
  277. token: '', // 微信小程序端 页面的传递过来的token
  278. data: '', // 微信小程序端 页面的传递过来的信息(已加密)
  279. };
  280. if ( response.body ) {
  281. res = JSON.parse(response.body);
  282. }
  283. // 检查 微信小程序端 发送过来的信息和token是否与当前页面的 token一致。并且 res.data 携带信息,在解密之后是 json 格式
  284. if ( res.token && res.token === token && res.data ) {
  285. const msgJson = JSON.parse(Decrypt(res.data));
  286. const reg = new RegExp(type);
  287. // 获取 projectId
  288. if ( reg.test(msgJson.type) ) {
  289. callback(msgJson.options,subscribeId);
  290. subscribeId.unsubscribe();
  291. return;
  292. }
  293. }
  294. } catch ( err ) {
  295. console.log('stomp error',err);
  296. }
  297. });
  298. };
  299. }
  300. export function wssInit() {
  301. return new Promise(( resolve,reject ) => {
  302. try {
  303. const socket = new SockJS(`${ getUrl() }/hafengWebsocket?token=${ window.token }`);
  304. window.stompClient = Stomp.over(socket);
  305. window.stompClient.debug = false;
  306. windowSendInit();
  307. window.stompClient.connect({},( frame ) => {
  308. // 请求 projectId
  309. window.toWXSendMsg({
  310. type: 'getProjectId',
  311. options: {},
  312. });
  313. window.subscribe('projectId',( options ) => {
  314. resolve(options);
  315. });
  316. });
  317. } catch ( err ) {
  318. reject(err);
  319. }
  320. });
  321. }
  322. // 获取当前的主题
  323. export function getsTheCurrentTopic() {
  324. let theme = 'theme-mall'
  325. let componentName = ''
  326. const source = uni.getStorageSync('source')
  327. const custTypeId = uni.getStorageSync('custTypeId')
  328. const com = ['purpleCom', 'blueCom', 'greenCom', 'officeBlueCom', 'purpleCom'];
  329. if (custTypeId < 3 || !custTypeId) {
  330. componentName = 'baseParkingFeeCom';
  331. theme = 'theme-mall'
  332. } else {
  333. componentName = com[custTypeId];
  334. theme = 'theme-office'
  335. }
  336. if (source === 'KIP') {
  337. theme = 'theme-office'
  338. }
  339. if (source === 'PUDONG') {
  340. theme = 'theme-pudong'
  341. }
  342. if (source === 'JINGAN') {
  343. theme = 'theme-jingan'
  344. }
  345. return { theme, componentName }
  346. }
  347. export function setToken() {
  348. window.token = `${window.location.href}`.replace(/.*wx\/(.*)\/.*/g, '$1');
  349. }
  350. // 如果用户的 vipcode 或者 userid 不存在提示用户返回首页重新进入
  351. export function backLbsHome() {
  352. const { theme } = getsTheCurrentTopic();
  353. const dialog = Dialog.alert({
  354. title: '温馨提示',
  355. message: '当前网络异常,请返回首页重试',
  356. confirmButtonColor: kipTheme[theme].primaryColor,
  357. beforeClose: async (action, done) => {
  358. const source = uni.getStorageSync('source')
  359. try {
  360. if(window?.__wxjs_environment === 'miniprogram') {
  361. wx?.miniProgram?.reLaunch({
  362. url: source === 'KIP' ? '/pages/tabbar/home/home' : '/pages/automatic/automaticIndex'
  363. })
  364. }
  365. if (isAlipayClient) {
  366. my?.reLaunch({
  367. url: '/pages/automatic/automaticIndex'
  368. })
  369. }
  370. } catch (e) {
  371. console.log('90909090', e)
  372. }
  373. done()
  374. }
  375. });
  376. }
  377. export * from './common/websocket.js'
  378. export * from './common/localStorage.js'
  379. export * from './alipayClient/index.js'