Files
chongdun-mianya/utils/request.js
2026-10-27 13:54:39 +08:00

34 lines
922 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 || '网络异常,请稍后重试'))
}
})
})
}