|
|
@@ -1,6 +1,6 @@
|
|
|
-import { CustomRequestOptions } from '@/interceptors/request'
|
|
|
+import type { CustomRequestOptions } from '@/interceptors/request'
|
|
|
|
|
|
-export const http = <T>(options: CustomRequestOptions) => {
|
|
|
+export function http<T>(options: CustomRequestOptions) {
|
|
|
// 1. 返回 Promise 对象
|
|
|
return new Promise<IResData<T>>((resolve, reject) => {
|
|
|
uni.request({
|
|
|
@@ -15,18 +15,20 @@ export const http = <T>(options: CustomRequestOptions) => {
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
// 2.1 提取核心数据 res.data
|
|
|
resolve(res.data as IResData<T>)
|
|
|
- } else if (res.statusCode === 401) {
|
|
|
+ }
|
|
|
+ else if (res.statusCode === 401) {
|
|
|
// 401错误 -> 清理用户信息,跳转到登录页
|
|
|
// userStore.clearUserInfo()
|
|
|
// uni.navigateTo({ url: '/pages/login/login' })
|
|
|
reject(res)
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ else {
|
|
|
// 其他错误 -> 根据后端错误信息轻提示
|
|
|
- !options.hideErrorToast &&
|
|
|
- uni.showToast({
|
|
|
- icon: 'none',
|
|
|
- title: (res.data as IResData<T>).msg || '请求错误',
|
|
|
- })
|
|
|
+ !options.hideErrorToast
|
|
|
+ && uni.showToast({
|
|
|
+ icon: 'none',
|
|
|
+ title: (res.data as IResData<T>).msg || '请求错误',
|
|
|
+ })
|
|
|
reject(res)
|
|
|
}
|
|
|
},
|
|
|
@@ -49,12 +51,7 @@ export const http = <T>(options: CustomRequestOptions) => {
|
|
|
* @param header 请求头,默认为json格式
|
|
|
* @returns
|
|
|
*/
|
|
|
-export const httpGet = <T>(
|
|
|
- url: string,
|
|
|
- query?: Record<string, any>,
|
|
|
- header?: Record<string, any>,
|
|
|
- options?: Partial<CustomRequestOptions>,
|
|
|
-) => {
|
|
|
+export function httpGet<T>(url: string, query?: Record<string, any>, header?: Record<string, any>, options?: Partial<CustomRequestOptions>) {
|
|
|
return http<T>({
|
|
|
url,
|
|
|
query,
|
|
|
@@ -72,13 +69,7 @@ export const httpGet = <T>(
|
|
|
* @param header 请求头,默认为json格式
|
|
|
* @returns
|
|
|
*/
|
|
|
-export const httpPost = <T>(
|
|
|
- url: string,
|
|
|
- data?: Record<string, any>,
|
|
|
- query?: Record<string, any>,
|
|
|
- header?: Record<string, any>,
|
|
|
- options?: Partial<CustomRequestOptions>,
|
|
|
-) => {
|
|
|
+export function httpPost<T>(url: string, data?: Record<string, any>, query?: Record<string, any>, header?: Record<string, any>, options?: Partial<CustomRequestOptions>) {
|
|
|
return http<T>({
|
|
|
url,
|
|
|
query,
|
|
|
@@ -91,13 +82,7 @@ export const httpPost = <T>(
|
|
|
/**
|
|
|
* PUT 请求
|
|
|
*/
|
|
|
-export const httpPut = <T>(
|
|
|
- url: string,
|
|
|
- data?: Record<string, any>,
|
|
|
- query?: Record<string, any>,
|
|
|
- header?: Record<string, any>,
|
|
|
- options?: Partial<CustomRequestOptions>,
|
|
|
-) => {
|
|
|
+export function httpPut<T>(url: string, data?: Record<string, any>, query?: Record<string, any>, header?: Record<string, any>, options?: Partial<CustomRequestOptions>) {
|
|
|
return http<T>({
|
|
|
url,
|
|
|
data,
|
|
|
@@ -111,12 +96,7 @@ export const httpPut = <T>(
|
|
|
/**
|
|
|
* DELETE 请求(无请求体,仅 query)
|
|
|
*/
|
|
|
-export const httpDelete = <T>(
|
|
|
- url: string,
|
|
|
- query?: Record<string, any>,
|
|
|
- header?: Record<string, any>,
|
|
|
- options?: Partial<CustomRequestOptions>,
|
|
|
-) => {
|
|
|
+export function httpDelete<T>(url: string, query?: Record<string, any>, header?: Record<string, any>, options?: Partial<CustomRequestOptions>) {
|
|
|
return http<T>({
|
|
|
url,
|
|
|
query,
|