123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- // 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.value || e.detail
- });
- },
- onCodeInput(e) {
- this.setData({
- code: e.detail.value || 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: app.globalData.apiBaseUrl || 'http://127.0.0.1:8000' + '/api/message/',
- data: { phone: this.data.phone },
- method: 'GET',
- success: (res) => {
- if (res.data && 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 && res.data.message) || '验证码发送失败',
- icon: "none",
- });
- }
- },
- fail: (err) => {
- wx.showToast({
- title: '网络错误,请重试',
- icon: 'none'
- });
- }
- });
- },
- tuichu(e) {
- wx.showModal({
- title: '提示',
- content: '确定要退出登录吗?',
- success: (res) => {
- if (res.confirm) {
- // 清除所有相关存储
- wx.removeStorageSync('token');
- wx.removeStorageSync('userInfo');
-
- // 重置全局数据
- app.globalData.token = null;
- app.globalData.userInfo = null;
-
- // 更新页面数据
- this.setData({
- userInfo: null,
- phone: "",
- code: ""
- });
-
- // 可选:跳转到登录页面
- wx.navigateTo({ url: '/pages/login/login' });
- }
- }
- });
- },
- denglu(e) {
- const pagePath = e.currentTarget.dataset.url;
- if (pagePath) {
- wx.navigateTo({ url: pagePath });
- } else {
- console.error('页面路径未定义');
- }
- },
- hcountDown() {
- var countDown = this.data.countDown - 1;
- if (countDown <= 0) {
- clearInterval(this.data.timer);
- this.setData({
- buttonText: '发送验证码',
- sendCodeDisabled: false,
- countDown: 60
- });
- return;
- }
- this.setData({
- buttonText: countDown + 's',
- countDown: countDown
- });
- },
- // 在login2方法中添加token验证
- // 确保登录成功后正确存储token
- login2(e) {
- wx.request({
- url: app.globalData.apiBaseUrl + '/api/login/',
- success: (res) => {
- if (res.data.status && res.data.data.token) {
- // 统一存储到storage和globalData
- wx.setStorageSync('token', res.data.data.token);
- app.globalData.token = res.data.data.token;
-
- // 测试token是否存储成功
- setTimeout(() => {
- console.log('Stored token:', wx.getStorageSync('token'));
- }, 500);
- }
- }
- });
- },
- denglu1(e) {
- const pagePath = e.currentTarget.dataset.url;
- const userInfo = app.globalData.userInfo || wx.getStorageSync('userInfo');
-
- if (userInfo && userInfo.token) {
- pagePath && wx.navigateTo({ url: pagePath });
- } else {
- wx.showToast({ title: '请先登录', icon: 'none' });
- wx.navigateTo({ url: '/pages/my/my' });
- }
- },
- login() {
- if (!this.data.phone || !this.data.code) {
- wx.showToast({ title: '请输入手机号和验证码', icon: 'none' });
- return;
- }
-
- wx.request({
- url: (app.globalData.apiBaseUrl || 'http://127.0.0.1:8000') + '/api/login/',
- method: 'POST',
- header: { 'Content-Type': 'application/json' },
- data: { phone: this.data.phone, code: this.data.code },
- success: (res) => {
- if (res.data && res.data.status) {
- const userData = res.data.data;
- const userInfo = {
- token: userData.token,
- id: userData.id,
- nickName: userData.nickName || '用户' + userData.id,
- avatarUrl: userData.avatarUrl || '/images/头像.png'
- };
-
- // 存储到storage和globalData
- wx.setStorageSync('token', userData.token);
- wx.setStorageSync('userInfo', userInfo);
- app.globalData.token = userData.token;
- app.globalData.userInfo = userInfo;
-
- this.setData({
- userInfo: userInfo,
- phone: "",
- code: ""
- });
-
- wx.showToast({
- title: '登录成功',
- icon: 'success'
- });
- } else {
- wx.showToast({
- title: (res.data && res.data.message) || '登录失败',
- icon: "none",
- });
- }
- },
- fail: (err) => {
- wx.showToast({ title: '网络错误', icon: 'none' });
- }
- });
- },
- onShow() {
- // 从storage中读取而不是globalData,因为globalData可能在页面刷新时丢失
- const token = wx.getStorageSync('token');
- const userInfo = wx.getStorageSync('userInfo');
-
- if (token && userInfo) {
- // 确保globalData同步
- app.globalData.token = token;
- app.globalData.userInfo = userInfo;
- this.setData({ userInfo: userInfo });
- } else {
- // 确保所有状态已清除
- this.setData({ userInfo: null });
- app.globalData.token = null;
- app.globalData.userInfo = null;
- }
- },
- onUnload() {
- clearInterval(this.data.timer);
- },
- });
|