Browse Source

对接山东题库和权限处理基本完成

谢杰标 2 years ago
parent
commit
ea7f18fb61

+ 1 - 1
common/config.js

@@ -1,6 +1,6 @@
 // test 测试环境
 const test = {
-  BASE_URL: "http://192.168.1.222:5055",
+  BASE_URL: "http://192.168.1.24:5055",
   BASE_IMG_URL: "https://file-dev.xyyxt.net/",
   domain: "h.xyyxt.net",
   tenantId: "867735392558919680", //详粤云学堂

+ 23 - 4
common/httpList/wxpay.js

@@ -37,19 +37,38 @@ export default {
       data: data,
     });
   },
-  // 获取公众号id
+  // 获取公众号openid
   getH5Openid(data) {
     return myRequest({
-      url: "/app/user/wx/gzh_bind",
+      url: "/app/common/wx/gzh_bind",
       method: "post",
+      noToken: true,
       data: data,
     });
   },
-  // 获取公众号id
+  // 获取小程序openid
   getWxOpenid(data) {
     return myRequest({
-      url: "/wx/exam/getOpenid",
+      url: "/app/common/wx/exam/getOpenid",
       method: "post",
+      noToken: true,
+      data: data,
+    });
+  },
+  // 检查账号是否绑定该openid
+  checkBindOpenId(data) {
+    return myRequest({
+      url: "/app/user/checkBindOpenId",
+      method: "post",
+      data: data,
+    });
+  },
+  // 小程序openId登录
+  openIdLogin(data) {
+    return myRequest({
+      url: "/app/common/openIdLogin",
+      method: "post",
+      noToken: true,
       data: data,
     });
   },

+ 0 - 31
common/methodTool.js

@@ -519,10 +519,7 @@ export default {
     let rightQuestionIds = []; //做对的题目id
     let totalQuestionNum = 0; //排除主观题的总条数(简单题)
     data.forEach((item) => {
-      console.log("答案--", item.ans, "选择--", item.ques);
-
       totalScore += item.score * (item.type == 4 ? item.jsonStr.length : 1);
-
       if (
         item.type < 4 ||
         (item.type == 4 && !item.jsonStr.some((e) => e.type == 5))
@@ -764,32 +761,4 @@ export default {
         });
     });
   },
-  examClientCanLearn(examLimitClient) {
-    // 限制学习
-    if (!examLimitClient) {
-      return true;
-    }
-    // #ifdef MP-WEIXIN
-    if (examLimitClient.includes("2")) {
-      return true;
-    }
-    // #endif
-    // #ifdef H5
-    if (examLimitClient.includes("1") && this.isWeixin()) {
-      return true;
-    }
-    // #endif
-    uni.showModal({
-      title: "提示",
-      showCancel: false,
-      content:
-        "请去" + (examLimitClient == 2 ? "微信小程序" : "公众号") + "学习",
-      success: (k) => {
-        if (k.confirm) {
-          uni.navigateBack();
-        }
-      },
-    });
-    return false;
-  },
 };

+ 1 - 1
common/request.js

@@ -109,7 +109,7 @@ export const myRequest = (options) => {
       },
       complete: () => {
         // uni.hideLoading()
-        //		uni.hideToast()
+        // uni.hideToast()
       },
     });
   });

+ 3 - 7
pages2/bank/questionBank.vue

@@ -921,7 +921,7 @@
 </template>
 
 <script>
