zhxy-jsd/src/utils/request/request.ts

51 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-04-22 10:22:33 +08:00
import {config, interceptor} from "@/utils/request/index";
import {ISREQUESTLOG} from "@/config";
import {URL_REG} from "@/utils/RegExp";
export const request: (options: UniNamespace.UploadFileOption | UniNamespace.RequestOptions, file?: boolean) => Promise<Requests<any>> = (options, file?) => {
return new Promise((resolve, reject) => {
let _config: UniNamespace.UploadFileOption | UniNamespace.RequestOptions
if (!URL_REG.test(options.url)) {
options.url = config.baseUrl + options.url;
}
options.complete = (response: any) => {
let statusCode = response.statusCode;
response.config = _config;
if (interceptor.response) {
let newResponse = interceptor.response(response);
if (newResponse) {
response = newResponse;
}
}
if (statusCode === 200) {
if (process.env.NODE_ENV === 'development' && ISREQUESTLOG) {
console.log('接口地址', _config.url)
console.log('接口参数', "data" in _config ? _config.data : '')
console.log('返回结果', response)
console.log('========================================================================================================================================================')
}
//成功
resolve(response);
} else {
//失败
reject(response);
}
};
_config = Object.assign({}, config, options);
if (interceptor.request) {
interceptor.request(_config);
}
if (file) {
_config.header = {}
}
file ? uni.uploadFile(<UniNamespace.UploadFileOption>_config) : uni.request(<UniNamespace.RequestOptions>_config);
});
}