refactor(request): 移除旧版请求工具并优化错误处理- 删除了 request-legacy.ts及相关文件
- 更新了所有 API 文件的导入路径 -优化了请求工具的错误处理逻辑 - 移除了冗余的调试信息 - 保留了关键的错误信息
This commit is contained in:
@@ -6,6 +6,7 @@ import type { ShopDealerUser, ShopDealerUserParam } from './model';
|
||||
* 分页查询分销商用户记录表
|
||||
*/
|
||||
export async function pageShopDealerUser(params: ShopDealerUserParam) {
|
||||
// 使用新的request方法,它会自动处理错误并返回完整的ApiResult
|
||||
const res = await request.get<ApiResult<PageResult<ShopDealerUser>>>(
|
||||
'/shop/shop-dealer-user/page',
|
||||
params
|
||||
|
||||
@@ -24,6 +24,12 @@ export default defineAppConfig({
|
||||
"detail/index"
|
||||
]
|
||||
},
|
||||
{
|
||||
"root": "coupon",
|
||||
"pages": [
|
||||
"index"
|
||||
]
|
||||
},
|
||||
{
|
||||
"root": "user",
|
||||
"pages": [
|
||||
|
||||
@@ -335,87 +335,6 @@ ${coupon.minPrice ? `最低消费:¥${coupon.minPrice}` : ''}
|
||||
|
||||
return (
|
||||
<ConfigProvider>
|
||||
{/* 搜索栏和功能入口 */}
|
||||
<View className="bg-white px-4 py-3">
|
||||
<View className="flex items-center justify-between gap-3">
|
||||
<View className="flex-1">
|
||||
<SearchBar
|
||||
placeholder="搜索优惠券"
|
||||
value={searchValue}
|
||||
className={'border'}
|
||||
onChange={setSearchValue}
|
||||
onSearch={handleSearch}
|
||||
/>
|
||||
</View>
|
||||
<Button
|
||||
size="small"
|
||||
fill="outline"
|
||||
icon={<Filter />}
|
||||
onClick={() => setShowFilter(true)}
|
||||
>
|
||||
筛选
|
||||
</Button>
|
||||
<Button
|
||||
size="small"
|
||||
fill="outline"
|
||||
icon={<Board />}
|
||||
onClick={() => setShowGuide(true)}
|
||||
>
|
||||
帮助
|
||||
</Button>
|
||||
</View>
|
||||
</View>
|
||||
|
||||
{/* 热门优惠券轮播 */}
|
||||
{hotCoupons.length > 0 && (
|
||||
<View className="bg-white mb-2">
|
||||
<View className="px-4 py-2 text-sm font-medium text-gray-700">🔥 热门推荐</View>
|
||||
<Swiper
|
||||
height={120}
|
||||
autoPlay
|
||||
loop
|
||||
indicator
|
||||
className="px-4 pb-3"
|
||||
>
|
||||
{hotCoupons.map((coupon, index) => (
|
||||
<SwiperItem key={index}>
|
||||
<View
|
||||
className="bg-gradient-to-r from-red-400 to-pink-500 rounded-lg p-4 text-white mr-4"
|
||||
onClick={() => handleCouponDetail(coupon)}
|
||||
>
|
||||
<View className="flex justify-between items-center">
|
||||
<View>
|
||||
<View className="text-lg font-bold">
|
||||
{coupon.type === 10 ? `¥${coupon.reducePrice}` :
|
||||
coupon.type === 20 ? `${coupon.discount}折` : '免费'}
|
||||
</View>
|
||||
<View className="text-sm opacity-90">
|
||||
{coupon.name}
|
||||
</View>
|
||||
{coupon.minPrice && (
|
||||
<View className="text-xs opacity-80">
|
||||
满¥{coupon.minPrice}可用
|
||||
</View>
|
||||
)}
|
||||
</View>
|
||||
<Button
|
||||
size="small"
|
||||
className="bg-white text-red-500 border-0"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
handleReceiveCoupon(coupon)
|
||||
}}
|
||||
>
|
||||
立即领取
|
||||
</Button>
|
||||
</View>
|
||||
</View>
|
||||
</SwiperItem>
|
||||
))}
|
||||
</Swiper>
|
||||
</View>
|
||||
)}
|
||||
|
||||
{/* Tab切换 */}
|
||||
<View className="bg-white">
|
||||
<Tabs value={activeTab} onChange={handleTabChange}>
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
pageShopDealerApply,
|
||||
updateShopDealerApply
|
||||
} from "@/api/shop/shopDealerApply";
|
||||
import {getShopDealerUser} from "@/api/shop/shopDealerUser";
|
||||
|
||||
const AddUserAddress = () => {
|
||||
const {user} = useUser()
|
||||
@@ -63,6 +64,23 @@ const AddUserAddress = () => {
|
||||
|
||||
// 提交表单
|
||||
const submitSucceed = async (values: any) => {
|
||||
if(!values.refereeId){
|
||||
return Taro.showToast({
|
||||
title: '请填写邀请人ID',
|
||||
icon: 'error'
|
||||
});
|
||||
}
|
||||
// 验证邀请人ID是否存在
|
||||
try {
|
||||
await getShopDealerUser(values.refereeId);
|
||||
} catch (error) {
|
||||
console.error('验证邀请人失败:', error);
|
||||
return Taro.showToast({
|
||||
title: '邀请人ID不存在',
|
||||
icon: 'error'
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
// 准备提交的数据
|
||||
const submitData = {
|
||||
@@ -123,11 +141,7 @@ const AddUserAddress = () => {
|
||||
if (loading) {
|
||||
return <Loading className={'px-2'}>加载中</Loading>
|
||||
}
|
||||
console.log(FormData,'FromData')
|
||||
|
||||
if(!FormData){
|
||||
return <Loading className={'px-2'}>加载中</Loading>
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<Form
|
||||
@@ -146,8 +160,8 @@ const AddUserAddress = () => {
|
||||
<Form.Item name="mobile" label="手机号" initialValue={user?.mobile} required>
|
||||
<Input placeholder="请输入手机号" disabled={true} maxLength={11}/>
|
||||
</Form.Item>
|
||||
<Form.Item name="refereeId" label="推荐人ID" initialValue={FormData?.refereeId} required>
|
||||
<Input placeholder="推荐人ID"/>
|
||||
<Form.Item name="refereeId" label="邀请人ID" initialValue={FormData?.refereeId} required>
|
||||
<Input placeholder="邀请人ID"/>
|
||||
</Form.Item>
|
||||
</CellGroup>
|
||||
</Form>
|
||||
|
||||
@@ -7,9 +7,9 @@ import {getUserInfo, getWxOpenId} from "@/api/layout";
|
||||
import {TenantId} from "@/config/app";
|
||||
import {getOrganization} from "@/api/system/organization";
|
||||
import {myUserVerify} from "@/api/system/userVerify";
|
||||
import {useShopInfo} from '@/hooks/useShopInfo';
|
||||
import { useShopInfo } from '@/hooks/useShopInfo';
|
||||
import {handleInviteRelation} from "@/utils/invite";
|
||||
import {View, Text} from '@tarojs/components'
|
||||
import {View,Text} from '@tarojs/components'
|
||||
import MySearch from "./MySearch";
|
||||
import './Header.scss';
|
||||
|
||||
@@ -86,7 +86,7 @@ const Header = (props: any) => {
|
||||
}
|
||||
|
||||
/* 获取用户手机号 */
|
||||
const handleGetPhoneNumber = ({detail}: { detail: { code?: string, encryptedData?: string, iv?: string } }) => {
|
||||
const handleGetPhoneNumber = ({detail}: {detail: {code?: string, encryptedData?: string, iv?: string}}) => {
|
||||
const {code, encryptedData, iv} = detail
|
||||
Taro.login({
|
||||
success: function () {
|
||||
@@ -168,7 +168,7 @@ const Header = (props: any) => {
|
||||
}}
|
||||
left={
|
||||
!IsLogin ? (
|
||||
<Space style={{display: 'flex', alignItems: 'center'}}>
|
||||
<View style={{display: 'flex', alignItems: 'center'}}>
|
||||
<Button style={{color: '#ffffff'}} open-type="getPhoneNumber" onGetPhoneNumber={handleGetPhoneNumber}>
|
||||
<Space>
|
||||
<Avatar
|
||||
@@ -176,10 +176,10 @@ const Header = (props: any) => {
|
||||
src={getWebsiteLogo()}
|
||||
/>
|
||||
<Text style={{color: '#ffffff'}}>{getWebsiteName()}</Text>
|
||||
<TriangleDown size={9} className={'text-white'}/>
|
||||
</Space>
|
||||
</Button>
|
||||
<TriangleDown size={9} className={'text-white'}/>
|
||||
</Space>
|
||||
</View>
|
||||
) : (
|
||||
<View style={{display: 'flex', alignItems: 'center', gap: '8px'}}>
|
||||
<Avatar
|
||||
|
||||
@@ -1,116 +0,0 @@
|
||||
/**
|
||||
* 兼容旧版API的请求工具
|
||||
* 这个文件是为了保持向后兼容性,让现有的API代码能够正常工作
|
||||
* 逐步迁移完成后可以删除此文件
|
||||
*/
|
||||
|
||||
import { getRaw, postRaw, putRaw, delRaw } from './request';
|
||||
import { BaseUrl, TenantId } from "@/config/app";
|
||||
import Taro from '@tarojs/taro';
|
||||
|
||||
let baseUrl = BaseUrl;
|
||||
|
||||
// 开发环境
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
// baseUrl = 'http://localhost:9200/api'
|
||||
}
|
||||
|
||||
// 兼容旧版的request函数
|
||||
export function request<T>(options: any): Promise<T> {
|
||||
const token = Taro.getStorageSync('access_token');
|
||||
const header: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
'TenantId': Taro.getStorageSync('TenantId') || TenantId
|
||||
};
|
||||
|
||||
if (token) {
|
||||
header['Authorization'] = token;
|
||||
}
|
||||
|
||||
// 构建完整URL
|
||||
let url = options.url;
|
||||
if (url.indexOf('http') === -1) {
|
||||
url = baseUrl + url;
|
||||
}
|
||||
|
||||
// 根据方法调用对应的新请求函数
|
||||
const method = (options.method || 'GET').toUpperCase();
|
||||
const config = {
|
||||
header: { ...header, ...options.header },
|
||||
showError: false // 让API层自己处理错误
|
||||
};
|
||||
|
||||
switch (method) {
|
||||
case 'GET':
|
||||
return getRaw<T>(url, null, config);
|
||||
case 'POST':
|
||||
return postRaw<T>(url, options.data, config);
|
||||
case 'PUT':
|
||||
return putRaw<T>(url, options.data, config);
|
||||
case 'DELETE':
|
||||
return delRaw<T>(url, options.data, config);
|
||||
default:
|
||||
return getRaw<T>(url, null, config);
|
||||
}
|
||||
}
|
||||
|
||||
// 兼容旧版的便捷方法
|
||||
export function get<T>(url: string, data?: any): Promise<T> {
|
||||
if (url.indexOf('http') === -1) {
|
||||
url = baseUrl + url;
|
||||
}
|
||||
|
||||
if (data) {
|
||||
// 处理查询参数
|
||||
if (data.params) {
|
||||
// 如果data有params属性,使用params作为查询参数
|
||||
const queryString = Object.keys(data.params)
|
||||
.filter(key => data.params[key] !== undefined && data.params[key] !== null)
|
||||
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data.params[key])}`)
|
||||
.join('&');
|
||||
if (queryString) {
|
||||
url += `?${queryString}`;
|
||||
}
|
||||
} else {
|
||||
// 否则直接使用data作为查询参数
|
||||
const queryString = Object.keys(data)
|
||||
.filter(key => data[key] !== undefined && data[key] !== null)
|
||||
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`)
|
||||
.join('&');
|
||||
if (queryString) {
|
||||
url += `?${queryString}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getRaw<T>(url, null, { showError: false });
|
||||
}
|
||||
|
||||
export function post<T>(url: string, data?: any): Promise<T> {
|
||||
if (url.indexOf('http') === -1) {
|
||||
url = baseUrl + url;
|
||||
}
|
||||
return postRaw<T>(url, data, { showError: false });
|
||||
}
|
||||
|
||||
export function put<T>(url: string, data?: any): Promise<T> {
|
||||
if (url.indexOf('http') === -1) {
|
||||
url = baseUrl + url;
|
||||
}
|
||||
return putRaw<T>(url, data, { showError: false });
|
||||
}
|
||||
|
||||
export function del<T>(url: string, data?: any): Promise<T> {
|
||||
if (url.indexOf('http') === -1) {
|
||||
url = baseUrl + url;
|
||||
}
|
||||
return delRaw<T>(url, data, { showError: false });
|
||||
}
|
||||
|
||||
export default {
|
||||
request,
|
||||
get,
|
||||
post,
|
||||
put,
|
||||
del
|
||||
};
|
||||
@@ -94,6 +94,11 @@ const responseInterceptor = <T>(response: any, config: RequestConfig): T => {
|
||||
|
||||
const { statusCode, data } = response;
|
||||
|
||||
// 调试信息(仅开发环境)
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
console.log('API Response:', { statusCode, url: config.url, success: statusCode === 200 });
|
||||
}
|
||||
|
||||
// HTTP状态码检查
|
||||
if (statusCode !== 200) {
|
||||
throw new RequestError(
|
||||
@@ -105,7 +110,10 @@ const responseInterceptor = <T>(response: any, config: RequestConfig): T => {
|
||||
}
|
||||
|
||||
// 如果没有数据,抛出错误
|
||||
if (!data) {
|
||||
if (data === null || data === undefined) {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
console.error('API响应数据为空:', { statusCode, url: config.url });
|
||||
}
|
||||
throw new RequestError(
|
||||
'响应数据为空',
|
||||
ErrorType.NETWORK_ERROR,
|
||||
@@ -115,7 +123,7 @@ const responseInterceptor = <T>(response: any, config: RequestConfig): T => {
|
||||
}
|
||||
|
||||
// 业务状态码检查
|
||||
if (typeof data === 'object' && 'code' in data) {
|
||||
if (typeof data === 'object' && data !== null && 'code' in data) {
|
||||
const apiResponse = data as ApiResponse<T>;
|
||||
|
||||
// 成功响应
|
||||
@@ -140,6 +148,9 @@ const responseInterceptor = <T>(response: any, config: RequestConfig): T => {
|
||||
}
|
||||
|
||||
// 业务错误
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
console.error('API业务错误:', { code: apiResponse.code, message: apiResponse.message });
|
||||
}
|
||||
throw new RequestError(
|
||||
apiResponse.message || '请求失败',
|
||||
ErrorType.BUSINESS_ERROR,
|
||||
|
||||
Reference in New Issue
Block a user