|
|
@@ -1,4 +1,5 @@
|
|
|
import { env, TIMEOUT } from '@/common/env.uts'
|
|
|
+import { logout } from '@/store/user.uts'
|
|
|
|
|
|
export type ApiResponse<T> = {
|
|
|
code : number
|
|
|
@@ -20,6 +21,16 @@ function getToken() : string {
|
|
|
return token as string
|
|
|
}
|
|
|
|
|
|
+function toastError(msg : string) : void {
|
|
|
+ uni.showToast({ title: msg, icon: 'none' })
|
|
|
+}
|
|
|
+
|
|
|
+function handleUnauthorized() : void {
|
|
|
+ logout()
|
|
|
+ toastError('登录已失效,请重新登录')
|
|
|
+ uni.reLaunch({ url: '/pages/mine/mine' })
|
|
|
+}
|
|
|
+
|
|
|
export function request<T>(options : RequestOptions) : Promise<T> {
|
|
|
return new Promise<T>((resolve, reject) => {
|
|
|
const header : UTSJSONObject = options.header ?? {}
|
|
|
@@ -29,6 +40,10 @@ export function request<T>(options : RequestOptions) : Promise<T> {
|
|
|
header['Authorization'] = 'Bearer ' + token
|
|
|
}
|
|
|
|
|
|
+ if (options.showLoading == true) {
|
|
|
+ uni.showLoading({ title: '加载中...' })
|
|
|
+ }
|
|
|
+
|
|
|
uni.request({
|
|
|
url: env.baseUrl + options.url,
|
|
|
method: options.method ?? 'GET',
|
|
|
@@ -36,26 +51,46 @@ export function request<T>(options : RequestOptions) : Promise<T> {
|
|
|
header: header,
|
|
|
timeout: TIMEOUT,
|
|
|
success: (res) => {
|
|
|
+ if (options.showLoading == true) {
|
|
|
+ uni.hideLoading()
|
|
|
+ }
|
|
|
const statusCode = res.statusCode
|
|
|
+ if (statusCode == 401 || statusCode == 403) {
|
|
|
+ handleUnauthorized()
|
|
|
+ reject(new Error('登录已失效'))
|
|
|
+ return
|
|
|
+ }
|
|
|
if (statusCode >= 200 && statusCode < 300) {
|
|
|
- const body = res.data as UTSJSONObject
|
|
|
- const code = body.getNumber('code')
|
|
|
- if (code != null && code == 200) {
|
|
|
- resolve(body.get('data') as T)
|
|
|
+ const body = res.data
|
|
|
+ if (body == null || !(body instanceof UTSJSONObject)) {
|
|
|
+ reject(new Error('响应格式错误'))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const json = body as UTSJSONObject
|
|
|
+ const code = json.getNumber('code')
|
|
|
+ if (code == null) {
|
|
|
+ resolve(json as T)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (code == 200) {
|
|
|
+ resolve(json.get('data') as T)
|
|
|
} else {
|
|
|
- const msg = body.getString('message') ?? '请求失败'
|
|
|
- uni.showToast({ title: msg, icon: 'none' })
|
|
|
+ const msg = json.getString('message') ?? '请求失败'
|
|
|
+ toastError(msg)
|
|
|
reject(new Error(msg))
|
|
|
}
|
|
|
} else {
|
|
|
const msg = '网络错误 ' + statusCode
|
|
|
- uni.showToast({ title: msg, icon: 'none' })
|
|
|
+ toastError(msg)
|
|
|
reject(new Error(msg))
|
|
|
}
|
|
|
},
|
|
|
fail: (err) => {
|
|
|
- const msg = err.errMsg ?? '请求失败'
|
|
|
- uni.showToast({ title: msg, icon: 'none' })
|
|
|
+ if (options.showLoading == true) {
|
|
|
+ uni.hideLoading()
|
|
|
+ }
|
|
|
+ const msg = err.errMsg != null && err.errMsg.length > 0 ? err.errMsg : '请求失败'
|
|
|
+ toastError(msg)
|
|
|
reject(new Error(msg))
|
|
|
}
|
|
|
})
|
|
|
@@ -69,3 +104,11 @@ export function get<T>(url : string, data ?: UTSJSONObject | null) : Promise<T>
|
|
|
export function post<T>(url : string, data ?: UTSJSONObject | null) : Promise<T> {
|
|
|
return request<T>({ url: url, method: 'POST', data: data })
|
|
|
}
|
|
|
+
|
|
|
+export function put<T>(url : string, data ?: UTSJSONObject | null) : Promise<T> {
|
|
|
+ return request<T>({ url: url, method: 'PUT', data: data })
|
|
|
+}
|
|
|
+
|
|
|
+export function del<T>(url : string, data ?: UTSJSONObject | null) : Promise<T> {
|
|
|
+ return request<T>({ url: url, method: 'DELETE', data: data })
|
|
|
+}
|