index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. return lbsObj['qa'];
  122. }
  123. // 微信小程序端登录之后的回调
  124. export function wxToLoginCallback(path, callback) {
  125. const oldPath = uni.getStorageSync('oldPath');
  126. // 如果是在微信小程序内部运行的话
  127. if (getIsMin() && oldPath !== path) {
  128. uni.setStorageSync('oldPath', path);
  129. // 前往登录
  130. window.toWXSendMsg({
  131. type: 'toLogin',
  132. options: {
  133. path: path,
  134. },
  135. });
  136. window.subscribe('callback', (options) => {
  137. console.log('登录页面的回调', JSON.stringify(options));
  138. if (options.isReload) {
  139. console.log('刷新页面');
  140. window.location.reload();
  141. } else {
  142. console.log('刷新页面:callback');
  143. callback && callback(options);
  144. }
  145. });
  146. return;
  147. }
  148. // 如果是在微信公众号环境运行的话
  149. /*if ( getIsWxh5() ) {
  150. return
  151. }*/
  152. }
  153. export function initEnv() {
  154. const href = window.location.href;
  155. console.log('当前页面的url地址 ', href);
  156. if (/dev-|8080/.test(href)) {
  157. // window.env = 'qa';
  158. // window.profileApi = 'https://qa-apim.kerryplus.com/c/api';
  159. // window.api = 'qaApi';
  160. window.env = 'dev';
  161. window.profileApi = 'https://dev-gateway-kip.kerryonvip.com/api';
  162. window.api = 'devApi';
  163. return;
  164. }
  165. if (/qa-/.test(href)) {
  166. window.env = 'qa';
  167. window.api = 'qaApi';
  168. window.profileApi = 'https://qa-apim.kerryplus.com/c/api';
  169. return;
  170. }
  171. window.env = 'prod';
  172. window.profileApi = 'https://apim.kerryplus.com/c/api';
  173. window.api = 'api';
  174. }
  175. export function requestInit() {
  176. let baseURL = window.profileApi + '/temporary-parking/v1';
  177. if (window.location.href.indexOf('tparking.') < 0) {
  178. baseURL = '/msApi';
  179. }
  180. window.requestms = createAxiosByinterceptors({
  181. // baseURL: `https://dev-kip-service-internal.kerryonvip.com/`,
  182. // baseURL: `http://tp.hht.test/`,
  183. // baseURL: window.profileApi, // TODO: 微服务发布到DEV环境之后取消注释
  184. baseURL,
  185. // baseURL: `/msApi`,
  186. });
  187. }
  188. // websocket 链接
  189. export function getUrl() {
  190. return `https://crm.kerryplus.com/xcrm-api`; // TODO: 临时更改websocket域名为prod
  191. // 如果 kerry+ 这边的访问环境是 sl 或者 lt,需要把 wss 指向 qa 环境。
  192. const href = `${window.location.href}`;
  193. if (/dev-|8080/.test(href)) {
  194. return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  195. }
  196. if (/qa-/.test(href)) {
  197. return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  198. }
  199. // return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  200. return `https://crm.kerryplus.com/xcrm-api`;
  201. }
  202. export function windowSendInit() {
  203. const token = window.token;
  204. window.toWXSendMsg = function ({ type = '', funcName = '', options = {} }) {
  205. /**
  206. * 向小程序端发送消息
  207. */
  208. if (!type) return;
  209. window.stompClient.send(
  210. '/sendToWechat',
  211. {},
  212. JSON.stringify({
  213. token,
  214. data: Encrypt(
  215. JSON.stringify({
  216. type: type,
  217. funcName,
  218. options,
  219. })
  220. ),
  221. })
  222. );
  223. };
  224. // 主动订阅事件回调
  225. window.subscribe = function (type, callback) {
  226. const subscribeId = window.stompClient.subscribe('/user/' + token + '/toH5', function (response) {
  227. try {
  228. let res = {
  229. token: '', // 微信小程序端 页面的传递过来的token
  230. data: '', // 微信小程序端 页面的传递过来的信息(已加密)
  231. };
  232. if (response.body) {
  233. res = JSON.parse(response.body);
  234. }
  235. // 检查 微信小程序端 发送过来的信息和token是否与当前页面的 token一致。并且 res.data 携带信息,在解密之后是 json 格式
  236. if (res.token && res.token === token && res.data) {
  237. const msgJson = JSON.parse(Decrypt(res.data));
  238. const reg = new RegExp(type);
  239. // 获取 projectId
  240. if (reg.test(msgJson.type)) {
  241. callback(msgJson.options, subscribeId);
  242. subscribeId.unsubscribe();
  243. return;
  244. }
  245. }
  246. } catch (err) {
  247. console.log('stomp error', err);
  248. }
  249. });
  250. };
  251. }
  252. export function wssInit() {
  253. return new Promise((resolve, reject) => {
  254. try {
  255. const socket = new SockJS(`${getUrl()}/hafengWebsocket?token=${window.token}`);
  256. window.stompClient = Stomp.over(socket);
  257. window.stompClient.debug = null;
  258. windowSendInit();
  259. window.stompClient.connect({}, (frame) => {
  260. // 请求 projectId
  261. window.toWXSendMsg({
  262. type: 'getProjectId',
  263. options: {},
  264. });
  265. window.subscribe('projectId', (options) => {
  266. resolve(options);
  267. });
  268. });
  269. } catch (err) {
  270. reject(err);
  271. }
  272. });
  273. }