UNPKG

4.2 kBJavaScriptView Raw
1import {
2 previewImage,
3 chooseImage,
4 uploadImage,
5 getLocalImgData,
6} from './lib/wx.js';
7import Uploader from './uploader.vue';
8
9/**
10 *
11 *
12 * @return {Promise} {image, serverId}
13 */
14function uploadWechatImage(localId) {
15 let serverId = null;
16 return uploadImage(localId).then((_res) => {
17 serverId = _res.serverId; // 记录res
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 */
32export 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 * 保存所有的serverId
48 */
49 serverIds: [],
50 };
51 },
52 methods: {
53 /**
54 * @param {Array<{image, serverId}>} data
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 * @param {} image 图片内容
70 * @param {} serverId
71 * @param {} res 资源数据
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 * @return {Array<{image, serverId}>}
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 * @param {} index
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