authority.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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) {
  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. url = url || window.location.href;
  16. if (process.env.NODE_ENV !== "development") {
  17. // 跳自己授权
  18. if (store.getters.config.gzhSelfLicense) {
  19. api.getWxConfig().then((res) => {
  20. location.replace(
  21. `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${
  22. res.data.data.gzhAppId
  23. }&redirect_uri=${encodeURIComponent(
  24. url
  25. )}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
  26. );
  27. });
  28. } else {
  29. url = url.split("//")[1];
  30. location.replace("https://www.xyyxt.net/home/index2?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() {
  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 { data } = await checkOpenidIsUser();
  105. if (!data) {
  106. modalComfirm({
  107. content: "该题库只允许同一用户学习",
  108. scb: () => {
  109. uni.switchTab({
  110. url: "/pages/questionBank/index",
  111. });
  112. },
  113. });
  114. throw new Error("data:" + data);
  115. }
  116. return false;
  117. }
  118. export async function goodsExamIsCanLearn(goodsId) {
  119. if (!goodsId) {
  120. return true;
  121. }
  122. let { data } = await api.goodsDetail(goodsId);
  123. let { examLimitClient } = data.data;
  124. if (await isCanDoExam(examLimitClient)) {
  125. throw new Error("openid不一致");
  126. }
  127. return true;
  128. }