WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2025-03-18 fdeb869c386da95150a087bc22bcebc4e57d0f76
forms/imagePicker/CImagePicker.vue
@@ -1,5 +1,8 @@
/**
 * CImagePicker
 * 图片选择器组件,用于在表单中上传和管理图片
 * 支持单张和多张图片上传,并提供压缩、预览等功能
 * 可限制上传图片的数量和来源(相册/相机)
 * @author Tevin
 */
@@ -15,14 +18,15 @@
        <AtImagePicker
            ref="picker"
            mode="aspectFit"
            :sourceType="sourceType"
            :multiple="count > 1"
            :count="count"
            :showAddBtn="!selectedFull"
            :length="3"
            :files="files"
            :onChange="(files,operationType,index)=>handleChange(files,operationType,index)"
            :onFail="evt=>handleFail(evt)"
            :onImageClick="(index, file)=>handleImgView(index,file)"
            :onChange="(files,operationType,index) => handleChange(files,operationType,index)"
            :onFail="evt => handleFail(evt)"
            :onImageClick="(index, file) => handleImgView(index,file)"
        />
        <CImageCompressor ref="compressor" />
        <CImagePreview ref="imgPreview" />
@@ -57,10 +61,20 @@
            type: Number,
            default: 1,
        },
        // 图片来源
        sourceType: {
            type: Array,
            default: () => ['album', 'camera'],
        },
        // 上传图片参数
        params: {
            type: Object,
            default: () => {},
        },
        // 是否开启缩略图
        needThumb: {
            type: Boolean,
            default: false,
        },
    },
    data() {
@@ -104,7 +118,7 @@
                    if (file.file && file.file.originalFileObj) {
                        if (
                            /image\/(gif|png|jpg|jpeg)/.test(
                                file.file.originalFileObj.type
                                file.file.originalFileObj.type,
                            )
                        ) {
                            return true;
@@ -127,8 +141,13 @@
                const needs = nextFiles
                    .map((file, needIndex) => {
                        const fileInfo = file.file;
                        // 没有 file 信息对象,或者不是 blob 类型
                        if (!fileInfo || fileInfo.path.indexOf('blob') < 0) {
                        // 没有 file 信息对象,或者不是 blob、wxfile、tmp 类型
                        if (
                            !fileInfo ||
                            (fileInfo.path.indexOf('blob') < 0 &&
                                fileInfo.path.indexOf('wxfile') < 0 &&
                                fileInfo.path.indexOf('http://tmp/') < 0)
                        ) {
                            return false;
                        }
                        // 尺寸小于 1M 的图片
@@ -207,7 +226,10 @@
                        callback(state, res);
                    }
                },
                this.params
                $fetchCommon.transKeyName('underline', {
                    ...this.params,
                    needThumb: this.needThumb ? 1 : 0,
                }),
            );
        },
    },
@@ -227,7 +249,7 @@
// 图片上传节流
const _readyUpload = {};
export const uploadImage = (files, callback, params) => {
export const uploadImage = (files, callback, formData = {}) => {
    if (!files || files.length === 0) {
        callback('success', []);
        return;
@@ -271,18 +293,28 @@
                        ...requestFile,
                        header,
                        name: 'file',
                        formData: { ...params },
                        formData,
                        timeout: 30 * 1000,
                        success(res) {
                            const res2 =
                                typeof res.data === 'string'
                                    ? JSON.parse(res.data)
                                    : res.data;
                            let res2;
                            try {
                                res2 =
                                    typeof res.data === 'string'
                                        ? JSON.parse(res.data)
                                        : res.data;
                            } catch (err) {
                                reject({
                                    ...requestFile,
                                    response: res,
                                    message: '上传图片异常!',
                                });
                                return;
                            }
                            // 上传成功
                            if (res2.state.code === 2000) {
                                const imgUrl = $fetchCommon.transImgPath(
                                    'fix',
                                    res2.data.src || res2.data.file || res2.data.url
                                    res2.data.src || res2.data.file || res2.data.url,
                                );
                                _readyUpload[file.url] = imgUrl;
                                resolve(imgUrl);
@@ -309,7 +341,7 @@
                            });
                        },
                    });
                })
                }),
            );
        }
        // 其他类型视为 url,忽略