Files
ShaFaFanXin/前端/utils/request.uts
2026-01-27 18:06:04 +08:00

175 lines
3.7 KiB
Plaintext

/**
* 网络请求封装
* 预留后端接口对接
*/
import { getEnvConfig, useMock, STORAGE_KEYS } from './config.uts'
import { getMockData } from './mock.uts'
// 请求配置类型
type RequestOptions = {
url : string
method ?: 'GET' | 'POST' | 'PUT' | 'DELETE'
data ?: UTSJSONObject | null
header ?: UTSJSONObject | null
showLoading ?: boolean
loadingText ?: string
}
// 响应数据类型
type ResponseData = {
code : number
message : string
data : any
}
/**
* 请求拦截器
*/
const requestInterceptor = (options : RequestOptions) : RequestOptions => {
// 添加token
const token = uni.getStorageSync(STORAGE_KEYS.TOKEN) as string
if (token != '') {
if (options.header == null) {
options.header = {} as UTSJSONObject
}
options.header!['Authorization'] = `Bearer ${token}`
}
return options
}
/**
* 响应拦截器
*/
const responseInterceptor = (response : UTSJSONObject) : ResponseData => {
const statusCode = response['statusCode'] as number
const data = response['data'] as UTSJSONObject
if (statusCode == 200) {
const code = (data['code'] ?? 0) as number
if (code == 0 || code == 200) {
return {
code: 0,
message: 'success',
data: data['data']
} as ResponseData
} else if (code == 401) {
// token过期,跳转登录
uni.removeStorageSync(STORAGE_KEYS.TOKEN)
uni.showToast({
title: '请重新登录',
icon: 'none'
})
return {
code: code,
message: (data['message'] ?? '请重新登录') as string,
data: null
} as ResponseData
} else {
return {
code: code,
message: (data['message'] ?? '请求失败') as string,
data: null
} as ResponseData
}
} else {
return {
code: statusCode,
message: '网络请求失败',
data: null
} as ResponseData
}
}
/**
* 统一请求方法
*/
export const request = (options : RequestOptions) : Promise<ResponseData> => {
return new Promise((resolve, reject) => {
// 使用Mock数据
if (useMock) {
const mockData = getMockData(options.url, options.method ?? 'GET', options.data)
if (mockData != null) {
setTimeout(() => {
resolve({
code: 0,
message: 'success',
data: mockData
} as ResponseData)
}, 300) // 模拟网络延迟
return
}
}
// 请求拦截
const finalOptions = requestInterceptor(options)
// 显示loading
if (finalOptions.showLoading == true) {
uni.showLoading({
title: finalOptions.loadingText ?? '加载中...'
})
}
const config = getEnvConfig()
const baseUrl = config['baseUrl'] as string
uni.request({
url: baseUrl + finalOptions.url,
method: finalOptions.method ?? 'GET',
data: finalOptions.data as UTSJSONObject | null,
header: finalOptions.header as UTSJSONObject | null,
success: (res) => {
if (finalOptions.showLoading == true) {
uni.hideLoading()
}
const result = responseInterceptor(res as UTSJSONObject)
if (result.code == 0) {
resolve(result)
} else {
uni.showToast({
title: result.message,
icon: 'none'
})
reject(result)
}
},
fail: (err) => {
if (finalOptions.showLoading == true) {
uni.hideLoading()
}
uni.showToast({
title: '网络连接失败',
icon: 'none'
})
reject({
code: -1,
message: '网络连接失败',
data: null
} as ResponseData)
}
})
})
}
/**
* GET请求
*/
export const get = (url : string, data ?: UTSJSONObject | null) : Promise<ResponseData> => {
return request({
url: url,
method: 'GET',
data: data
} as RequestOptions)
}
/**
* POST请求
*/
export const post = (url : string, data ?: UTSJSONObject | null) : Promise<ResponseData> => {
return request({
url: url,
method: 'POST',
data: data
} as RequestOptions)
}