import request from '~/api/request'; Page({ data: { code: '', phoneNumber: '', timer: null, getCodeText: '获取验证码', }, onLoad(options) { }, bindKeyInput(e){ const { mode } = e.currentTarget.dataset; this.setData({ [mode]: e.detail.value }) }, async getCode(){ if (!this.data.phoneNumber) { wx.showToast({ title: '手机号码不能为空', icon: 'none' }); return; } // 验证手机号格式 if (!/^1[3-9]\d{9}$/.test(this.data.phoneNumber)) { wx.showToast({ title: '手机号码格式不正确', icon: 'none' }); return; } // 如果正在倒计时,不允许重复获取 if (this.data.timer) { return; } // 发送验证码 const res = await request('patient/send_code', 'post', { mobile: this.data.phoneNumber, type: 1 }) console.log('验证码已发送'); wx.showToast({ title: '验证码已发送', icon: 'success' }); // 开始倒计时 let countdown = 60; this.setData({ getCodeText: `${countdown}s后重新获取` }); this.data.timer = setInterval(() => { countdown--; if (countdown <= 0) { clearInterval(this.data.timer); this.setData({ timer: null, getCodeText: '获取验证码' }); } else { this.setData({ getCodeText: `${countdown}s后重新获取` }); } }, 1000); }, async login() { // 验证手机号 if (!this.data.phoneNumber) { wx.showToast({ title: '手机号码不能为空', icon: 'none' }); return; } // 验证手机号格式 if (!/^1[3-9]\d{9}$/.test(this.data.phoneNumber)) { wx.showToast({ title: '手机号码格式不正确', icon: 'none' }); return; } // 验证验证码 if (!this.data.code) { wx.showToast({ title: '验证码不能为空', icon: 'none' }); return; } // 验证验证码格式(假设验证码为6位数字) // if (!/^\d{6}$/.test(this.data.code)) { // wx.showToast({ // title: '请输入6位数字验证码', // icon: 'none' // }); // return; // } const res = await request('patient/code_login', 'post', { code: this.data.code, mobile: this.data.phoneNumber }); wx.showToast({ title: '登录成功', icon: 'success' }); await wx.setStorageSync('access_token', res.token); if (res.is_personal_information_complete) { wx.switchTab({ url: `/pages/my/index`, }); } else { wx.navigateTo({ url: `/pages/my/info-edit/index`, }); } }, });