Files
wxapp_escort/pages/escort/attendantdetail.js
T

93 lines
2.7 KiB
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.
const API = require('../../utils/api.js')
const SELECTED_ATTENDANT_KEY = 'escort_selected_attendant'
function calcAge(birthday) {
if (!birthday) return ''
const birth = new Date(String(birthday).replace(/-/g, '/'))
if (Number.isNaN(birth.getTime())) return ''
const now = new Date()
let age = now.getFullYear() - birth.getFullYear()
const monthDiff = now.getMonth() - birth.getMonth()
if (monthDiff < 0 || (monthDiff === 0 && now.getDate() < birth.getDate())) age -= 1
return age > 0 ? `${age} 岁` : ''
}
Page({
data: {
attendant: null,
loading: true,
loadError: '',
selectMode: false
},
onLoad(options) {
this.id = options.id
const selectMode = String(options.select) === '1'
this.setData({ selectMode })
if (!this.id) {
this.setData({ loading: false, loadError: '缺少陪诊师ID' })
return
}
this.loadAttendant()
},
async loadAttendant() {
this.setData({ loading: true, loadError: '' })
try {
const res = await API.escort.getAttendantById(this.id)
if (!res || res.code !== 0) throw new Error((res && res.msg) || '加载失败')
const attendant = this.decorate(res.data && res.data.attendant)
this.setData({ attendant })
wx.setNavigationBarTitle({ title: attendant.name || '陪诊师详情' })
} catch (err) {
this.setData({ loadError: err.message || '加载失败,请稍后重试' })
} finally {
this.setData({ loading: false })
}
},
decorate(attendant) {
if (!attendant) return null
const name = attendant.name || ''
const sexMap = { male: '男', female: '女' }
const meta = [
sexMap[attendant.sex] || '',
calcAge(attendant.birthday),
attendant.workYears ? `从业 ${attendant.workYears} 年` : ''
].filter(Boolean)
return {
...attendant,
initial: name.replace(/[**]/g, '').slice(0, 1) || '陪',
metaText: meta.join(' · '),
areaText: (attendant.serviceAreas || []).join('、'),
languageText: (attendant.languages || []).join('、'),
trainings: attendant.trainings || [],
specialties: attendant.specialties || []
}
},
onRetry() {
if (this.id) this.loadAttendant()
},
onSelect() {
const attendant = this.data.attendant
if (!attendant || !attendant.id) return
wx.setStorageSync(SELECTED_ATTENDANT_KEY, {
id: attendant.id,
name: attendant.name,
workYears: attendant.workYears || 0,
orderCount: attendant.orderCount || 0
})
if (this.data.selectMode) {
// 下单页 → 列表 → 详情,选人后直接回到下单页
const pages = getCurrentPages()
wx.navigateBack({ delta: pages.length >= 3 ? 2 : 1 })
return
}
wx.redirectTo({ url: '/pages/escort/itemorder' })
}
})