index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import qs from 'qs';
  2. export function getMobileOperatingSystem() {
  3. // #ifdef H5
  4. const userAgent = navigator.userAgent || navigator.vendor || window.opera;
  5. // Windows Phone must come first because its UA also contains "Android"
  6. if (/windows phone/i.test(userAgent)) {
  7. return "Windows Phone";
  8. }
  9. if (/android/i.test(userAgent)) {
  10. return "Android";
  11. }
  12. // iOS detection from: http://stackoverflow.com/a/9039885/177710
  13. if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
  14. return "iOS";
  15. }
  16. return "unknown";
  17. // #endif
  18. // #ifndef H5
  19. return "unknown";
  20. // #endif
  21. }
  22. export function getQueryParam() {
  23. // let query: Record<string, string> = {};
  24. let query = {};
  25. query = location.search
  26. .slice(1)
  27. .split('&')
  28. .map((p) => p.split('='))
  29. // .reduce((obj: Record<string, string>, pair) => {
  30. .reduce((obj, pair) => {
  31. const [key, value] = pair.map(decodeURIComponent);
  32. obj[key] = value;
  33. return obj;
  34. }, {});
  35. return query;
  36. }
  37. //
  38. export function isInWeixinH5() {
  39. // TODO: 发布前取消注释
  40. return navigator.userAgent.toLowerCase().indexOf("micromessenger") !== -1;
  41. // return true;
  42. }
  43. // 判断当前运行平台
  44. export function getPlatform() {
  45. const userAgent = navigator.userAgent.toLowerCase()
  46. // 微信小程序
  47. if (/miniprogram/g.test(userAgent)) {
  48. return "miniprogram"
  49. }
  50. // 微信公众号
  51. if (/micromessenger/g.test(userAgent)) {
  52. return "micromessenger"
  53. }
  54. // TODO: 发布前取消注释
  55. return 'browser'
  56. }
  57. export function getUrlParams(url = window.location.href) {
  58. const str = `${url}`.split('?')[1]
  59. if(!str) return {}
  60. return qs.parse(str);
  61. }