WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2021-04-21 487719b3188aaafcca49104b4ce62035dc29e953
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
 * CImagePicker
 * @author Tevin
 */
 
<template>
    <view class="c-image-picker">
        <AtInput
            ref="input"
            :name="itemRes.name"
            :title="itemRes.label"
            :required="itemRes.required"
            :error="itemRes.error"
        />
        <AtImagePicker
            ref="picker"
            multiple
            mode="aspectFit"
            :count="9"
            :length="3"
            :files="files"
            :onChange="(files,operationType,index)=>handleChange(files,operationType,index)"
            :onFail="evt=>handleFail(evt)"
            :onImageClick="(index, file)=>handleImgView(index,file)"
        />
        <AtCurtain
            class="c-image-picker-view"
            closeBtnPosition="top-right"
            :isOpened="showImg"
            :onClose="evt=>handleImgClose()"
        >
            <image
                class="img"
                mode="aspectFit"
                :src="curtainImg"
            />
        </AtCurtain>
    </view>
</template>
 
<script>
import Taro from '@tarojs/taro';
import { $ } from '@tarojs/extend';
import { AtInput, AtImagePicker, AtCurtain } from 'taro-ui-vue';
import { $fetcherCommon } from '@fetchers/FCommon';
import './cImagePicker.scss';
 
export default {
    name: 'CImagePicker',
    components: {
        AtInput,
        AtImagePicker,
        AtCurtain,
    },
    props: {
        // 表单数据资源(表单组件内部机制专用)
        itemRes: Object,
    },
    data() {
        return {
            showImg: false,
            curtainImg: '',
        };
    },
    computed: {
        files() {
            const value = this.itemRes.formData[this.itemRes.name];
            let files = [];
            if (Object.prototype.toString.call(value) === '[object String]') {
                files = value.split(',').map(url => ({ url }));
            } else if (Object.prototype.toString.call(value) === '[object Array]') {
                files = value.map(url => ({ url }));
            }
            files.push({ type: 'btn' });
            return files;
        },
    },
    methods: {
        handleChange(files, operationType, index) {
            const value = [];
            files.forEach(file => {
                if (file.type === 'btn') {
                    return;
                }
                value.push(file.url);
            });
            this.itemRes.onChange(value);
        },
        handleImgView(index, file) {
            this.curtainImg = file.url;
            this.showImg = true;
        },
        handleImgClose() {
            this.showImg = false;
        },
        handleFail(msg) {
            Taro.showToast({
                title: msg,
                icon: 'none',
                mask: true,
                duration: 2000,
            });
        },
        $uploadImage(callback) {
            const url = $fetcherCommon.getUploadImgURL();
            const uploadTeam = [];
            const imgs = [];
            this.files.forEach(file => {
                if (file.type === 'btn') {
                    return;
                }
                // blob 二进制文件才上传
                if (file.url.indexOf('blob') >= 0) {
                    uploadTeam.push(
                        new Promise((resolve, reject) => {
                            Taro.uploadFile({
                                url,
                                filePath: 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(
                                            $fetcherCommon.transImgPath(
                                                'fix',
                                                res2.data.src
                                            )
                                        );
                                    } 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);
        }
    },
};
</script>