From 91751b1b9b16c0044e16ad80c237b7275a409046 Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期四, 06 三月 2025 14:47:12 +0800
Subject: [PATCH] 修改首页和登录相关

---
 pages/login/login.js |  318 +++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 270 insertions(+), 48 deletions(-)

diff --git a/pages/login/login.js b/pages/login/login.js
index a957679..a4dd503 100644
--- a/pages/login/login.js
+++ b/pages/login/login.js
@@ -1,4 +1,8 @@
 const app = getApp();
+const storage = require('../../utils/storage.js');
+const {
+  post
+} = require('../../api/request.js');
 
 Page({
 
@@ -6,79 +10,217 @@
    * 椤甸潰鐨勫垵濮嬫暟鎹�
    */
   data: {
-    mobile: '',
-    code: '',
-    codeSent: false,
-    countdown: 60,
+    phone: '',
+    verificationCode: '',
+    codeText: '鑾峰彇楠岃瘉鐮�',
+    counting: false,
+    countDown: 60,
+    projectName: '鍢夊唱鍏抽」鐩�', // 榛樿椤圭洰鍚嶇О
+    selectedProject: 'JYG', // 榛樿椤圭洰浠g爜
+    showErrorDialog: false
   },
 
-  bindMobileInput: function (e) {
+  /**
+   * 缁戝畾鎵嬫満鍙疯緭鍏�
+   */
+  bindPhoneInput: function (e) {
     this.setData({
-      mobile: e.detail.value,
+      phone: e.detail.value
     });
   },
-  sendCode: function () {
-    if (!this.data.mobile) {
+
+  /**
+   * 缁戝畾楠岃瘉鐮佽緭鍏�
+   */
+  bindCodeInput: function (e) {
+    this.setData({
+      verificationCode: e.detail.value
+    });
+  },
+
+  /**
+   * 鍙戦�侀獙璇佺爜
+   */
+  sendVerificationCode: function () {
+    const {
+      phone,
+      counting
+    } = this.data;
+
+    // 濡傛灉姝e湪鍊掕鏃讹紝鍒欎笉鍏佽鍐嶆鍙戦��
+    if (counting) {
+      return;
+    }
+
+    // 楠岃瘉鎵嬫満鍙锋牸寮�
+    if (!/^1\d{10}$/.test(phone)) {
       wx.showToast({
-        title: '璇疯緭鍏ユ墜鏈哄彿',
-        icon: 'none',
-        duration: 2000,
+        title: '璇疯緭鍏ユ纭殑鎵嬫満鍙�',
+        icon: 'none'
       });
       return;
     }
 
-    // 鍦ㄨ繖閲屽鐞嗗彂閫侀獙璇佺爜鐨勯�昏緫锛屽彲浠ヨ皟鐢ㄥ悗鍙版帴鍙e疄鐜�
-    // 浠ヤ笅鏄竴涓畝鍗曠殑绀轰緥锛屼粎浣滃弬鑰�
-    wx.showToast({
-      title: '楠岃瘉鐮佸凡鍙戦��',
-      icon: 'success',
-      duration: 2000,
-    });
+    // 寮�濮嬪�掕鏃�
+    this.startCountDown();
 
-    this.setData({
-      codeSent: true,
-    });
-
-    // 鍚姩鍊掕鏃�
-    this.startCountdown();
+    // 鍙戦�侀獙璇佺爜璇锋眰
+    this.postCode();
   },
-  //鍊掕鏃�
-  startCountdown: function () {
-    let that = this;
-    let timer = setInterval(function () {
-      let countdown = that.data.countdown - 1;
-      that.setData({
-        countdown: countdown,
-      });
 
-      if (countdown <= 0) {
+  /**
+   * 寮�濮嬪�掕鏃�
+   */
+  startCountDown: function () {
+    this.setData({
+      counting: true,
+      countDown: 60
+    });
+
+    const timer = setInterval(() => {
+      if (this.data.countDown <= 1) {
         clearInterval(timer);
-        that.setData({
-          codeSent: false,
-          countdown: 60,
+        this.setData({
+          counting: false,
+          codeText: '鑾峰彇楠岃瘉鐮�'
+        });
+      } else {
+        this.setData({
+          countDown: this.data.countDown - 1,
+          codeText: `${this.data.countDown - 1}绉掑悗閲嶅彂`
         });
       }
     }, 1000);
+
+    // 淇濆瓨timer寮曠敤锛屼互渚垮湪椤甸潰鍗歌浇鏃舵竻闄�
+    this.countDownTimer = timer;
+  },
+
+  /**
+   * 鍋滄鍊掕鏃�
+   */
+  stopCountDown: function () {
+    if (this.countDownTimer) {
+      clearInterval(this.countDownTimer);
+    }
+    this.setData({
+      counting: false,
+      codeText: '鑾峰彇楠岃瘉鐮�'
+    });
   },
 
   /**
    * 鐧诲綍
    */
-  login: function (e) {
-    console.log("login")
-    let userName = "寮犱笁"
-    wx.navigateTo({
-      url: '/pages/wxbind/wxbind'
-    })
+  login: function () {
+    const {
+      phone,
+      verificationCode
+    } = this.data;
+
+    // 楠岃瘉鎵嬫満鍙峰拰楠岃瘉鐮�
+    if (!/^1\d{10}$/.test(phone)) {
+      wx.showToast({
+        title: '璇疯緭鍏ユ纭殑鎵嬫満鍙�',
+        icon: 'none'
+      });
+      return;
+    }
+
+    if (!/^\d{6}$/.test(verificationCode)) {
+      wx.showToast({
+        title: '璇疯緭鍏�6浣嶉獙璇佺爜',
+        icon: 'none'
+      });
+      return;
+    }
+
+    // 鏄剧ず鍔犺浇涓�
+    wx.showLoading({
+      title: '鐧诲綍涓�...',
+      mask: true
+    });
+    this.wsLogin();
+
+
+    // 鍙戦�佺櫥褰曡姹�
+    post('/api/loginByCode', {
+      phone: phone,
+      code: verificationCode,
+      projectCode: this.data.selectedProject
+    }).then(res => {
+      wx.hideLoading();
+
+      if (res.code === 0 && res.data) {
+        // 淇濆瓨鐢ㄦ埛淇℃伅鍜宼oken
+        storage.setUserInfo(res.data);
+        storage.setToken(res.data.token);
+
+        // 璺宠浆鍒伴椤�
+        wx.switchTab({
+          url: '/pages/index/index'
+        });
+      } else {
+        wx.showToast({
+          title: res.msg || '鐧诲綍澶辫触锛岃閲嶈瘯',
+          icon: 'none'
+        });
+      }
+    }).catch(err => {
+      wx.hideLoading();
+      console.error('鐧诲綍澶辫触', err);
+      wx.showToast({
+        title: '缃戠粶寮傚父锛岃閲嶈瘯',
+        icon: 'none'
+      });
+    });
   },
 
+  /**
+   * 璺宠浆鍒版敞鍐岄〉闈�
+   */
+  goToRegister: function () {
+    wx.showToast({
+      title: '娉ㄥ唽鍔熻兘鏆傛湭寮�鏀�',
+      icon: 'none',
+      duration: 2000
+    });
+  },
 
+  /**
+   * 璺宠浆鍒板繕璁板瘑鐮侀〉闈�
+   */
+  goToForgetPassword: function () {
+    wx.showToast({
+      title: '鎵惧洖瀵嗙爜鍔熻兘鏆傛湭寮�鏀�',
+      icon: 'none',
+      duration: 2000
+    });
+  },
 
   /**
    * 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰鍔犺浇
    */
-  onLoad(options) {
-
+  onLoad: function (options) {
+    // 鑾峰彇閫夋嫨鐨勯」鐩�
+    if (options.project) {
+      this.setData({
+        selectedProject: options.project,
+        projectName: options.project === 'JYG' ? '鍢夊唱鍏抽」鐩�' : '姘戝嫟椤圭洰'
+      });
+    } else {
+      // 灏濊瘯浠庢湰鍦板瓨鍌ㄨ幏鍙栧凡閫夋嫨鐨勯」鐩�
+      storage.getItem('selectedProject').then(project => {
+        if (project) {
+          this.setData({
+            selectedProject: project,
+            projectName: project === 'JYG' ? '鍢夊唱鍏抽」鐩�' : '姘戝嫟椤圭洰'
+          });
+        }
+      }).catch(err => {
+        console.error('鑾峰彇宸查�夋嫨椤圭洰澶辫触:', err);
+      });
+    }
   },
 
   /**
@@ -106,7 +248,11 @@
    * 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰鍗歌浇
    */
   onUnload() {
-
+    // 娓呴櫎楠岃瘉鐮佸�掕鏃跺畾鏃跺櫒
+    if (this.countDownTimer) {
+      clearInterval(this.countDownTimer);
+      this.countDownTimer = null;
+    }
   },
 
   /**
@@ -129,7 +275,83 @@
   onShareAppMessage() {
 
   },
-
-
-
+  //鑾峰彇楠岃瘉鐮�
+  postCode: function () {
+    const params = {
+      url: 'wx/client/send_sms?phoneNumber=' + this.data.phone
+    };
+    post(params)
+      .then((data) => {
+        wx.showToast({
+          title: '楠岃瘉鐮佸凡鍙戦��',
+          icon: 'success',
+          duration: 2000,
+        });
+        this.setData({
+          codeSent: true,
+        });
+        // 鍚姩鍊掕鏃�
+        this.startCountdown();
+      })
+      .catch((error) => {
+        wx.showToast({
+          title: error.msg,
+          icon: 'none'
+        });
+        console.error('Failed to add item:', error);
+      });
+  },
+  wsLogin() {
+    wx.login({
+      success: res => {
+        if (res.code) {
+          console.log('鐧诲綍鎴愬姛锛岃幏鍙栧埌鐨刢ode:', res.code);
+          // 鍙戦�� res.code 鍒板悗鍙版湇鍔″櫒鎹㈠彇 openId, sessionKey, unionId
+          this.verify(res.code)
+        } else {
+          console.log('鐧诲綍澶辫触锛�' + res.errMsg);
+        }
+      }
+    });
+  },
+  //鐢ㄦ埛缁戝畾
+  verify(wxCode) {
+    const params = {
+      url: 'wx/client/verify',
+      data: {
+        phoneNumber: this.data.phone,
+        securityCode: this.data.verificationCode,
+        code: wxCode
+      }
+    };
+    post(params)
+      .then((data) => {
+        wx.hideLoading();
+        getApp().globalData.sessionId = String(data.content.sessionId)
+        storage.setItem("sessionId", String(data.content.sessionId))
+        getApp().globalData.clientId = String(data.content.clientId)
+        storage.setItem("clientId", String(data.content.clientId))
+        this.bindSuccess();
+      })
+      .catch((error) => {
+        wx.hideLoading();
+        wx.showToast({
+          title: error.msg,
+          icon: 'error',
+          duration: 3000,
+        });
+        console.error('Failed to add item:', error);
+      });
+  },
+  bindSuccess: function () {
+    if (!this.data.isButtonEnabled) return;
+    wx.showToast({
+      title: '缁戝畾鎴愬姛',
+      icon: 'success'
+    });
+    // 璺宠浆鍒� TabBar 椤甸潰
+    wx.redirectTo({
+      url: '/pages/home/home' // 杩欓噷濉啓浣犳兂瑕佽烦杞殑 TabBar 椤甸潰璺緞
+    });
+  },
 })
\ No newline at end of file

--
Gitblit v1.8.0