美文网首页
unicloud云函数的学习之入门二

unicloud云函数的学习之入门二

作者: 焚心123 | 来源:发表于2023-05-22 16:06 被阅读0次
    • 在入门一种我们有学习到使用DB Schema,下面我们继续使用DB Schema进行简单的介绍及学习
      1、进行简单的使用DB Schema进行添加(具体的参数及用法可以查看下方的图片进行学习)
    • 在database下的test.schema.json中进行配置
    {
        "bsonType": "object",
        "required": ["title"],//必填项,需要某一个属性必填可写这里
        "permission": {
            "read": true,//可读
            "create": true,//可写
            "update": false,//可修改
            "delete": false//可删除
        },
        "properties": {//当前的表属性,都在这里进行定义
            "_id": {
                "description": "ID,系统自动生成"
            },
            "title":{//字段名
                "bsonType": "string",
                "title": "文章标题",//属性名,
                "description": "文章的标题名称简介",
                "errorMessage":"标题必须填写",//这是校验失败的提示
                "permission": {//二次校验,不允许进行更新
                    "update":false
                }
            },
            "content":{
                "bsonType": "string",
                "title": "文章的内容",
                "description": "文章内容的说明"
            },
            "posttime":{
                "bsonType": "timestamp",
                "title": "发布时间",
                "description": "文章发布的时间",
                "defaultValue":{//这个是当前的系统时间,可进行覆盖(你传递值就会覆盖)
                    "$env": "now"
                }
            },
            "hits":{
                "bsonType": "int",
                "title": "阅读量",
                "defaultValue":33
            }
            
            
        }
    }
    
    • 在页面中进行新增
    <template>
        <view class="content">
            <button @click="createdDb">新增</button>
        </view>
    </template>
    <script>
    const db = uniCloud.database();
    export default {
        setup() {
            const createdDb = () => {
                db.collection('test')
                    .add({
                        title:'文章1',//这个字段不传的话,会进行报错提示,你可以不传试试
                        content:'我是文章内容'
                    })
                    .then((res) => {
                        console.log(res);
                    });
            };
    
            return {
                createdDb
            };
        }
    };
    </script>
    
    
    image.png

    2、引入uni-id(先说下uni-id有哪些好处?有写好的页面,和封装好的云函数,直接调用就可使用)


    image.png
    • 看下图进行下载安装(会提示你是否注册,点击注册),安装之后,在点击第三步进行配置,否则直接调用会报错,提示你需要进行配置


      image.png
    • 配置uni-id的config,找到图中的文件,没有文件就进行新建就行,文件名跟图中的保持一致,然后将图二中的数据,在文档中进行复制到自己的config文件中(token和密钥需要自己配置,配置后如果有人用了就不能在进行改动,否则之前用户的密码啥的会进行丢失)


      image.png
    image.png

    注意:passwordSecret目前使用数组好像还有点问题,可以改为string类型的,文中的注释记得去掉

    {
        "passwordSecret": "995F698D1E4A9401",
        "passwordStrength": "medium",
        "tokenSecret": "995F698D1E4A9401",
        "requestAuthSecret": "",
        "tokenExpiresIn": 7200,
        "tokenExpiresThreshold": 3600,
        "passwordErrorLimit": 6,
        "passwordErrorRetryTime": 3600,
        "autoSetInviteCode": false,
        "forceInviteCode": false,
        "idCardCertifyLimit": 1,
        "realNameCertifyLimit": 5,
        "sensitiveInfoEncryptSecret": "",
        "frvNeedAlivePhoto": false,
        "userRegisterDefaultRole": [],
        "app": {
            "tokenExpiresIn": 2592000,
            "tokenExpiresThreshold": 864000,
            "oauth": {
    
                "weixin": {
                    "appid": "",
                    "appsecret": ""
                },
    
                "qq": {
                    "appid": "",
                    "appsecret": ""
                },
                "apple": {
                    "bundleId": ""
                }
            }
        },
        "web": {
            "tokenExpiresIn": 7200,
            "tokenExpiresThreshold": 3600,
            "oauth": {
                "weixin-h5": {
                    "appid": "",
                    "appsecret": ""
                },
                "weixin-web": {
                    "appid": "",
                    "appsecret": ""
                }
            }
        },
        "mp-weixin": {
            "tokenExpiresIn": 259200,
            "tokenExpiresThreshold": 86400,
            "oauth": {
    
                "weixin": {
                    "appid": "",
                    "appsecret": ""
                }
            }
        },
        "mp-qq": {
            "tokenExpiresIn": 259200,
            "tokenExpiresThreshold": 86400,
            "oauth": {
    
                "qq": {
                    "appid": "",
                    "appsecret": ""
                }
            }
        },
        "mp-alipay": {
            "tokenExpiresIn": 259200,
            "tokenExpiresThreshold": 86400,
            "oauth": {
    
                "alipay": {
                    "appid": "",
                    "privateKey": "",
                    "keyType": "PKCS8"
                }
            }
        },
        "service": {
            "sms": {
                "name": "",
                "codeExpiresIn": 180,
                "smsKey": "",
                "smsSecret": "",
                "scene": {
                    "bind-mobile-by-sms": {
                        "templateId": "",
                        "codeExpiresIn": 240
                    }
                }
            },
            "univerify": {
                "appid": "",
                "apiKey": "",
                "apiSecret": ""
            }
        }
    }
    

    3、在app.json中就可以看到uni-id注册的一些页面,我们用登录页面进行测试下

    • 页面路径:uni_modules/uni-id-pages/pages/register/register
    • 使用登录,提示你未注册
    • 使用注册,注册成功
    • 在unicloud控制台中查看会新增两个表,一个日志表,一个用户表

    4、当我们登录成功之后每个用户都会有一个id唯一的标识符,但是在其他的schema表中怎么添加这个id呢

    foreignKey schema中的关联表关系 并赋值一个默认值为uid,这样就可以啦

        "properties": {//当前的表属性,都在这里进行定义
            "_id": {
                "description": "ID,系统自动生成"
            },
            "userId":{
                "bsonType": "string",
                "title": "用户ID",
                "description": "用户登录之后生成的id",
                "defaultValue":{
                    "$env": "uid"
                },
                "foreignKey": "uni-id-users._id"
            },
            "title":{//字段名
                "bsonType": "string",
                "title": "文章标题",//属性名,
                "description": "文章的标题名称简介",
                "errorMessage":"标题必须填写",//这是校验失败的提示
                "permission": {//二次校验,不允许进行更新
                    "update":false
                }
            },
            "content":{
                "bsonType": "string",
                "title": "文章的内容",
                "description": "文章内容的说明"
            },
            "posttime":{
                "bsonType": "timestamp",
                "title": "发布时间",
                "description": "文章发布的时间",
                "defaultValue":{
                    "$env": "now"
                }
            },
            "hits":{
                "bsonType": "int",
                "title": "阅读量",
                "defaultValue":33
            }
            
            
        }
    

    5、schema表中的权限增删改查的权限验证


    image.png
    • 当我们有的数据,需要用户登录之后,在进行读写修改,我们就可以这样进行权限的验证了
    • doc就是当前这个schema表
    "permission": {
            "read": "auth.uid!=null",//当用户登录之后才会有id,才能进行读取
            "create": "auth.uid!=null",//登录之后才可以写入
            "update": "doc.userId == auth.uid",//当前登录的用户id等于当前新增的id的时候才可以更新
            "delete": false//可删除
        },
    
    • 我们可以试验下,之前登录过,然后我们有新增数据的话,可以试下,此时是没有问题的,然后我们跳转到登录退出页面或者直接将本地存储的信息删除就可以了,再试下看能不能新增,此时是报错的,就成功了

    相关文章

      网友评论

          本文标题:unicloud云函数的学习之入门二

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