index.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 getIsMin() {
  64. const platform = getPlatform();
  65. return platform === 'miniprogram';
  66. }
  67. // 是否在微信公众号中运行
  68. export function getIsWxh5() {
  69. const platform = getPlatform();
  70. return platform === 'micromessenger';
  71. }
  72. // 获取appid
  73. export function getAppIdByGroupIdAndMallId( {groupId,mallId,type} ) {
  74. const platform = getPlatform();
  75. if ( platform === 'miniprogram' ) {
  76. return 'wx92c3e55fbef6b2af';
  77. }
  78. if ( platform === 'micromessenger' ) {
  79. // 后期在其他公众号上线H5应用,appid需要根据地址栏的 project 动态处理, 已预留入口
  80. // console.log(89);
  81. const env = window.env === 'qa' ? 'qa' : 'prod';
  82. let appInfo = {};
  83. Object.keys(lbsDictionary).forEach(( lbsId ) => {
  84. const elm = lbsDictionary[lbsId];
  85. // console.log(92, env, elm[env].groupId, groupId, elm[env].mallId, mallId);
  86. if ( elm[env].groupId === groupId && elm[env].mallId === mallId ) {
  87. appInfo = {
  88. appid: elm[env].appid,
  89. // secret: elm[env].secret,
  90. projectId: elm[env].projectId,
  91. };
  92. }
  93. });
  94. // console.log(101, appInfo);
  95. if ( JSON.stringify(appInfo) === '{}' ) {
  96. // groupId, mallId 错误
  97. return;
  98. }
  99. if ( type === 'appid' ) {
  100. return appInfo.appid;
  101. }
  102. if ( type === 'all' ) {
  103. return appInfo;
  104. }
  105. return 'wx907c27f16841a919';
  106. }
  107. return '';
  108. }
  109. export function getUrlParams( url = window.location.href ) {
  110. const str = `${ url }`.split('?')[1];
  111. if ( !str ) return {};
  112. return qs.parse(str);
  113. }
  114. // 根据不同环境和lsbid返回 groupId 和 mallId
  115. export function getGroupIdAndMallIdByLsbId( lbsId ) {
  116. console.log(125,lbsId);
  117. const lbsObj = lbsDictionary[lbsId];
  118. if ( window.env === 'prod' ) {
  119. return lbsObj['prod'];
  120. }
  121. if ( window.env === 'dev' ) {
  122. return lbsObj['dev'];
  123. }
  124. return lbsObj['qa'];
  125. }
  126. // 微信小程序端登录之后的回调
  127. export function wxToLoginCallback( path,callback ) {
  128. const oldPath = uni.getStorageSync('oldPath');
  129. // 如果是在微信小程序内部运行的话
  130. if ( getIsMin() && oldPath !== path ) {
  131. uni.setStorageSync('oldPath',path);
  132. // 前往登录
  133. window.toWXSendMsg({
  134. type: 'toLogin',
  135. options: {
  136. path: path,
  137. },
  138. });
  139. window.subscribe('callback',( options ) => {
  140. console.log('登录页面的回调',JSON.stringify(options));
  141. if ( options.isReload ) {
  142. console.log('刷新页面');
  143. window.location.reload();
  144. } else {
  145. console.log('刷新页面:callback');
  146. callback && callback(options);
  147. }
  148. });
  149. return;
  150. }
  151. // 如果是在微信公众号环境运行的话
  152. /*if ( getIsWxh5() ) {
  153. return
  154. }*/
  155. }
  156. export function initEnv() {
  157. const href = window.location.href;
  158. console.log('当前页面的url地址 ',href);
  159. if ( /dev-|8080/.test(href) ) {
  160. window.env = 'qa';
  161. window.profileApi = 'https://qa-apim.kerryplus.com/c/api';
  162. window.api = 'qaApi';
  163. // window.env = 'dev';
  164. // window.profileApi = 'https://dev-gateway-kip.kerryonvip.com/api';
  165. // window.api = 'devApi';
  166. // window.env = 'prod';
  167. // window.profileApi = 'https://sl-apim.kerryplus.com/c/api';
  168. // window.api = 'api';
  169. return;
  170. }
  171. if ( /qa-/.test(href) ) {
  172. window.env = 'qa';
  173. window.api = 'qaApi';
  174. window.profileApi = 'https://qa-apim.kerryplus.com/c/api';
  175. return;
  176. }
  177. if ( /sl-/.test(href) ) {
  178. window.env = 'prod';
  179. window.profileApi = 'https://sl-apim.kerryplus.com/c/api';
  180. window.api = 'api';
  181. return;
  182. }
  183. window.env = 'prod';
  184. window.profileApi = 'https://apim.kerryplus.com/c/api';
  185. window.api = 'api';
  186. }
  187. export function requestInit() {
  188. let baseURL = window.profileApi + '/temporary-parking/v1';
  189. if (window.location.href.indexOf('parking.') < 0) {
  190. // baseURL = '/msApi';
  191. }
  192. window.requestms = createAxiosByinterceptors({
  193. // baseURL: `https://dev-kip-service-internal.kerryonvip.com/`,
  194. // baseURL: `http://tp.hht.test/`,
  195. // baseURL: window.profileApi, // TODO: 微服务发布到DEV环境之后取消注释
  196. baseURL,
  197. // baseURL: `/msApi`,
  198. });
  199. }
  200. // websocket 链接
  201. export function getUrl() {
  202. return `https://crm.kerryplus.com/xcrm-api`; // TODO: 临时更改websocket域名为prod
  203. // 如果 kerry+ 这边的访问环境是 sl 或者 lt,需要把 wss 指向 qa 环境。
  204. const href = `${ window.location.href }`;
  205. if ( /dev-|8080/.test(href) ) {
  206. return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  207. }
  208. if ( /qa-/.test(href) ) {
  209. return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  210. }
  211. // return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  212. return `https://crm.kerryplus.com/xcrm-api`;
  213. }
  214. export function windowSendInit() {
  215. const token = window.token;
  216. window.toWXSendMsg = function ( {type = '',funcName = '',options = {}} ) {
  217. /**
  218. * 向小程序端发送消息
  219. */
  220. if ( !type ) return;
  221. window.stompClient.send(
  222. '/sendToWechat',
  223. {},
  224. JSON.stringify({
  225. token,
  226. data: Encrypt(
  227. JSON.stringify({
  228. type: type,
  229. funcName,
  230. options,
  231. })
  232. ),
  233. })
  234. );
  235. };
  236. // 主动订阅事件回调
  237. window.subscribe = function ( type,callback ) {
  238. const subscribeId = window.stompClient.subscribe('/user/' + token + '/toH5',function ( response ) {
  239. try {
  240. let res = {
  241. token: '', // 微信小程序端 页面的传递过来的token
  242. data: '', // 微信小程序端 页面的传递过来的信息(已加密)
  243. };
  244. if ( response.body ) {
  245. res = JSON.parse(response.body);
  246. }
  247. // 检查 微信小程序端 发送过来的信息和token是否与当前页面的 token一致。并且 res.data 携带信息,在解密之后是 json 格式
  248. if ( res.token && res.token === token && res.data ) {
  249. const msgJson = JSON.parse(Decrypt(res.data));
  250. const reg = new RegExp(type);
  251. // 获取 projectId
  252. if ( reg.test(msgJson.type) ) {
  253. callback(msgJson.options,subscribeId);
  254. subscribeId.unsubscribe();
  255. return;
  256. }
  257. }
  258. } catch ( err ) {
  259. console.log('stomp error',err);
  260. }
  261. });
  262. };
  263. }
  264. export function wssInit() {
  265. return new Promise(( resolve,reject ) => {
  266. try {
  267. const socket = new SockJS(`${ getUrl() }/hafengWebsocket?token=${ window.token }`);
  268. window.stompClient = Stomp.over(socket);
  269. window.stompClient.debug = null;
  270. windowSendInit();
  271. window.stompClient.connect({},( frame ) => {
  272. // 请求 projectId
  273. window.toWXSendMsg({
  274. type: 'getProjectId',
  275. options: {},
  276. });
  277. window.subscribe('projectId',( options ) => {
  278. resolve(options);
  279. });
  280. });
  281. } catch ( err ) {
  282. reject(err);
  283. }
  284. });
  285. }