Files
xk-client-wx/utils/api-response.js

79 lines
1.9 KiB
JavaScript
Raw Normal View History

/**
* 接口所属后端固定取值禁止运行时猜测 / result||data 混取
*
* 写代码时先确认该请求打的是 xk-api 还是 Yii再选用对应方法
* - xk-apiunwrapXkApi / xkOk / xkPayload / xkMessage code / result / message
* - Yii unwrapYiiApi / yiiOk / yiiPayload / yiiMessage errcode / data / msg
*
* 患者端 request 返回完整 uni 响应业务包在 res.data
*/
/** 取出业务 bodyuni 完整响应 → res.data已是 body 则原样) */
export function getApiBody(res) {
if (res == null || typeof res !== 'object') {
return res
}
if ('statusCode' in res || 'errMsg' in res) {
return res.data
}
return res
}
/** xk-api 是否成功code === 0 */
export function xkOk(res) {
const body = getApiBody(res)
return !!(body && Number(body.code) === 0)
}
/** xk-api 业务数据(固定取 result */
export function xkPayload(res) {
const body = getApiBody(res)
return body ? body.result : undefined
}
/** xk-api 提示文案(固定取 message */
export function xkMessage(res) {
const body = getApiBody(res)
return (body && body.message) || ''
}
/** xk-api 一次解包 */
export function unwrapXkApi(res) {
const body = getApiBody(res)
return {
ok: xkOk(res),
payload: xkPayload(res),
message: xkMessage(res),
body: body,
}
}
/** Yii 是否成功errcode === 0 */
export function yiiOk(res) {
const body = getApiBody(res)
return !!(body && Number(body.errcode) === 0)
}
/** Yii 业务数据(固定取 data */
export function yiiPayload(res) {
const body = getApiBody(res)
return body ? body.data : undefined
}
/** Yii 提示文案(固定取 msg */
export function yiiMessage(res) {
const body = getApiBody(res)
return (body && body.msg) || ''
}
/** Yii 一次解包 */
export function unwrapYiiApi(res) {
const body = getApiBody(res)
return {
ok: yiiOk(res),
payload: yiiPayload(res),
message: yiiMessage(res),
body: body,
}
}