小程序云開發(fā)之用戶注冊登錄
本文實(shí)例為大家分享了小程序云開發(fā)用戶注冊登錄的具體代碼,供大家參考,具體內(nèi)容如下


注冊界面和文件


登錄界面和文件


這里的UI使用iviewUI 不懂可以看我的另一篇文章IviewUI
先說注冊界面
json如下

wxml如下
<!--pages/register/index.wxml--> <view> <i-input bind:change='inputName' maxlength="15" title="賬號(hào)" autofocus placeholder="請輸入賬號(hào)" /> <i-input bind:change='inputPassword' maxlength="15" title="密碼" autofocus placeholder="請輸入密碼" /> <i-button bindtap='register' type="success">注冊</i-button> </view>
js頁面
// pages/register/index.js
let app = getApp();
//獲取云數(shù)據(jù)庫引用
const db = wx.cloud.database();
const admin = db.collection('adminlist');
let name = null;
let password = null;
Page({
data: {
},
//輸入用戶名
inputName:function(event){
name = event.detail.detail.value
},
//輸入密碼
inputPassword(event){
password = event.detail.detail.value
},
// .where({
// _openid: app.globalData.openid // 填入當(dāng)前用戶 openid
// })
// wx.showModal({
// title: '提示',
// content: '您已注冊,確定要更新賬號(hào)密碼嗎?',
// success: function (res) {
// if (res.confirm) {
// console.log('用戶點(diǎn)擊確定')
// that.saveuserinfo();
// }
// }
// })
//注冊
register(){
let that = this;
let flag = false //是否存在 true為存在
//查詢用戶是否已經(jīng)注冊
admin.get({
success:(res)=> {
let admins = res.data; //獲取到的對象數(shù)組數(shù)據(jù)
// console.log(admins);
for (let i=0; i<admins.length; i++){ //遍歷數(shù)據(jù)庫對象集合
if (name === admins[i].name){ //用戶名存在
flag = true;
// break;
}
}
if(flag === true){ //已注冊
wx.showToast({
title: '賬號(hào)已注冊!',
icon: 'success',
duration: 2500
})
}else{ //未注冊
that.saveuserinfo()
}
}
})
},
//注冊用戶信息
saveuserinfo() {
// let that = this;
admin.add({ //添加數(shù)據(jù)
data:{
name:name,
password: password
}
}).then(res => {
console.log('注冊成功!')
wx.showToast({
title: '注冊成功!',
icon: 'success',
duration: 3000
})
wx.redirectTo({
url: '/pages/login/login',
})
})
},
})
因?yàn)槭褂迷崎_發(fā)數(shù)據(jù)庫所以先在app.js中初始化加入下面這段代碼
下面的fighting1323797232-e05624就是我們云開發(fā)的環(huán)境id
wx.cloud.init({
env: 'fighting'1323797232-e05624',
traceUser: true
})

環(huán)境ID在這里

這里需要進(jìn)云數(shù)據(jù)庫創(chuàng)建一個(gè)adminlist集合

