From 2c08a98f7e85ec7c9376f27a7933e75e44d672f7 Mon Sep 17 00:00:00 2001
From: liukangdong <898885815@qq.com>
Date: 星期二, 27 八月 2024 15:52:59 +0800
Subject: [PATCH] ll

---
 wechat_staff/pages/login/index.js |  217 ++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 179 insertions(+), 38 deletions(-)

diff --git a/wechat_staff/pages/login/index.js b/wechat_staff/pages/login/index.js
index 1371551..d7b0acd 100644
--- a/wechat_staff/pages/login/index.js
+++ b/wechat_staff/pages/login/index.js
@@ -1,4 +1,11 @@
-import { sendSms,loginByPhone, getDictData } from '../../api/index'
+import {
+  sendSms,
+  getDictData,
+  getWxMiniPhone,
+  loginByPhone,
+  bindingOpenid,
+  wxLoginCustomer
+} from '../../api/index'
 Page({
 
   /**
@@ -10,15 +17,18 @@
     status: '0', // 鏍囪瘑
     countDown: 0,
     phone: '',
-    code: '', 
+    openid:'',
+    code: '',
     showModal: false,
     activeHtml: '',
   },
   getCode() {
     let countDown = this.data.countDown
-    if(countDown > 0) return
-    const { phone } = this.data
-    if(!phone || phone.length != 11){
+    if (countDown > 0) return
+    const {
+      phone
+    } = this.data
+    if (!phone || phone.length != 11) {
       return wx.showToast({
         title: '璇疯緭鍏ユ纭殑鎵嬫満鍙�',
         icon: 'none'
@@ -31,39 +41,90 @@
         title: '楠岃瘉鐮佸彂閫佹垚鍔�',
         icon: 'none'
       })
-      this.setData({ countDown: 60 })
+      this.setData({
+        countDown: 60
+      })
       let timer = setInterval(() => {
         let countDown = this.data.countDown
-        if(countDown === 0){
+        if (countDown === 0) {
           return clearInterval(timer)
         }
-        countDown --
-        this.setData({ countDown })
+        countDown--
+        this.setData({
+          countDown
+        })
       }, 1000)
     })
   },
-  loginPhone() {
-    const { code, phone } = this.data
-    loginByPhone({code, phone}).then(res => {
-      wx.setStorageSync('member', res.data.users)
-      wx.setStorageSync('token', res.data.token)
-      wx.showToast({
-        title: '鐧诲綍鎴愬姛',
-      })
-      setTimeout(() => {
-        wx.switchTab({
-          url: '/pages/index/index',
-        })
-      }, 300)
-    })
+  onShow(){
+    wx.hideHomeButton()
   },
-  changeStatus() {
-    if(!this.data.agreementFalg){
+  handleJudge(e) {
+    const flag = e.currentTarget.dataset.flag
+    console.log(e);
+    this.setData({ agreementFalg: flag, showModal: false })
+  },
+  loginPhone() {
+    const {
+      code,
+      phone,
+      openid
+    } = this.data
+    if (!this.data.agreementFalg) {
       return wx.showToast({
         title: '璇峰厛闃呰骞跺悓鎰忕浉鍏冲崗璁�',
         icon: 'none'
       })
     }
+    if (phone.length != 11) {
+      return wx.showToast({
+        title: '璇疯緭鍏ユ纭殑鎵嬫満鍙�',
+        icon: 'none'
+      })
+    }
+    if (!code) {
+      return wx.showToast({
+        title: '璇疯緭鍏ラ獙璇佺爜',
+        icon: 'none'
+      })
+    }
+
+    loginByPhone({
+      code,
+      openid,
+      phone
+    }).then(res => {
+      if (res.data && res.data.token) {
+        // wx.login({
+        //   timeout: 5000,
+        //   success: (loginRes) =>  {
+        //     if (loginRes.code) {
+        //       this.bindingOpenid(loginRes.code, res.data.token)
+        //     }
+        //   }
+        // })
+        if(res.data.phone){
+          res.data.users.phone = res.data.phone || res.data.users.phone
+        }
+        wx.setStorageSync('member', res.data.users)
+        wx.setStorageSync('token', res.data.token)
+        wx.showToast({
+          title: '鐧诲綍鎴愬姛',
+        })
+        setTimeout(() => {
+          wx.switchTab({
+            url: '/pages/index/index',
+          })
+        }, 300)
+      }else{
+        wx.showToast({
+          title: '浣犺繕涓嶆槸璇ョ郴缁熺敤鎴凤紝璇疯仈绯荤浉鍏崇鐞嗗憳娉ㄥ唽',
+          icon: 'none'
+        })
+      }
+    })
+  },
+  changeStatus() {
     this.setData({
       status: '1'
     })
@@ -76,13 +137,98 @@
   onLoad(options) {
     var app = getApp().globalData
     this.setData({
-      primary: app.primary
+      primary: app.primary,
+      bottomLift: app.bottomLift
     })
+    this.autoLogin()
     this.initData()
   },
-  onOpen(e){
-    const { serviceHtml, privacyHtml } = this.data
-    const { index } = e.target.dataset
+  autoLogin(){
+    var that =this
+    wx.login({
+      timeout: 5000,
+      success(res) {
+        if (res.code) {
+          //鍙戣捣缃戠粶璇锋眰
+          wxLoginCustomer({
+            code: res.code
+          }).then(res => {
+            console.log(res)
+            wx.setStorageSync('sessionKey', res.data.sessionKey)
+            that.setData({openid:res.data.openid})
+            if (res && res.data !=null && res.data.token!=null) { 
+              console.log(res)
+              wx.hideLoading()
+              wx.setStorageSync('member', res.data.users || '')
+              wx.setStorageSync('token', res.data.token || '')
+              wx.switchTab({
+                url: '/pages/index/index',
+              }) 
+            } 
+          })
+        } else {
+          wx.reLaunch({
+            url: '/pages/login/index',
+          })
+          wx.hideLoading()
+          console.log('鐧诲綍澶辫触锛�' + res)
+        }
+      },
+      fail(err) {
+        wx.hideLoading()
+        console.log(err)
+      }
+    })
+  },
+  getPhoneNumber(e) {
+    const data = {
+      ...e.detail
+    }
+    getWxMiniPhone({
+      encryptedData: data.encryptedData,
+      iv: data.iv,
+      openid:this.data.openid,
+      sessionKey: wx.getStorageSync('sessionKey'),
+    }).then(res => {
+      if (res.data && res.data.token) { 
+        if(res.data.phone){
+          res.data.users.phone = res.data.phone || res.data.users.phone
+        }
+        wx.setStorageSync('member', res.data.users)
+        wx.setStorageSync('token', res.data.token)
+        wx.switchTab({
+          url: '/pages/index/index',
+        })
+      }else{
+        wx.showToast({
+          title: '浣犺繕涓嶆槸璇ョ郴缁熺敤鎴凤紝璇疯仈绯荤浉鍏崇鐞嗗憳娉ㄥ唽',
+          icon: 'none'
+        })
+      }
+
+    })
+  },
+  bindingOpenid(code, token) {
+    bindingOpenid({code, token})
+  },
+  loginIn() {
+    const {
+      agreementFalg
+    } = this.data
+    if (!agreementFalg) return wx.showToast({
+      title: '璇峰厛闃呰骞跺悓鎰忕浉鍏冲崗璁�',
+      icon: 'none',
+      duration: 2000
+    })
+  },
+  onOpen(e) {
+    const {
+      serviceHtml,
+      privacyHtml
+    } = this.data
+    const {
+      index
+    } = e.target.dataset
     let activeHtml = index == '0' ? serviceHtml : privacyHtml
     this.setData({
       activeHtml,
@@ -90,9 +236,11 @@
     })
   },
   onClose() {
-    this.setData({showModal: false})
+    this.setData({
+      showModal: false
+    })
   },
-  initData(){
+  initData() {
     getDictData({
       code: 'ZBOM_CUSTOMIZED',
       label: 'SERVER_AGREEMENT'
@@ -115,13 +263,6 @@
    * 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰鍒濇娓叉煋瀹屾垚
    */
   onReady() {
-
-  },
-
-  /**
-   * 鐢熷懡鍛ㄦ湡鍑芥暟--鐩戝惉椤甸潰鏄剧ず
-   */
-  onShow() {
 
   },
 

--
Gitblit v1.9.3