Explorar o código

修复重复登录问题

zaijin %!s(int64=2) %!d(string=hai) anos
pai
achega
9c9fa0760f

+ 7 - 2
common/http.interceptor.js

@@ -27,11 +27,16 @@ const install = (Vue, vm) => {
       // 判断浏览器
       const ua = window.navigator.userAgent.toLowerCase();
       if (ua.match(/MicroMessenger/i) == 'micromessenger') {
+				// 防止重复跳转
+        if (backUrl.indexOf('backUrl') === (-1)) {
+          vm.$u.route('pages/wechatLogin/wechatLogin', { backUrl });
+        }
         // 微信中打开
-        vm.$u.route('pages/wechatLogin/wechatLogin', { backUrl });
       } else {
         // 普通浏览器中打开
-        localStorage.setItem('backUrl', backUrl);
+				if (backUrl.indexOf('backUrl') === (-1)) {
+				  localStorage.setItem('backUrl', backUrl);
+				}
         localStorage.removeItem('lifeData');
         uni.showModal({
           title: '提示',

+ 3 - 1
pages/center/phoneLogin/phoneLogin.vue

@@ -122,7 +122,9 @@
 						this.$u.vuex('vuex_token', this.accessToken);
 						this.$u.vuex('vuex_user', res.data);
 						this.$u.vuex('vuex_hasLogin', true);
-						this.wechatLogin()
+						setTimeout(() => {
+							this.jumpIndex()
+						}, 500)
 					} else {
 						this.$refs.uToast.show({
 							title: res.msg,

+ 116 - 106
pages/wechatLogin/wechatLogin.vue

@@ -1,113 +1,123 @@
 <template>
-	<view>
-		<u-toast ref="uToast" />
-	</view>
+  <view>
+    <u-toast ref="uToast" />
+  </view>
 </template>
 
 <script>
-	import getUrlParams from './../../utils/getUrlParams.js';
-	export default {
-		data() {
-			return {
-				backUrl: '',
-				code: ''
-			};
-		},
-		onLoad(page) {
-			let local = location.href;
-			let locationLocaturl = location.search;
-			// 微信返回的回调地址
-			let backUrl = local.split('backUrl=')[1];
-			if (backUrl) {
-				this.backUrl = decodeURIComponent(backUrl);
-			}
-			console.log('backUrl', decodeURIComponent(backUrl));
-			let code = getUrlParams(locationLocaturl, 'code');
-			console.log('code', code);
-			if (code) {
-				this.code = code;
-				let wechatLoginKey = localStorage.getItem('wechatLoginKey')
-				if (wechatLoginKey) {
-					wechatLoginKey = JSON.parse(wechatLoginKey);
-					if (code === wechatLoginKey.code) {
-						this.getToken(wechatLoginKey.openId);
-					} else {
-						this.handleGetWXInfo(this.code);
-					}
-				} else {
-					this.handleGetWXInfo(this.code);
-				}
-			} else {
-				// location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.config.wxAppid}&redirect_uri=${encodeURIComponent(
-				//     local
-				//   )}&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect`;
-				location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.config.wxAppid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
-			}
-		},
-		methods: {
-			// 通过code获取 openId等用户信息
-			handleGetWXInfo(code) {
-				uni.showLoading({
-					title: '加载中'
-				});
-				this.$u.api
-					.getWXInfo(code)
-					.then((res) => {
-						if (res.code === 200) {
-							this.$u.vuex('vuex_wxinfo', res.data);
-							localStorage.setItem('wechatLoginKey', JSON.stringify({
-								code,
-								openId: res.data.openId
-							}))
-							this.getToken(res.data.openId);
-						} else {
-							this.$refs.uToast.show({
-								title: res.msg || '获取用户信息失败!',
-								type: 'error'
-							});
-							uni.hideLoading();
-						}
-					})
-					.catch((err) => {
-						uni.hideLoading();
-					});
-			},
-			/**
-			 * 通过openId获取token
-			 * @param {Object} openId
-			 */
-			getToken(openId) {
-				this.$u.api.codeV2Api.sendSmsCodeV2api({
-					openId
-				}).then((res) => {
-					if (res.code === 200) {
-						this.$u.vuex('vuex_token', res.data.accessToken);
-						this.$u.vuex('vuex_hasLogin', true);
-						if (res.data.needVerify) {
-							localStorage.setItem('backUrl', this.backUrl);
-							this.$u.route({
-								url: 'pages/center/phoneLogin/phoneLogin'
-							});
-						} else {
-							this.$u.vuex('vuex_user', res.data);
-							if (this.backUrl.indexOf('wechatLogin') > (-1)) {
-								location.href = '/'
-							} else {
-								location.href = this.backUrl;
-							}
-						}
-						uni.hideLoading();
-					} else {
-						this.$refs.uToast.show({
-							title: res.msg || '获取用户信息失败!',
-							type: 'error'
-						});
-						uni.hideLoading();
-					}
-				});
-			}
-		}
-	};
+import getUrlParams from '@/utils/getUrlParams.js';
+export default {
+  data() {
+    return {
+      backUrl: '',
+      code: ''
+    };
+  },
+  onLoad(page) {
+    console.log('page:', page);
+    const locationLocaturl = location.search;
+    // 微信返回的回调地址
+    const backUrl = page?.backUrl;
+    if (backUrl) {
+      this.backUrl = backUrl;
+    }
+    console.log('backUrl', backUrl);
+    let code = getUrlParams(locationLocaturl, 'code');
+    console.log('code', code);
+    if (code) {
+      this.code = code;
+      // 判断重复回调问题
+      let wechatLoginKey = localStorage.getItem('wechatLoginKey');
+      if (wechatLoginKey) {
+        wechatLoginKey = JSON.parse(wechatLoginKey);
+        if (code === wechatLoginKey.code) {
+          this.getToken(wechatLoginKey.openId);
+        } else {
+          this.handleGetWXInfo(this.code);
+        }
+      } else {
+        this.handleGetWXInfo(this.code);
+      }
+    } else {
+      const local = location.href;
+      location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?
+				appid=${this.config.wxAppid}&
+				redirect_uri=${encodeURIComponent(local)}&
+				response_type=code&
+				scope=snsapi_userinfo&
+				state=STATE#wechat_redirect`;
+    }
+  },
+  methods: {
+    // 通过code获取 openId等用户信息
+    handleGetWXInfo(code) {
+      uni.showLoading({
+        title: '加载中'
+      });
+      this.$u.api
+        .getWXInfo(code)
+        .then((res) => {
+          if (res.code === 200) {
+            this.$u.vuex('vuex_wxinfo', res.data);
+            localStorage.setItem(
+              'wechatLoginKey',
+              JSON.stringify({
+                code,
+                openId: res.data.openId
+              })
+            );
+            this.getToken(res.data.openId);
+          } else {
+            this.$refs.uToast.show({
+              title: res.msg || '获取用户信息失败!',
+              type: 'error'
+            });
+            uni.hideLoading();
+          }
+        })
+        .catch((err) => {
+          uni.hideLoading();
+        });
+    },
+    /**
+     * 通过openId获取token
+     * @param {Object} openId
+     */
+    getToken(openId) {
+      this.$u.api.codeV2Api
+        .sendSmsCodeV2api({
+          openId
+        })
+        .then((res) => {
+          if (res.code === 200) {
+            this.$u.vuex('vuex_token', res.data.accessToken);
+            this.$u.vuex('vuex_hasLogin', true);
+            if (res.data.needVerify) {
+              localStorage.setItem('backUrl', this.backUrl);
+              this.$u.route({
+                url: 'pages/center/phoneLogin/phoneLogin',
+								type: 'reLaunch'
+              });
+            } else {
+              this.$u.vuex('vuex_user', res.data);
+              if (this.backUrl.indexOf('wechatLogin') > -1) {
+                location.href = '/';
+              } else {
+                location.href = this.backUrl;
+              }
+            }
+            uni.hideLoading();
+          } else {
+            this.$refs.uToast.show({
+              title: res.msg || '获取用户信息失败!',
+              type: 'error'
+            });
+            uni.hideLoading();
+          }
+        });
+    }
+  }
+};
 </script>
 
 <style></style>