const RESERVATION_FLOW_STEP = { INSPECTION: 10, BILL: 20, CONTRACT: 30, CHECKIN: 40 } function getLanguage(language) { return language === 'en' ? 'en' : 'zh' } function parseJsonStringMaybe(value) { if (typeof value !== 'string') { return value } const text = value.trim() if (!text) { return value } const isJsonLike = ( (text.startsWith('{') && text.endsWith('}')) || (text.startsWith('[') && text.endsWith(']')) ) if (!isJsonLike) { return value } try { return JSON.parse(text) } catch (error) { return value } } function normalizeArray(value) { if (Array.isArray(value)) { return value } return [] } export function parseReservationFlowResponse(response) { const rawPayload = response && response.data ? response.data : response const payload = parseJsonStringMaybe(rawPayload) if (Array.isArray(payload)) { return payload } if (payload && Array.isArray(payload.data)) { return payload.data } if (payload && payload.data && Array.isArray(payload.data.data)) { return payload.data.data } return [] } export function getReservationOrderNo(source = {}) { const candidates = [ source.orderNo, source.OrderNo, source.reservationOrderNo, source.searchedOrderNo, source.bookingOrderNo ] for (let index = 0; index < candidates.length; index += 1) { const value = String(candidates[index] || '').trim() if (value) { return value } } return '' } export function getReservationCurrentNode(flow = {}) { const nodes = normalizeArray(flow && flow.flowNodes) .filter(item => item && typeof item === 'object') .sort((a, b) => Number(a.sort || 0) - Number(b.sort || 0)) if (!nodes.length) { return null } return nodes.find(item => item.isCurrent) || nodes[nodes.length - 1] } export function buildReservationFlowMap(flowList = []) { return normalizeArray(flowList) .filter(item => item && typeof item === 'object') .reduce((result, item) => { const orderNo = getReservationOrderNo(item) if (!orderNo) { return result } result[orderNo] = { ...item, orderNo, currentNode: getReservationCurrentNode(item) } return result }, {}) } export function attachReservationFlow(item = {}, flowMap = {}) { const orderNo = getReservationOrderNo(item) const matchedFlow = orderNo ? flowMap[orderNo] : null return { ...item, reservationOrderNo: orderNo, reservationFlow: matchedFlow || null, reservationCurrentNode: matchedFlow && matchedFlow.currentNode ? matchedFlow.currentNode : null } } export function getReservationStepMeta(step, language = 'zh') { const currentLanguage = getLanguage(language) const map = { [RESERVATION_FLOW_STEP.INSPECTION]: { label: currentLanguage === 'en' ? 'Inspection' : '验房', actionText: currentLanguage === 'en' ? 'View Inspection' : '查看验房' }, [RESERVATION_FLOW_STEP.BILL]: { label: currentLanguage === 'en' ? 'Bill' : '账单', actionText: currentLanguage === 'en' ? 'Go to Bill' : '去缴费' }, [RESERVATION_FLOW_STEP.CONTRACT]: { label: currentLanguage === 'en' ? 'Contract' : '合同', actionText: currentLanguage === 'en' ? 'Go to Contract' : '去签约' }, [RESERVATION_FLOW_STEP.CHECKIN]: { label: currentLanguage === 'en' ? 'Check-in' : '入住', actionText: currentLanguage === 'en' ? 'Go to Check-in' : '去入住' } } return map[Number(step)] || { label: currentLanguage === 'en' ? 'Current Step' : '当前步骤', actionText: currentLanguage === 'en' ? 'View Current Step' : '查看当前步骤' } } export function getReservationCurrentStepText(source = {}, language = 'zh') { const node = source && source.reservationCurrentNode ? source.reservationCurrentNode : getReservationCurrentNode(source) if (!node) { return '' } return String(node.stepDesc || '').trim() || getReservationStepMeta(node.step, language).label } export function getReservationCurrentActionText(source = {}, language = 'zh') { const node = source && source.reservationCurrentNode ? source.reservationCurrentNode : getReservationCurrentNode(source) if (!node) { return '' } return getReservationStepMeta(node.step, language).actionText } export { RESERVATION_FLOW_STEP }