index.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import { __assign, __awaiter, __generator, __spreadArray } from "tslib";
  2. import { Component, triggerEvent, getValueFromProps } from '../_util/simply';
  3. import { UploaderDefaultProps } from './props';
  4. import { chooseImage } from '../_util/jsapi/choose-image';
  5. import createValue from '../mixins/value';
  6. Component(UploaderDefaultProps, {
  7. chooseImage: function () {
  8. return __awaiter(this, void 0, void 0, function () {
  9. var _a, onBeforeUpload, onUpload, fileList, _b, maxCount, sourceType, localFileList, chooseImageRes, err_1, beforeUploadRes, err_2, tasks;
  10. var _this = this;
  11. return __generator(this, function (_c) {
  12. switch (_c.label) {
  13. case 0:
  14. _a = getValueFromProps(this, [
  15. 'onBeforeUpload',
  16. 'onUpload',
  17. ]), onBeforeUpload = _a[0], onUpload = _a[1];
  18. if (!onUpload) {
  19. throw new Error('need props onUpload');
  20. }
  21. fileList = this.getValue();
  22. _b = getValueFromProps(this, [
  23. 'maxCount',
  24. 'sourceType',
  25. ]), maxCount = _b[0], sourceType = _b[1];
  26. _c.label = 1;
  27. case 1:
  28. _c.trys.push([1, 3, , 4]);
  29. return [4 /*yield*/, chooseImage({
  30. count: typeof maxCount === 'undefined'
  31. ? Infinity
  32. : maxCount - fileList.length,
  33. sourceType: sourceType,
  34. })];
  35. case 2:
  36. chooseImageRes = _c.sent();
  37. localFileList = (chooseImageRes.tempFiles ||
  38. chooseImageRes.tempFilePaths ||
  39. chooseImageRes.apFilePaths ||
  40. chooseImageRes.filePaths ||
  41. [])
  42. .map(function (item) {
  43. if (typeof item === 'string') {
  44. return {
  45. path: item,
  46. };
  47. }
  48. if (item.path) {
  49. return {
  50. path: item.path,
  51. size: item.size,
  52. };
  53. }
  54. })
  55. .filter(function (item) { return !!item; });
  56. return [3 /*break*/, 4];
  57. case 3:
  58. err_1 = _c.sent();
  59. triggerEvent(this, 'chooseImageError', err_1);
  60. return [2 /*return*/];
  61. case 4:
  62. if (!onBeforeUpload) return [3 /*break*/, 8];
  63. _c.label = 5;
  64. case 5:
  65. _c.trys.push([5, 7, , 8]);
  66. return [4 /*yield*/, onBeforeUpload(localFileList)];
  67. case 6:
  68. beforeUploadRes = _c.sent();
  69. if (beforeUploadRes === false) {
  70. return [2 /*return*/];
  71. }
  72. if (Array.isArray(beforeUploadRes)) {
  73. localFileList = beforeUploadRes;
  74. }
  75. return [3 /*break*/, 8];
  76. case 7:
  77. err_2 = _c.sent();
  78. return [2 /*return*/];
  79. case 8:
  80. tasks = localFileList.map(function (file) { return _this.uploadFile(file); });
  81. return [4 /*yield*/, Promise.all(tasks)];
  82. case 9:
  83. _c.sent();
  84. return [2 /*return*/];
  85. }
  86. });
  87. });
  88. },
  89. uploadFile: function (localFile) {
  90. return __awaiter(this, void 0, void 0, function () {
  91. var onUpload, uid, tempFileList, url, err_3;
  92. return __generator(this, function (_a) {
  93. switch (_a.label) {
  94. case 0:
  95. onUpload = getValueFromProps(this, 'onUpload');
  96. uid = this.getCount();
  97. tempFileList = __spreadArray(__spreadArray([], this.getValue(), true), [
  98. {
  99. path: localFile.path,
  100. size: localFile.size,
  101. uid: uid,
  102. status: 'uploading',
  103. },
  104. ], false);
  105. if (!this.isControlled()) {
  106. this.update(tempFileList);
  107. }
  108. triggerEvent(this, 'change', tempFileList);
  109. _a.label = 1;
  110. case 1:
  111. _a.trys.push([1, 3, , 4]);
  112. return [4 /*yield*/, onUpload(localFile)];
  113. case 2:
  114. url = _a.sent();
  115. if (typeof url !== 'string' || !url) {
  116. this.updateFile(uid, {
  117. status: 'error',
  118. });
  119. return [2 /*return*/];
  120. }
  121. this.updateFile(uid, {
  122. status: 'done',
  123. url: url,
  124. });
  125. return [3 /*break*/, 4];
  126. case 3:
  127. err_3 = _a.sent();
  128. this.updateFile(uid, {
  129. status: 'error',
  130. });
  131. return [3 /*break*/, 4];
  132. case 4: return [2 /*return*/];
  133. }
  134. });
  135. });
  136. },
  137. updateFile: function (uid, file) {
  138. var fileList = this.getValue();
  139. var tempFileList = fileList.map(function (item) {
  140. if (item.uid === uid) {
  141. return __assign(__assign({}, item), file);
  142. }
  143. return item;
  144. });
  145. if (!this.isControlled()) {
  146. this.update(tempFileList);
  147. }
  148. triggerEvent(this, 'change', tempFileList);
  149. },
  150. onRemove: function (e) {
  151. return __awaiter(this, void 0, void 0, function () {
  152. var fileList, onRemove, uid, file, result, tempFileList;
  153. return __generator(this, function (_a) {
  154. switch (_a.label) {
  155. case 0:
  156. fileList = this.getValue();
  157. onRemove = getValueFromProps(this, 'onRemove');
  158. uid = e.currentTarget.dataset.uid;
  159. file = fileList.find(function (item) { return item.uid === uid; });
  160. if (!onRemove) return [3 /*break*/, 2];
  161. return [4 /*yield*/, onRemove(file)];
  162. case 1:
  163. result = _a.sent();
  164. if (result === false) {
  165. return [2 /*return*/];
  166. }
  167. _a.label = 2;
  168. case 2:
  169. tempFileList = fileList.filter(function (item) { return item.uid !== uid; });
  170. if (!this.isControlled()) {
  171. this.update(tempFileList);
  172. }
  173. triggerEvent(this, 'change', tempFileList);
  174. return [2 /*return*/];
  175. }
  176. });
  177. });
  178. },
  179. onPreview: function (e) {
  180. var uid = e.currentTarget.dataset.uid;
  181. var fileList = this.getValue();
  182. var file = fileList.find(function (item) { return item.uid === uid; });
  183. triggerEvent(this, 'preview', file);
  184. },
  185. updateShowUploadButton: function () {
  186. var maxCount = getValueFromProps(this, 'maxCount');
  187. this.setData({
  188. showUploadButton: !maxCount || this.getValue().length < maxCount,
  189. });
  190. },
  191. count: 0,
  192. getCount: function () {
  193. // 使用 Date.now() 与 useId 作为前缀,防止每次前缀都相同
  194. this.count = (this.count || 0) + 1;
  195. // 使用 Date.now() 与 useId 作为前缀,防止每次前缀都相同
  196. var id = this.id;
  197. id = this.$id;
  198. var prefix = id + '-' + Date.now();
  199. return "".concat(prefix, "-").concat(this.count);
  200. },
  201. }, null, [
  202. createValue({
  203. defaultValueKey: 'defaultFileList',
  204. valueKey: 'fileList',
  205. transformValue: function (fileList) {
  206. var _this = this;
  207. if (fileList === void 0) { fileList = []; }
  208. return {
  209. needUpdate: true,
  210. value: (fileList || []).map(function (item) {
  211. var file = __assign({}, item);
  212. if (typeof item.url === 'undefined') {
  213. file.url = '';
  214. }
  215. if (typeof item.uid === 'undefined') {
  216. file.uid = _this.getCount();
  217. }
  218. if (typeof item.status === 'undefined') {
  219. file.status = 'done';
  220. }
  221. return file;
  222. }),
  223. };
  224. },
  225. }),
  226. ], {
  227. didMount: function () {
  228. this.updateShowUploadButton();
  229. },
  230. didUpdate: function (prevProps, prevData) {
  231. if (!this.isEqualValue(prevData)) {
  232. this.updateShowUploadButton();
  233. }
  234. },
  235. });