首次提交
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
/**
|
||||
* HTTP请求工具类
|
||||
* 使用 @kit.NetworkKit 进行网络请求
|
||||
*/
|
||||
import { http } from '@kit.NetworkKit';
|
||||
import { BusinessError } from '@kit.BasicServicesKit';
|
||||
import Config from './Config';
|
||||
|
||||
// 响应数据接口
|
||||
export interface iResponseModel<T> {
|
||||
code: number;
|
||||
message: string;
|
||||
data: T;
|
||||
}
|
||||
|
||||
// HTTP请求头接口
|
||||
interface HttpHeader {
|
||||
'Content-Type': string;
|
||||
'Accept': string;
|
||||
'Authorization'?: string;
|
||||
}
|
||||
|
||||
// HTTP请求选项接口
|
||||
interface HttpOptions {
|
||||
method: http.RequestMethod;
|
||||
header: HttpHeader;
|
||||
extraData?: Object;
|
||||
connectTimeout: number;
|
||||
readTimeout: number;
|
||||
}
|
||||
|
||||
export class RequestUtil {
|
||||
/**
|
||||
* GET请求
|
||||
*/
|
||||
static get<T>(url: string): Promise<iResponseModel<T>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let httpRequest = http.createHttp();
|
||||
|
||||
const header: HttpHeader = {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
};
|
||||
|
||||
const options: HttpOptions = {
|
||||
method: http.RequestMethod.GET,
|
||||
header: header,
|
||||
connectTimeout: Config.TIMEOUT,
|
||||
readTimeout: Config.TIMEOUT
|
||||
};
|
||||
|
||||
httpRequest.request(
|
||||
Config.BASE_URL + url,
|
||||
options,
|
||||
(err: BusinessError, data: http.HttpResponse) => {
|
||||
if (!err) {
|
||||
try {
|
||||
let result: iResponseModel<T> = JSON.parse(data.result as string);
|
||||
resolve(result);
|
||||
} catch (e) {
|
||||
reject(new Error('JSON解析失败'));
|
||||
}
|
||||
} else {
|
||||
reject(new Error(err.message));
|
||||
}
|
||||
httpRequest.destroy();
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* GET请求(带query参数)
|
||||
* @param url 请求地址,例如 /monthDetail
|
||||
* @param params 查询参数,例如 { paramType: 'WATER_TEMPERATURE', deviceCode: 'D001' }
|
||||
*/
|
||||
static getWithParams<T>(url: string, params: Record<string, string | number | boolean>): Promise<iResponseModel<T>>
|
||||
{
|
||||
const query = Object.keys(params)
|
||||
.map((key: string) => `${encodeURIComponent(key)}=${encodeURIComponent(String(params[key]))}`)
|
||||
.join('&');
|
||||
|
||||
const fullUrl = query.length > 0 ? `${url}?${query}` : url;
|
||||
return RequestUtil.get<T>(fullUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST请求
|
||||
* @param url 请求地址
|
||||
* @param cls 请求体对象
|
||||
* @param needToken 是否需要Token
|
||||
*/
|
||||
static post<T>(url: string, cls: Object, needToken: boolean): Promise<iResponseModel<T>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let httpRequest = http.createHttp();
|
||||
|
||||
let header: HttpHeader;
|
||||
if (needToken) {
|
||||
const token = AppStorage.get<string>('token');
|
||||
header = {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
'Authorization': 'Bearer ' + token
|
||||
};
|
||||
} else {
|
||||
header = {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
};
|
||||
}
|
||||
|
||||
const options: HttpOptions = {
|
||||
method: http.RequestMethod.POST,
|
||||
header: header,
|
||||
extraData: cls,
|
||||
connectTimeout: Config.TIMEOUT,
|
||||
readTimeout: Config.TIMEOUT
|
||||
};
|
||||
|
||||
httpRequest.request(
|
||||
Config.BASE_URL + url,
|
||||
options,
|
||||
(err: BusinessError, data: http.HttpResponse) => {
|
||||
if (!err) {
|
||||
try {
|
||||
let result: iResponseModel<T> = JSON.parse(data.result as string);
|
||||
resolve(result);
|
||||
} catch (e) {
|
||||
reject(new Error('JSON解析失败'));
|
||||
}
|
||||
} else {
|
||||
reject(new Error(err.message));
|
||||
}
|
||||
httpRequest.destroy();
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT请求
|
||||
* @param url 请求地址
|
||||
* @param cls 请求体对象
|
||||
*/
|
||||
static put<T>(url: string, cls: Object): Promise<iResponseModel<T>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let httpRequest = http.createHttp();
|
||||
|
||||
const token = AppStorage.get<string>('token');
|
||||
const header: HttpHeader = {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
'Authorization': 'Bearer ' + token
|
||||
};
|
||||
|
||||
const options: HttpOptions = {
|
||||
method: http.RequestMethod.PUT,
|
||||
header: header,
|
||||
extraData: cls,
|
||||
connectTimeout: Config.TIMEOUT,
|
||||
readTimeout: Config.TIMEOUT
|
||||
};
|
||||
|
||||
httpRequest.request(
|
||||
Config.BASE_URL + url,
|
||||
options,
|
||||
(err: BusinessError, data: http.HttpResponse) => {
|
||||
if (!err) {
|
||||
try {
|
||||
let result: iResponseModel<T> = JSON.parse(data.result as string);
|
||||
resolve(result);
|
||||
} catch (e) {
|
||||
reject(new Error('JSON解析失败'));
|
||||
}
|
||||
} else {
|
||||
reject(new Error(err.message));
|
||||
}
|
||||
httpRequest.destroy();
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default RequestUtil;
|
||||
Reference in New Issue
Block a user