uniCloud

2024-04-11  本文已影响0人  迷失的信徒

一、uniCloud 基础用法

1、认识 uniCloud

2、在HBuilderX 中配置 uniCloud

3、使用 uniCloudWeb控制台

4、云函数

'use strict';
exports.main = async (event, context) => {
    //event为客户端上传的参数
    console.log('event : ', event)
    
    //返回数据给客户端
    return event
};
methods: {
    函数名() {
        uniCloud.callFunction({
            name: "云函数名",
            data: {},
            success(res) {},
            fail(err) {}
        })
    }
}

5、云数据库

'use strict';

// 获取数据库的引用
const db = uniCloud.database()

exports.main = async (event, context) => {
    // 获取集合的引用
    const collection = db.collection('集合的名字')
    
    // 新增数据
    // 新增 1 条记录
    let res1 = await collection.add({
        name: "zs"
    })
    // 新增 多 条记录
    let res2 = await collection.add([
        {
            name: "lisi",
            age: 12
        },
        {
            name: "wangwu"
        }
    ])
    
    // 删除记录
    const res3 = await collection.doc('该条记录的ID').remove()

    // 修改记录
    const res4 = await collection.doc('该条记录的ID').update()
    const res5 = await collection.doc('该条记录的ID').set()

    // 查询记录
    // 查询指定ID的记录
    const res6 = await collection.doc('该条记录的ID').get()
    // 查询指定字段的记录
    const res6 = await collection.where(查询条件).get()
    
    //返回数据给客户端
    return event
};

6、云存储

methods: {
    addImage() {
        let count = 9 - this.imageLists.length
        let that = this
        uni.chooseImage({
            count: count,
            success(res) {
                const tempFilePaths = res.tempFilePaths
                tempFilePaths.forEach((item, index) => {
                    // 处理 H5 多选的状况
                    if (index < count) {
                        that.imageLists.push({
                            url: item
                        })
                    }
                })
            }
        })
    },
            
    async submit() {
        let imagesPath = []
        // uni.showLoading()
        for (let i = 0; i < this.imageLists.length; i++) {
            const filePath = this.imageLists[i].url
            this.uploadFiles(filePath)
        }
    },
    async uploadFiles(filePath) {
        const result = await uniCloud.uploadFile({
            cloudPath: new Date(),
            filePath: filePath
        })
        return result.fileID
    },

    // 删除云存储中的文件
    uniCloud.deleteFile({
        fileList: ['云存储中的下载地址'],
        success(res) {
            console.log(res)
        },
        fail(err) {
            console.log(err)
        }
    })
}
上一篇 下一篇

猜你喜欢

热点阅读