index.js 9.1 KB

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