'use strict'; function nowTimestamp() { return Math.floor(Date.now() / 1000); } function isObject(value) { return value && typeof value === 'object' && !Array.isArray(value); } function isStandardPayload(payload) { return isObject(payload) && Object.prototype.hasOwnProperty.call(payload, 'flag') && Object.prototype.hasOwnProperty.call(payload, 'code') && Object.prototype.hasOwnProperty.call(payload, 'message'); } function toSuccess(payload, message = 'ok') { if (isStandardPayload(payload)) { const normalized = { ...payload, flag: payload.flag === true || payload.flag === 'success' ? 'success' : payload.flag, }; if (!Object.prototype.hasOwnProperty.call(normalized, 'timestamp')) { normalized.timestamp = nowTimestamp(); } return normalized; } return { flag: 'success', code: 1, message, data: payload ?? null, timestamp: nowTimestamp(), }; } function toFailure(options = {}) { const { message = '请求失败', httpStatus = 500, code, data = null, } = options; return { flag: 'failure', code: typeof code === 'number' ? code : httpStatus, message, data, timestamp: nowTimestamp(), }; } module.exports = { toSuccess, toFailure, };