first commit
This commit is contained in:
33
utils/request.js
Normal file
33
utils/request.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import config from '@/config'
|
||||
|
||||
/**
|
||||
* 服务端请求封装
|
||||
*
|
||||
* 约定返回体:{ code: 0, message: '', data: {...} }
|
||||
* code 为 0 或未返回时视为成功,data 直接透出。
|
||||
*/
|
||||
export function request({ url, method = 'GET', data = {}, header = {} }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: /^https?:\/\//.test(url) ? url : config.apiBaseUrl + url,
|
||||
method,
|
||||
data,
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
...header
|
||||
},
|
||||
success: (res) => {
|
||||
const body = res.data || {}
|
||||
const ok = res.statusCode >= 200 && res.statusCode < 300
|
||||
if (ok && (body.code === undefined || body.code === 0)) {
|
||||
resolve(body.data !== undefined ? body.data : body)
|
||||
return
|
||||
}
|
||||
reject(new Error(body.message || `请求失败(${res.statusCode})`))
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(new Error(err.errMsg || '网络异常,请稍后重试'))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user