注冊成功后,開始實(shí)現(xiàn)登陸功能
login.wxml
<!--pages/login/login.wxml--> <view> <i-input bind:change='inputName' maxlength="15" title="賬號(hào)" placeholder="請輸入賬號(hào)" /> <i-input bind:change='inputPassword' maxlength="15" title="密碼" placeholder="請輸入密碼" /> <i-button bindtap='login' type="primary">登錄</i-button> <i-button bindtap='register' type="success">注冊</i-button> </view>
json和以上注冊的json一樣
js邏輯頁面實(shí)現(xiàn)如下:
// pages/login/login.js
let app = getApp();
// 獲取云數(shù)據(jù)庫引用
const db = wx.cloud.database();
const admin = db.collection('adminlist');
let name = null;
let password = null;
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
},
//輸入用戶名
inputName: function (event) {
name = event.detail.detail.value
},
//輸入密碼
inputPassword(event) {
password = event.detail.detail.value
},
//登陸
login(){
let that = this;
//登陸獲取用戶信息
admin.get({
success:(res)=>{
let user = res.data;
// console.log(res.data);
for (let i = 0; i < user.length; i++) { //遍歷數(shù)據(jù)庫對象集合
if (name === user[i].name) { //用戶名存在
if (password !== user[i].password) { //判斷密碼是否正確
wx.showToast({
title: '密碼錯(cuò)誤?。?,
icon: 'success',
duration: 2500
})
} else {
console.log('登陸成功!')
wx.showToast({
title: '登陸成功?。?,
icon: 'success',
duration: 2500
})
wx.switchTab({ //跳轉(zhuǎn)首頁
url: '/pages/shopcart/shopcart', //這里的URL是你登錄完成后跳轉(zhuǎn)的界面
})
}
}else{ //不存在
wx.showToast({
title: '無此用戶名?。?,
icon: 'success',
duration: 2500
})
}
}
}
})
},
register(){
wx.navigateTo({
url: '/pages/register/index'
})
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成
* 頁面初次渲染完成時(shí)觸發(fā)。一個(gè)頁面只會(huì)調(diào)用一次,代表頁面已經(jīng)準(zhǔn)備妥當(dāng),可以和視圖層進(jìn)行交互
*/
onReady: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面顯示
* 頁面顯示/切入前臺(tái)時(shí)觸發(fā)
*/
onShow: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面隱藏
*/
onHide: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面卸載
*/
onUnload: function () {
},
/**
* 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動(dòng)作
*/
onPullDownRefresh: function () {
},
/**
* 頁面上拉觸底事件的處理函數(shù)
*/
onReachBottom: function () {
},
/**
* 用戶點(diǎn)擊右上角分享
*/
onShareAppMessage: function () {
}
})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Layui帶搜索的下拉框的使用以及動(dòng)態(tài)數(shù)據(jù)綁定方法
今天小編就為大家分享一篇Layui帶搜索的下拉框的使用以及動(dòng)態(tài)數(shù)據(jù)綁定方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
JavaScript+HTML?實(shí)現(xiàn)網(wǎng)頁錄制音頻與下載
在這個(gè)數(shù)字化的時(shí)代,網(wǎng)頁端的音頻處理能力已經(jīng)成為一個(gè)非常熱門的需求,本文將詳細(xì)介紹如何利用 getUserMedia 和 MediaRecorder 這兩個(gè)強(qiáng)大的 API,實(shí)現(xiàn)網(wǎng)頁端音頻的錄制、處理和播放等功能,需要的朋友可以參考下2024-07-07
JS OOP包機(jī)制,類創(chuàng)建的方法定義
JS OOP包機(jī)制,類創(chuàng)建的方法定義,需要的朋友可以參考下。2009-11-11
js實(shí)現(xiàn)簡單的日歷顯示效果函數(shù)示例
這篇文章主要介紹了js實(shí)現(xiàn)簡單的日歷顯示效果函數(shù),結(jié)合完整實(shí)例形式分析了JavaScript實(shí)現(xiàn)的日歷功能相關(guān)原理與具體操作技巧,需要的朋友可以參考下2019-11-11
微信小程序?qū)崿F(xiàn)錄音時(shí)的麥克風(fēng)動(dòng)畫效果實(shí)例
這篇文章主要給大家介紹了關(guān)于微信小程序?qū)崿F(xiàn)錄音時(shí)的麥克風(fēng)動(dòng)畫效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用微信小程序具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
如何判斷微信內(nèi)置瀏覽器(通過User Agent實(shí)現(xiàn))
在進(jìn)行微信公眾賬號(hào)開發(fā)的時(shí)候,需要知道當(dāng)前的瀏覽器是微信內(nèi)置的瀏覽器,那么如何判斷呢?這是就只有通過瀏覽器的User Agent來進(jìn)行判斷了2014-09-09

