hookehuyr

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

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

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

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

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

将axios请求超时从5秒增加到10秒以提高网络稳定性
/*
* @Date: 2025-07-10 16:13:08
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-07-14 15:19:19
* @LastEditTime: 2025-07-14 15:45:57
* @FilePath: /jgdl/src/api/other.js
* @Description: 文件描述
*/
......@@ -58,10 +58,10 @@ export const toggleFavoriteDelAPI = (params) => fn(fetch.post(Api.TOGGLE_FAVORIT
/**
* @description: 提交反馈
* @param {*} params
* @param {string} params.category - 反馈类型(1=功能建议,2=界面设计,3=车辆信息, 4=其他)
* @param {string} params.content - 反馈内容
* @param {string} params.category - 反馈类型(1=功能建议,3=界面设计,5=车辆信息, 7=其他)
* @param {string} params.note - 反馈内容
* @param {string} params.images - 上传图片(选填) 最多3张
* @param {string} params.contact_info - 联系方式(选填) 手机号或微信号
* @param {string} params.contact - 联系方式(选填) 手机号或微信号
* @returns
*/
export const submitFeedbackAPI = (params) => fn(fetch.post(Api.SUBMIT_FEEDBACK, params));
......
<!--
* @Date: 2022-09-19 14:11:06
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-07-09 11:47:21
* @LastEditTime: 2025-07-14 15:37:49
* @FilePath: /jgdl/src/pages/auth/index.vue
* @Description: 文件描述
-->
......@@ -14,7 +14,6 @@
<script setup>
import Taro from '@tarojs/taro'
import { ref } from "vue";
import request from '@/utils/request';
</script>
......@@ -25,66 +24,14 @@ import { getCurrentPageParam } from "@/utils/weapp";
export default {
name: "authPage",
mounted () {
// 授权登陆
Taro.login({
success: function (res) {
if (res.code) {
//发起网络请求
Taro.showLoading({
title: '授权中',
})
request.post('/srv/?a=openid', {
code: res.code,
openid: 'h-008'
// openid: 'o5NFZ5cFQtLRy3aVHaZMLkjHFusI'
// openid: 'o5NFZ5TpgG4FwYursGCLjcUJH2ak'
// openid: 'o5NFZ5cqroPYwawCp8FEOxewtgnw'
})
.then(res => {
if (res.data.code) {
var cookie = res.cookies[0];
if (cookie != null) {
wx.setStorageSync("sessionid", res.cookies[0]);//服务器返回的 Set-Cookie,保存到本地
//TAG 小程序绑定cookie
// 修改请求头
request.defaults.headers.cookie = res.cookies[0];
// if (res.data.data.avatar) {
// Taro.reLaunch({
// url: '../../' + getCurrentPageParam().url
// })
// } else { // 头像没有设置跳转完善信息页面
// Taro.redirectTo({
// url: '../apxUserInfo/index'
// })
// }
// TAG:处理分享跳转问题
const params = getCurrentPageParam();
if (getCurrentPageParam().url === 'pages/productDetail/index') { // 详情页的分享跳转处理
Taro.reLaunch({
url: `../../${params.url}?id=${params.id}`
})
} else { // 其他页面分享跳首页
Taro.reLaunch({
url: `/pages/index/index`
})
}
Taro.hideLoading();
}
} else {
console.warn(res.data.msg);
Taro.hideLoading();
}
})
.catch(err => {
console.error(err);
Taro.hideLoading();
});
} else {
console.error('登录失败!' + res.errMsg)
}
}
})
/**
* 页面准备完成后执行登录
*/
onReady() {
// 添加短暂延迟确保页面完全准备好
setTimeout(() => {
this.performLogin();
}, 100);
},
data () {
return {
......@@ -107,6 +54,142 @@ export default {
// })
},
methods: {
/**
* 执行登录授权流程
*/
performLogin() {
Taro.login({
success: (res) => {
if (res.code) {
this.handleLoginSuccess(res.code);
} else {
console.error('登录失败!' + res.errMsg);
this.handleLoginError('登录失败,请重试');
}
},
fail: (err) => {
console.error('登录调用失败:', err);
this.handleLoginError('登录失败,请重试');
}
});
},
/**
* 处理登录成功后的网络请求
* @param {string} code - 微信登录code
*/
handleLoginSuccess(code) {
Taro.showLoading({
title: '授权中',
});
request.post('/srv/?a=openid', {
code: code,
openid: 'h-008'
// openid: 'o5NFZ5cFQtLRy3aVHaZMLkjHFusI'
// openid: 'o5NFZ5TpgG4FwYursGCLjcUJH2ak'
// openid: 'o5NFZ5cqroPYwawCp8FEOxewtgnw'
})
.then(res => {
if (res.data.code) {
const cookie = res.cookies[0];
if (cookie != null) {
// 保存sessionid到本地存储
wx.setStorageSync("sessionid", res.cookies[0]);
// 修改请求头
request.defaults.headers.cookie = res.cookies[0];
// 添加延迟确保存储操作完成
setTimeout(() => {
this.handleNavigateAfterAuth();
}, 200);
} else {
// cookie为空时跳转到首页
this.navigateToHome();
}
} else {
console.warn(res.data.msg);
this.handleLoginError(res.data.msg || '授权失败');
}
})
.catch(err => {
console.error('网络请求失败:', err);
this.handleLoginError('网络请求失败,请重试');
})
.finally(() => {
Taro.hideLoading();
});
},
/**
* 处理授权成功后的页面跳转
*/
handleNavigateAfterAuth() {
try {
const params = getCurrentPageParam();
if (params.url === 'pages/productDetail/index') {
// 详情页的分享跳转处理 - 使用绝对路径
this.navigateToPage(`/pages/productDetail/index?id=${params.id}`);
} else {
// 其他页面跳转到首页
this.navigateToHome();
}
} catch (error) {
console.error('页面跳转参数解析失败:', error);
// 降级处理:跳转到首页
this.navigateToHome();
}
},
/**
* 跳转到指定页面
* @param {string} url - 页面路径
*/
navigateToPage(url) {
Taro.reLaunch({
url: url
}).catch(err => {
console.error('页面跳转失败:', err);
// 降级处理:跳转到首页
this.navigateToHome();
});
},
/**
* 跳转到首页
*/
navigateToHome() {
Taro.reLaunch({
url: `/pages/index/index`
}).catch(err => {
console.error('跳转首页失败:', err);
// 最后的降级处理
Taro.showToast({
title: '页面跳转失败',
icon: 'error'
});
});
},
/**
* 处理登录错误
* @param {string} message - 错误信息
*/
handleLoginError(message) {
Taro.hideLoading();
Taro.showToast({
title: message,
icon: 'error',
duration: 2000
});
// 错误情况下延迟跳转到首页
setTimeout(() => {
this.navigateToHome();
}, 2000);
},
bindGetUserInfo (e) {
console.warn(e.detail.userInfo)
},
......
<!--
* @Date: 2022-09-19 14:11:06
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-07-14 15:20:15
* @LastEditTime: 2025-07-14 15:46:39
* @FilePath: /jgdl/src/pages/feedBack/index.vue
* @Description: 意见反馈页面
-->
......@@ -280,17 +280,17 @@ const handleSubmit = async () => {
// 将分类ID转换为数字格式
const categoryMap = {
'feature': '1',
'ui': '2',
'vehicle': '3',
'other': '4'
'ui': '3',
'vehicle': '5',
'other': '7'
}
// 构建提交数据
const submitData = {
category: categoryMap[selectedCategory.value],
content: feedbackText.value.trim(),
contact_info: contactInfo.value.trim(),
images: uploadedImages.value.join(',')
note: feedbackText.value.trim(),
contact: contactInfo.value.trim(),
images: uploadedImages.value
}
// 调用真实的API接口提交数据
......@@ -300,7 +300,7 @@ const handleSubmit = async () => {
if (response.code) {
showSuccessModal.value = true
// 2秒后自动关闭并重置表单
setTimeout(() => {
closeSuccessModal()
......
......@@ -56,7 +56,7 @@ const clearSessionId = () => {
const service = axios.create({
baseURL: BASE_URL, // url = base url + request url
// withCredentials: true, // send cookies when cross-domain requests
timeout: 5000, // request timeout
timeout: 10000, // request timeout - 增加到10秒以提高网络稳定性
})
service.defaults.params = {
......