Browse Source

微信登录

zaijin 2 years ago
parent
commit
e36cebc680
2 changed files with 106 additions and 87 deletions
  1. 2 3
      pages/center/phoneLogin/phoneLogin.vue
  2. 104 84
      pages/wechatLogin/wechatLogin.vue

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

@@ -123,7 +123,6 @@
 						this.$u.vuex('vuex_user', res.data);
 						this.$u.vuex('vuex_hasLogin', true);
 						this.wechatLogin()
-
 					} else {
 						this.$refs.uToast.show({
 							title: res.msg,
@@ -141,8 +140,8 @@
 			},
 			// 微信已登录则跳转到首页
 			jumpIndex() {
-				let ret = localStorage.getItem('backUrl')
-				if (ret && ret.indexOf('phoneLogin') < 0) {
+				let ret = localStorage.getItem('backUrl');
+				if (ret.indexOf('wechatLogin') > (-1) || (ret && ret.indexOf('phoneLogin') < 0)) {
 					// 截取url
 					const pagesIndex = ret.indexOf('pages')
 					if (pagesIndex > (-1)) {

+ 104 - 84
pages/wechatLogin/wechatLogin.vue

@@ -1,92 +1,112 @@
 <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 = window.location.href;
-    let locationLocaturl = window.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;
-      this.handleGetWXInfo(this.code);
-    } else {
-      window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.config.wxAppid}&redirect_uri=${encodeURIComponent(
+	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&#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);
-            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_user', res.data);
-          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 {
-            location.href = this.backUrl;
-          }
-          uni.hideLoading();
-        } else {
-          this.$refs.uToast.show({
-            title: res.msg || '获取用户信息失败!',
-            type: 'error'
-          });
-          uni.hideLoading();
-        }
-      });
-    }
-  }
-};
+      )}&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#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();
+					}
+				});
+			}
+		}
+	};
 </script>
 
 <style></style>