authority.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import store from "@/store/index.js";
  2. import api from "@/common/api.js";
  3. import { getQueryString } from "../common/navTo";
  4. export function getCode() {
  5. return new Promise((resolve, reject) => {
  6. // #ifdef H5
  7. if (location.search.includes("code")) {
  8. const code = getQueryString("code");
  9. uni.setStorageSync("h5_code", code);
  10. resolve(code);
  11. } else {
  12. // 没有code,就重定向到地址https://www.xyyxt.net?ask_type=https://api.xyyxt.net/pages2/order/confirm_pay 去获取code,授权后就会把code带上然后访问域名
  13. // ?fromCart=&code=061F5a1w3aolh03SLe1w3sMsCF4F5a16&state=STATE
  14. if (process.env.NODE_ENV !== "development") {
  15. const url = window.location.host + "/pages2/order/confirm_pay";
  16. // 跳自己授权
  17. if (store.getters.config.gzhSelfLicense) {
  18. api.getWxConfig().then((res) => {
  19. location.replace(
  20. `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${
  21. res.data.data.gzhAppId
  22. }&redirect_uri=${encodeURIComponent(
  23. "https://" + url
  24. )}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
  25. );
  26. });
  27. } else {
  28. location.replace("https://www.xyyxt.net/?ask_type=" + url);
  29. }
  30. }
  31. }
  32. // #endif
  33. // #ifdef MP-WEIXIN
  34. uni.login({
  35. provider: "weixin",
  36. success: (loginRes) => {
  37. uni.setStorageSync("wx_code", loginRes.code);
  38. resolve(loginRes.code);
  39. },
  40. });
  41. // #endif
  42. });
  43. }
  44. export function codeGetOpenid(code) {
  45. return new Promise((resolve, reject) => {
  46. // #ifdef H5
  47. api.getH5Openid(code).then((res) => {
  48. console.log("🚀 ~ file: authority.js:48 ~ codeGetOpenid ~ res:", res);
  49. });
  50. // #endif
  51. // #ifdef MP-WEIXIN
  52. api.getWxOpenid(code).then((res) => {
  53. console.log("🚀 ~ file: authority.js:48 ~ codeGetOpenid ~ res:", res);
  54. });
  55. // #endif
  56. });
  57. }