unicloud云函数的学习之入门二

2023-05-22  本文已影响0人  焚心123
{
    "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

注意: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注册的一些页面,我们用登录页面进行测试下

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
"permission": {
        "read": "auth.uid!=null",//当用户登录之后才会有id,才能进行读取
        "create": "auth.uid!=null",//登录之后才可以写入
        "update": "doc.userId == auth.uid",//当前登录的用户id等于当前新增的id的时候才可以更新
        "delete": false//可删除
    },
上一篇下一篇

猜你喜欢

热点阅读