Merge branch 'master' of http://192.168.12.3:3000/xingyy/uni-ticket-system
This commit is contained in:
commit
f62148c7f5
@ -23,10 +23,6 @@ interface RequestOptions {
|
||||
}
|
||||
type RequestInterceptor = (config: RequestOptions) => RequestOptions;
|
||||
type ResponseInterceptor = (response: any) => any;
|
||||
interface Interceptors {
|
||||
request: (config: RequestOptions) => RequestOptions;
|
||||
response: (response: any) => any;
|
||||
}
|
||||
class uniFetch {
|
||||
baseUrl: string;
|
||||
defaultHeader: Record<string, string>;
|
||||
@ -34,7 +30,7 @@ class uniFetch {
|
||||
constructor({ baseUrl = '', defaultHeader = {}, interceptors = { request: null, response: null } }: {
|
||||
baseUrl?: string;
|
||||
defaultHeader?: Record<string, string>;
|
||||
interceptors?: Interceptors;
|
||||
interceptors?: { request: RequestInterceptor | null; response: ResponseInterceptor | null};
|
||||
}) {
|
||||
this.baseUrl = baseUrl;
|
||||
this.defaultHeader = {
|
||||
@ -51,18 +47,18 @@ class uniFetch {
|
||||
setDefaultHeader(header: Record<string, string>): void {
|
||||
this.defaultHeader = header;
|
||||
}
|
||||
|
||||
request(options: RequestOptions): Promise<any> {
|
||||
options = options || {};
|
||||
options.baseUrl = options.baseUrl || this.baseUrl;
|
||||
options.url = options.baseUrl + options.url;
|
||||
options.data = options.data || {};
|
||||
options.method = options.method || "GET";
|
||||
options.method = (options.method?.toUpperCase() || "GET") as HttpMethod;
|
||||
options.header = options.header || this.defaultHeader;
|
||||
if (this.interceptors.request) {
|
||||
options = this.interceptors.request(options);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
// @ts-ignore
|
||||
uni.request({
|
||||
...options,
|
||||
success: (res) => {
|
||||
@ -109,9 +105,9 @@ class uniFetch {
|
||||
}
|
||||
|
||||
private buildRequestOptions(options: RequestOptions): RequestOptions {
|
||||
if (options.params) {
|
||||
if (options.params && Object.keys(options.params).length > 0) {
|
||||
const queryString = Object.keys(options.params)
|
||||
.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(options.params[key])}`)
|
||||
.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(options.params![key])}`)
|
||||
.join('&');
|
||||
options.url += `?${queryString}`;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user