// pages/apply/apply.ts import userService from '../../services/userService'; Page({ /** * 页面的初始数据 */ data: { applyForm: { name: '', idCard: '', phone: '' } }, /** * 生命周期函数--监听页面加载 */ onLoad() { console.log('申请页面加载'); }, /** * 表单输入处理 */ onApplyFormInput(e: any) { const { field } = e.currentTarget.dataset; const { value } = e.detail; this.setData({ [`applyForm.${field}`]: value }); }, /** * 取消按钮点击 */ onCancel() { // 返回上一页 wx.navigateBack(); }, /** * 提交申请 */ async onSubmit() { const { name, idCard, phone } = this.data.applyForm; // 表单验证 if (!this.validateForm()) { return; } try { wx.showLoading({ title: '提交中...', mask: true }); // 调用API提交申请 const result = await this.submitApplication({ name, idCard, phone }); if (result.success) { wx.showToast({ title: '申请提交成功', icon: 'success', duration: 2000 }); // 更新全局用户信息 const app = getApp(); if (app.globalData.userInfo) { app.globalData.userInfo = { ...app.globalData.userInfo, name: result.employeeInfo.name, phone: result.employeeInfo.phone, role: result.employeeInfo.role }; console.log('全局用户信息已更新:', app.globalData.userInfo); } // 延迟返回并刷新首页 setTimeout(() => { // 获取当前页面栈 const pages = getCurrentPages(); if (pages.length >= 2) { // 获取首页实例并调用刷新方法 const indexPage = pages[pages.length - 2]; if (indexPage && indexPage.refreshPageAfterLogin) { indexPage.refreshPageAfterLogin(); } } wx.navigateBack(); }, 1500); } else { wx.showToast({ title: result.message || '提交失败', icon: 'none', duration: 2000 }); } } catch (error) { console.error('提交申请失败:', error); wx.showToast({ title: '网络错误,请重试', icon: 'none', duration: 2000 }); } finally { wx.hideLoading(); } }, /** * 表单验证 */ validateForm(): boolean { const { name, idCard, phone } = this.data.applyForm; if (!name || !idCard || !phone) { wx.showToast({ title: '请填写完整信息', icon: 'none', duration: 2000 }); return false; } // 身份证号验证 if (idCard.length !== 18) { wx.showToast({ title: '请输入正确的身份证号', icon: 'none', duration: 2000 }); return false; } // 手机号验证 if (!/^1[3-9]\d{9}$/.test(phone)) { wx.showToast({ title: '请输入正确的手机号', icon: 'none', duration: 2000 }); return false; } return true; }, /** * 提交申请到服务器 */ async submitApplication(data: any): Promise { try { // 先执行微信登录获取token const loginResult = await userService.wxLogin(); if (!loginResult.success) { throw new Error('微信登录失败,请重试'); } // 调用实际的注册接口 return await userService.register({ name: data.name, phone: data.phone }); } catch (error) { console.error('注册流程失败:', error); throw error; } }, /** * 生命周期函数--监听页面卸载 */ onUnload() { console.log('申请页面卸载'); } });