谢杰标 3 yıl önce
ebeveyn
işleme
a891029d0b

+ 19 - 17
common/authorize.js

@@ -1,4 +1,6 @@
 import { getWxConfig, getMobileConfig, OfficialLogin } from "@/utils/user";
+import { getOpenId } from "@/utils/login";
+
 export function getQueryString(name) {
   const url = location.search; //获取url中"?"符后的字串
   let theRequest = new Object();
@@ -19,7 +21,7 @@ export function authorize(url = window.location.href) {
   if (!isWechat()) {
     return;
   }
-  if (uni.getStorageSync("code")) {
+  if (uni.getStorageSync("openid")) {
     return;
   }
   // 没有code,就重定向到地址https://www.xyyxt.net?ask_type=https://api.xyyxt.net/pages2/order/confirm_pay 去获取code,授权后就会把code带上然后访问域名
@@ -49,30 +51,30 @@ export function bindCode(code) {
     code,
   }).then((res) => {});
 }
-
-// url获取code
 export function backCode(cb = bindCode) {
-  console.log(1321);
-  let code = uni.getStorageSync("code");
-  if (code) {
-    console.log(cb, "绑定");
-    cb(code);
-  }
-  if (!location.search.includes("code")) {
+  let code = getQueryString("code");
+  if (!code) {
     return;
   }
-  code = getQueryString("code");
-  uni.setStorageSync("code", code);
   cb(code);
 }
 
-export function backCode1(cb = bindCode) {
-  if (!location.search.includes("code")) {
+export function backOpenId(cb) {
+  let openid = uni.getStorageSync("openid");
+  if (openid) {
+    cb && cb(openid);
     return;
   }
-  code = getQueryString("code");
-  uni.setStorageSync("code", code);
-  cb(code);
+  let code = getQueryString("code");
+  if (!code) {
+    return;
+  }
+  getOpenId({ code }).then((res) => {
+    console.log(res);
+    uni.setStorageSync("openid", res.openid);
+    uni.setStorageSync("unionid", res.unionid);
+    cb && cb(res.openid);
+  });
 }
 
 export function isWechat() {

+ 1 - 1
common/config.js

@@ -1,7 +1,7 @@
 // test 测试环境
 
 const dev = {
-  BASE_URL: "http://120.79.166.78:19013",
+  BASE_URL: "http://192.168.1.7:5030",
   BASE_IMG_URL: "https://file-dev.xyyxt.net/",
 };
 

+ 4 - 4
pages/bill/index.vue

@@ -41,7 +41,7 @@
 
 <script>
 import { getSharePoster, bindLink } from "@/utils/bill";
-import { authorize, backCode } from "@/common/authorize";
+import { authorize, backOpenId } from "@/common/authorize";
 import { openidLogin } from "@/utils/login";
 import tkiQrcode from "tki-qrcode";
 import wechat from "@/common/wechat";
@@ -62,7 +62,7 @@ export default {
     authorize();
   },
   onShow() {
-    backCode(this.login);
+    backOpenId(this.login);
   },
   components: {
     tkiQrcode,
@@ -127,8 +127,8 @@ export default {
           this.$method.isLogin() && this.bindLink();
         });
     },
-    login(code) {
-      openidLogin({ openid: code }).then((data) => {
+    login(openid) {
+      openidLogin({ openid }).then((data) => {
         this.$store.commit("LOGIN_CB", data);
         this.$store.dispatch("getUserInfo");
       });

+ 9 - 18
pages/cashout/index.vue

@@ -61,7 +61,7 @@
 
 <script>
 import { mapGetters } from "vuex";
-import { authorize, backCode1 } from "@/common/authorize";
+import { authorize, backCode } from "@/common/authorize";
 import { checkBindGzh, withdrawal } from "@/utils/user";
 export default {
   data() {
@@ -72,7 +72,7 @@ export default {
     };
   },
   onShow() {
-    backCode1();
+    backCode();
   },
   onLoad(options) {
     if (this.$method.isGoLogin()) {
@@ -98,22 +98,13 @@ export default {
       this.show = false;
     },
     open() {
-      this.checkBindGzh().then((res) => {
-        this.show = true;
-        this.money = this.userInfo.cash;
-      });
-    },
-    // 获取授权
-    checkBindGzh() {
-      return new Promise((resolve, reject) => {
-        checkBindGzh().then((res) => {
-          if (res || process.env.NODE_ENV == "development") {
-            resolve();
-          } else {
-            authorize();
-            reject();
-          }
-        });
+      checkBindGzh().then((res) => {
+        if (res || process.env.NODE_ENV == "development") {
+          this.show = true;
+          this.money = this.userInfo.cash;
+        } else {
+          authorize();
+        }
       });
     },
     toRecord() {

+ 21 - 7
pages/login/login.vue

@@ -108,7 +108,8 @@
 <script>
 import { encryptor } from "@/common/jse";
 import { loginSms, accountLogin, smsLogin, openidLogin } from "@/utils/login";
-import { authorize, backCode } from "@/common/authorize";
+import { authorize, backOpenId } from "@/common/authorize";
+import { checkBindGzh, OfficialLogin } from "@/utils/user";
 export default {
   data() {
     return {
@@ -175,6 +176,7 @@ export default {
       codeTips: "",
       isUse: false,
       options: {},
+      openid: "",
     };
   },
   onLoad(options) {
@@ -182,7 +184,7 @@ export default {
     authorize();
   },
   onShow() {
-    backCode(this.openidLogin);
+    backOpenId(this.openidLogin);
   },
   onReady() {
     this.$refs.uForm1.setRules(this.rules);
@@ -223,6 +225,7 @@ export default {
       })
         .then((data) => {
           this.$u.toast("登录成功");
+          this.openid && this.bindOpenId();
           this.loginCallback(data);
         })
         .finally(() => {
@@ -267,9 +270,20 @@ export default {
     sectionChange(index) {
       this.current = index;
     },
+    // 绑定openid和unionid
+    bindOpenId() {
+      checkBindGzh().then((res) => {
+        if (!res) {
+          OfficialLogin({
+            gzhOpenid: this.openid,
+            unionId: uni.getStorageSync("unionid"),
+          });
+        }
+      });
+    },
     loginCallback(data) {
-      backCode();
       this.$store.commit("LOGIN_CB", data);
+      this.openid && this.bindOpenId();
       this.$store.dispatch("getUserInfo").then((res) => {
         if (this.options.backBill) {
           let href = uni.getStorageSync("BillHerf");
@@ -289,10 +303,10 @@ export default {
         }
       });
     },
-    openidLogin(code) {
-      openidLogin({ openid: code }).then((data) => {
-        this.$store.commit("LOGIN_CB", data);
-        this.$store.dispatch("getUserInfo");
+    openidLogin(openid) {
+      this.openid = openid;
+      openidLogin({ openid }).then((data) => {
+        this.loginCallback(data);
       });
     },
   },

+ 13 - 3
pages/login/register.vue

@@ -66,7 +66,12 @@
 
 <script>
 import { encryptor } from "@/common/jse";
-import { registerUser, registerSms, accountLogin } from "@/utils/login";
+import {
+  registerUser,
+  registerSms,
+  accountLogin,
+  OfficialLogin,
+} from "@/utils/login";
 import { authorize, backCode } from "@/common/authorize";
 export default {
   data() {
@@ -136,6 +141,7 @@ export default {
       },
       isUse: false,
       options: {},
+      code: "",
     };
   },
   methods: {
@@ -152,12 +158,14 @@ export default {
                 content: "注册成功",
                 showCancel: false,
                 success: (resst) => {
-                  backCode()
+                  backCode();
                   accountLogin({
                     account: this.form.tel,
                     pwd: encryptor(this.form.pwd),
                   }).then((data) => {
                     this.$store.commit("LOGIN_CB", data);
+                    // 绑定code==>openid
+                    this.code && OfficialLogin({ code: this.code });
                     this.$store.dispatch("getUserInfo").then((res) => {
                       if (this.options.backBill) {
                         let href = uni.getStorageSync("BillHerf");
@@ -223,7 +231,9 @@ export default {
     authorize();
   },
   onShow() {
-    backCode();
+    backCode((code) => {
+      this.code = code;
+    });
   },
 };
 </script>

+ 9 - 1
utils/login.js

@@ -35,7 +35,15 @@ export function openidLogin(data) {
     noToken: true,
   });
 }
-
+// 获取openid
+export function getOpenId(data) {
+  return myRequest({
+    url: "/app/common/distribution/gzh_openid",
+    method: "get",
+    data: data,
+    noToken: true,
+  });
+}
 //登录用户信息
 export function getInfo(data) {
   return myRequest({