Kaynağa Gözat

课程中心绑定微信,登录页绑定微信

11wqe1 1 hafta önce
ebeveyn
işleme
80d280a7f5

+ 36 - 1
src/components/pages/data.vue

@@ -509,7 +509,7 @@ export default {
           }, 0);
         }, 0);
     },
-    setOpenId(openid) {
+    addOpenid(openid) {
       this.ajax.post(this.$store.state.api + "UpdateOpenId", [
         { userid: this.userid, openid: openid },
       ]).then((res) => {
@@ -519,7 +519,42 @@ export default {
       }).catch((err) => {
         this.$message.error("绑定失败请重新扫码");
         console.error(err);
+        	setTimeout(() => {
+						this.wechatDialogVisible = true;
+						this.wechatLogin();
+					}, 1000);
       })
+		},
+    setOpenId(openid) {
+      this.ajax
+      .post(this.$store.state.api + "selectliyuanOpenid", [
+        { pid: openid },
+      ])
+      .then((res) => {
+					if (!res.data[0].length) {
+						this.addOpenid(openid);
+					} else {
+						let uname = res.data[0].map(item => item.username).join(',');
+						console.log('uname',uname);
+						
+						this.$message.error("该微信已绑定" + uname + "账号");
+						this.wechatDialogVisible = false;
+					}
+				})
+				.catch((err) => {
+					console.log("err", err);
+				});
+
+      // this.ajax.post(this.$store.state.api + "UpdateOpenId", [
+      //   { userid: this.userid, openid: openid },
+      // ]).then((res) => {
+      //   this.$message.success("绑定成功");
+      //   this.wechatDialogVisible = false;
+      //   this.getDetail();
+      // }).catch((err) => {
+      //   this.$message.error("绑定失败请重新扫码");
+      //   console.error(err);
+      // })
     }
   },
   mounted() {

+ 88 - 25
src/components/pages/testPerson/info/infoDialognew/index.vue

@@ -208,6 +208,11 @@ export default {
 		dialogVisibleInfo(newVal) {
 			if (newVal) {
 				this.getTypeInfo();
+				this.monitorWechat()
+			}else{
+				if (this.messageHandler) {
+					window.removeEventListener("message", this.messageHandler);	
+				}
 			}
 		},
 	},
@@ -228,6 +233,7 @@ export default {
 	},
 	data() {
 		return {
+			messageHandler:'',
 			avator: avator,
 			info: {},
 			imgLoading: false,
@@ -600,10 +606,35 @@ export default {
 				}, 0);
 			}, 0);
 		},
-		setOpenId(openid) {
-			 this.ajax.post(this.$store.state.api + "UpdateOpenId", [
-                    { userid: this.userid, openid: openid },
-                ]).then((res) => {
+		monitorWechat(){
+			let _this = this;
+			this.messageHandler = function (e) {
+				// 监听 message 事件
+				console.log("userinfoA", e);
+				if (e.data && e.data.method == "getOpenId") {
+					console.log(e);
+					console.log(e.data.code);
+					console.log(_this.loginType);
+
+					if (e.data.code == 200) {
+						_this.setOpenId(e.data.data.openid);
+					} else {
+						_this.$message.error("扫码登录失败请重新扫码");
+						_this.wechatDialogVisible = false;
+						setTimeout(() => {
+							_this.wechatDialogVisible = true;
+							_this.wechatLogin();
+						}, 1000);
+					}
+				}
+			};
+
+			window.addEventListener("message", this.messageHandler);
+		},
+		addOpenid(openid) {
+			this.ajax.post(this.$store.state.api + "UpdateOpenId", [
+					{ userid: this.userid, openid: openid },
+				]).then((res) => {
 					this.$message.success("绑定成功");
 					this.wechatDialogVisible = false;
 					this.getData();
@@ -613,35 +644,67 @@ export default {
 					console.error(err);
 				});
 		},
+		setOpenId(openid) {
+			this.ajax
+			.post(this.$store.state.api + "selectliyuanOpenid", [
+				{ pid: openid },
+			])
+			.then((res) => {
+				if (!res.data[0].length) {
+					this.addOpenid(openid);
+				} else {
+					let uname = res.data[0].map(item => item.username).join(',');
+					console.log('uname',uname);
+					
+					this.$message.error("该微信已绑定" + uname + "账号");
+					this.wechatDialogVisible = false;
+				}
+			})
+			.catch((err) => {
+				console.log("err", err);
+			});
+
+		// this.ajax.post(this.$store.state.api + "UpdateOpenId", [
+		//   { userid: this.userid, openid: openid },
+		// ]).then((res) => {
+		//   this.$message.success("绑定成功");
+		//   this.wechatDialogVisible = false;
+		//   this.getDetail();
+		// }).catch((err) => {
+		//   this.$message.error("绑定失败请重新扫码");
+		//   console.error(err);
+		// })
+		},
 		Ochange() {
 			this.$forceUpdate();
 		},
 	},
+	
 	mounted() {
 		this.$nextTick();
 		this.getTypeInfo();
 
-		let _this = this;
-		window.addEventListener("message", function (e) {
-			// 监听 message 事件
-			console.log(e);
-			if (e.data && e.data.method == "getOpenId") {
-				console.log(e);
-				console.log(e.data.code);
-				console.log(_this.loginType);
-
-				if (e.data.code == 200) {
-					_this.setOpenId(e.data.data.openid);
-				} else {
-					_this.$message.error("扫码登录失败请重新扫码");
-					_this.wechatDialogVisible = false;
-					setTimeout(() => {
-						_this.wechatDialogVisible = true;
-						_this.wechatLogin();
-					}, 1000);
-				}
-			}
-		});
+		// let _this = this;
+		// window.addEventListener("message", function (e) {
+		// 	// 监听 message 事件
+		// 	console.log(e);
+		// 	if (e.data && e.data.method == "getOpenId") {
+		// 		console.log(e);
+		// 		console.log(e.data.code);
+		// 		console.log(_this.loginType);
+
+		// 		if (e.data.code == 200) {
+		// 			_this.setOpenId(e.data.data.openid);
+		// 		} else {
+		// 			_this.$message.error("扫码登录失败请重新扫码");
+		// 			_this.wechatDialogVisible = false;
+		// 			setTimeout(() => {
+		// 				_this.wechatDialogVisible = true;
+		// 				_this.wechatLogin();
+		// 			}, 1000);
+		// 		}
+		// 	}
+		// });
 	},
 };
 </script>