doctor-mini/pages/loginCode/loginCode.js
2025-06-23 23:56:31 +08:00

130 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.

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
});
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`,
});
}
},
});