index.js 11 KB

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