Browse Source

微信登录

zaijin 2 năm trước cách đây
mục cha
commit
e36cebc680

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

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

+ 104 - 84
pages/wechatLogin/wechatLogin.vue

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