在登录页面,先获取用户输入的用户名和密码。在点击登录的时候,先根据userName调数据库的密码和用户输入的密码是否相等。如果相等将用户的信息保存到全局变量中。
var app = getApp(); Page({ data: { bindName: '', bindPassword: '', isChecked: false, userName: '', phone: '', realName: '', card: '', email: '', userId: '' }, // 点击注册账号 registerTap: function() { wx.redirectTo({ url: '../register/register' }) }, // 获取用户名 bindNameInput: function(e) { this.setData({ bindName: e.detail.value }) var that = this; if (that.data.bindName.length !== 0 && that.data.bindPassword.length !== 0) { this.setData({ isChecked: true }) } else if (that.data.bindName.length === 0) { this.setData({ isChecked: false }) } }, // 获取密码 bindPasswordInput: function(e) { this.setData({ bindPassword: e.detail.value }) var that = this; if (that.data.bindName.length !== 0 && that.data.bindPassword.length !== 0) { this.setData({ isChecked: true }) } else if (that.data.bindPassword.length === 0) { this.setData({ isChecked: false }) } }, // 点击登录 bindingSuccess: function() { var that = this; var bindName = that.data.bindName; var bindPassword = that.data.bindPassword; if (bindName.length !== 0 && bindPassword.length !== 0) { // 初始化云 wx.cloud.init({ env: 'wubaib-9543f7', traceUser: true }); // 初始化数据库 const db = wx.cloud.database(); db.collection('userInformation').where({ userName: bindName }).get().then(res => { console.log(res.data); if (res.data[0].userPassword === bindPassword) { console.log("登录成功"); // 保存手机号,真实姓名,身份证号,邮箱 保存用户名 that.setData({ userName: res.data[0].userName, phone: res.data[0].phone, realName: res.data[0].realName, card: res.data[0].card, email: res.data[0].email, userId: res.data[0]._id }) app.appData.userinfo = { phone: that.data.phone, realName: that.data.realName, card: that.data.card, email: that.data.email } app.appData.account = { userName: that.data.userName } app.appData.userId = { userId: that.data.userId } wx.switchTab({ url: '../personalCenter/personalCenter', }) } else { wx.showToast({ title: '用户名或密码错误', icon: 'none', duration: 2000 }) } }) } }, })
登录WXML
<view> <input placeholder='用户名' maxlength='11' bindinput="bindNameInput"></input> </view> <view> <input placeholder='密码' password="true" bindinput="bindPasswordInput"></input> </view> <view bindtap='bindingSuccess'>立即登录</view> <view bindtap='registerTap'>注册账号</view>
注册第一步的WXML
<!--返回主页 --> <view bindtap='backHomeTap'> <image src='https://www.jb51.net/images/homeIcon.png'></image> </view> <!--头部 --> <view> <!--创建账户 --> <view> <view>1</view> <text>创建账户</text> </view> <!--完善个人信息 --> <view> <view>2</view> <text>完善个人信息</text> </view> <!--注册成功 --> <view> <view>3</view> <text>注册成功</text> </view> <view></view> <view></view> </view> <view> <!--用户名 --> <view> <view>用户名</view> <input placeholder='请输入数字,字母或中文' maxlength='25' bindinput='userNameInput'></input> </view> <!--密码 --> <view> <view>密码</view> <input placeholder='长度6~14位' password='true' maxlength='14' bindinput='userPasswordInput'></input> </view> <!--确认密码 --> <view> <view>确认密码</view> <input placeholder='请再次输入密码' password='true' maxlength='14' bindinput='userPasswordAgainInput'></input> </view> </view> <!--agree --> <view> <checkbox checked="{{check}}" bindtap="checkboxChange"/> <text>我已阅读并接受</text> <text>《用户注册条款》</text> </view> <!--nextButton --> <view bindtap='perfectInforTap'>下一步,完善个人信息</view> <!--binding --> <view> <text>已有账号</text> <text bindtap='bindingTap'>请绑定</text> </view>
注册第二步WXML