authority.js 3.6 KB

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