封装
type BeforeRequestFunction = (data : Request) => void;
type AfterRequestFunction = ((option : any) => void);
const $JsonAndArrayTOString = (data : UTSJSONObject | null) : string => {
if (data === null) return ""
let str = "?"
for (var key in data) {
if (Array.isArray(data[key])) {
var arr : any[] = data[key] as any[]
arr.forEach((item : any) => {
str += `${key}[]=${item}&`
})
} else {
str += `${key}=${data[key]}&`
}
}
return str.slice(0, -1)
}
class Request {
private data : any | null
private baseUrl : string | null
private parametUrl : string | null
private url : string
private method : RequestMethod
private timeout : number
private header : UTSJSONObject
private requestTag ?: RequestTask
private beforeRequest ?: BeforeRequestFunction
private afterRequest ?: AfterRequestFunction
constructor() {
this.baseUrl = null
this.parametUrl = null
this.url = ""
this.method = 'GET'
this.data = null
this.timeout = 6000
this.header = new UTSJSONObject()
this.beforeRequest = null
this.afterRequest = null
this.requestTag = null
}
setBaseUrl(baseUrl : string) {
this.baseUrl = baseUrl;
}
setTimeout(timeout : number) {
this.timeout = timeout
}
addHeader(key : string, value : any) {
this.header.set(key, value)
}
setBeforeRequest(beforeRequest : BeforeRequestFunction) {
this.beforeRequest = beforeRequest
}
setAfterRequest(afterRequest : AfterRequestFunction) {
this.afterRequest = afterRequest
}
setAbrot() {
requestTag?.abort()
}
getParametUrl() : string | null {
return this.parametUrl
}
get(url : string, data : UTSJSONObject | null) : Promise<any> {
this.method = 'GET'
this.url = `${this.baseUrl}${url}${$JsonAndArrayTOString(data)}`
this.parametUrl = url
this.data = data
return this.requset()
}
post(url : string, data : UTSJSONObject | null) : Promise<any> {
this.method = 'POST'
this.url = `${this.baseUrl}${url}`
this.parametUrl = url
this.data = data
return this.requset()
}
put(url : string, data : UTSJSONObject | null) : Promise<any> {
this.method = 'PUT'
this.url = `${this.baseUrl}${url}`
this.parametUrl = url
return this.requset()
}
delete(url : string, data : UTSJSONObject | null) : Promise<any> {
this.method = 'DELETE'
this.url = `${this.baseUrl}${url}${$JsonAndArrayTOString(data)}`
this.parametUrl = url
this.data = null
return this.requset()
}
private requset() : Promise<any> {
this.beforeRequest?.invoke(this)
return new Promise((resolve, reject) => {
uni.request({
url: this.url,
method: this.method,
data: this.data,
header: this.header,
timeout: this.timeout,
success: (res) => { resolve(res) },
fail: (err) => { reject(err) },
complete: (res) => {
this.afterRequest?.invoke(res)
}
})
})
}
}
export const $http = new Request()
配置
import { $http } from "@/utils/Request";
$http.setTimeout(12000)
$http.setBaseUrl("https://xxx.xxx.com")
$http.setBeforeRequest((res) => {
res.addHeader("Content-Type", "application/json")
res.addHeader("Authorization", `Bearer ${11}`)
console.log("请求前参数",res);
})
$http.setAfterRequest((res)=>{
console.log("请求后参数",JSON.stringify(res));
})
export default $http
使用
import Api from "@/utils/RequsetPackage.uts"
export const Login = async (req : UTSJSONObject):Promise<any> => {
let res = await Api.get("/api/login", req)
return res
}