类型抽离
This commit is contained in:
parent
5ad075c7c9
commit
0ceaf0d5d8
27
src/types/request.ts
Normal file
27
src/types/request.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
interface BaseRequestConfig {
|
||||||
|
headers?: {
|
||||||
|
isToken: boolean
|
||||||
|
},
|
||||||
|
header?: any,
|
||||||
|
url: string,
|
||||||
|
params?: any,
|
||||||
|
timeout?: number | undefined,
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RequestConfig extends BaseRequestConfig {
|
||||||
|
method?: "OPTIONS" | "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "TRACE" | "CONNECT" | undefined
|
||||||
|
baseUrl?: string,
|
||||||
|
data?: any
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RequestUploadConfig extends BaseRequestConfig {
|
||||||
|
filePath: string,
|
||||||
|
name?: string,
|
||||||
|
formData: any
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ResponseData {
|
||||||
|
code: number,
|
||||||
|
data: any,
|
||||||
|
msg: string
|
||||||
|
}
|
||||||
@ -3,28 +3,11 @@ import config from '@/config'
|
|||||||
import { getToken } from '@/utils/auth'
|
import { getToken } from '@/utils/auth'
|
||||||
import errorCode from '@/utils/errorCode'
|
import errorCode from '@/utils/errorCode'
|
||||||
import { toast, showConfirm, tansParams } from '@/utils/common'
|
import { toast, showConfirm, tansParams } from '@/utils/common'
|
||||||
|
import { RequestConfig, ResponseData } from '@/types/request'
|
||||||
|
|
||||||
|
let timeout = 10000
|
||||||
const baseUrl = config.baseUrl
|
const baseUrl = config.baseUrl
|
||||||
|
|
||||||
interface RequestConfig{
|
|
||||||
headers?:{
|
|
||||||
isToken:boolean
|
|
||||||
},
|
|
||||||
header?:any,
|
|
||||||
params?:any,
|
|
||||||
url:string,
|
|
||||||
method?:"OPTIONS" | "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "TRACE" | "CONNECT" | undefined
|
|
||||||
baseUrl?:string,
|
|
||||||
timeout?:number | undefined,
|
|
||||||
data?:any
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ResponseData{
|
|
||||||
code:number,
|
|
||||||
data:any,
|
|
||||||
msg:string
|
|
||||||
}
|
|
||||||
|
|
||||||
const request = (config:RequestConfig) => {
|
const request = (config:RequestConfig) => {
|
||||||
// 是否需要设置 token
|
// 是否需要设置 token
|
||||||
const isToken = (config.headers || {}).isToken === false
|
const isToken = (config.headers || {}).isToken === false
|
||||||
@ -41,7 +24,7 @@ const request = (config:RequestConfig) => {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
uni.request({
|
uni.request({
|
||||||
method: config.method || 'GET',
|
method: config.method || 'GET',
|
||||||
timeout: config.timeout || 10000,
|
timeout: config.timeout || timeout,
|
||||||
url: config.baseUrl || baseUrl + config.url,
|
url: config.baseUrl || baseUrl + config.url,
|
||||||
data: config.data,
|
data: config.data,
|
||||||
header: config.header,
|
header: config.header,
|
||||||
|
|||||||
@ -3,11 +3,14 @@ import config from '@/config'
|
|||||||
import { getToken } from '@/utils/auth'
|
import { getToken } from '@/utils/auth'
|
||||||
import errorCode from '@/utils/errorCode'
|
import errorCode from '@/utils/errorCode'
|
||||||
import { toast, showConfirm, tansParams } from '@/utils/common'
|
import { toast, showConfirm, tansParams } from '@/utils/common'
|
||||||
|
import { RequestUploadConfig } from '@/types/request'
|
||||||
|
|
||||||
let timeout = 10000
|
let timeout = 10000
|
||||||
const baseUrl = config.baseUrl
|
const baseUrl = config.baseUrl
|
||||||
|
|
||||||
const upload = config => {
|
|
||||||
|
|
||||||
|
const upload = (config:RequestUploadConfig) => {
|
||||||
// 是否需要设置 token
|
// 是否需要设置 token
|
||||||
const isToken = (config.headers || {}).isToken === false
|
const isToken = (config.headers || {}).isToken === false
|
||||||
config.header = config.header || {}
|
config.header = config.header || {}
|
||||||
@ -31,6 +34,7 @@ const upload = config => {
|
|||||||
success: (res) => {
|
success: (res) => {
|
||||||
let result = JSON.parse(res.data)
|
let result = JSON.parse(res.data)
|
||||||
const code = result.code || 200
|
const code = result.code || 200
|
||||||
|
// @ts-ignore
|
||||||
const msg = errorCode[code] || result.msg || errorCode['default']
|
const msg = errorCode[code] || result.msg || errorCode['default']
|
||||||
if (code === 200) {
|
if (code === 200) {
|
||||||
resolve(result)
|
resolve(result)
|
||||||
@ -52,6 +56,7 @@ const upload = config => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
fail: (error) => {
|
fail: (error) => {
|
||||||
|
// @ts-ignore
|
||||||
let { message } = error
|
let { message } = error
|
||||||
if (message == 'Network Error') {
|
if (message == 'Network Error') {
|
||||||
message = '后端接口连接异常'
|
message = '后端接口连接异常'
|
||||||
Loading…
Reference in New Issue
Block a user