79 lines
1.9 KiB
JavaScript
79 lines
1.9 KiB
JavaScript
/**
|
||
* 按「接口所属后端」固定取值,禁止运行时猜测 / result||data 混取
|
||
*
|
||
* 写代码时先确认该请求打的是 xk-api 还是 Yii,再选用对应方法:
|
||
* - xk-api:unwrapXkApi / xkOk / xkPayload / xkMessage → code / result / message
|
||
* - Yii: unwrapYiiApi / yiiOk / yiiPayload / yiiMessage → errcode / data / msg
|
||
*
|
||
* 患者端 request 返回完整 uni 响应,业务包在 res.data
|
||
*/
|
||
|
||
/** 取出业务 body(uni 完整响应 → 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,
|
||
}
|
||
}
|