|
@@ -1,6 +1,8 @@
|
|
|
import store from "@/store/index.js";
|
|
|
import api from "@/common/api.js";
|
|
|
+import { isWeixin, modalComfirm } from "./shared";
|
|
|
import { getQueryString } from "../common/navTo";
|
|
|
+import goods from "../common/httpList/goods";
|
|
|
export function getCode() {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
// #ifdef H5
|
|
@@ -34,7 +36,6 @@ export function getCode() {
|
|
|
uni.login({
|
|
|
provider: "weixin",
|
|
|
success: (loginRes) => {
|
|
|
- uni.setStorageSync("wx_code", loginRes.code);
|
|
|
resolve(loginRes.code);
|
|
|
},
|
|
|
});
|
|
@@ -42,17 +43,87 @@ export function getCode() {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-export function codeGetOpenid(code) {
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- // #ifdef H5
|
|
|
- api.getH5Openid(code).then((res) => {
|
|
|
- console.log("🚀 ~ file: authority.js:48 ~ codeGetOpenid ~ res:", res);
|
|
|
- });
|
|
|
- // #endif
|
|
|
- // #ifdef MP-WEIXIN
|
|
|
- api.getWxOpenid(code).then((res) => {
|
|
|
- console.log("🚀 ~ file: authority.js:48 ~ codeGetOpenid ~ res:", res);
|
|
|
- });
|
|
|
- // #endif
|
|
|
- });
|
|
|
+// 获得openid
|
|
|
+export async function getOpenid(isNeedUid = false) {
|
|
|
+ let openid = uni.getStorageSync("openid");
|
|
|
+ if (!openid) {
|
|
|
+ let code = await getCode();
|
|
|
+ openid = await codeGetOpenid(code);
|
|
|
+ uni.setStorageSync("openid", openid);
|
|
|
+ }
|
|
|
+ return openid;
|
|
|
+}
|
|
|
+
|
|
|
+// code去换openid
|
|
|
+export async function codeGetOpenid(code) {
|
|
|
+ // #ifdef H5
|
|
|
+ let { data } = await api.getH5Openid({ code });
|
|
|
+ return data.msg;
|
|
|
+ // #endif
|
|
|
+ // #ifdef MP-WEIXIN
|
|
|
+ let { data } = await api.getWxOpenid({ code });
|
|
|
+ uni.setStorageSync("unionId", data.data.unionId);
|
|
|
+ return data.data.openId;
|
|
|
+ // #endif
|
|
|
+}
|
|
|
+
|
|
|
+export async function checkOpenidIsUser() {
|
|
|
+ let openid = uni.getStorageSync("openid");
|
|
|
+ if (!openid) {
|
|
|
+ openid = await getOpenid();
|
|
|
+ }
|
|
|
+ let fromPlat = 1;
|
|
|
+ // #ifdef MP-WEIXIN
|
|
|
+ fromPlat = 2;
|
|
|
+ // #endif
|
|
|
+ let { data } = await api.checkBindOpenId({ openid, fromPlat });
|
|
|
+ console.log("🚀 ~ file: authority.js:72 ~ checkOpenidIsUser ~ data:", data);
|
|
|
+ return data;
|
|
|
+}
|
|
|
+export async function examClientCanLearn(examLimitClient) {
|
|
|
+ // 限制学习
|
|
|
+ if (!examLimitClient) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ // #ifdef MP-WEIXIN
|
|
|
+ if (examLimitClient.includes("2")) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ // #endif
|
|
|
+ // #ifdef H5
|
|
|
+ if (examLimitClient.includes("1") && isWeixin()) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ // #endif
|
|
|
+ modalComfirm(
|
|
|
+ "请去" + (examLimitClient == 2 ? "微信小程序" : "公众号") + "学习"
|
|
|
+ );
|
|
|
+ return false;
|
|
|
+}
|
|
|
+
|
|
|
+export async function isCanDoExam(examLimitClient) {
|
|
|
+ if (!examClientCanLearn(examLimitClient)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ let isFlag = false;
|
|
|
+ let data = await checkOpenidIsUser();
|
|
|
+ if (data.code === 200) {
|
|
|
+ isFlag = data.data;
|
|
|
+ }
|
|
|
+ if (!isFlag) {
|
|
|
+ modalComfirm("该题库只允许同一用户学习");
|
|
|
+ }
|
|
|
+ return isFlag;
|
|
|
+}
|
|
|
+
|
|
|
+export async function goodsExamIsCanLearn(goodsId) {
|
|
|
+ if (!goodsId) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ let { data } = await api.goodsDetail(goodsId);
|
|
|
+ let { examLimitClient } = data.data;
|
|
|
+ if (await isCanDoExam(examLimitClient)) {
|
|
|
+ throw new Error("openid不一致");
|
|
|
+ }
|
|
|
+ return true;
|
|
|
}
|