// appointment.js const { formatDate, appointmentDB } = require('../../utils/api') const app = getApp() // 订阅消息模板ID const SUBSCRIBE_TEMPLATE_ID = 'EF5CDtuZwrGbt8iyOoi-sY7J6hZamX0AbWPLoK-qnEw' Page({ data: { form: { name: '', phone: '', company: '', reason: '', date: '', time: '', hostName: '', hostId: '', personId: '', area: '', plateNumber: '' }, areas: [], departments: [], areaIndex: -1, persons: [], personNames: [], personIndex: -1, today: '', timeStart: '', timeEnd: '', submitting: false, fieldErrors: {} }, onLoad() { if (app.globalData.isLoggedIn) { this.initPage() } else if (app.globalData.loginFailed) { this.showLoginTipAndGoBack() } else { // 登录异步未完成,等待回调 app.loginReadyCallback = (userInfo) => { app.loginReadyCallback = null if (userInfo) { this.initPage() } else { this.showLoginTipAndGoBack() } } } }, initPage() { // 使用本地时区获取当天日期,用于 picker 最小日期限制 const today = formatDate(new Date()) this.setData({ today }) this.loadDepartments() }, async loadDepartments() { try { const list = await appointmentDB.getDepartmentSelector() const departments = list.map(item => ({ departmentCode: item.departmentCode, departmentName: item.departmentName })) this.setData({ departments, areas: departments.map(d => d.departmentName) }) } catch (err) { console.error('获取拜访区域列表失败', err) wx.showToast({ title: '获取拜访区域失败', icon: 'none' }) } }, showLoginTipAndGoBack() { wx.showToast({ title: '请先登录', icon: 'none', complete: () => { const pages = getCurrentPages() if (pages.length > 1) { wx.navigateBack() } else { wx.redirectTo({ url: '/pages/index/index' }) } }}) }, onNameInput(e) { this.setData({ 'form.name': e.detail.value }) this._clearFieldError('name') }, onPhoneInput(e) { this.setData({ 'form.phone': e.detail.value }) this._clearFieldError('phone') }, onCompanyInput(e) { this.setData({ 'form.company': e.detail.value }) this._clearFieldError('company') }, onReasonInput(e) { this.setData({ 'form.reason': e.detail.value }) this._clearFieldError('reason') }, onHostNameInput(e) { this.setData({ 'form.hostName': e.detail.value }) }, onPlateNumberChange(e) { this.setData({ 'form.plateNumber': e.detail.value }) }, onNameBlur() { if (!this.data.form.name.trim()) { this.setData({ 'fieldErrors.name': '姓名不能为空' }) } }, onPhoneBlur() { if (!this.data.form.phone.trim()) { this.setData({ 'fieldErrors.phone': '手机号不能为空' }) } }, onCompanyBlur() { if (!this.data.form.company.trim()) { this.setData({ 'fieldErrors.company': '公司不能为空' }) } }, onReasonBlur() { if (!this.data.form.reason.trim()) { this.setData({ 'fieldErrors.reason': '来访事由不能为空' }) } }, _clearFieldError(field) { if (this.data.fieldErrors[field]) { this.setData({ ['fieldErrors.' + field]: '' }) } }, onAreaChange(e) { const index = Number(e.detail.value) const department = this.data.departments[index] const departmentCode = department ? department.departmentCode : '' this.setData({ areaIndex: index, 'form.area': department ? department.departmentName : '', 'form.hostId': '', 'form.hostName': '', 'form.personId': '', persons: [], personNames: [], personIndex: -1 }) if (departmentCode) { this.loadPersons(departmentCode) } }, async loadPersons(department) { wx.showLoading({ title: '加载被访人中...' }) try { const list = await appointmentDB.getPersonSelector(department) const persons = list.map(item => ({ personId: item.personId, personName: item.personName, personId: item.personId })) this.setData({ persons, personNames: persons.map(p => p.personName), personIndex: -1 }) } catch (err) { console.error('获取被访人列表失败', err) wx.showToast({ title: '获取被访人列表失败', icon: 'none' }) } finally { wx.hideLoading() } }, onPersonChange(e) { const index = Number(e.detail.value) const person = this.data.persons[index] if (person && person.personId) { this.setData({ personIndex: index, 'form.hostId': person.personId, 'form.hostName': person.personName, 'form.personId': person.personId }) } }, onDateChange(e) { this.setData({ 'form.date': e.detail.value }) }, onTimeStartChange(e) { this.setData({ timeStart: e.detail.value }, this._updateTimeRange) }, onTimeEndChange(e) { this.setData({ timeEnd: e.detail.value }, this._updateTimeRange) }, _updateTimeRange() { const { timeStart, timeEnd } = this.data if (timeStart && timeEnd) { if (timeEnd <= timeStart) { this.setData({ 'form.time': '', 'fieldErrors.timeRange': '结束时间必须晚于开始时间' }) wx.showToast({ title: '结束时间必须晚于开始时间', icon: 'none' }) return } this.setData({ 'form.time': timeStart + '-' + timeEnd, 'fieldErrors.timeRange': '' }) } else { this.setData({ 'form.time': '' }) } }, validateForm() { const { name, phone, company, reason, date, time, hostName, area } = this.data.form if (!name.trim()) { wx.showToast({ title: '请输入访客姓名', icon: 'none' }) return false } if (!phone.trim() || !/^1\d{10}$/.test(phone)) { wx.showToast({ title: '请输入正确的手机号', icon: 'none' }) return false } if (!company.trim()) { wx.showToast({ title: '请输入所属公司', icon: 'none' }) return false } if (!reason.trim()) { wx.showToast({ title: '请输入来访事由', icon: 'none' }) return false } if (!date) { wx.showToast({ title: '请选择来访日期', icon: 'none' }) return false } if (!time) { wx.showToast({ title: '请选择来访时间', icon: 'none' }) return false } if (!hostName.trim()) { wx.showToast({ title: '请选择被访人', icon: 'none' }) return false } if (!area) { wx.showToast({ title: '请选择拜访区域', icon: 'none' }) return false } return true }, async onSubmit() { if (!this.validateForm()) return if (this.data.submitting) return // 先请求订阅消息授权,再提交预约 try { await wx.requestSubscribeMessage({ tmplIds: [SUBSCRIBE_TEMPLATE_ID] }) } catch (e) { // 用户拒绝授权也允许继续提交,只是收不到通知 console.warn('订阅消息授权失败或用户拒绝', e) } this.setData({ submitting: true }) try { const openid = app.globalData.userInfo.openid await appointmentDB.create({ ...this.data.form, openid }) this.setData({ submitting: false }) wx.showModal({ title: '提交成功', content: '您的预约已提交,请等待审核', showCancel: false, confirmText: '查看记录', confirmColor: '#1890ff', success: () => { wx.redirectTo({ url: '/pages/records/records' }) } }) } catch (err) { this.setData({ submitting: false }) console.error('提交预约失败', err) wx.showToast({ title: '提交失败,请重试', icon: 'none' }) } } })