hookehuyr

feat(登录): 重构登录授权流程并增加错误处理

将登录逻辑从mounted迁移到onReady,添加延迟确保页面准备完成
提取登录逻辑为独立方法,增加错误处理和降级方案
优化页面跳转逻辑,添加重试机制和用户反馈
```

```msg
fix(反馈): 更新反馈接口参数和分类映射

修改反馈接口参数名(content->note, contact_info->contact)
更新分类映射值(2->3, 4->7)以匹配后端变更
```

```msg
perf(请求): 增加请求超时时间至10秒

将axios请求超时从5秒增加到10秒以提高网络稳定性
1 /* 1 /*
2 * @Date: 2025-07-10 16:13:08 2 * @Date: 2025-07-10 16:13:08
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2025-07-14 15:19:19 4 + * @LastEditTime: 2025-07-14 15:45:57
5 * @FilePath: /jgdl/src/api/other.js 5 * @FilePath: /jgdl/src/api/other.js
6 * @Description: 文件描述 6 * @Description: 文件描述
7 */ 7 */
...@@ -58,10 +58,10 @@ export const toggleFavoriteDelAPI = (params) => fn(fetch.post(Api.TOGGLE_FAVORIT ...@@ -58,10 +58,10 @@ export const toggleFavoriteDelAPI = (params) => fn(fetch.post(Api.TOGGLE_FAVORIT
58 /** 58 /**
59 * @description: 提交反馈 59 * @description: 提交反馈
60 * @param {*} params 60 * @param {*} params
61 - * @param {string} params.category - 反馈类型(1=功能建议,2=界面设计,3=车辆信息, 4=其他) 61 + * @param {string} params.category - 反馈类型(1=功能建议,3=界面设计,5=车辆信息, 7=其他)
62 - * @param {string} params.content - 反馈内容 62 + * @param {string} params.note - 反馈内容
63 * @param {string} params.images - 上传图片(选填) 最多3张 63 * @param {string} params.images - 上传图片(选填) 最多3张
64 - * @param {string} params.contact_info - 联系方式(选填) 手机号或微信号 64 + * @param {string} params.contact - 联系方式(选填) 手机号或微信号
65 * @returns 65 * @returns
66 */ 66 */
67 export const submitFeedbackAPI = (params) => fn(fetch.post(Api.SUBMIT_FEEDBACK, params)); 67 export const submitFeedbackAPI = (params) => fn(fetch.post(Api.SUBMIT_FEEDBACK, params));
......
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-07-09 11:47:21 4 + * @LastEditTime: 2025-07-14 15:37:49
5 * @FilePath: /jgdl/src/pages/auth/index.vue 5 * @FilePath: /jgdl/src/pages/auth/index.vue
6 * @Description: 文件描述 6 * @Description: 文件描述
7 --> 7 -->
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
14 14
15 <script setup> 15 <script setup>
16 import Taro from '@tarojs/taro' 16 import Taro from '@tarojs/taro'
17 -import { ref } from "vue";
18 import request from '@/utils/request'; 17 import request from '@/utils/request';
19 18
20 </script> 19 </script>
...@@ -25,66 +24,14 @@ import { getCurrentPageParam } from "@/utils/weapp"; ...@@ -25,66 +24,14 @@ import { getCurrentPageParam } from "@/utils/weapp";
25 24
26 export default { 25 export default {
27 name: "authPage", 26 name: "authPage",
28 - mounted () { 27 + /**
29 - // 授权登陆 28 + * 页面准备完成后执行登录
30 - Taro.login({ 29 + */
31 - success: function (res) { 30 + onReady() {
32 - if (res.code) { 31 + // 添加短暂延迟确保页面完全准备好
33 - //发起网络请求 32 + setTimeout(() => {
34 - Taro.showLoading({ 33 + this.performLogin();
35 - title: '授权中', 34 + }, 100);
36 - })
37 - request.post('/srv/?a=openid', {
38 - code: res.code,
39 - openid: 'h-008'
40 - // openid: 'o5NFZ5cFQtLRy3aVHaZMLkjHFusI'
41 - // openid: 'o5NFZ5TpgG4FwYursGCLjcUJH2ak'
42 - // openid: 'o5NFZ5cqroPYwawCp8FEOxewtgnw'
43 - })
44 - .then(res => {
45 - if (res.data.code) {
46 - var cookie = res.cookies[0];
47 - if (cookie != null) {
48 - wx.setStorageSync("sessionid", res.cookies[0]);//服务器返回的 Set-Cookie,保存到本地
49 - //TAG 小程序绑定cookie
50 - // 修改请求头
51 - request.defaults.headers.cookie = res.cookies[0];
52 - // if (res.data.data.avatar) {
53 - // Taro.reLaunch({
54 - // url: '../../' + getCurrentPageParam().url
55 - // })
56 - // } else { // 头像没有设置跳转完善信息页面
57 - // Taro.redirectTo({
58 - // url: '../apxUserInfo/index'
59 - // })
60 - // }
61 - // TAG:处理分享跳转问题
62 - const params = getCurrentPageParam();
63 - if (getCurrentPageParam().url === 'pages/productDetail/index') { // 详情页的分享跳转处理
64 - Taro.reLaunch({
65 - url: `../../${params.url}?id=${params.id}`
66 - })
67 - } else { // 其他页面分享跳首页
68 - Taro.reLaunch({
69 - url: `/pages/index/index`
70 - })
71 - }
72 - Taro.hideLoading();
73 - }
74 - } else {
75 - console.warn(res.data.msg);
76 - Taro.hideLoading();
77 - }
78 - })
79 - .catch(err => {
80 - console.error(err);
81 - Taro.hideLoading();
82 - });
83 - } else {
84 - console.error('登录失败!' + res.errMsg)
85 - }
86 - }
87 - })
88 }, 35 },
89 data () { 36 data () {
90 return { 37 return {
...@@ -107,6 +54,142 @@ export default { ...@@ -107,6 +54,142 @@ export default {
107 // }) 54 // })
108 }, 55 },
109 methods: { 56 methods: {
57 + /**
58 + * 执行登录授权流程
59 + */
60 + performLogin() {
61 + Taro.login({
62 + success: (res) => {
63 + if (res.code) {
64 + this.handleLoginSuccess(res.code);
65 + } else {
66 + console.error('登录失败!' + res.errMsg);
67 + this.handleLoginError('登录失败,请重试');
68 + }
69 + },
70 + fail: (err) => {
71 + console.error('登录调用失败:', err);
72 + this.handleLoginError('登录失败,请重试');
73 + }
74 + });
75 + },
76 +
77 + /**
78 + * 处理登录成功后的网络请求
79 + * @param {string} code - 微信登录code
80 + */
81 + handleLoginSuccess(code) {
82 + Taro.showLoading({
83 + title: '授权中',
84 + });
85 +
86 + request.post('/srv/?a=openid', {
87 + code: code,
88 + openid: 'h-008'
89 + // openid: 'o5NFZ5cFQtLRy3aVHaZMLkjHFusI'
90 + // openid: 'o5NFZ5TpgG4FwYursGCLjcUJH2ak'
91 + // openid: 'o5NFZ5cqroPYwawCp8FEOxewtgnw'
92 + })
93 + .then(res => {
94 + if (res.data.code) {
95 + const cookie = res.cookies[0];
96 + if (cookie != null) {
97 + // 保存sessionid到本地存储
98 + wx.setStorageSync("sessionid", res.cookies[0]);
99 + // 修改请求头
100 + request.defaults.headers.cookie = res.cookies[0];
101 +
102 + // 添加延迟确保存储操作完成
103 + setTimeout(() => {
104 + this.handleNavigateAfterAuth();
105 + }, 200);
106 + } else {
107 + // cookie为空时跳转到首页
108 + this.navigateToHome();
109 + }
110 + } else {
111 + console.warn(res.data.msg);
112 + this.handleLoginError(res.data.msg || '授权失败');
113 + }
114 + })
115 + .catch(err => {
116 + console.error('网络请求失败:', err);
117 + this.handleLoginError('网络请求失败,请重试');
118 + })
119 + .finally(() => {
120 + Taro.hideLoading();
121 + });
122 + },
123 +
124 + /**
125 + * 处理授权成功后的页面跳转
126 + */
127 + handleNavigateAfterAuth() {
128 + try {
129 + const params = getCurrentPageParam();
130 +
131 + if (params.url === 'pages/productDetail/index') {
132 + // 详情页的分享跳转处理 - 使用绝对路径
133 + this.navigateToPage(`/pages/productDetail/index?id=${params.id}`);
134 + } else {
135 + // 其他页面跳转到首页
136 + this.navigateToHome();
137 + }
138 + } catch (error) {
139 + console.error('页面跳转参数解析失败:', error);
140 + // 降级处理:跳转到首页
141 + this.navigateToHome();
142 + }
143 + },
144 +
145 + /**
146 + * 跳转到指定页面
147 + * @param {string} url - 页面路径
148 + */
149 + navigateToPage(url) {
150 + Taro.reLaunch({
151 + url: url
152 + }).catch(err => {
153 + console.error('页面跳转失败:', err);
154 + // 降级处理:跳转到首页
155 + this.navigateToHome();
156 + });
157 + },
158 +
159 + /**
160 + * 跳转到首页
161 + */
162 + navigateToHome() {
163 + Taro.reLaunch({
164 + url: `/pages/index/index`
165 + }).catch(err => {
166 + console.error('跳转首页失败:', err);
167 + // 最后的降级处理
168 + Taro.showToast({
169 + title: '页面跳转失败',
170 + icon: 'error'
171 + });
172 + });
173 + },
174 +
175 + /**
176 + * 处理登录错误
177 + * @param {string} message - 错误信息
178 + */
179 + handleLoginError(message) {
180 + Taro.hideLoading();
181 + Taro.showToast({
182 + title: message,
183 + icon: 'error',
184 + duration: 2000
185 + });
186 +
187 + // 错误情况下延迟跳转到首页
188 + setTimeout(() => {
189 + this.navigateToHome();
190 + }, 2000);
191 + },
192 +
110 bindGetUserInfo (e) { 193 bindGetUserInfo (e) {
111 console.warn(e.detail.userInfo) 194 console.warn(e.detail.userInfo)
112 }, 195 },
......
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-07-14 15:20:15 4 + * @LastEditTime: 2025-07-14 15:46:39
5 * @FilePath: /jgdl/src/pages/feedBack/index.vue 5 * @FilePath: /jgdl/src/pages/feedBack/index.vue
6 * @Description: 意见反馈页面 6 * @Description: 意见反馈页面
7 --> 7 -->
...@@ -280,17 +280,17 @@ const handleSubmit = async () => { ...@@ -280,17 +280,17 @@ const handleSubmit = async () => {
280 // 将分类ID转换为数字格式 280 // 将分类ID转换为数字格式
281 const categoryMap = { 281 const categoryMap = {
282 'feature': '1', 282 'feature': '1',
283 - 'ui': '2', 283 + 'ui': '3',
284 - 'vehicle': '3', 284 + 'vehicle': '5',
285 - 'other': '4' 285 + 'other': '7'
286 } 286 }
287 287
288 // 构建提交数据 288 // 构建提交数据
289 const submitData = { 289 const submitData = {
290 category: categoryMap[selectedCategory.value], 290 category: categoryMap[selectedCategory.value],
291 - content: feedbackText.value.trim(), 291 + note: feedbackText.value.trim(),
292 - contact_info: contactInfo.value.trim(), 292 + contact: contactInfo.value.trim(),
293 - images: uploadedImages.value.join(',') 293 + images: uploadedImages.value
294 } 294 }
295 295
296 // 调用真实的API接口提交数据 296 // 调用真实的API接口提交数据
...@@ -300,7 +300,7 @@ const handleSubmit = async () => { ...@@ -300,7 +300,7 @@ const handleSubmit = async () => {
300 300
301 if (response.code) { 301 if (response.code) {
302 showSuccessModal.value = true 302 showSuccessModal.value = true
303 - 303 +
304 // 2秒后自动关闭并重置表单 304 // 2秒后自动关闭并重置表单
305 setTimeout(() => { 305 setTimeout(() => {
306 closeSuccessModal() 306 closeSuccessModal()
......
...@@ -56,7 +56,7 @@ const clearSessionId = () => { ...@@ -56,7 +56,7 @@ const clearSessionId = () => {
56 const service = axios.create({ 56 const service = axios.create({
57 baseURL: BASE_URL, // url = base url + request url 57 baseURL: BASE_URL, // url = base url + request url
58 // withCredentials: true, // send cookies when cross-domain requests 58 // withCredentials: true, // send cookies when cross-domain requests
59 - timeout: 5000, // request timeout 59 + timeout: 10000, // request timeout - 增加到10秒以提高网络稳定性
60 }) 60 })
61 61
62 service.defaults.params = { 62 service.defaults.params = {
......