1 | import {
|
2 | previewImage,
|
3 | chooseImage,
|
4 | uploadImage,
|
5 | getLocalImgData,
|
6 | } from './lib/wx.js';
|
7 | import Uploader from './uploader.vue';
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function uploadWechatImage(localId) {
|
15 | let serverId = null;
|
16 | return uploadImage(localId).then((_res) => {
|
17 | serverId = _res.serverId;
|
18 | return localId;
|
19 | })
|
20 | .then(getLocalImgData)
|
21 | .then((imageData) => {
|
22 | return {
|
23 | image: imageData,
|
24 | serverId: serverId,
|
25 | }
|
26 | });
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | export default {
|
33 | name: 'WechatUploader',
|
34 | extends: Uploader,
|
35 | props: {
|
36 | |
37 |
|
38 |
|
39 | useWechatPreview: {
|
40 | type: Boolean,
|
41 | default: true,
|
42 | },
|
43 | },
|
44 | data() {
|
45 | return {
|
46 | |
47 |
|
48 |
|
49 | serverIds: [],
|
50 | };
|
51 | },
|
52 | methods: {
|
53 | |
54 |
|
55 |
|
56 | setImages(data) {
|
57 | const images = [];
|
58 | const serverIds = [];
|
59 | for (let i = 0, len = data.length; i < len; i++) {
|
60 | images.push(data[i].image);
|
61 | serverIds.push(data[i].serverId);
|
62 | }
|
63 | this.images = images;
|
64 | this.serverIds = serverIds;
|
65 | },
|
66 | |
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | add(image, serverId, res) {
|
74 | if (this.images.length < this.size) {
|
75 | this.images.push(image);
|
76 | this.serverIds.push(serverId);
|
77 | this.$emit('add', {
|
78 | image,
|
79 | serverId,
|
80 | res,
|
81 | });
|
82 | return true;
|
83 | }
|
84 | return false;
|
85 | },
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 | getImages() {
|
92 | const result = [];
|
93 | for (let i = 0, len = this.images.length; i < len; i++) {
|
94 | result.push({
|
95 | image: this.images[i],
|
96 | serverId: this.serverIds[i],
|
97 | });
|
98 | }
|
99 | return result;
|
100 | },
|
101 | |
102 |
|
103 |
|
104 |
|
105 |
|
106 | remove(index) {
|
107 | if (0 <= index && index < this.size) {
|
108 | this.images.splice(index, 1);
|
109 | this.serverIds.splice(index, 1);
|
110 | this.$emit('remove', index);
|
111 | return true;
|
112 | }
|
113 | return false;
|
114 | },
|
115 | |
116 |
|
117 |
|
118 | onClickRequest() {
|
119 | this.request();
|
120 | },
|
121 | |
122 |
|
123 |
|
124 | request() {
|
125 | const vm = this;
|
126 | return chooseImage(vm.size - vm.images.length)
|
127 | .then((res) => {
|
128 | const localIds = res.localIds;
|
129 | if (localIds.length > 0) {
|
130 | vm.$emit('choose', res);
|
131 | vm.$emit('load');
|
132 | return vm.uploadWechatImages(localIds).then(function() {
|
133 | vm.$emit('finish');
|
134 | });
|
135 | }
|
136 | })
|
137 | },
|
138 | |
139 |
|
140 |
|
141 | uploadWechatImages(localIds) {
|
142 | const vm = this;
|
143 | const localId = localIds.shift();
|
144 | return uploadWechatImage(localId)
|
145 | .then(function({ image, serverId, res, }) {
|
146 | vm.add(image, serverId, res);
|
147 | if (localIds.length > 0) {
|
148 | return vm.uploadWechatImages(localIds);
|
149 | }
|
150 | });
|
151 | },
|
152 | },
|
153 | mounted() {
|
154 | this.$on('click', function(index) {
|
155 | previewImage(this.images[index], this.images);
|
156 | });
|
157 | },
|
158 | }; |
\ | No newline at end of file |