41 lines
1.1 KiB
Plaintext
41 lines
1.1 KiB
Plaintext
// 本地开发、体验版和正式版统一请求生产后端,便于复现生产问题。
|
|
const PROD_API_BASE = 'https://app01.min.wooo.host'
|
|
|
|
export function apiBase(): string {
|
|
return PROD_API_BASE
|
|
}
|
|
|
|
function errorMessage(data: any): string {
|
|
if (data == null) return 'request failed'
|
|
const obj = data as any
|
|
if (obj.error != null) return obj.error as string
|
|
return 'request failed'
|
|
}
|
|
|
|
export function request(method: RequestMethod, path: string, data: any | null = null): Promise<any> {
|
|
return new Promise<any>((resolve, reject) => {
|
|
uni.request<any>({
|
|
url: apiBase() + path,
|
|
method,
|
|
data,
|
|
header: {
|
|
'content-type': 'application/json'
|
|
},
|
|
success: (res) => {
|
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
resolve(res.data)
|
|
return
|
|
}
|
|
reject(new Error(errorMessage(res.data)))
|
|
},
|
|
fail: (err) => {
|
|
reject(new Error(err.errMsg))
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
export function post(path: string, data: any): Promise<any> {
|
|
return request('POST' as RequestMethod, path, data)
|
|
}
|