Files
danyang-match/miniapp/pages/service-station/service-station.ts
T

88 lines
2.2 KiB
TypeScript

import api from '../../utils/api'
interface ServiceLocation {
id: string
name: string
shortName?: string
address?: string
contactPhone?: string
contactPhoneDisplay?: string
openingHoursText?: string
services?: string[]
latitude?: number | string
longitude?: number | string
}
interface ServiceStationPageData {
serviceLocation: ServiceLocation | null
loading: boolean
loadError: boolean
}
Page({
data: {
serviceLocation: null,
loading: true,
loadError: false,
},
onLoad() {
this.loadStation()
},
async loadStation() {
this.setData({ loading: true, loadError: false })
try {
const home = await api.get<{ serviceLocation?: ServiceLocation }>('/home')
this.setData({ serviceLocation: home.serviceLocation || null, loading: false })
} catch (error) {
console.error('加载服务站失败', error)
this.setData({ loading: false, loadError: true })
}
},
call() {
const station = this.data.serviceLocation
if (!station) return
const phoneNumber = station.contactPhone || station.contactPhoneDisplay || ''
const digitCount = phoneNumber.replace(/\D/g, '').length
if (!phoneNumber || phoneNumber.includes('*') || digitCount < 5) {
wx.showToast({ title: '联系电话请以预约确认为准', icon: 'none' })
return
}
wx.makePhoneCall({ phoneNumber })
},
openMap() {
const station = this.data.serviceLocation
if (!station) return
const latitude = Number(station.latitude)
const longitude = Number(station.longitude)
if (Number.isFinite(latitude) && Number.isFinite(longitude) && latitude && longitude) {
wx.openLocation({
latitude,
longitude,
name: station.name,
address: station.address || '',
})
return
}
if (station.address) {
wx.setClipboardData({ data: station.address })
return
}
wx.showToast({ title: '暂未提供地图位置', icon: 'none' })
},
book() {
const station = this.data.serviceLocation
const id = station ? station.id : ''
const query = id ? `?serviceLocationId=${encodeURIComponent(id)}` : ''
wx.navigateTo({ url: `/pages/service-appointment/service-appointment${query}` })
},
onRetry() {
this.loadStation()
},
})