小程序云開發(fā)之用戶注冊登錄

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

創(chuàng)新互聯(lián)專業(yè)成都網(wǎng)站制作、成都網(wǎng)站建設(shè),集網(wǎng)站策劃、網(wǎng)站設(shè)計、網(wǎng)站制作于一體,網(wǎng)站seo、網(wǎng)站優(yōu)化、網(wǎng)站營銷、軟文平臺等專業(yè)人才根據(jù)搜索規(guī)律編程設(shè)計,讓網(wǎng)站在運行后,在搜索中有好的表現(xiàn),專業(yè)設(shè)計制作為您帶來效益的網(wǎng)站!讓網(wǎng)站建設(shè)為您創(chuàng)造效益。

小程序云開發(fā)之用戶注冊登錄
小程序云開發(fā)之用戶注冊登錄

注冊界面和文件

小程序云開發(fā)之用戶注冊登錄
小程序云開發(fā)之用戶注冊登錄

登錄界面和文件

小程序云開發(fā)之用戶注冊登錄
小程序云開發(fā)之用戶注冊登錄

這里的UI使用iviewUI 不懂可以看我的另一篇文章IviewUI

先說注冊界面
json如下

小程序云開發(fā)之用戶注冊登錄

wxml如下

<!--pages/register/index.wxml-->
<view>
<i-input bind:change='inputName' maxlength="15" title="賬號" autofocus placeholder="請輸入賬號" />
<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: '您已注冊,確定要更新賬號密碼嗎?',
 // success: function (res) {
 // if (res.confirm) {
 // console.log('用戶點擊確定')
 // 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: '賬號已注冊!',
 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',
 })
 })
 },
})

因為使用云開發(fā)數(shù)據(jù)庫所以先在app.js中初始化加入下面這段代碼

下面的fighting1323797232-e05624就是我們云開發(fā)的環(huán)境id

wx.cloud.init({
 env: 'fighting'1323797232-e05624',
 traceUser: true
 })

小程序云開發(fā)之用戶注冊登錄

環(huán)境ID在這里

小程序云開發(fā)之用戶注冊登錄

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

小程序云開發(fā)之用戶注冊登錄

注冊成功后,開始實現(xiàn)登陸功能

login.wxml

<!--pages/login/login.wxml-->
<view>
<i-input bind:change='inputName' maxlength="15" title="賬號" placeholder="請輸入賬號" />
<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邏輯頁面實現(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: '密碼錯誤??!',
 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)聽頁面初次渲染完成
 * 頁面初次渲染完成時觸發(fā)。一個頁面只會調(diào)用一次,代表頁面已經(jīng)準(zhǔn)備妥當(dāng),可以和視圖層進行交互
 */
 onReady: function () {

 },

 /**
 * 生命周期函數(shù)--監(jiān)聽頁面顯示
 * 頁面顯示/切入前臺時觸發(fā)
 */
 onShow: function () {

 },

 /**
 * 生命周期函數(shù)--監(jiān)聽頁面隱藏
 */
 onHide: function () {

 },

 /**
 * 生命周期函數(shù)--監(jiān)聽頁面卸載
 */
 onUnload: function () {

 },

 /**
 * 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動作
 */
 onPullDownRefresh: function () {

 },

 /**
 * 頁面上拉觸底事件的處理函數(shù)
 */
 onReachBottom: function () {

 },

 /**
 * 用戶點擊右上角分享
 */
 onShareAppMessage: function () {

 }
})

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

分享標(biāo)題:小程序云開發(fā)之用戶注冊登錄
分享地址:http://bm7419.com/article10/iidcgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、App開發(fā)、標(biāo)簽優(yōu)化、域名注冊、網(wǎng)站改版、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司