authorize.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { getWxConfig, getMobileConfig, OfficialLogin } from "@/utils/user";
  2. export function getQueryString(name) {
  3. const url = location.search; //获取url中"?"符后的字串
  4. let theRequest = new Object();
  5. if (url.indexOf("?") != -1) {
  6. let str = url.substr(1);
  7. let strs = str.split("&");
  8. for (let i = 0; i < strs.length; i++) {
  9. theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
  10. }
  11. }
  12. if (name) {
  13. return theRequest[name];
  14. }
  15. return theRequest;
  16. }
  17. // 授权
  18. export function authorize(url = window.location.href) {
  19. if (!isWechat()) {
  20. return;
  21. }
  22. if (uni.getStorageSync("code")) {
  23. return;
  24. }
  25. // 没有code,就重定向到地址https://www.xyyxt.net?ask_type=https://api.xyyxt.net/pages2/order/confirm_pay 去获取code,授权后就会把code带上然后访问域名
  26. // ?fromCart=&code=061F5a1w3aolh03SLe1w3sMsCF4F5a16&state=STATE
  27. getMobileConfig().then((res) => {
  28. if (JSON.parse(res.mobileConfig).gzhSelfLicense) {
  29. getWxConfig().then((res) => {
  30. location.replace(
  31. `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${
  32. res.gzhAppId
  33. }&redirect_uri=${encodeURIComponent(
  34. "https://" + url
  35. )}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
  36. );
  37. });
  38. } else {
  39. url = url.split("//")[1];
  40. location.replace("https://www.xyyxt.net/home/index2?ask_type=" + url);
  41. }
  42. });
  43. }
  44. export function bindCode(code) {
  45. if (!uni.getStorageSync("user_account")) {
  46. return;
  47. }
  48. OfficialLogin({
  49. code,
  50. }).then((res) => {});
  51. }
  52. // url获取code
  53. export function backCode(cb = bindCode) {
  54. console.log(1321);
  55. let code = uni.getStorageSync("code");
  56. if (code) {
  57. console.log(cb, "绑定");
  58. cb(code);
  59. }
  60. if (!location.search.includes("code")) {
  61. return;
  62. }
  63. code = getQueryString("code");
  64. uni.setStorageSync("code", code);
  65. cb(code);
  66. }
  67. export function backCode1(cb = bindCode) {
  68. if (!location.search.includes("code")) {
  69. return;
  70. }
  71. code = getQueryString("code");
  72. uni.setStorageSync("code", code);
  73. cb(code);
  74. }
  75. export function isWechat() {
  76. var ua = window.navigator.userAgent.toLowerCase();
  77. return ua.match(/micromessenger/i) == "micromessenger";
  78. }