| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- Page({
- data: {
- phoneNumber: '',
- code: '',
- hasPhoneNumber: false,
- hasCode: false,
- isSendingCode: false,
- isLoggedIn: false,
- // 初始不显示倒计时
- countdown: 0,
- // 控制是否显示倒计时
- showCountdown: false,
- },
- loginByCode: function(phoneNumber, code) {
- wx.request({
- url: 'https://your-backend.com/login', // 你的后端登录接口
- method: 'POST',
- data: {
- phoneNumber: phoneNumber,
- code: code
- },
- success(res) {
- if (res.data.success) {
- // 保存登录凭证
- wx.setStorageSync('loginToken', res.data.token);
- wx.showToast({
- title: '登录成功',
- icon: 'success',
- duration: 2000
- });
- // 跳转到主页面等
- } else {
- wx.showToast({
- title: '登录失败',
- icon: 'none',
- duration: 2000
- });
- }
- },
- fail(err) {
- console.error(err);
- wx.showToast({
- title: '网络请求失败',
- icon: 'none',
- duration: 2000
- });
- }
- });
- },
- sendVerificationCode:function(e) {
- let phoneNumber = e.detail.value.phoneNumber; // 假设这是从输入框获取的手机号
- // 调用API发送验证码
- wx.request({
- url: 'https://your-backend.com/api/send-verification-code',
- method: 'POST',
- data: {
- phoneNumber: phoneNumber
- },
- success: function(res) {
- if (res.statusCode === 200 && res.data.success) {
- wx.showToast({
- title: '验证码已发送',
- icon: 'success',
- duration: 2000
- });
- } else {
- wx.showToast({
- title: '发送验证码失败',
- icon: 'none',
- duration: 2000
- });
- }
- },
- fail: function() {
- wx.showToast({
- title: '网络请求失败',
- icon: 'none',
- duration: 2000
- });
- }
- });
- },
- handlePhoneNumberInput: function(e) {
- this.setData({
- phoneNumber: e.detail.value,
- hasPhoneNumber: e.detail.value.trim() !== '',
- isSendingCode: false, // 清除发送状态,如果用户修改了手机号
- });
- },
- handleCodeInput: function(e) {
- this.setData({
- code: e.detail.value,
- hasCode: e.detail.value.trim() !== '',
- });
- },
- sendCode: function() {
- console.log('123213');
- if (!this.data.hasPhoneNumber) return;
- this.setData({
- isSendingCode: true,
- });
- // this.sendVerificationCode(this.data.phoneNumber).then(() => {
- // wx.showToast({ title: '验证码已发送', icon: 'success' });
- // this.setData({
- // isSendingCode: false,
- // });
-
- // }).catch(err => {
- // wx.showToast({ title: '发送失败', icon: 'none' });
- // this.setData({
- // isSendingCode: false,
- // });
- // });
- console.log('验证码已发出');
- // 启动倒计时
- this.startCountdown(60);
- },
- login: function() {
- if (!this.data.hasPhoneNumber || !this.data.hasCode) {
- wx.showModal({
- title: '提示',
- content: '手机号或验证码不能为空',
- showCancel: false
- });
- return;
- }
- if (!(/^1[3-9]\d{9}$/.test(this.data.phoneNumber))) {
- wx.showModal({
- title: '提示',
- content: '手机号格式不正确',
- showCancel: false
- });
- return;
- }
-
- // this.loginByCode(this.data.phoneNumber, this.data.code).then(token => {
- // wx.setStorageSync('loginToken', token);
- // this.setData({
- // isLoggedIn: true,
- // });
- // wx.reLaunch({
- // url: '/pages/scan/scan',
- // });
- // }).catch(err => {
- // wx.showToast({ title: '登录失败', icon: 'none' });
- // });
- this.setData({
- isLoggedIn: true,
- });
- wx.setStorageSync('loginToken', 'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6IjQ1OTJhZDlhLTI0NjMtNDlhOS05YjlkLTQ0NTNhYWM5MDI1NSIsInVzZXJuYW1lIjoiYWRtaW4ifQ.efQTBQSRiiKgPWjiIbeOMyCiScrWuCiFn0T4CcylzZKaeMvN241puUV4OoloG98dw6J67pCCwbuIf7n9gFSZmQ');
-
- wx.showModal({
- title: '提示',
- content: '登录成功',
- showCancel: false
- });
-
- setTimeout(function() {
- wx.reLaunch({
- url: '/subpages/scan/scan',
- })
- }, 2000) // 设置延时时间,单位为毫秒
-
- },
- startCountdown: function(seconds) {
- let that = this;
- this.setData({
- showCountdown: true,
- countdown: seconds
- });
- let timer = setInterval(function() {
- seconds--;
- if (seconds <= 0) {
- clearInterval(timer);
- that.setData({
- showCountdown: false,
- countdown: 0
- });
- } else {
- that.setData({
- countdown: seconds
- });
- }
- }, 1000);
- },
- logout: function() {
- // 清除登录状态
- wx.removeStorageSync('loginToken'); // 清除token
- this.setData({
- isLoggedIn: false,
- // 可能还需要清除其他与登录状态相关的数据
- });
- // 跳转到登录页面或其他页面
- wx.reLaunch({
- url: '/pages/login/login',
- });
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- // 清除登录状态
- // wx.removeStorageSync('loginToken'); // 清除token
- },
-
- })
|