2024-09-19 12:57:55 +08:00
|
|
|
|
|
|
|
|
|
|
import { requestConfig } from './common.js';
|
|
|
|
|
|
|
|
|
|
|
|
export default class Request {
|
|
|
|
|
|
// 构造方法
|
|
|
|
|
|
constructor(config = {}, reqInterceptor = null, resInterceptor = null, successHandler, failHandler = null, completeHandler = null){
|
|
|
|
|
|
// 基础属性
|
|
|
|
|
|
this.baseUrl = config.baseUrl
|
|
|
|
|
|
this.header = config.header || {
|
|
|
|
|
|
"Content-Type":"application/x-www-form-urlencoded"
|
|
|
|
|
|
}
|
|
|
|
|
|
// 自定义响应处理器
|
|
|
|
|
|
this.success = successHandler
|
|
|
|
|
|
this.fail = failHandler
|
|
|
|
|
|
this.complete = completeHandler
|
|
|
|
|
|
// 自定义拦截器(请求、响应)
|
|
|
|
|
|
this.requestInterceptor = reqInterceptor
|
|
|
|
|
|
this.responseInterceptor = resInterceptor
|
|
|
|
|
|
}
|
|
|
|
|
|
// 请求type request/upload/download
|
|
|
|
|
|
async request(options, successHandler = null, failHandler = null, completeHandler = null){
|
|
|
|
|
|
const task = options.task || false
|
|
|
|
|
|
const type = options.type || "request"
|
|
|
|
|
|
// delete options.task
|
|
|
|
|
|
let config = null
|
|
|
|
|
|
try {
|
|
|
|
|
|
config = await requestConfig(this, options, successHandler, failHandler, completeHandler)
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
// reject the error
|
|
|
|
|
|
return Promise.reject(e)
|
|
|
|
|
|
}
|
|
|
|
|
|
if(!config || typeof config != 'object'){
|
|
|
|
|
|
return Promise.reject({})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const _this = this
|
|
|
|
|
|
if(task){
|
|
|
|
|
|
config["success"] = (response) => {
|
|
|
|
|
|
if (_this.responseInterceptor) {
|
|
|
|
|
|
_this.responseInterceptor(response, config)
|
|
|
|
|
|
}
|
|
|
|
|
|
_this.success && _this.success(response)
|
|
|
|
|
|
successHandler && successHandler(response)
|
|
|
|
|
|
}
|
|
|
|
|
|
config["fail"] = (response) => {
|
|
|
|
|
|
_this.fail && _this.fail(response)
|
|
|
|
|
|
failHandler && failHandler(response)
|
|
|
|
|
|
}
|
|
|
|
|
|
config["complete"] = (response) => {
|
|
|
|
|
|
_this.complete && _this.complete(response)
|
|
|
|
|
|
completeHandler && completeHandler(response)
|
|
|
|
|
|
}
|
|
|
|
|
|
if (type === "request"){
|
|
|
|
|
|
return uni.request(config)
|
|
|
|
|
|
}else if(type === "upload"){
|
|
|
|
|
|
return uni.uploadFile(config)
|
|
|
|
|
|
}else{
|
|
|
|
|
|
return uni.downloadFile(config)
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
config["success"] = (response) => {
|
|
|
|
|
|
let _res = null
|
|
|
|
|
|
if(_this.responseInterceptor){
|
|
|
|
|
|
_res = _this.responseInterceptor(response, config)
|
|
|
|
|
|
}
|
|
|
|
|
|
_this.success && _this.success(response)
|
|
|
|
|
|
successHandler && successHandler(response)
|
|
|
|
|
|
// 为了对reject的内容更加可控,增加了一个控制字段 wakaryReqToReject
|
|
|
|
|
|
if(_res.wakaryReqToReject){
|
|
|
|
|
|
delete _res.wakaryReqToReject
|
|
|
|
|
|
reject(_res)
|
|
|
|
|
|
}else{
|
|
|
|
|
|
resolve(_res)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
config["fail"] = (error) => {
|
|
|
|
|
|
_this.fail && _this.fail(error)
|
|
|
|
|
|
failHandler && failHandler(error)
|
|
|
|
|
|
reject(error)
|
|
|
|
|
|
}
|
|
|
|
|
|
config["complete"] = (response) => {
|
|
|
|
|
|
_this.complete && _this.complete(response)
|
|
|
|
|
|
completeHandler && completeHandler(response)
|
|
|
|
|
|
}
|
|
|
|
|
|
if( type === "request"){
|
|
|
|
|
|
uni.request(config)
|
|
|
|
|
|
} else if(type === "upload"){
|
|
|
|
|
|
uni.uploadFile(config)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
uni.downloadFile(config)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2025-02-13 10:31:01 +08:00
|
|
|
|
}
|