hookehuyr

feat(api): 为API请求添加静默模式选项

为fn函数和多个API接口添加silent选项,用于控制是否显示错误提示
简化未加入家庭时的跳转逻辑,移除冗余检查
更新测试环境的openid配置
......@@ -59,8 +59,6 @@ export const getMyFamiliesAPI = () => fn(fetch.get(Api.LIST_MY_FAMILIES));
/**
* @description: 获取家庭首页数据
* @param {Object} [params] - 请求参数
* @param {string} [params.family_id] - 家庭ID,默认是上次选中的家庭
* @returns {Promise} 返回家庭首页聚合数据
* @returns {Object} response - 响应对象
* @returns {number} response.code - 响应状态码
......@@ -86,7 +84,7 @@ export const getMyFamiliesAPI = () => fn(fetch.get(Api.LIST_MY_FAMILIES));
* @returns {string} response.data.step_ranking[].avatar_url - 头像
* @returns {string} response.data.step_ranking[].today_step - 用户今日步数
*/
export const getFamilyDashboardAPI = (params) => fn(fetch.get(Api.GET_DASHBOARD, params));
export const getFamilyDashboardAPI = (options = {}) => fn(fetch.get(Api.GET_DASHBOARD), options);
/**
* @description: 创建家庭
......
/*
* @Date: 2022-05-18 22:56:08
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-05-25 22:35:00
* @FilePath: /meihuaApp/src/api/fn.js
* @LastEditTime: 2025-09-12 22:50:45
* @FilePath: /lls_program/src/api/fn.js
* @Description: 文件描述
*/
import axios from '@/utils/request';
......@@ -12,9 +12,13 @@ import Taro from '@tarojs/taro'
/**
* 网络请求功能函数
* @param {*} api 请求axios接口
* @param {Object} options 配置选项
* @param {boolean} options.silent 是否静默处理错误,不显示错误提示弹框
* @returns 请求成功后,获取数据
*/
export const fn = (api) => {
export const fn = (api, options = {}) => {
const { silent = false } = options;
return api
.then(res => {
if (res.data.code) {
......@@ -22,11 +26,14 @@ export const fn = (api) => {
} else {
// tslint:disable-next-line: no-console
console.warn(res);
Taro.showToast({
title: res.data.msg,
icon: 'none',
duration: 2000
});
// 只有在非静默模式下才显示错误提示
if (!silent) {
Taro.showToast({
title: res.data.msg,
icon: 'none',
duration: 2000
});
}
return false;
}
})
......
/*
* @Date: 2025-09-11 12:20:17
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-09-11 15:16:03
* @LastEditTime: 2025-09-12 22:53:27
* @FilePath: /lls_program/src/api/photo.js
* @Description: 文件描述
*/
......@@ -28,7 +28,7 @@ const Api = {
* @returns {Boolean} response.data.is_my - 是否是我的相册 1=是, 0=否
* @returns {string} response.data.thumbnail - 缩略图URL
*/
export const getPhotoListAPI = (params = {}) => fn(fetch.post(Api.PHOTO_LIST, params));
export const getPhotoListAPI = (params = {}, options = {}) => fn(fetch.post(Api.PHOTO_LIST, params), options);
/**
* @description 保存相册
......
......@@ -141,6 +141,8 @@ const fetchAlbumData = async () => {
const response = await getPhotoListAPI({
page: 0,
limit: 4 // 首页只显示4张
}, {
silent: true
});
if (response.code) {
......
......@@ -316,7 +316,7 @@ const totalFamilySteps = ref(0);
const refreshDashboardData = async () => {
try {
console.log('开始刷新Dashboard页面数据');
const { code, data } = await getFamilyDashboardAPI();
const { code, data } = await getFamilyDashboardAPI({ silent: true });
if (code) {
// 获取家庭ID
family_id.value = data.family.id;
......@@ -363,22 +363,12 @@ useLoad(async () => {
// 检查用户是否已加入家庭
const hasFamily = await checkUserHasFamily()
// 如果用户没有加入家庭,检查当前页面是否已经是Welcome页面
// 如果用户没有加入家庭,跳转到欢迎页面
if (!hasFamily) {
// 获取当前页面路由
const pages = Taro.getCurrentPages();
const currentPage = pages[pages.length - 1];
const currentRoute = currentPage?.route || '';
// 只有当前页面不是Welcome页面时才跳转,避免重复跳转
if (currentRoute !== 'pages/Welcome/index') {
console.warn('用户未加入家庭,跳转到欢迎页面');
await Taro.reLaunch({
url: '/pages/Welcome/index'
})
} else {
console.log('当前已在Welcome页面,跳过跳转');
}
console.warn('用户未加入家庭,跳转到欢迎页面');
await Taro.reLaunch({
url: '/pages/Welcome/index'
})
return
}
},
......
/*
* @Date: 2025-01-25 10:00:00
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-09-12 21:49:10
* @LastEditTime: 2025-09-12 22:56:27
* @FilePath: /lls_program/src/utils/authRedirect.js
* @Description: 授权重定向处理工具函数
*/
......@@ -226,10 +226,9 @@ export const addShareFlag = (path) => {
* 在后台处理授权,不跳转页面,避免用户感知
* @param {Function} onSuccess - 授权成功回调
* @param {Function} onError - 授权失败回调
* @param {boolean} skipRedirect - 是否跳过重定向,默认false
* @returns {Promise} 授权结果
*/
export const silentAuth = async (onSuccess, onError, skipRedirect = false) => {
export const silentAuth = async (onSuccess, onError) => {
try {
// 检查是否已经授权
if (!needAuth()) {
......@@ -265,12 +264,12 @@ export const silentAuth = async (onSuccess, onError, skipRedirect = false) => {
// 测试环境下传递openid,正式环境不传递
if (process.env.NODE_ENV === 'development') {
// requestData.openid = 'h-008';
requestData.openid = 'h-008';
// requestData.openid = 'h-009';
// requestData.openid = 'h-010';
// requestData.openid = 'h-011';
// requestData.openid = 'h-012';
requestData.openid = 'h-013';
// requestData.openid = 'h-013';
// requestData.openid = 'oWbdFvkD5VtloC50wSNR9IWiU2q8';
// requestData.openid = 'oex8h5QZnZJto3ttvO6swSvylAQo';
}
......@@ -308,15 +307,13 @@ export const silentAuth = async (onSuccess, onError, skipRedirect = false) => {
try {
const hasFamily = await checkUserHasFamily()
// 如果用户没有加入家庭,根据skipRedirect参数决定是否跳转
// 如果用户没有加入家庭,跳转到欢迎页面
if (!hasFamily) {
if (!skipRedirect) {
await Taro.reLaunch({
url: '/pages/Welcome/index'
})
}
await Taro.reLaunch({
url: '/pages/Welcome/index'
})
const result = { ...response.data, redirected: !skipRedirect }
const result = { ...response.data, redirected: true }
if (onSuccess) {
onSuccess(result)
}
......