index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { spread } from "@/api/user";
  2. import Cache from "@/utils/cache";
  3. /**
  4. * 静默授权绑定上下级,使用在已经登录后扫描了别人的推广二维码
  5. * @param {Object} puid
  6. */
  7. export function silenceBindingSpread() {
  8. //#ifdef H5 || APP
  9. let puid = Cache.get("spread");
  10. //#endif
  11. //#ifdef MP
  12. let puid = getApp().globalData.spid;
  13. //#endif
  14. puid = parseInt(puid);
  15. if (Number.isNaN(puid)) {
  16. puid = 0;
  17. }
  18. if (puid) {
  19. spread(puid)
  20. .then((res) => {})
  21. .catch((res) => {});
  22. //#ifdef H5
  23. Cache.clear("spread");
  24. //#endif
  25. //#ifdef MP
  26. getApp().globalData.spid = 0;
  27. getApp().globalData.code = 0;
  28. //#endif
  29. } else {
  30. Cache.set("spread", 0);
  31. }
  32. }
  33. export function isWeixin() {
  34. return navigator.userAgent.toLowerCase().indexOf("micromessenger") !== -1;
  35. }
  36. export function getNetworkType() {
  37. return new Promise((resolve, reject) => {
  38. uni.getNetworkType({
  39. success: (res) => {
  40. // 无网络时 networkType 为 'none'
  41. if (res.networkType === "none") {
  42. resolve(1); // 无网络
  43. } else {
  44. resolve(0); // 有网络
  45. }
  46. },
  47. fail: (err) => {
  48. reject(err);
  49. },
  50. });
  51. });
  52. }
  53. export function parseQuery() {
  54. const res = {};
  55. const query = (location.href.split("?")[1] || "")
  56. .trim()
  57. .replace(/^(\?|#|&)/, "");
  58. if (!query) {
  59. return res;
  60. }
  61. query.split("&").forEach((param) => {
  62. const parts = param.replace(/\+/g, " ").split("=");
  63. const key = decodeURIComponent(parts.shift());
  64. const val = parts.length > 0 ? decodeURIComponent(parts.join("=")) : null;
  65. if (res[key] === undefined) {
  66. res[key] = val;
  67. } else if (Array.isArray(res[key])) {
  68. res[key].push(val);
  69. } else {
  70. res[key] = [res[key], val];
  71. }
  72. });
  73. return res;
  74. }
  75. export function weAtob(string) {
  76. const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  77. const b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
  78. string = String(string).replace(/[\t\n\f\r ]+/g, '');
  79. if (!b64re.test(string)) {
  80. throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");
  81. }
  82. string += '=='.slice(2 - (string.length & 3));
  83. let bitmap;
  84. let i = 0;
  85. let r1;
  86. let r2;
  87. let result = '';
  88. for (; i < string.length; ) {
  89. bitmap =
  90. (b64.indexOf(string.charAt(i++)) << 18) |
  91. (b64.indexOf(string.charAt(i++)) << 12) |
  92. ((r1 = b64.indexOf(string.charAt(i++))) << 6) |
  93. (r2 = b64.indexOf(string.charAt(i++)));
  94. result +=
  95. r1 === 64
  96. ? String.fromCharCode((bitmap >> 16) & 255)
  97. : r2 === 64
  98. ? String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255)
  99. : String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255, bitmap & 255);
  100. }
  101. return result;
  102. };
  103. // #ifdef H5
  104. const VUE_APP_WS_URL =
  105. process.env.VUE_APP_WS_URL || `ws://${location.hostname}:20001`;
  106. export { VUE_APP_WS_URL };
  107. // #endif
  108. export const VUE_APP_API_URL = "";
  109. export default parseQuery;