74 lines
2.0 KiB
Plaintext
74 lines
2.0 KiB
Plaintext
const LOCAL_API_BASE = 'http://localhost:9800'
|
|
const PROD_API_BASE = 'https://app01.min.wooo.host'
|
|
|
|
export function apiBase(): string {
|
|
if (isLocalMiniProgramDev()) return LOCAL_API_BASE
|
|
return PROD_API_BASE
|
|
}
|
|
|
|
function isLocalMiniProgramDev(): boolean {
|
|
// #ifdef MP-WEIXIN
|
|
try {
|
|
const accountInfo = uni.getAccountInfoSync()
|
|
const info = accountInfo as any
|
|
if (info.miniProgram == null) return false
|
|
const miniProgram = info.miniProgram as any
|
|
if (miniProgram.envVersion == null) return false
|
|
const envVersion = miniProgram.envVersion as string
|
|
return envVersion == 'develop'
|
|
} catch (e) {
|
|
return false
|
|
}
|
|
// #endif
|
|
return false
|
|
}
|
|
|
|
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 get(path: string): Promise<any> {
|
|
return request('GET' as RequestMethod, path, null)
|
|
}
|
|
|
|
export function post(path: string, data: any): Promise<any> {
|
|
return request('POST' as RequestMethod, path, data)
|
|
}
|
|
|
|
export function patch(path: string, data: any): Promise<any> {
|
|
return request('PATCH' as RequestMethod, path, data)
|
|
}
|
|
|
|
export function mediaUrl(value: string | null): string {
|
|
if (value == null || value.length == 0) return ''
|
|
if (value.indexOf('http://') == 0 || value.indexOf('https://') == 0) return value
|
|
if (value.indexOf('/') == 0) return apiBase() + value
|
|
return value
|
|
}
|