<template>
    <view>
        首页
        <button type="primary" @click="getList">调用get_list云函数</button>
        <button type="primary" @click="uploadImg">上传图片</button>
        <button type="primary" @click="deleteImg">删除图片</button>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                title: 'Hello'
            }
        },
        onLoad() {
            console.log("页面加载:首页")
        },
        // tarbar 点击触发 可以做页面数据重新渲染的动作
        onTabItemTap(e) {
            console.log(e);
        },
        methods: {
            getList(){
                uniCloud.callFunction({
                    name:"get_list",
                    data:{
                        // 请求name 为 name1 的数据
                        name: 'name1'
                    },
                    success(res) {
                        console.log(res);
                    },
                    fail(err) {
                        console.log(err)
                    }
                })
            },
            uploadImg() {
                new Promise((resolve, reject) => {
                    uni.chooseImage({
                        count: 1,
                        success: res => {
                            const path = res.tempFilePaths[0]
                            console.log('path是:' + path)
                            let ext
                            // #ifdef H5
                            ext = res.tempFiles[0].name.split('.').pop()
                            // #endif
                            // #ifndef H5
                            // 字节跳动小程序ios端选择文件会带query
                            ext = res.tempFilePaths[0].split('?')[0].split('.').pop()
                            // #endif
                            // 图片后缀
                            console.log('ext是:' + ext)
                            const options = {
                                filePath: path,
                                cloudPath: Date.now() + '.' + ext
                            }
                            resolve(options)
                        },
                        fail: () => {
                            reject(new Error('Fail_Cancel'))
                        }
                    })
                }).then((options) => {
                    uni.showLoading({
                        title: '文件上传中...'
                    })
                    return uniCloud.uploadFile({
                        ...options,
                        onUploadProgress(e) {
                            console.log(e)
                        }
                    })
                }).then(res => {
                    uni.hideLoading()
                    console.log(res);
                    uni.showModal({
                        content: '图片上传成功,fileId为:' + res.fileID,
                        showCancel: false
                    })
                }).catch((err) => {
                    uni.hideLoading()
                    console.log(err);
                    if (err.message !== 'Fail_Cancel') {
                        uni.showModal({
                            content: `图片上传失败,错误信息为:${err.message}`,
                            showCancel: false
                        })
                    }
                })
            },
            deleteImg(){
                uniCloud.deleteFile({
                    fileList:['https://vkceyugu.cdn.bspapp.com/VKCEYUGU-zk-demo/fec169cc-d907-4392-b834-c31da3f6f662.png'],
                    success(res) {
                        console.log(res)
                    },
                    fail(err) {
                        console.log(err)
                    }
                })
            }
        }
    }
</script>

<style>
    
</style>

相关文章:

  • 2022-12-23
  • 2021-10-24
  • 2021-12-24
  • 2021-09-07
  • 2021-12-24
  • 2021-05-10
猜你喜欢
  • 2021-08-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案