Przeglądaj źródła

微信公众号免登录代码提交

wangqiang 8 miesięcy temu
rodzic
commit
682b82d318
3 zmienionych plików z 90 dodań i 8 usunięć
  1. 83 2
      App.vue
  2. 1 0
      pages/login/login.vue
  3. 6 6
      store/modules/user.js

+ 83 - 2
App.vue

@@ -1,5 +1,7 @@
 <script>
 	import Vue from 'vue'
+    import BASE_URL from './config.js'
+    import {mapActions} from 'vuex'
 	export default {
 		onLaunch: function() {
 			uni.getSystemInfo({
@@ -18,7 +20,7 @@
 					let custom = wx.getMenuButtonBoundingClientRect();
 					Vue.prototype.Custom = custom;
 					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
-					// #endif		
+					// #endif
 
 					// #ifdef MP-ALIPAY
 					Vue.prototype.StatusBar = e.statusBarHeight;
@@ -110,7 +112,86 @@
 		},
 		onHide: function() {
 			console.log('App Hide')
-		}
+		},
+        created: async function() {
+            // 获取 URL 中的 code 参数
+            const urlParams = new URLSearchParams(window.location.search);
+            const code = urlParams.get('code');
+
+            console.log('传入 code -------');
+            console.log(code);
+
+            if (code) {
+                try {
+                    // 发起请求获取用户信息
+                    const [err, res] = await uni.request({
+                        url: BASE_URL + `/flowable-server/app/proxy?appid=wxa79f618dcaf992f7&secret=24f99f0d9ed67b9078cd545be50f9ccb&code=` + code,
+                        method: 'GET'
+                    });
+
+                    if (res && res.statusCode === 200) {
+                        console.log('用户信息获取成功-----------');
+                        const data = res.data;
+                        console.log(data);
+                        console.log(data.openId);
+                        // 将微信用户的 openId 存到本地
+                        uni.setStorageSync('openId', data.openId);
+                        console.log('getStorageSync-> ', uni.getStorageSync('openId'));
+                    } else {
+                        console.error('获取用户信息失败:', res.data);
+                    }
+                } catch (err) {
+                    console.error('请求失败:', err);
+                }
+            }
+
+            // 判断当前微信号的openId 是否已经存入到数据库了,如果存入了,那么就直接通过openId匹配登录,否则就进入登录页面
+            const flag = true;
+            const localOpenId = uni.getStorageSync('openId');
+            if (localOpenId) {
+                try {
+                    // 发起请求获取库中的openId
+                    const [err, res] = await uni.request({
+                        url: BASE_URL + `/system-server/sys/user/allOpenIds`,
+                        method: 'GET'
+                    });
+
+                    if (res && res.statusCode === 200) {
+                        const data = res.data;
+                        console.log('allOpenIds:', data);
+                        // 确保 data 是数组
+                        if (Array.isArray(data) && data.includes(localOpenId)) {
+                            // 包含的话,则直接根据 openId来实现登录
+                            console.log(`data 包含 openId: ${localOpenId}`);
+                            const [err, res] = await uni.request({
+                                url: BASE_URL + `/auth-server/user/sys/wxLogin`,
+                                method: 'POST',
+                                data: { openId: localOpenId },
+                                header: {
+                                    'domain': 'ydddl'
+                                }
+                            });
+                            if (res && res.statusCode === 200) {
+                                this.$store.commit('SET_TOKEN',res.data.token);
+                                await this.refreshUserInfo();
+                                uni.reLaunch({
+                                    url: '../index/index',
+                                });
+                            }
+                        }
+                    }
+                } catch (err) {
+                    console.error('请求失败:', err);
+                }
+            }
+
+            if (flag) {
+                console.log('进来了');
+            }
+        },
+        methods: {
+            ...mapActions(['refreshUserInfo']),
+        }
 
 	}
 </script>

+ 1 - 0
pages/login/login.vue

@@ -159,6 +159,7 @@
 				 * 实际开发中,根据业务需要进行处理,这里仅做示例。
 				 */
 			this.$refs.uForm.validate().then(res => {
+				this.inputForm.openId = uni.getStorageSync('openId');
 				loginService.login(this.inputForm).then((data) => {
 				  this.$store.commit('SET_TOKEN',data.token);
 				  this.refreshUserInfo();

+ 6 - 6
store/modules/user.js

@@ -19,7 +19,7 @@ const user = {
 			state.avatar = userInfo.photo
 			$auth.setUserInfo(userInfo);
 		},
-		
+
 		SET_USER_AVATAR(state,photo){
 			state.avatar = photo;
 		},
@@ -48,13 +48,13 @@ const user = {
 		}
 	},
 	actions: {
-		refreshUserInfo({commit}) {
-			userService.info().then((data) => {
+		async refreshUserInfo({commit}) {
+			await userService.info().then((data) => {
 				commit('SET_USERINFO', data.user)
 				commit('SET_USERNAME', data.user.name)
 			})
-			
-			userService.getMenus().then((data) => {
+
+			await userService.getMenus().then((data) => {
 				commit('SET_PERMISSIONS', data.permissions || [])
 				commit('SET_DICTLIST', data.dictList || [])
 			})
@@ -62,4 +62,4 @@ const user = {
 	}
 }
 
-export default user
+export default user