今天仔细看了看代码,觉得还有一些地方不太完善,需要给用户提供解绑账号的方式,同时查询用户注册信息只需要openid即可,账号密码已经验证过了,所以这里在验证用户账号与绑定账号信息不相符合时,还需要提供注销方式,这里采用wx.showModal弹出复选框,wx.showModal 与 wx.showToast可以同时显示,注意showModal显示前hidetoast.
需要设计新的注销界面,注销需要验证账号以及密码,成功后实现注销,同时注销界面也需要提供返回界面,防止用户误点击出不去。这个使用navigateto navigateback即可
1.修改之前的云函数
由于逻辑是先判断uid psw 是否符合,再判断openid 与uid 是否匹配,所以修改前面的云函数,只判断openid有无注册,不需要再添加uid pwd
注释uid pwd 即可
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
return await db.collection('test_bind').where({
openid : event.openid,
// uid : event.uid,
// pwd : event.pwd
}).get()
}
2. 构建新的注册按钮逻辑
账号密码符合数据库时,开始判断是否绑定openid,未绑定则添加绑定,绑定则分以下两种情况:
1. 绑定与输入用户一致,直接登陆
2. 绑定与输入用户不一致,提示是否需要解绑账号
中间需要注意this.setdata that.setdata (由于小程序的异步处理问题)
如:调取
this.data.rectifyuid 要加data,后面的前面定义的部分
submitInfo: function(e){
wx.showToast({
title: "登录中...",
icon: "loading",
duration: 10000
})
var that = this;
var uid = e.detail.value.uid;
var pwd = e.detail.value.pwd;
// var vcode = e.detail.value.vcode;
if ((uid.length == 0 || pwd.length == 0)) {
wx.showToast({
title: '输入有误',
image: '/images/info.png',
icon: 'none',
duration: 1000
});
} else {
collection.where({
uid : uid,
pwd : pwd
}).get({
success: res => {
if(res.data.length == 0 ){
// console.log(res.data)
wx.showToast({
icon: 'none',
title: '账号或密码错误'
})
}else{
console.log('[数据库] [查询记录] 成功: ', res.data[0]['uid'])
//云函数返回test_bind 中是否已有绑定信息
wx.cloud.callFunction({
name:'isRegistered',
data:{
openid : app.globalData.openid,
},
complete: res => {
let length = res.result.data.length
console.log('[返回数据长度]length',length)
//回调中处理数据解决异步问题
if(length == 0){
//没绑定openid时添加绑定信息
//云函数绑定openid
wx.cloud.callFunction({
name: 'bindOpenid',
data: {
openid : app.globalData.openid,
uid : uid,
pwd : pwd
},
complete: res => {
console.log('callFunction test result: ', res)
}
})
//保存全局数据,跳转页面
app.globalData.uid = uid;
app.globalData.newpwd = pwd;
//设置本地Storage,维持登录态用
wx.setStorageSync('uid', uid);
wx.setStorageSync('newpwd', pwd);
wx.navigateTo({
//没这方面数据,瞎填一手
url: '/pages/welcome/welcome?uid=' + uid + '&name=' + 'XXX' + '&classroom=' + 'AI班',
})
}else{
//已绑定用户
that.setData({
rectifyuid : res.result.data[0]['uid']
})
console.log('[rectify_uid]: ', this.data.rectifyuid)
if(length == 1 && uid == this.data.rectifyuid){
//绑定用于与输入uid相同,直接进入
//提示showModal时隐藏toast
wx.hideToast({
success: (res) => {},
})
//保存全局数据,跳转页面
app.globalData.uid = uid;
app.globalData.newpwd = pwd;
//设置本地Storage,维持登录态用
wx.setStorageSync('uid', uid);
wx.setStorageSync('newpwd', pwd);
wx.navigateTo({
//没这方面数据,瞎填一手
url: '/pages/welcome/welcome?uid=' + uid + '&name=' + 'XXX' + '&classroom=' + 'AI班',
})
}else{
//绑定的用户不是当前登陆账号
//提示showModal时隐藏toast
wx.hideToast({
success: (res) => {},
})
wx.showModal({
title: '警告',
content: '您已绑定其他账号,是否解绑账号',
success: function (res) {
if (res.confirm) {//这里是点击了确定以后
console.log('用户点击确定,解绑账号')
wx.navigateTo({
url: '/pages/index/unbind',
})
} else {//这里是点击了取消以后
console.log('用户点击取消,不解绑账号')
}
}
})
}
}
}
})
}
},
fail: err => {
wx.showToast({
icon: 'none',
title: '查询记录失败'
})
console.error('[数据库] [查询记录] 失败:', err)
}
})
}
},
3. 增加新的删除云函数
注意不同云函数返回的res data 结构不同,利用console.log 打印出来观察处理
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
//反正只创建了一个环境,直接不设置env
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
//暂时设置为test_bind
return await db.collection('test_bind').where({
openid : event.openid,
uid : event.uid,
pwd : event.pwd
}).remove()
}
4. 构建新的用于解除绑定的界面,
在原来注册见面上改一下就行,利用wx.showModel 提供跳转的对话框,使用navigate跳转及返回
添加了hide ,当学号长度超过12时自动隐藏键盘控制长度
加入 focus ,高亮选中对话框
5. 最终效果
解绑成功后自动返回注册界面,同时原来的想要绑定的新账户仍然在该处,直接绑定即可