WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2022-07-16 101360c50d36bd850ff110099284fdb01a67f11a
图片上传,提供单独公开接口
1 files modified
149 ■■■■ changed files
forms/imagePicker/CImagePicker.vue 149 ●●●● patch | view | raw | blame | history
forms/imagePicker/CImagePicker.vue
@@ -175,16 +175,129 @@
            });
        },
        $uploadImage(callback) {
            const files = [];
            this.files.forEach(file => {
                if (file.type === 'btn') {
                    return;
                }
                file.fileName = this.fileNames[file.url];
                files.push(file);
            });
            uploadImage(files, (state, res) => {
                if (state === 'success') {
                    this.itemRes.onChange(res);
                    setTimeout(() => {
                        callback(state);
                    }, 10);
                } else if (state === 'error') {
                    callback(state, res);
                }
            });
            // let url = $fetchCommon.getUploadImgURL();
            // if (url.indexOf('http') < 0) {
            //     url = $hostBoot.getHost() + url;
            // }
            // const uploadTeam = [];
            // const imgs = [];
            // this.files.forEach(file => {
            //     if (file.type === 'btn') {
            //         return;
            //     }
            //     // 临时文件才上传
            //     if (
            //         file.url.indexOf('blob') >= 0 ||
            //         file.url.indexOf('wxfile') >= 0 ||
            //         file.url.indexOf('http://tmp/') >= 0
            //     ) {
            //         let header = {};
            //         if (process.env.TARO_ENV === 'weapp') {
            //             const localCookies = JSON.parse(
            //                 Taro.getStorageSync('cookies') || '{}'
            //             );
            //             const cookiesArr = [];
            //             Object.keys(localCookies).forEach(key => {
            //                 cookiesArr.push(key + '=' + localCookies[key]);
            //             });
            //             header['Cookie'] = cookiesArr.join('; ');
            //         }
            //         uploadTeam.push(
            //             new Promise((resolve, reject) => {
            //                 Taro.uploadFile({
            //                     url,
            //                     header,
            //                     filePath: file.url,
            //                     fileName: this.fileNames[file.url],
            //                     name: 'file',
            //                     formData: {},
            //                     success(res) {
            //                         const res2 =
            //                             typeof res.data === 'string'
            //                                 ? JSON.parse(res.data)
            //                                 : res.data;
            //                         if (res2.state.code === 2000) {
            //                             resolve(
            //                                 $fetchCommon.transImgPath(
            //                                     'fix',
            //                                     res2.data.src ||
            //                                         res2.data.file ||
            //                                         res2.data.url
            //                                 )
            //                             );
            //                         } else {
            //                             reject({ message: res2.state.msg });
            //                         }
            //                     },
            //                     cancel() {
            //                         reject({ message: '上传图片已取消!' });
            //                     },
            //                     fail() {
            //                         reject({ message: '上传图片失败!' });
            //                     },
            //                 });
            //             })
            //         );
            //     }
            //     // 其他类型视为 url,忽略
            //     else {
            //         uploadTeam.push(Promise.resolve(file.url));
            //     }
            // });
            // Promise.all(uploadTeam)
            //     .then(res => {
            //         this.itemRes.onChange(res);
            //         setTimeout(() => {
            //             callback('success');
            //         }, 0);
            //     })
            //     .catch(err => {
            //         callback('error', err);
            //     });
        },
    },
    mounted() {
        if (process.env.TARO_ENV === 'h5') {
            $(this.$refs.input.$el)
                .find('.at-input__input')
                .prepend(this.$refs.picker.$el);
        } else if (process.env.TARO_ENV === 'weapp') {
            $(this.$refs.input.$el)
                .find('.at-input__container')
                .append(this.$refs.picker.$el);
        }
    },
};
export const uploadImage = (files, callback) => {
    if (!files || files.length === 0) {
        callback('success', []);
    }
            let url = $fetchCommon.getUploadImgURL();
            if (url.indexOf('http') < 0) {
                url = $hostBoot.getHost() + url;
            }
            const uploadTeam = [];
            const imgs = [];
            this.files.forEach(file => {
                if (file.type === 'btn') {
                    return;
                }
    files.forEach(file => {
                // 临时文件才上传
                if (
                    file.url.indexOf('blob') >= 0 ||
@@ -193,9 +306,7 @@
                ) {
                    let header = {};
                    if (process.env.TARO_ENV === 'weapp') {
                        const localCookies = JSON.parse(
                            Taro.getStorageSync('cookies') || '{}'
                        );
                const localCookies = JSON.parse(Taro.getStorageSync('cookies') || '{}');
                        const cookiesArr = [];
                        Object.keys(localCookies).forEach(key => {
                            cookiesArr.push(key + '=' + localCookies[key]);
@@ -208,7 +319,7 @@
                                url,
                                header,
                                filePath: file.url,
                                fileName: this.fileNames[file.url],
                        fileName: file.fileName || '',
                                name: 'file',
                                formData: {},
                                success(res) {
@@ -220,9 +331,7 @@
                                        resolve(
                                            $fetchCommon.transImgPath(
                                                'fix',
                                                res2.data.src ||
                                                    res2.data.file ||
                                                    res2.data.url
                                        res2.data.src || res2.data.file || res2.data.url
                                            )
                                        );
                                    } else {
@@ -246,26 +355,10 @@
            });
            Promise.all(uploadTeam)
                .then(res => {
                    this.itemRes.onChange(res);
                    setTimeout(() => {
                        callback('success');
                    }, 0);
            callback('success', res);
                })
                .catch(err => {
                    callback('error', err);
                });
        },
    },
    mounted() {
        if (process.env.TARO_ENV === 'h5') {
            $(this.$refs.input.$el)
                .find('.at-input__input')
                .prepend(this.$refs.picker.$el);
        } else if (process.env.TARO_ENV === 'weapp') {
            $(this.$refs.input.$el)
                .find('.at-input__container')
                .append(this.$refs.picker.$el);
        }
    },
};
</script>