hookehuyr

feat(auth): 实现静默授权并优化授权流程

重构授权处理逻辑,新增静默授权功能避免页面跳转
优化401错误处理,使用静默授权替代页面跳转
移除未加入家庭时的强制跳转逻辑
重构returnToOriginalPage函数,优化页面跳转逻辑
...@@ -336,10 +336,6 @@ useDidShow(async () => { ...@@ -336,10 +336,6 @@ useDidShow(async () => {
336 if (rankingCardRef.value) { 336 if (rankingCardRef.value) {
337 rankingCardRef.value.refreshData(); 337 rankingCardRef.value.refreshData();
338 } 338 }
339 - } else {
340 - // 如果没有加入家庭(code为0),跳转到欢迎页面
341 - Taro.redirectTo({ url: '/pages/Welcome/index' });
342 - return; // 直接返回,不执行后续逻辑
343 } 339 }
344 340
345 // TODO: 获取广告信息 341 // TODO: 获取广告信息
......
1 <!-- 1 <!--
2 * @Date: 2022-09-19 14:11:06 2 * @Date: 2022-09-19 14:11:06
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2025-09-07 01:37:03 4 + * @LastEditTime: 2025-09-12 10:20:03
5 * @FilePath: /lls_program/src/pages/auth/index.vue 5 * @FilePath: /lls_program/src/pages/auth/index.vue
6 * @Description: 文件描述 6 * @Description: 文件描述
7 --> 7 -->
...@@ -22,12 +22,54 @@ import request from '@/utils/request'; ...@@ -22,12 +22,54 @@ import request from '@/utils/request';
22 <script> 22 <script>
23 import "./index.less"; 23 import "./index.less";
24 import { getCurrentPageParam } from "@/utils/weapp"; 24 import { getCurrentPageParam } from "@/utils/weapp";
25 -import { returnToOriginalPage } from "@/utils/authRedirect"; 25 +import { returnToOriginalPage, silentAuth } from '@/utils/authRedirect'
26 26
27 export default { 27 export default {
28 name: "authPage", 28 name: "authPage",
29 - mounted () { 29 + mounted() {
30 - // 授权登陆 30 + // 页面加载时优先尝试静默授权
31 + this.performAuth();
32 + },
33 + data () {
34 + return {
35 + canIUse: wx.canIUse('button.open-type.getUserInfo')
36 + }
37 + },
38 + onLoad: function() {
39 + // 查看是否授权
40 + // wx.getSetting({
41 + // success (res){
42 + // if (res.authSetting['scope.userInfo']) {
43 + // // 已经授权,可以直接调用 getUserInfo 获取头像昵称
44 + // wx.getUserInfo({
45 + // success: function(res) {
46 + // console.warn(res.userInfo)
47 + // }
48 + // })
49 + // }
50 + // }
51 + // })
52 + },
53 + methods: {
54 + performAuth() {
55 + // 优先尝试静默授权
56 + silentAuth().then(() => {
57 + // 静默授权成功,检查当前页面是否需要跳转
58 + const pages = Taro.getCurrentPages();
59 + const currentPage = pages[pages.length - 1];
60 + const currentRoute = currentPage?.route;
61 +
62 + // 只有在auth页面时才需要跳转回原页面
63 + if (currentRoute === 'pages/auth/index') {
64 + returnToOriginalPage();
65 + }
66 + }).catch(() => {
67 + // 静默授权失败,执行手动授权
68 + this.manualAuth();
69 + });
70 + },
71 + manualAuth() {
72 + // 手动授权登陆
31 Taro.login({ 73 Taro.login({
32 success: function (res) { 74 success: function (res) {
33 if (res.code) { 75 if (res.code) {
...@@ -42,14 +84,14 @@ export default { ...@@ -42,14 +84,14 @@ export default {
42 84
43 // 测试环境下传递openid,正式环境不传递 85 // 测试环境下传递openid,正式环境不传递
44 if (process.env.NODE_ENV === 'development') { 86 if (process.env.NODE_ENV === 'development') {
45 - requestData.openid = 'h-008'; 87 + // requestData.openid = 'h-008';
46 // requestData.openid = 'h-009'; 88 // requestData.openid = 'h-009';
47 // requestData.openid = 'h-010'; 89 // requestData.openid = 'h-010';
48 // requestData.openid = 'h-011'; 90 // requestData.openid = 'h-011';
49 // requestData.openid = 'h-012'; 91 // requestData.openid = 'h-012';
50 // requestData.openid = 'h-013'; 92 // requestData.openid = 'h-013';
51 // requestData.openid = 'oWbdFvkD5VtloC50wSNR9IWiU2q8'; 93 // requestData.openid = 'oWbdFvkD5VtloC50wSNR9IWiU2q8';
52 - // requestData.openid = 'oex8h5QZnZJto3ttvO6swSvylAQo'; 94 + requestData.openid = 'oex8h5QZnZJto3ttvO6swSvylAQo';
53 } 95 }
54 96
55 request.post('/srv/?a=openid', requestData) 97 request.post('/srv/?a=openid', requestData)
...@@ -61,15 +103,6 @@ export default { ...@@ -61,15 +103,6 @@ export default {
61 //TAG 小程序绑定cookie 103 //TAG 小程序绑定cookie
62 // 修改请求头 104 // 修改请求头
63 request.defaults.headers.cookie = res.cookies[0]; 105 request.defaults.headers.cookie = res.cookies[0];
64 - // if (res.data.data.avatar) {
65 - // Taro.reLaunch({
66 - // url: '../../' + getCurrentPageParam().url
67 - // })
68 - // } else { // 头像没有设置跳转完善信息页面
69 - // Taro.redirectTo({
70 - // url: '../apxUserInfo/index'
71 - // })
72 - // }
73 // TAG:处理分享跳转问题 - 使用新的重定向逻辑 106 // TAG:处理分享跳转问题 - 使用新的重定向逻辑
74 returnToOriginalPage(); 107 returnToOriginalPage();
75 Taro.hideLoading(); 108 Taro.hideLoading();
...@@ -89,27 +122,6 @@ export default { ...@@ -89,27 +122,6 @@ export default {
89 } 122 }
90 }) 123 })
91 }, 124 },
92 - data () {
93 - return {
94 - canIUse: wx.canIUse('button.open-type.getUserInfo')
95 - }
96 - },
97 - onLoad: function() {
98 - // 查看是否授权
99 - // wx.getSetting({
100 - // success (res){
101 - // if (res.authSetting['scope.userInfo']) {
102 - // // 已经授权,可以直接调用 getUserInfo 获取头像昵称
103 - // wx.getUserInfo({
104 - // success: function(res) {
105 - // console.warn(res.userInfo)
106 - // }
107 - // })
108 - // }
109 - // }
110 - // })
111 - },
112 - methods: {
113 bindGetUserInfo (e) { 125 bindGetUserInfo (e) {
114 console.warn(e.detail.userInfo) 126 console.warn(e.detail.userInfo)
115 }, 127 },
......
This diff is collapsed. Click to expand it.
1 /* 1 /*
2 * @Date: 2022-09-19 14:11:06 2 * @Date: 2022-09-19 14:11:06
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2025-09-04 15:33:42 4 + * @LastEditTime: 2025-09-12 10:44:34
5 * @FilePath: /lls_program/src/utils/request.js 5 * @FilePath: /lls_program/src/utils/request.js
6 * @Description: 简单axios封装,后续按实际处理 6 * @Description: 简单axios封装,后续按实际处理
7 */ 7 */
8 // import axios from 'axios' 8 // import axios from 'axios'
9 import axios from 'axios-miniprogram'; 9 import axios from 'axios-miniprogram';
10 // // import qs from 'Qs' 10 // // import qs from 'Qs'
11 -import { navigateToAuth } from '@/utils/authRedirect' 11 +import { silentAuth } from '@/utils/authRedirect'
12 12
13 // import { ProgressStart, ProgressEnd } from '@/components/axios-progress/progress'; 13 // import { ProgressStart, ProgressEnd } from '@/components/axios-progress/progress';
14 // import store from '@/store' 14 // import store from '@/store'
...@@ -147,18 +147,28 @@ service.interceptors.response.use( ...@@ -147,18 +147,28 @@ service.interceptors.response.use(
147 147
148 /** 148 /**
149 * 处理401未授权状态 149 * 处理401未授权状态
150 - * 清除本地sessionid并跳转到登录页 150 + * 清除本地sessionid并使用静默授权
151 */ 151 */
152 if (response.data.code === 401) { 152 if (response.data.code === 401) {
153 // 清除无效的sessionid 153 // 清除无效的sessionid
154 clearSessionId(); 154 clearSessionId();
155 - /** 155 +
156 - * 未授权跳转登录页 156 + // 使用静默授权处理,避免页面跳转
157 - * 授权完成后 返回当前页面 157 + return silentAuth(
158 - */ 158 + () => {
159 - setTimeout(() => { 159 + // 授权成功后重新发起原始请求
160 - navigateToAuth(); 160 + const originalRequest = response.config;
161 - }, 1000); 161 + return service.request(originalRequest);
162 + },
163 + (error) => {
164 + // 静默授权失败,直接返回错误,不跳转页面
165 + console.error('静默授权失败:', error);
166 + return Promise.reject(new Error('授权失败,请稍后重试'));
167 + }
168 + ).catch(() => {
169 + // 如果静默授权完全失败,直接返回错误,不跳转页面
170 + return Promise.reject(new Error('授权失败,请稍后重试'));
171 + });
162 } 172 }
163 return response 173 return response
164 }, 174 },
......