今天差不多算是理清了云开发,把登陆注册部分捋出来了
用了两个集合(文档型数据库),其中一个为昨天说的jsonLine格式批量导入原始的学号以及默认密码。
另外一个使用的是bind,即将用户针对该App的openid 存储起来,与学号密码进行绑定,不充足的地方是没有进行加密处理。
根据判断用户是否绑定用户决定是否向数据库修改,因为文档型数据库有点弄不懂主码,去重这部分概念,所以使用这种逻辑进行处理。
中间使用文档型数据库遇到了很多问题,不熟悉操作等等。还有重启开发者平台神奇解决了的bug.
如何使用云开发,这里我们用到了云数据库以及云函数两个部分,云函数我是新建了一个云函数指导,在里面部署云函数部分的。
云数据库可以选择指定env或者不指定,如果你只新建了一个环境,默认选择那一个也行。
1. 部署云数据库环境及调用云函数获得openid
/*在app.js onLaunch函数下
2. 调用云函数获取用户openid,云函数为云函数指导App自带的,根据指引部署即可
3. 初始化数据库环境
*/
onLaunch: function() {
// console.log('App Launch')
var uid = wx.getStorageSync('uid');
var pwd = wx.getStorageSync('newpwd');
var newpwd = wx.getStorageSync('newpwd');
if (!wx.cloud) {
console.error('请使用 2.2.3 或以上的基础库以使用云能力')
} else {
wx.cloud.init({
// env 参数说明:
// env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源
// 此处请填入环境 ID, 环境 ID 可打开云控制台查看
// 如不填则使用默认环境(第一个创建的环境)
// env: 'test-k0z9u',
traceUser: true,
})
wx.cloud.callFunction({
name: 'login',
data: {},
success: res => {
console.log('[login] user openid: ', res.result.openid)
this.globalData.openid = res.result.openid
},
fail: err => {
console.error('获取用户openid失败', err)
}
})
}
},
2. 注册页面index.js下获得数据库引用
const db = wx.cloud.database()
//暂时设置为bind
const collection = db.collection('test')
const bindcollection = db.collection('test_bind')
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;
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)
//云函数返回test_bind 中是否已有绑定信息
wx.cloud.callFunction({
name:'isRegistered',
data:{
openid : app.globalData.openid,
uid : uid,
pwd : pwd
},
complete: res => {
// console.log('callFunction test result: ', res)
let length = res.result.data.length
console.log('[返回数据长度]length',length)
//回调中处理数据解决异步问题
if(!length){
//云函数绑定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班',
})
}
},
fail: err => {
wx.showToast({
icon: 'none',
title: '查询记录失败'
})
console.error('[数据库] [查询记录] 失败:', err)
}
})
}
},
这里需要注意的是由于微信小程序是异步处理数据,可能会出现你想要的结果没有同步地给到下一个数据处理环节,我看解决办法有什么async/await ,这部分在云函数确实用到了,但这里我不大会建造这种函数,还看到什么promise结构,.then() .catch()什么的,也用不了,不知道如何去使用。
所以这里我采取的方法是,在调用云函数的处理这块,在回调中处理数据解决异步的问题,这里我把获取openid设置在app.js也是在回调中解决问题这个思路之前对解决异步问题的尝试。
3.部署云函数
在处理登陆步骤中用到了一些云函数,这里我们在新建的项目中部署的云函数。
通过右击cloudfunctions新建Node.js云函数
![](https://i-blog.csdnimg.cn/blog_migrate/f064b490f358b6c2dfce4f15a0103825.png)
修改index.js进行部署
//isRegistered
// 云函数入口文件
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()
}
//bindOpenid
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
//暂时设置为test_bind
return await db.collection('test_bind').add({
data: {
openid : event.openid,
uid : event.uid,
pwd : event.pwd
}
})
}
选择云端安装依赖进行使用
![](https://i-blog.csdnimg.cn/blog_migrate/16d2f0e3078167ce298864db2f13697d.png)
4. 实现效果
![](https://i-blog.csdnimg.cn/blog_migrate/9944d37bd29916f65561c92d8d1c0cc4.png)
![](https://i-blog.csdnimg.cn/blog_migrate/91f0a544f18421e3dcde4901d8f74dbe.png)
![](https://i-blog.csdnimg.cn/blog_migrate/121fe42595c9efd865f487151fe48266.png)
现在只有学号数据,所以目前貌似只能这样处理了。