feat:"完成页面接口的对接"
This commit is contained in:
@@ -6,8 +6,8 @@
|
||||
export const ENV = {
|
||||
// 开发环境
|
||||
development: {
|
||||
baseUrl: 'http://localhost:3000/api',
|
||||
imageBaseUrl: 'http://localhost:3000'
|
||||
baseUrl: 'http://192.168.1.43:3000/api',
|
||||
imageBaseUrl: 'http://192.168.1.43:3000'
|
||||
},
|
||||
// 生产环境
|
||||
production: {
|
||||
@@ -67,7 +67,22 @@ export const SOFA_CATEGORIES = [
|
||||
// 翻新服务类型
|
||||
export const SERVICE_TYPES = [
|
||||
{ id: 'repair', name: '局部修复', icon: '/static/icons/repair.png' },
|
||||
{ id: 'recolor', name: '改色翻新', icon: '/static/icons/recolor.png' },
|
||||
{ id: 'refurbish', name: '整体翻新', icon: '/static/icons/refurbish.png' },
|
||||
{ id: 'custom', name: '定制换皮', icon: '/static/icons/custom.png' }
|
||||
{ id: 'refurbish', name: '整体翻新', icon: '/static/icons/refurbish.png' }
|
||||
]
|
||||
|
||||
/**
|
||||
* 获取服务类型名称
|
||||
*/
|
||||
export const getServiceTypeName = (type : string) : string => {
|
||||
const map : Map<string, string> = new Map([
|
||||
['leather', '真皮翻新'],
|
||||
['fabric', '布艺翻新'],
|
||||
['functional', '功能维修'],
|
||||
['antique', '古董修复'],
|
||||
['office', '办公沙发'],
|
||||
['cleaning', '清洁保养'],
|
||||
['repair', '维修改装'],
|
||||
['custom', '定制沙发']
|
||||
])
|
||||
return map.get(type) ?? type
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ type RequestOptions = {
|
||||
type ResponseData = {
|
||||
code : number
|
||||
message : string
|
||||
data : any
|
||||
data : any | null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,16 +42,31 @@ const requestInterceptor = (options : RequestOptions) : RequestOptions => {
|
||||
*/
|
||||
const responseInterceptor = (response : UTSJSONObject) : ResponseData => {
|
||||
const statusCode = response['statusCode'] as number
|
||||
const data = response['data'] as UTSJSONObject
|
||||
const data = response['data'] as UTSJSONObject | null
|
||||
|
||||
console.log('响应拦截器 - statusCode:', statusCode)
|
||||
console.log('响应拦截器 - data:', data)
|
||||
|
||||
// 处理空响应
|
||||
if (data == null) {
|
||||
return {
|
||||
code: statusCode,
|
||||
message: '响应数据为空',
|
||||
data: null
|
||||
} as ResponseData
|
||||
}
|
||||
|
||||
if (statusCode == 200) {
|
||||
const code = (data['code'] ?? 0) as number
|
||||
console.log('响应拦截器 - 解析的 code:', code)
|
||||
if (code == 0 || code == 200) {
|
||||
return {
|
||||
const result = {
|
||||
code: 0,
|
||||
message: 'success',
|
||||
data: data['data']
|
||||
message: (data['message'] ?? 'success') as string,
|
||||
data: data['data'] ?? null
|
||||
} as ResponseData
|
||||
console.log('响应拦截器 - 返回成功结果:', result)
|
||||
return result
|
||||
} else if (code == 401) {
|
||||
// token过期,跳转登录
|
||||
uni.removeStorageSync(STORAGE_KEYS.TOKEN)
|
||||
@@ -68,7 +83,7 @@ const responseInterceptor = (response : UTSJSONObject) : ResponseData => {
|
||||
return {
|
||||
code: code,
|
||||
message: (data['message'] ?? '请求失败') as string,
|
||||
data: null
|
||||
data: data['data'] ?? null
|
||||
} as ResponseData
|
||||
}
|
||||
} else {
|
||||
@@ -111,8 +126,18 @@ export const request = (options : RequestOptions) : Promise<ResponseData> => {
|
||||
}
|
||||
|
||||
const config = getEnvConfig()
|
||||
const BASE_URL = 'http://192.168.1.43:3000/api'
|
||||
const baseUrl = config['baseUrl'] as string
|
||||
|
||||
|
||||
// 清理 data 中的 undefined 字段,避免被序列化为 'undefined'
|
||||
if (finalOptions.data && typeof finalOptions.data === 'object') {
|
||||
for (const key in finalOptions.data) {
|
||||
if (finalOptions.data[key] === undefined) {
|
||||
delete finalOptions.data[key]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uni.request({
|
||||
url: baseUrl + finalOptions.url,
|
||||
method: finalOptions.method ?? 'GET',
|
||||
@@ -122,10 +147,15 @@ export const request = (options : RequestOptions) : Promise<ResponseData> => {
|
||||
if (finalOptions.showLoading == true) {
|
||||
uni.hideLoading()
|
||||
}
|
||||
console.log('请求成功原始响应:', res)
|
||||
const result = responseInterceptor(res as UTSJSONObject)
|
||||
console.log('拦截器处理后结果:', result)
|
||||
console.log('result.code 类型:', typeof result.code, '值:', result.code)
|
||||
if (result.code == 0) {
|
||||
console.log('判断为成功,resolve')
|
||||
resolve(result)
|
||||
} else {
|
||||
console.log('判断为失败,显示 toast 并 reject')
|
||||
uni.showToast({
|
||||
title: result.message,
|
||||
icon: 'none'
|
||||
|
||||
Reference in New Issue
Block a user