index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import qs from 'qs';
  2. import { lbsDictionary } from '@/common/js/BaseDictionary';
  3. export function getMobileOperatingSystem() {
  4. // #ifdef H5
  5. const userAgent = navigator.userAgent || navigator.vendor || window.opera;
  6. // Windows Phone must come first because its UA also contains "Android"
  7. if (/windows phone/i.test(userAgent)) {
  8. return 'Windows Phone';
  9. }
  10. if (/android/i.test(userAgent)) {
  11. return 'Android';
  12. }
  13. // iOS detection from: http://stackoverflow.com/a/9039885/177710
  14. if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
  15. return 'iOS';
  16. }
  17. return 'unknown';
  18. // #endif
  19. // #ifndef H5
  20. return 'unknown';
  21. // #endif
  22. }
  23. export function getQueryParam() {
  24. // let query: Record<string, string> = {};
  25. let query = {};
  26. query = location.search
  27. .slice(1)
  28. .split('&')
  29. .map((p) => p.split('='))
  30. // .reduce((obj: Record<string, string>, pair) => {
  31. .reduce((obj, pair) => {
  32. const [key, value] = pair.map(decodeURIComponent);
  33. obj[key] = value;
  34. return obj;
  35. }, {});
  36. return query;
  37. }
  38. //
  39. export function isInWeixinH5() {
  40. // TODO: 发布前取消注释
  41. return navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1;
  42. // return true;
  43. }
  44. // 判断当前运行平台
  45. export function getPlatform() {
  46. const userAgent = navigator.userAgent.toLowerCase();
  47. // 微信小程序
  48. if (/miniprogram/g.test(userAgent)) {
  49. return 'miniprogram';
  50. }
  51. // 微信公众号
  52. if (/micromessenger/g.test(userAgent)) {
  53. return 'micromessenger';
  54. }
  55. return 'browser';
  56. }
  57. // 是否在微信小程序中运行
  58. export function getIsMin() {
  59. const platform = getPlatform();
  60. return platform === 'miniprogram';
  61. }
  62. // 是否在微信公众号中运行
  63. export function getIsWxh5() {
  64. const platform = getPlatform();
  65. return platform === 'micromessenger';
  66. }
  67. // 获取appid
  68. export function getAppIdByGroupIdAndMallId({ groupId, mallId, type }) {
  69. const platform = getPlatform();
  70. if (platform === 'miniprogram') {
  71. return 'wx92c3e55fbef6b2af';
  72. }
  73. if (platform === 'micromessenger') {
  74. // 后期在其他公众号上线H5应用,appid需要根据地址栏的 project 动态处理, 已预留入口
  75. // console.log(89);
  76. const env = window.env === 'qa' ? 'qa' : 'prod';
  77. let appInfo = {};
  78. Object.keys(lbsDictionary).forEach((lbsId) => {
  79. const elm = lbsDictionary[lbsId];
  80. // console.log(92, env, elm[env].groupId, groupId, elm[env].mallId, mallId);
  81. if (elm[env].groupId === groupId && elm[env].mallId === mallId) {
  82. appInfo = {
  83. appid: elm[env].appid,
  84. // secret: elm[env].secret,
  85. projectId: elm[env].projectId,
  86. };
  87. }
  88. });
  89. // console.log(101, appInfo);
  90. if (JSON.stringify(appInfo) === '{}') {
  91. // groupId, mallId 错误
  92. return;
  93. }
  94. if (type === 'appid') {
  95. return appInfo.appid;
  96. }
  97. if (type === 'all') {
  98. return appInfo;
  99. }
  100. return 'wx907c27f16841a919';
  101. }
  102. return '';
  103. }
  104. export function getUrlParams(url = window.location.href) {
  105. const str = `${url}`.split('?')[1];
  106. if (!str) return {};
  107. return qs.parse(str);
  108. }
  109. // 根据不同环境和lsbid返回 groupId 和 mallId
  110. export function getGroupIdAndMallIdByLsbId(lbsId) {
  111. const lbsObj = lbsDictionary[lbsId];
  112. if (window.env === 'prod') {
  113. return lbsObj['prod'];
  114. }
  115. return lbsObj['qa'];
  116. }
  117. // 微信小程序端登录之后的回调
  118. export function wxToLoginCallback(path, callback) {
  119. // 如果是在微信小程序内部运行的话
  120. if (getIsMin()) {
  121. // 前往登录
  122. window.toWXSendMsg({
  123. type: 'toLogin',
  124. options: {
  125. path: path,
  126. },
  127. });
  128. window.subscribe('callback', (options) => {
  129. console.log('登录成功之后的回调', JSON.stringify(options))
  130. if (options.isReload) {
  131. window.location.reload();
  132. } else {
  133. callback && callback(options);
  134. }
  135. });
  136. return;
  137. }
  138. // 如果是在微信公众号环境运行的话
  139. /*if ( getIsWxh5() ) {
  140. return
  141. }*/
  142. }