|
|
@@ -5,16 +5,18 @@ import { Rsp } from '../api/rsp/Rsp'
|
|
|
// 传输参数token
|
|
|
export const TOKEN_KEY: string = 'token'
|
|
|
|
|
|
-//请求网络的基底址
|
|
|
-export const BASE_URL: string = 'http://120.27.232.27:9190'
|
|
|
+// 请求网络的基址
|
|
|
+// 外网IP "http://120.27.232.27:48080"
|
|
|
+// 本地IP "http://192.168.0.10:48080"
|
|
|
+export const BASE_URL: string = 'http://120.27.232.27:48080'
|
|
|
|
|
|
interface EmptyInterface {}
|
|
|
|
|
|
|
|
|
//Next版本不支持在箭头函数上写纯泛型,
|
|
|
// function requestHttp(url: url地址, method: 请求方法类型,默认为get, data?: 参数类型) : 返回类型是: Promise里面的T类型的数据
|
|
|
-async function requestHttp<T extends Rsp>(url: string = '', method: http.RequestMethod = http.RequestMethod.GET,
|
|
|
- data?: object): Promise<T> {
|
|
|
+async function requestHttp<T>(url: string = '', method: http.RequestMethod = http.RequestMethod.GET,
|
|
|
+ data?: object): Promise<Rsp<T>> {
|
|
|
// 创建一个网络请求
|
|
|
const httpRequest = http.createHttp()
|
|
|
// 拼接地址
|
|
|
@@ -31,53 +33,55 @@ async function requestHttp<T extends Rsp>(url: string = '', method: http.Request
|
|
|
}).join('&') //['a=1','b=2','c=3']
|
|
|
}
|
|
|
}
|
|
|
- //设置请求对象
|
|
|
+ // 设置请求对象
|
|
|
let config: http.HttpRequestOptions = {
|
|
|
- //method同名方法赋值,参数名和属性名相同时只需要写一个method等价于method:method
|
|
|
+ // method同名方法赋值,参数名和属性名相同时只需要写一个method等价于method:method
|
|
|
method,
|
|
|
- //超时时间
|
|
|
+ // 超时时间
|
|
|
readTimeout: 10000,
|
|
|
- //get的extraData参数在上面处理过了 在这儿不需要再传一遍
|
|
|
+ // get的extraData参数在上面处理过了 在这儿不需要再传一遍
|
|
|
extraData: method === http.RequestMethod.GET ? '' : data || {} as EmptyInterface,
|
|
|
- //响应参数的类型,指定为对象后有BUG,当结果有问题时,项目会直接瘫痪
|
|
|
+ // 响应参数的类型,指定为对象后有BUG,当结果有问题时,项目会直接瘫痪
|
|
|
// expectDataType: http.HttpDataType.OBJECT,
|
|
|
- //请求头
|
|
|
+ // 请求头
|
|
|
header: {
|
|
|
'Content-Type': 'application/json',
|
|
|
- "Authorization": AppStorage.get(TOKEN_KEY) as string || ''
|
|
|
+ "Authorization": AppStorage.get(TOKEN_KEY) as string || '',
|
|
|
+ "tenant-id": "1" // 租户id
|
|
|
}
|
|
|
}
|
|
|
- //发请求
|
|
|
+ // 发请求
|
|
|
try {
|
|
|
const res = await httpRequest.request(reqUrl, config)
|
|
|
console.log(method, '--->', reqUrl, JSON.stringify(config.extraData))
|
|
|
console.log(method, '<---', reqUrl, res.responseCode, res.result)
|
|
|
- //res.responseCode响应状态码,这里的401还会认为是请求成功
|
|
|
+ // res.responseCode响应状态码,这里的401还会认为是请求成功
|
|
|
if (res.responseCode === 401) {
|
|
|
// 401 token超时
|
|
|
// 删除持久化数据token
|
|
|
AppStorage.set<string>(TOKEN_KEY, '')
|
|
|
- promptAction.openToast({ message: 'token timeout' })
|
|
|
+ promptAction.openToast({ message: 'token timeout' }).catch()
|
|
|
// 回登录
|
|
|
// router.replaceUrl({ url: 'pages/Login/LoginPage' })
|
|
|
// 返回错误 终止
|
|
|
return Promise.reject(new Error('token timeout'))
|
|
|
} else if (res.responseCode === 404) {
|
|
|
- promptAction.openToast({ message: 'not find' })
|
|
|
+ promptAction.openToast({ message: 'not find' }).catch()
|
|
|
return Promise.reject(new Error('not find'))
|
|
|
} else {
|
|
|
// 指定为字符串,然后再转成一个对象,类型是不明确的要使用泛型,返回第一层+泛型,泛型的定义是一个类和之前的有所差距
|
|
|
- const result = JSON.parse(res.result as string) as T
|
|
|
- // 再判断返回的状态码进行处理,不是200都是失败
|
|
|
- if (result.code === 200) {
|
|
|
- return result as T
|
|
|
+ const apiRsp = JSON.parse(res.result as string) as Rsp<T>
|
|
|
+ // 再判断返回的状态码进行处理,不是0,200都是失败
|
|
|
+ if ([0, 200].includes(apiRsp.code)) {
|
|
|
+ return apiRsp as Rsp<T>
|
|
|
} else {
|
|
|
- promptAction.openToast({ message: result.msg })
|
|
|
- return Promise.reject(new Error(result.msg))
|
|
|
+ promptAction.openToast({ message: apiRsp.msg }).catch()
|
|
|
+ return Promise.reject(new Error(apiRsp.msg))
|
|
|
}
|
|
|
}
|
|
|
} catch (error) {
|
|
|
- promptAction.openToast({ message: error })
|
|
|
+ console.error(method, '--->', reqUrl, JSON.stringify(error))
|
|
|
+ promptAction.openToast({ message: error }).catch()
|
|
|
return Promise.reject(error)
|
|
|
} finally {
|
|
|
// 销毁请求请求结束
|
|
|
@@ -95,7 +99,7 @@ export class Request {
|
|
|
* @param data 携带数据
|
|
|
* @returns
|
|
|
*/
|
|
|
- static get<T extends Rsp>(url: string, data?: object): Promise<T> {
|
|
|
+ static get<T>(url: string, data?: object): Promise<Rsp<T>> {
|
|
|
return requestHttp<T>(url, http.RequestMethod.GET, data)
|
|
|
}
|
|
|
|
|
|
@@ -105,7 +109,7 @@ export class Request {
|
|
|
* @param data 携带数据
|
|
|
* @returns
|
|
|
*/
|
|
|
- static post<T extends Rsp>(url: string, data?: object): Promise<T> {
|
|
|
+ static post<T>(url: string, data?: object): Promise<Rsp<T>> {
|
|
|
return requestHttp<T>(url, http.RequestMethod.POST, data)
|
|
|
}
|
|
|
|
|
|
@@ -115,7 +119,7 @@ export class Request {
|
|
|
* @param data 携带数据
|
|
|
* @returns
|
|
|
*/
|
|
|
- static put<T extends Rsp>(url: string, data?: object): Promise<T> {
|
|
|
+ static put<T>(url: string, data?: object): Promise<Rsp<T>> {
|
|
|
return requestHttp<T>(url, http.RequestMethod.PUT, data)
|
|
|
}
|
|
|
|
|
|
@@ -125,7 +129,7 @@ export class Request {
|
|
|
* @param data 携带数据
|
|
|
* @returns
|
|
|
*/
|
|
|
- static delete<T extends Rsp>(url: string, data?: object): Promise<T> {
|
|
|
+ static delete<T>(url: string, data?: object): Promise<Rsp<T>> {
|
|
|
return requestHttp<T>(url, http.RequestMethod.DELETE, data)
|
|
|
}
|
|
|
}
|