const _Http = getApp().globalData.http;
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        /* 最大上传数量 */
        maxCount: {
            type: String,
            value: 9
        },
        /* 文件上传类型限制 all media image file video */
        accept: {
            type: String,
            value: "all"
        },
        /* 上传回调 */
        uploadCallback: {
            type: Function
        },
        /* 文件夹ID */
        parentid: {
            type: String,
            value: wx.getStorageSync('siteP').appfolderid
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        fileList: []
    },

    /**
     * 组件的方法列表
     */
    methods: {
        /* 上传文件 */
        afterRead(event) {
            for (let i = 0; i < event.detail.file.length; i++) {
                // 初始化数据
                let that = this,
                    data = this.requestType(event.detail.file[i]);
                data.content.filename = data.content.filename ? data.content.filename : `${Date.now()}.${data.content.filetype}`;
                //发送请求
                wx.getFileSystemManager().readFile({
                    filePath: event.detail.file[i].url,
                    success: result => {
                        //返回临时文件路径
                        const fileData = result.data;
                        _Http.basic(data).then(res => {
                            if (res.msg == "成功") {
                                that.uploadFile(res.data, fileData)
                            } else {
                                wx.showToast({
                                    title: `${fileData.filename}.${fileData.serialfilename}`,
                                    icon: "none"
                                })
                            }
                        })
                    },
                    fail: console.error
                })
            }
        },
        /* 请求类型 */
        requestType(file) {
            //获取文件后缀
            var index = file.url.lastIndexOf(".");
            var ext = file.url.substr(index + 1);
            //文件名称
            return {
                "classname": "system.attachment.huawei.OBS",
                "method": "getFileName",
                "content": {
                    "filename": file.name,
                    "filetype": ext,
                    "parentid": this.data.parentid
                }
            }
        },
        /* 上传成功反馈 */
        uploadFile(res, data) {
            var that = this;
            wx.request({
                url: res.uploadurl,
                method: "PUT",
                data: data,
                header: {
                    'content-type': 'application/octet-stream'
                },
                success(a) {
                    _Http.basic({
                        "classname": "system.attachment.huawei.OBS",
                        "method": "uploadSuccess",
                        "content": {
                            "serialfilename": res.serialfilename
                        }
                    }).then(s => {
                        console.log("文件上传反馈", s)
                        // if (s.msg != "成功") return;
                        that.triggerEvent("uploadCallback", s.data.attachmentids);
                    }).catch(err => {
                        console.log(err)
                    })
                }
            })
        },
    }
})