-import f from "@/utils/authority";
+import { goodsExamIsCanLearn } from "@/utils/authority";
 export default {
   data() {
     return {
@@ -964,6 +964,7 @@ export default {
     };
   },
   async onLoad(option) {
+    await goodsExamIsCanLearn(option.goodsid);
     this.entryType = option.entryType || "";
     this.bankNum = option.bankNum;
     // this.courseId = option.courseId || ''
@@ -976,9 +977,7 @@ export default {
     this.isFromVideo = option.isFromVideo || "";
     this.gradeId = option.gradeId || "";
     let isBack = option.isback;
-
     let showDialog = uni.getStorageSync("showDialog");
-
     if (showDialog) {
       this.showDialog = false;
     } else {
@@ -1063,10 +1062,7 @@ export default {
     bankExam() {
       return new Promise((resolve, reject) => {
         this.$api.bankExam(this.id).then((res) => {
-          const { examLimitClient, doType } = res.data.data;
-          if (!this.$method.examClientCanLearn(examLimitClient)) {
-            reject();
-          }
+          const { doType } = res.data.data;
           if (this.entryType == "daily") {
             // 每日一练类型都是为练习
             this.bankType = 1;

+ 2 - 3
pages2/bank/question_report.vue

@@ -221,6 +221,7 @@
 
 <script>
 import { mapGetters } from "vuex";
+import { goodsExamIsCanLearn } from "@/utils/authority";
 export default {
   data() {
     return {
@@ -259,6 +260,7 @@ export default {
 
   async onLoad(option) {
     console.log(option);
+    await goodsExamIsCanLearn(option.goodsid);
     this.entryType = option.entryType;
     this.bankNum = option.bankNum;
     this.orderGoodsId = option.orderGoodsId;
@@ -528,9 +530,6 @@ export default {
       // '/bank/exam/'+data
       return new Promise((resolve, reject) => {
         this.$api.bankExam(this.examId).then((res) => {
-          if (!this.$method.examClientCanLearn(res.data.data.examLimitClient)) {
-            reject();
-          }
           this.examData = res.data.data;
           resolve();
         });

+ 2 - 3
pages2/class/questionBank.vue

@@ -894,6 +894,7 @@
 <script>
 import PopupPhoto from "@/components/popup/index.vue";
 import BankBom from "../../components/common/BankBom.vue";
+import { goodsExamIsCanLearn } from "@/utils/authority";
 export default {
   components: {
     PopupPhoto,
@@ -965,6 +966,7 @@ export default {
     },
   },
   async onLoad(option) {
+    await goodsExamIsCanLearn(option.goodsid);
     this.examType = option.type;
     this.id = option.id;
     this.goodsId = option.goodsid || "";
@@ -1516,9 +1518,6 @@ export default {
     bankExam() {
       return this.$api.bankExam(this.id).then((res) => {
         const { examLimitClient, doType } = res.data.data;
-        if (!this.$method.examClientCanLearn(examLimitClient)) {
-          return
-        }
         this.bankType = doType;
         this.examData = res.data.data;
         this.allTimes = this.examData.answerTime * 60;

+ 28 - 28
pages4/login/login.vue

@@ -128,17 +128,20 @@
       <view class="wxBtn">
         <view class="wxBtn_title">
           <view class="line"></view>
-          <view class="text">手机号快捷登录</view>
+          <view class="text">快捷登录</view>
           <view class="line"></view>
         </view>
-        <button
+        <button type="default" @click="quickLogin" class="wxloginBtn fl_c">
+          <image mode="widthFix" src="/static/icon_phone.png"></image>
+        </button>
+        <!-- <button
           type="default"
           open-type="getPhoneNumber"
           @getphonenumber="wxLogin"
           class="wxloginBtn fl_c"
         >
           <image mode="widthFix" src="/static/icon_phone.png"></image>
-        </button>
+        </button> -->
       </view>
       <!-- #endif -->
     </view>
@@ -158,6 +161,7 @@ RVQplIVs5z3MxcUa9ptKPHUTgh8xMCBvl8sUJKwkmn4vYWeDfHT22EL7Hr1pTMwU
 hF6WiNlWfQTVoF1rhwIDAQAB
 -----END PUBLIC KEY-----`;
 import JSEncrypt from "@/pages4/static/jsencrypt.min.js";
+import { getOpenid } from "../../utils/authority";
 export default {
   data() {
     return {
@@ -228,6 +232,7 @@ export default {
       isBack: false,
       isDualAuth: false,
       isAct: "",
+      bindForm: {},
     };
   },
   onLoad(option) {
@@ -249,7 +254,7 @@ export default {
   onShow() {
     // 获取code
     // #ifdef MP-WEIXIN
-    this.getwxCode();
+    // this.getwxCode();
     // #endif
   },
   onReady() {
@@ -559,7 +564,12 @@ export default {
       this.$api.getInfo({ fromPlat: 1 }).then((resdata) => {
         if (resdata.data.code == 200) {
           this.$store.state.userInfo = resdata.data.data;
-
+          if (this.bindForm.openId) {
+            this.$api.openIdLogin({
+              ...this.bindForm,
+              userId: resdata.data.data.userId,
+            });
+          }
           if (!this.isBack) {
             let goPath = "";
             if (types == "wxlogin" || types == "smslogin") {
@@ -592,30 +602,20 @@ export default {
           }
         }
       });
-
-      // } else {
-      // 	//未完善信息,存为临时信息
-      // 	uni.setStorageSync('user_account_temp', res.data.data.user_account);
-      // 	uni.setStorageSync('token_temp', res.data.data.token);
-      // 	this.$navTo.togo('/pages2/register/bind');
-
-      // }
     },
-    // scanLoginCheck小程序校验PC登录二维码,执行登录获取到令牌,然后把扫码的路径最后面的6位标识码提交给后台就行
-    // submitCode() {
-    // 	console.log('跳转请求提交code到接口', this.scanCode)
-    // 	this.$api.scanLoginCheck({
-    // 		scanCode: this.scanCode
-    // 	}).then((res) => {
-    // 		if (res.data.code == 200) {
-    // 			uni.navigateTo({
-    // 				url: '/pages4/login/pcLoginSuccess'
-    // 			})
-    // 		} else {
-    // 			this.$u.toast(res.data.msg)
-    // 		}
-    // 	})
-    // }
+    async quickLogin() {
+      let openId = await getOpenid();
+      let res = await this.$api.openIdLogin({ openId });
+      if (res.data.code === 200) {
+        this.loginCallback(res);
+      } else {
+        this.bindForm = { openId, unionId: uni.getStorageSync("unionId") };
+        uni.showToast({
+          icon: "none",
+          title: res.data.msg + "请用密码或验证码登录!",
+        });
+      }
+    },
   },
 };
 </script>

+ 2 - 3
pages5/examBank/index.vue

@@ -867,6 +867,7 @@
 <script>
 import { mapGetters, mapActions } from "vuex";
 import BankBom from "../../components/common/BankBom.vue";
+import { goodsExamIsCanLearn } from "@/utils/authority";
 export default {
   data() {
     return {
@@ -908,6 +909,7 @@ export default {
     };
   },
   async onLoad(option) {
+    await goodsExamIsCanLearn(option.goodsid);
     this.examStartTime = option.examStartTime;
     this.examEndTime = option.examEndTime;
     this.subscribeId = option.subscribeId || "";
@@ -1008,9 +1010,6 @@ export default {
       return new Promise((resolve, reject) => {
         this.$api.bankExam(this.examId).then((res) => {
           const { examLimitClient, doType } = res.data.data;
-          if (!this.$method.examClientCanLearn(examLimitClient)) {
-            reject();
-          }
           this.bankType = doType;
           this.examData = res.data.data;
           if (this.bankType == 2) {

+ 0 - 3
pages5/examReport/index.vue

@@ -579,9 +579,6 @@ export default {
     bankExam() {
       return new Promise((resolve, reject) => {
         this.$api.bankExam(this.examId).then((res) => {
-          if (!this.$method.examClientCanLearn(res.data.data.examLimitClient)) {
-            reject();
-          }
           this.examData = res.data.data;
           resolve();
         });

+ 85 - 14
utils/authority.js

@@ -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;
 }

+ 51 - 0
utils/shared.js

@@ -0,0 +1,51 @@
+export function isWeixin() {
+  var ua = window.navigator.userAgent.toLowerCase();
+  if (
+    ua.match(/MicroMessenger/i) == "micromessenger" ||
+    ua.match(/_SQ_/i) == "_sq_"
+  ) {
+    return true;
+  } else {
+    return false;
+  }
+}
+
+export function isBase64(str) {
+  var reg =
+    /^\s*data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s]*?)\s*$/i;
+  return reg.test(str);
+}
+export function isObject(object) {
+  return object !== null && typeof object === "object";
+}
+export function isEmptyObject(object) {
+  return this.isObject(object) && Object.keys(object).length < 1;
+}
+
+export function modalComfirm(config = {}, t) {
+  if (!isObject(config)) {
+    config = {
+      content: config,
+      title: t,
+    };
+  }
+  let {
+    title = "提示",
+    showCancel = false,
+    content,
+    scb = uni.navigateBack(),
+    ecb,
+  } = config;
+  uni.showModal({
+    title: title,
+    showCancel: showCancel,
+    content: content,
+    success: (k) => {
+      if (k.confirm) {
+        scb && scb();
+      } else {
+        ecb && ecb();
+      }
+    },
+  });
+}