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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
| <template>
| <div>
| <div style="display: flex;">
| <div style="width: 100%; margin-right: 10px">
| <Input
| v-model="currentValue"
| @on-change="handleChange"
| :placeholder="placeholder"
| :size="size"
| :disabled="disabled"
| :readonly="readonly"
| :maxlength="maxlength"
| >
| <Poptip
| transfer
| trigger="hover"
| title="图片预览"
| placement="right"
| width="350"
| style="width: 17px; cursor: pointer"
| slot="append"
| >
| <Button :icon="previewIcon"></Button>
| <div slot="content">
| <img
| v-show="currentValue"
| :src="currentValue"
| style="
| width: 100%;
| margin: 0 auto;
| display: block;
| cursor: zoom-in;
| "
| @click="viewImage"
| />
| <span v-show="!currentValue">无效的图片链接</span>
| <a
| v-show="currentValue"
| @click="viewImage"
| style="margin-top: 5px; text-align: right; display: block"
| >查看大图</a
| >
| </div>
| </Poptip>
| </Input>
| </div>
| <Upload
| :action="uploadFileUrl"
| :headers="accessToken"
| :on-success="handleSuccess"
| :on-error="handleError"
| :format="format"
| :accept="accept"
| :max-size="maxSize * 1024"
| :on-format-error="handleFormatError"
| :on-exceeded-size="handleMaxSize"
| :before-upload="beforeUpload"
| :show-upload-list="false"
| :v-show="showUpload"
| >
| <Button
| :loading="loading"
| :size="size"
| :disabled="disabled"
| :icon="icon"
| :type="type"
| :shape="shape"
| :ghost="ghost"
| >{{ text }}</Button
| >
| </Upload>
| </div>
| </div>
| </template>
|
| <script>
| import "viewerjs/dist/viewer.css";
| import Viewer from "viewerjs";
| import { uploadFile } from "@/api/index";
| export default {
| name: "uploadPicInput",
| props: {
| value: String,
| size: String,
| placeholder: {
| type: String,
| default: "可输入图片链接",
| },
| disabled: {
| type: Boolean,
| default: false,
| },
| readonly: {
| type: Boolean,
| default: false,
| },
| maxlength: Number,
| previewIcon: {
| type: String,
| default: "md-eye",
| },
| icon: {
| type: String,
| default: "ios-cloud-upload-outline",
| },
| text: {
| type: String,
| default: "上传图片",
| },
| type: String,
| shape: String,
| ghost: {
| type: Boolean,
| default: false,
| },
| maxSize: {
| type: Number,
| default: 5,
| },
| accept: {
| type: String,
| default: ".jpg, .jpeg, .png, .gif",
| },
| showUpload: {
| type: Boolean,
| default: true,
| },
| },
| computed: {
| format() {
| if (this.accept) {
| let format = [];
| this.accept.split(",").forEach((e) => {
| format.push(e.replace(".", "").replace(" ", ""));
| });
| return format;
| } else {
| return [];
| }
| },
| },
| data() {
| return {
| accessToken: {},
| currentValue: this.value,
| loading: false,
| uploadFileUrl: uploadFile,
| };
| },
| methods: {
| init() {
| this.accessToken = {
| accessToken: this.getStore("accessToken"),
| };
| },
| viewImage() {
| let image = new Image();
| image.src = this.currentValue;
| let viewer = new Viewer(image, {
| hidden: function () {
| viewer.destroy();
| },
| });
| viewer.show();
| },
| handleFormatError(file) {
| this.loading = false;
| this.$Notice.warning({
| title: "不支持的文件格式",
| desc:
| "所选文件‘ " +
| file.name +
| " ’格式不正确, 请选择 " +
| this.accept +
| " 格式文件",
| });
| },
| handleMaxSize(file) {
| this.loading = false;
| this.$Notice.warning({
| title: "文件大小过大",
| desc:
| "所选文件‘ " +
| file.name +
| " ’大小过大, 不得超过 " +
| this.maxSize +
| "M.",
| });
| },
| beforeUpload() {
| this.loading = true;
| return true;
| },
| handleSuccess(res, file) {
| this.loading = false;
| if (res.success) {
| this.currentValue = res.result;
| this.$emit("input", this.currentValue);
| this.$emit("on-change", this.currentValue);
| } else {
| this.$Message.error(res.message);
| }
| },
| handleError(error, file, fileList) {
| this.loading = false;
| this.$Message.error(error.toString());
| },
| handleChange(v) {
| this.$emit("input", this.currentValue);
| this.$emit("on-change", this.currentValue);
| },
| setCurrentValue(value) {
| if (value === this.currentValue) {
| return;
| }
| this.currentValue = value;
| this.$emit("on-change", this.currentValue);
| },
| },
| watch: {
| value(val) {
| this.setCurrentValue(val);
| },
| },
| mounted() {
| this.init();
| },
| };
| </script>
|
|