美文网首页
uniapp云服务空间使用uni-id的api

uniapp云服务空间使用uni-id的api

作者: 焚心123 | 来源:发表于2022-02-14 13:59 被阅读0次
    • 在uniapp的项目中想要使用uni-id的插件及里面的API进行登录注册等云函数,我们新建了一个页面,uni-id在页面中也进行了引入,在前台调用的时候,却提示uni-id找不到,这是为神魔呢?在这里告诉你,因为我们还缺少了两个文件,要想使用uni-id的API及他的模块,我们就把下方的图片中的文件引入到我们创建的云函数中
    image.png
    • 通过官方给的api,怎么实现一个自定义的code验证码,不需要发送短信就可以使用的code
    • 先新建一个云函数,并引入上方的两个文件夹,使用uni-id的插件,
    'use strict';
    
    // 云函数bind-mobile代码
    const uniID = require('uni-id')
    const db = uniCloud.database();
    exports.main = async function(event,context) {
        
        
        switch (event.action){
            case 'set-code'://自定义设置验证码,在云数据库中uni-verify进行查看验证码
                
                // 生成验证码可以按自己的需求来,这里以生成6位数字为例
                  const randomStr = '00000' + Math.floor(Math.random() * 1000000)
                  const code = randomStr.substring(randomStr.length - 6)
                    const res = await uniID.setVerifyCode({
                        mobile:event.mobile,
                        code,
                    expiresIn: 180,
                    type: 'register'
                    })
                    return res
                break;
            case 'bind-mobile'://绑定手机号
                
                const payload = await uniID.checkToken(event.uniIdToken)
                if(payload.code > 0) {
                  return payload
                }
                  const res1 = await uniID.bindMobile({
                  uid: payload.uid,
                      mobile:event.mobile,
                      code:event.code
                  })
                  return res1
                break;
            case 'get-code'://获取code验证码
                const res2 = db.collection('uni-verify').where({
                     mobile:event.mobile,
                     type:{
                        state:0 
                     }
                }).get();
                return res2;
                break;
            default:
                break;
        }
    }
    

    相关文章

      网友评论

          本文标题:uniapp云服务空间使用uni-id的api

          本文链接:https://www.haomeiwen.com/subject/lmhbbktx.html