// pages/my/my.js var app=getApp(); Page({ /** * 页面的初始数据 */ data: { buttonText:'发送验证码', phone:"", code:"", avatar:"/images/头像.png", userInfo:null, countDown:60, timer:null, sendCodeDisabled:false, }, onPhoneInput(e){ this.setData({ phone:e.detail }); }, onCodeInput(e){ this.setData({ code:e.detail }); }, sendCode(){ if(this.data.sendCodeDisabled){ return; } if(this.data.phone.length<11){ wx.showToast({ title: '手机号长度错误', icon:"none", }); return; }; var reg=/^(1[3|4|5|6|7|8|9])\d{9}$/; if(!reg.test(this.data.phone)){ wx.showToast({ title: '手机号格式错误', icon:"none", }); return; } wx.request({ url: 'http://127.0.0.1:8000/api/message/', data: { phone:this.data.phone }, method: 'GET', success: (res) => { console.log(res); if(res.data.status){ this.setData({ sendCodeDisabled:true, timer:setInterval(this.hcountDown,1000) }); wx.showToast({ title: res.data.message, icon:"none", }); }else{ wx.showToast({ title: res.data.message, icon:"none", }); } }, }) }, tuichu(e){ app.delUserInfo(); this.setData({ userInfo:null }); }, denglu(e){ const pagePath = e.currentTarget.dataset.url; // 改为 data-url if (pagePath) { // 跳转到对应的页面 wx.navigateTo({ url: pagePath, }); } else { console.error('页面路径未定义'); }; }, hcountDown(){ var countDown=this.data.countDown; if(countDown===0){ clearInterval(this.data.timer); this.setData({ buttonText:'发送验证码', sendCodeDisabled:false, countDown:60 }); return; } this.setData({ buttonText:countDown+'s', countDown:countDown-1 }); }, login2(e){ wx.showLoading({ title: '登录中...', mask: true // 防止触摸穿透 }); wx.request({ url: 'http://127.0.0.1:8000/api/login/', data: { phone:this.data.phone, code:this.data.code }, method: 'POST', dataType:'json', success: (res) => { console.log(res); if(res.data.status){ console.log(res.data.data); console.log(e); this.setData({ userInfo:e.detail.userInfo }); app.initUserInfo(res.data.data,e.detail.userInfo) }else{ wx.showToast({ title:res.data.message, icon:"none", }); } }, }); wx.hideLoading(); }, denglu1(e) { const pagePath = e.currentTarget.dataset.url; if (app.globalData.userInfo && app.globalData.userInfo.token) { if (pagePath) { wx.navigateTo({ url: pagePath, }); } else { console.error('页面路径未定义'); } } else { wx.showToast({ title: '请先登录', icon: 'none' }); wx.navigateTo({ url: '/pages/my/my', }); } }, login(){ wx.request({ url: 'http://127.0.0.1:8000/api/login/', data: { phone:this.data.phone, code:this.data.code }, method: 'POST', dataType:'json', success: (res) => { console.log(res); if(res.data.status){ console.log(res.data.data); this.setData({ userInfo:res.data.data.token, id:res.data.data.id, }); }else{ wx.showToast({ title:res.data.message, icon:"none", }); } }, }) }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { var userInfo=app.globalData.userInfo this.setData({ userInfo:userInfo }); }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { clearInterval(this.data.timer); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })