一、新建頁面
新建頁面loadByAccount
二、代碼實現
1、loadByAccount.wxml
1 <view>賬號</view> 2 <input type="text" placeholder="請輸入賬號" bindinput="getAccount"></input> 3 <view>密碼</view> 4 <input type="text" placeholder="請輸入密碼" bindinput="getPassword"></input> 5 <view class="btn"> 6 <button type="primary" bindtap="loadByAccount">登錄</button> 7 </view> 8 <view class="tosign"> 9 <view bindtap="toSign" >注冊</view> 10 </view>
2、loadByAccount.wxss
1 input{ 2 margin: 20rpx; 3 padding-left: 10rpx; 4 height: 80rpx; 5 border: 1rpx solid #c3c3c3; 6 } 7 view{ 8 margin: 20rpx; 9 } 10 .btn{ 11 display: flex; 12 margin-top: 50rpx; 13 } 14 .tosign{ 15 margin-top: 50rpx; 16 text-align: center; 17 }
3、loadByAccount.js
1 //自定義變量,存儲用戶輸入的賬號 2 let account = '' 3 //自定義變量,存儲用戶輸入的密碼 4 let password = '' 5 Page({ 6 7 //點擊跳轉到注冊頁 8 toSign(){ 9 wx.navigateTo({ 10 url: '/pages/sign/sign', 11 }) 12 }, 13 14 //獲取用戶輸入的賬號、密碼 15 getAccount(e){ 16 console.log("用戶輸入的賬號",e.detail.value); 17 account = e.detail.value 18 }, 19 getPassword(e){ 20 console.log("用戶輸入的密碼",e.detail.value); 21 password = e.detail.value 22 }, 23 24 //登錄功能 25 loadByAccount(){ 26 //校驗賬號 27 if(account.length<4){ 28 wx.showToast({ 29 title: '賬號至少4位', 30 icon:"none" 31 }) 32 return 33 } 34 //登錄功能的實現 35 wx.cloud.database().collection("users") 36 .where({ 37 Account:account 38 }) 39 .get({}) 40 .then(res=>{ 41 console.log("獲取賬號成功",res); 42 //校驗密碼長度 43 if(password.length<4){ 44 wx.showToast({ 45 title: '密碼至少4位', 46 icon:"none" 47 }) 48 return 49 } 50 //校驗密碼是否等於數據庫中的密碼 51 if(password==res.data[0].Password){ 52 console.log("登錄成功",res); 53 //顯示登錄成功提示 54 wx.showToast({ 55 title: '登錄成功', 56 icon:"success", 57 duration:2000, 58 //提示2秒后自動跳轉到首頁 59 success:function(){ 60 setTimeout(function(){ 61 wx.switchTab({ 62 url: '/pages/index/index', 63 }) 64 },2000) 65 } 66 }) 67 }else{ 68 console.log("密碼不正確,登錄失敗"); 69 wx.showToast({ 70 title: '密碼不正確', 71 icon:"none" 72 }) 73 } 74 }) 75 .catch(err=>{ 76 console.log("獲取賬號失敗",err); 77 wx.showToast({ 78 title: '賬號不存在', 79 icon:"none" 80 }) 81 }) 82 }, 83 })
三、效果展示