authority.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import store from "@/store/index.js";
  2. import api from "@/common/api.js";
  3. import { isWeixin, modalComfirm } from "./shared";
  4. import { getQueryString } from "../common/navTo";
  5. import goods from "../common/httpList/goods";
  6. export function getCode() {
  7. return new Promise((resolve, reject) => {
  8. // #ifdef H5
  9. if (location.search.includes("code")) {
  10. const code = getQueryString("code");
  11. uni.setStorageSync("h5_code", code);
  12. resolve(code);
  13. } else {
  14. // 没有code,就重定向到地址https://www.xyyxt.net?ask_type=https://api.xyyxt.net/pages2/order/confirm_pay 去获取code,授权后就会把code带上然后访问域名
  15. // ?fromCart=&code=061F5a1w3aolh03SLe1w3sMsCF4F5a16&state=STATE
  16. if (process.env.NODE_ENV !== "development") {
  17. const url = window.location.host + "/pages2/order/confirm_pay";
  18. // 跳自己授权
  19. if (store.getters.config.gzhSelfLicense) {
  20. api.getWxConfig().then((res) => {
  21. location.replace(
  22. `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${
  23. res.data.data.gzhAppId
  24. }&redirect_uri=${encodeURIComponent(
  25. "https://" + url
  26. )}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
  27. );
  28. });
  29. } else {
  30. location.replace("https://www.xyyxt.net/?ask_type=" + url);
  31. }
  32. }
  33. }
  34. // #endif
  35. // #ifdef MP-WEIXIN
  36. uni.login({
  37. provider: "weixin",
  38. success: (loginRes) => {
  39. resolve(loginRes.code);
  40. },
  41. });
  42. // #endif
  43. });
  44. }
  45. // 获得openid
  46. export async function getOpenid(isNeedUid = false) {
  47. let openid = uni.getStorageSync("openid");
  48. if (!openid) {
  49. let code = await getCode();
  50. openid = await codeGetOpenid(code);
  51. uni.setStorageSync("openid", openid);
  52. }
  53. return openid;
  54. }
  55. // code去换openid
  56. export async function codeGetOpenid(code) {
  57. // #ifdef H5
  58. let { data } = await api.getH5Openid({ code });
  59. return data.msg;
  60. // #endif
  61. // #ifdef MP-WEIXIN
  62. let { data } = await api.getWxOpenid({ code });
  63. uni.setStorageSync("unionId", data.data.unionId);
  64. return data.data.openId;
  65. // #endif
  66. }
  67. export async function checkOpenidIsUser() {
  68. let openid = uni.getStorageSync("openid");
  69. if (!openid) {
  70. openid = await getOpenid();
  71. }
  72. let fromPlat = 1;
  73. // #ifdef MP-WEIXIN
  74. fromPlat = 2;
  75. // #endif
  76. let { data } = await api.checkBindOpenId({ openid, fromPlat });
  77. console.log("🚀 ~ file: authority.js:72 ~ checkOpenidIsUser ~ data:", data);
  78. return data;
  79. }
  80. export function examClientCanLearn(examLimitClient) {
  81. // 限制学习
  82. if (!examLimitClient) {
  83. return true;
  84. }
  85. // #ifdef MP-WEIXIN
  86. if (examLimitClient.includes("2")) {
  87. return true;
  88. }
  89. // #endif
  90. // #ifdef H5
  91. if (examLimitClient.includes("1") && isWeixin()) {
  92. return true;
  93. }
  94. // #endif
  95. modalComfirm(
  96. "请去" + (examLimitClient == 2 ? "微信小程序" : "公众号") + "学习"
  97. );
  98. throw new Error("examLimitClient:" + examLimitClient);
  99. }
  100. export async function isCanDoExam(examLimitClient) {
  101. if (!examLimitClient || !examClientCanLearn(examLimitClient)) {
  102. return false;
  103. }
  104. let isFlag = false;
  105. let data = await checkOpenidIsUser();
  106. if (data.code === 200) {
  107. isFlag = data.data;
  108. }
  109. if (!isFlag) {
  110. modalComfirm("该题库只允许同一用户学习");
  111. throw new Error("isFlag:" + isFlag);
  112. }
  113. return isFlag;
  114. }
  115. export async function goodsExamIsCanLearn(goodsId) {
  116. if (!goodsId) {
  117. return true;
  118. }
  119. let { data } = await api.goodsDetail(goodsId);
  120. let { examLimitClient } = data.data;
  121. if (await isCanDoExam(examLimitClient)) {
  122. throw new Error("openid不一致");
  123. }
  124. return true;
  125. }