feat(api): 为API请求添加静默模式选项
为fn函数和多个API接口添加silent选项,用于控制是否显示错误提示 简化未加入家庭时的跳转逻辑,移除冗余检查 更新测试环境的openid配置
Showing
6 changed files
with
23 additions
and
29 deletions
| ... | @@ -59,8 +59,6 @@ export const getMyFamiliesAPI = () => fn(fetch.get(Api.LIST_MY_FAMILIES)); | ... | @@ -59,8 +59,6 @@ export const getMyFamiliesAPI = () => fn(fetch.get(Api.LIST_MY_FAMILIES)); |
| 59 | 59 | ||
| 60 | /** | 60 | /** |
| 61 | * @description: 获取家庭首页数据 | 61 | * @description: 获取家庭首页数据 |
| 62 | - * @param {Object} [params] - 请求参数 | ||
| 63 | - * @param {string} [params.family_id] - 家庭ID,默认是上次选中的家庭 | ||
| 64 | * @returns {Promise} 返回家庭首页聚合数据 | 62 | * @returns {Promise} 返回家庭首页聚合数据 |
| 65 | * @returns {Object} response - 响应对象 | 63 | * @returns {Object} response - 响应对象 |
| 66 | * @returns {number} response.code - 响应状态码 | 64 | * @returns {number} response.code - 响应状态码 |
| ... | @@ -86,7 +84,7 @@ export const getMyFamiliesAPI = () => fn(fetch.get(Api.LIST_MY_FAMILIES)); | ... | @@ -86,7 +84,7 @@ export const getMyFamiliesAPI = () => fn(fetch.get(Api.LIST_MY_FAMILIES)); |
| 86 | * @returns {string} response.data.step_ranking[].avatar_url - 头像 | 84 | * @returns {string} response.data.step_ranking[].avatar_url - 头像 |
| 87 | * @returns {string} response.data.step_ranking[].today_step - 用户今日步数 | 85 | * @returns {string} response.data.step_ranking[].today_step - 用户今日步数 |
| 88 | */ | 86 | */ |
| 89 | -export const getFamilyDashboardAPI = (params) => fn(fetch.get(Api.GET_DASHBOARD, params)); | 87 | +export const getFamilyDashboardAPI = (options = {}) => fn(fetch.get(Api.GET_DASHBOARD), options); |
| 90 | 88 | ||
| 91 | /** | 89 | /** |
| 92 | * @description: 创建家庭 | 90 | * @description: 创建家庭 | ... | ... |
| 1 | /* | 1 | /* |
| 2 | * @Date: 2022-05-18 22:56:08 | 2 | * @Date: 2022-05-18 22:56:08 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2024-05-25 22:35:00 | 4 | + * @LastEditTime: 2025-09-12 22:50:45 |
| 5 | - * @FilePath: /meihuaApp/src/api/fn.js | 5 | + * @FilePath: /lls_program/src/api/fn.js |
| 6 | * @Description: 文件描述 | 6 | * @Description: 文件描述 |
| 7 | */ | 7 | */ |
| 8 | import axios from '@/utils/request'; | 8 | import axios from '@/utils/request'; |
| ... | @@ -12,9 +12,13 @@ import Taro from '@tarojs/taro' | ... | @@ -12,9 +12,13 @@ import Taro from '@tarojs/taro' |
| 12 | /** | 12 | /** |
| 13 | * 网络请求功能函数 | 13 | * 网络请求功能函数 |
| 14 | * @param {*} api 请求axios接口 | 14 | * @param {*} api 请求axios接口 |
| 15 | + * @param {Object} options 配置选项 | ||
| 16 | + * @param {boolean} options.silent 是否静默处理错误,不显示错误提示弹框 | ||
| 15 | * @returns 请求成功后,获取数据 | 17 | * @returns 请求成功后,获取数据 |
| 16 | */ | 18 | */ |
| 17 | -export const fn = (api) => { | 19 | +export const fn = (api, options = {}) => { |
| 20 | + const { silent = false } = options; | ||
| 21 | + | ||
| 18 | return api | 22 | return api |
| 19 | .then(res => { | 23 | .then(res => { |
| 20 | if (res.data.code) { | 24 | if (res.data.code) { |
| ... | @@ -22,11 +26,14 @@ export const fn = (api) => { | ... | @@ -22,11 +26,14 @@ export const fn = (api) => { |
| 22 | } else { | 26 | } else { |
| 23 | // tslint:disable-next-line: no-console | 27 | // tslint:disable-next-line: no-console |
| 24 | console.warn(res); | 28 | console.warn(res); |
| 29 | + // 只有在非静默模式下才显示错误提示 | ||
| 30 | + if (!silent) { | ||
| 25 | Taro.showToast({ | 31 | Taro.showToast({ |
| 26 | title: res.data.msg, | 32 | title: res.data.msg, |
| 27 | icon: 'none', | 33 | icon: 'none', |
| 28 | duration: 2000 | 34 | duration: 2000 |
| 29 | }); | 35 | }); |
| 36 | + } | ||
| 30 | return false; | 37 | return false; |
| 31 | } | 38 | } |
| 32 | }) | 39 | }) | ... | ... |
| 1 | /* | 1 | /* |
| 2 | * @Date: 2025-09-11 12:20:17 | 2 | * @Date: 2025-09-11 12:20:17 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-09-11 15:16:03 | 4 | + * @LastEditTime: 2025-09-12 22:53:27 |
| 5 | * @FilePath: /lls_program/src/api/photo.js | 5 | * @FilePath: /lls_program/src/api/photo.js |
| 6 | * @Description: 文件描述 | 6 | * @Description: 文件描述 |
| 7 | */ | 7 | */ |
| ... | @@ -28,7 +28,7 @@ const Api = { | ... | @@ -28,7 +28,7 @@ const Api = { |
| 28 | * @returns {Boolean} response.data.is_my - 是否是我的相册 1=是, 0=否 | 28 | * @returns {Boolean} response.data.is_my - 是否是我的相册 1=是, 0=否 |
| 29 | * @returns {string} response.data.thumbnail - 缩略图URL | 29 | * @returns {string} response.data.thumbnail - 缩略图URL |
| 30 | */ | 30 | */ |
| 31 | -export const getPhotoListAPI = (params = {}) => fn(fetch.post(Api.PHOTO_LIST, params)); | 31 | +export const getPhotoListAPI = (params = {}, options = {}) => fn(fetch.post(Api.PHOTO_LIST, params), options); |
| 32 | 32 | ||
| 33 | /** | 33 | /** |
| 34 | * @description 保存相册 | 34 | * @description 保存相册 | ... | ... |
| ... | @@ -141,6 +141,8 @@ const fetchAlbumData = async () => { | ... | @@ -141,6 +141,8 @@ const fetchAlbumData = async () => { |
| 141 | const response = await getPhotoListAPI({ | 141 | const response = await getPhotoListAPI({ |
| 142 | page: 0, | 142 | page: 0, |
| 143 | limit: 4 // 首页只显示4张 | 143 | limit: 4 // 首页只显示4张 |
| 144 | + }, { | ||
| 145 | + silent: true | ||
| 144 | }); | 146 | }); |
| 145 | 147 | ||
| 146 | if (response.code) { | 148 | if (response.code) { | ... | ... |
| ... | @@ -316,7 +316,7 @@ const totalFamilySteps = ref(0); | ... | @@ -316,7 +316,7 @@ const totalFamilySteps = ref(0); |
| 316 | const refreshDashboardData = async () => { | 316 | const refreshDashboardData = async () => { |
| 317 | try { | 317 | try { |
| 318 | console.log('开始刷新Dashboard页面数据'); | 318 | console.log('开始刷新Dashboard页面数据'); |
| 319 | - const { code, data } = await getFamilyDashboardAPI(); | 319 | + const { code, data } = await getFamilyDashboardAPI({ silent: true }); |
| 320 | if (code) { | 320 | if (code) { |
| 321 | // 获取家庭ID | 321 | // 获取家庭ID |
| 322 | family_id.value = data.family.id; | 322 | family_id.value = data.family.id; |
| ... | @@ -363,22 +363,12 @@ useLoad(async () => { | ... | @@ -363,22 +363,12 @@ useLoad(async () => { |
| 363 | // 检查用户是否已加入家庭 | 363 | // 检查用户是否已加入家庭 |
| 364 | const hasFamily = await checkUserHasFamily() | 364 | const hasFamily = await checkUserHasFamily() |
| 365 | 365 | ||
| 366 | - // 如果用户没有加入家庭,检查当前页面是否已经是Welcome页面 | 366 | + // 如果用户没有加入家庭,跳转到欢迎页面 |
| 367 | if (!hasFamily) { | 367 | if (!hasFamily) { |
| 368 | - // 获取当前页面路由 | ||
| 369 | - const pages = Taro.getCurrentPages(); | ||
| 370 | - const currentPage = pages[pages.length - 1]; | ||
| 371 | - const currentRoute = currentPage?.route || ''; | ||
| 372 | - | ||
| 373 | - // 只有当前页面不是Welcome页面时才跳转,避免重复跳转 | ||
| 374 | - if (currentRoute !== 'pages/Welcome/index') { | ||
| 375 | console.warn('用户未加入家庭,跳转到欢迎页面'); | 368 | console.warn('用户未加入家庭,跳转到欢迎页面'); |
| 376 | await Taro.reLaunch({ | 369 | await Taro.reLaunch({ |
| 377 | url: '/pages/Welcome/index' | 370 | url: '/pages/Welcome/index' |
| 378 | }) | 371 | }) |
| 379 | - } else { | ||
| 380 | - console.log('当前已在Welcome页面,跳过跳转'); | ||
| 381 | - } | ||
| 382 | return | 372 | return |
| 383 | } | 373 | } |
| 384 | }, | 374 | }, | ... | ... |
| 1 | /* | 1 | /* |
| 2 | * @Date: 2025-01-25 10:00:00 | 2 | * @Date: 2025-01-25 10:00:00 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-09-12 21:49:10 | 4 | + * @LastEditTime: 2025-09-12 22:56:27 |
| 5 | * @FilePath: /lls_program/src/utils/authRedirect.js | 5 | * @FilePath: /lls_program/src/utils/authRedirect.js |
| 6 | * @Description: 授权重定向处理工具函数 | 6 | * @Description: 授权重定向处理工具函数 |
| 7 | */ | 7 | */ |
| ... | @@ -226,10 +226,9 @@ export const addShareFlag = (path) => { | ... | @@ -226,10 +226,9 @@ export const addShareFlag = (path) => { |
| 226 | * 在后台处理授权,不跳转页面,避免用户感知 | 226 | * 在后台处理授权,不跳转页面,避免用户感知 |
| 227 | * @param {Function} onSuccess - 授权成功回调 | 227 | * @param {Function} onSuccess - 授权成功回调 |
| 228 | * @param {Function} onError - 授权失败回调 | 228 | * @param {Function} onError - 授权失败回调 |
| 229 | - * @param {boolean} skipRedirect - 是否跳过重定向,默认false | ||
| 230 | * @returns {Promise} 授权结果 | 229 | * @returns {Promise} 授权结果 |
| 231 | */ | 230 | */ |
| 232 | -export const silentAuth = async (onSuccess, onError, skipRedirect = false) => { | 231 | +export const silentAuth = async (onSuccess, onError) => { |
| 233 | try { | 232 | try { |
| 234 | // 检查是否已经授权 | 233 | // 检查是否已经授权 |
| 235 | if (!needAuth()) { | 234 | if (!needAuth()) { |
| ... | @@ -265,12 +264,12 @@ export const silentAuth = async (onSuccess, onError, skipRedirect = false) => { | ... | @@ -265,12 +264,12 @@ export const silentAuth = async (onSuccess, onError, skipRedirect = false) => { |
| 265 | 264 | ||
| 266 | // 测试环境下传递openid,正式环境不传递 | 265 | // 测试环境下传递openid,正式环境不传递 |
| 267 | if (process.env.NODE_ENV === 'development') { | 266 | if (process.env.NODE_ENV === 'development') { |
| 268 | - // requestData.openid = 'h-008'; | 267 | + requestData.openid = 'h-008'; |
| 269 | // requestData.openid = 'h-009'; | 268 | // requestData.openid = 'h-009'; |
| 270 | // requestData.openid = 'h-010'; | 269 | // requestData.openid = 'h-010'; |
| 271 | // requestData.openid = 'h-011'; | 270 | // requestData.openid = 'h-011'; |
| 272 | // requestData.openid = 'h-012'; | 271 | // requestData.openid = 'h-012'; |
| 273 | - requestData.openid = 'h-013'; | 272 | + // requestData.openid = 'h-013'; |
| 274 | // requestData.openid = 'oWbdFvkD5VtloC50wSNR9IWiU2q8'; | 273 | // requestData.openid = 'oWbdFvkD5VtloC50wSNR9IWiU2q8'; |
| 275 | // requestData.openid = 'oex8h5QZnZJto3ttvO6swSvylAQo'; | 274 | // requestData.openid = 'oex8h5QZnZJto3ttvO6swSvylAQo'; |
| 276 | } | 275 | } |
| ... | @@ -308,15 +307,13 @@ export const silentAuth = async (onSuccess, onError, skipRedirect = false) => { | ... | @@ -308,15 +307,13 @@ export const silentAuth = async (onSuccess, onError, skipRedirect = false) => { |
| 308 | try { | 307 | try { |
| 309 | const hasFamily = await checkUserHasFamily() | 308 | const hasFamily = await checkUserHasFamily() |
| 310 | 309 | ||
| 311 | - // 如果用户没有加入家庭,根据skipRedirect参数决定是否跳转 | 310 | + // 如果用户没有加入家庭,跳转到欢迎页面 |
| 312 | if (!hasFamily) { | 311 | if (!hasFamily) { |
| 313 | - if (!skipRedirect) { | ||
| 314 | await Taro.reLaunch({ | 312 | await Taro.reLaunch({ |
| 315 | url: '/pages/Welcome/index' | 313 | url: '/pages/Welcome/index' |
| 316 | }) | 314 | }) |
| 317 | - } | ||
| 318 | 315 | ||
| 319 | - const result = { ...response.data, redirected: !skipRedirect } | 316 | + const result = { ...response.data, redirected: true } |
| 320 | if (onSuccess) { | 317 | if (onSuccess) { |
| 321 | onSuccess(result) | 318 | onSuccess(result) |
| 322 | } | 319 | } | ... | ... |
-
Please register or login to post a comment