index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. "use strict";
  2. var _component = require("../common/component");
  3. var _utils = require("./utils");
  4. var _shared = require("./shared");
  5. var _validator = require("../common/validator");
  6. var _my = require("../../__antmove/api/index.js")(my);
  7. var wx = _my;
  8. (0, _component.VantComponent)({
  9. props: Object.assign(Object.assign({
  10. disabled: Boolean,
  11. multiple: Boolean,
  12. uploadText: String,
  13. useBeforeRead: Boolean,
  14. afterRead: null,
  15. beforeRead: null,
  16. previewSize: {
  17. type: null,
  18. value: 80
  19. },
  20. name: {
  21. type: [Number, String],
  22. value: ""
  23. },
  24. accept: {
  25. type: String,
  26. value: "image"
  27. },
  28. fileList: {
  29. type: Array,
  30. value: [],
  31. observer: "formatFileList"
  32. },
  33. maxSize: {
  34. type: Number,
  35. value: Number.MAX_VALUE
  36. },
  37. maxCount: {
  38. type: Number,
  39. value: 100
  40. },
  41. deletable: {
  42. type: Boolean,
  43. value: true
  44. },
  45. showUpload: {
  46. type: Boolean,
  47. value: true
  48. },
  49. previewImage: {
  50. type: Boolean,
  51. value: true
  52. },
  53. previewFullImage: {
  54. type: Boolean,
  55. value: true
  56. },
  57. imageFit: {
  58. type: String,
  59. value: "scaleToFill"
  60. },
  61. uploadIcon: {
  62. type: String,
  63. value: "photograph"
  64. }
  65. }, _shared.chooseImageProps), _shared.chooseVideoProps),
  66. data: {
  67. lists: [],
  68. isInCount: true
  69. },
  70. methods: {
  71. formatFileList: function formatFileList() {
  72. var _this$data = this.data,
  73. _this$data$fileList = _this$data.fileList,
  74. fileList = _this$data$fileList === void 0 ? [] : _this$data$fileList,
  75. maxCount = _this$data.maxCount;
  76. var lists = fileList.map(function (item) {
  77. return Object.assign(Object.assign({}, item), {
  78. isImage: (0, _utils.isImageFile)(item),
  79. isVideo: (0, _utils.isVideoFile)(item),
  80. deletable: (0, _validator.isBoolean)(item.deletable) ? item.deletable : true
  81. });
  82. });
  83. this.setData({
  84. lists: lists,
  85. isInCount: lists.length < maxCount
  86. });
  87. },
  88. getDetail: function getDetail(index) {
  89. return {
  90. name: this.data.name,
  91. index: index == null ? this.data.fileList.length : index
  92. };
  93. },
  94. startUpload: function startUpload() {
  95. var _this = this;
  96. var _this$data2 = this.data,
  97. maxCount = _this$data2.maxCount,
  98. multiple = _this$data2.multiple,
  99. lists = _this$data2.lists,
  100. disabled = _this$data2.disabled;
  101. if (disabled) return;
  102. (0, _utils.chooseFile)(Object.assign(Object.assign({}, this.data), {
  103. maxCount: maxCount - lists.length
  104. })).then(function (res) {
  105. _this.onBeforeRead(multiple ? res : res[0]);
  106. })["catch"](function (error) {
  107. _this.$emit("error", error);
  108. });
  109. },
  110. onBeforeRead: function onBeforeRead(file) {
  111. var _this2 = this;
  112. var _this$data3 = this.data,
  113. beforeRead = _this$data3.beforeRead,
  114. useBeforeRead = _this$data3.useBeforeRead;
  115. var res = true;
  116. if (typeof beforeRead === "function") {
  117. res = beforeRead(file, this.getDetail());
  118. }
  119. if (useBeforeRead) {
  120. res = new Promise(function (resolve, reject) {
  121. _this2.$emit("before-read", Object.assign(Object.assign({
  122. file: file
  123. }, _this2.getDetail()), {
  124. callback: function callback(ok) {
  125. ok ? resolve() : reject();
  126. }
  127. }));
  128. });
  129. }
  130. if (!res) {
  131. return;
  132. }
  133. if ((0, _validator.isPromise)(res)) {
  134. res.then(function (data) {
  135. return _this2.onAfterRead(data || file);
  136. });
  137. } else {
  138. this.onAfterRead(file);
  139. }
  140. },
  141. onAfterRead: function onAfterRead(file) {
  142. var _this$data4 = this.data,
  143. maxSize = _this$data4.maxSize,
  144. afterRead = _this$data4.afterRead;
  145. var oversize = Array.isArray(file) ? file.some(function (item) {
  146. return item.size > maxSize;
  147. }) : file.size > maxSize;
  148. if (oversize) {
  149. this.$emit("oversize", Object.assign({
  150. file: file
  151. }, this.getDetail()));
  152. return;
  153. }
  154. if (typeof afterRead === "function") {
  155. afterRead(file, this.getDetail());
  156. }
  157. this.$emit("after-read", Object.assign({
  158. file: file
  159. }, this.getDetail()));
  160. },
  161. deleteItem: function deleteItem(event) {
  162. var index = event.currentTarget.dataset.index;
  163. this.$emit("delete", Object.assign(Object.assign({}, this.getDetail(index)), {
  164. file: this.data.fileList[index]
  165. }));
  166. },
  167. onPreviewImage: function onPreviewImage(event) {
  168. if (!this.data.previewFullImage) return;
  169. var index = event.currentTarget.dataset.index;
  170. var lists = this.data.lists;
  171. var item = lists[index];
  172. wx.previewImage({
  173. urls: lists.filter(function (item) {
  174. return (0, _utils.isImageFile)(item);
  175. }).map(function (item) {
  176. return item.url;
  177. }),
  178. current: item.url,
  179. fail: function fail() {
  180. wx.showToast({
  181. title: "预览图片失败",
  182. icon: "none"
  183. });
  184. }
  185. });
  186. },
  187. onPreviewVideo: function onPreviewVideo(event) {
  188. if (!this.data.previewFullImage) return;
  189. var index = event.currentTarget.dataset.index;
  190. var lists = this.data.lists;
  191. wx.previewMedia({
  192. sources: lists.filter(function (item) {
  193. return (0, _utils.isVideoFile)(item);
  194. }).map(function (item) {
  195. return Object.assign(Object.assign({}, item), {
  196. type: "video"
  197. });
  198. }),
  199. current: index,
  200. fail: function fail() {
  201. wx.showToast({
  202. title: "预览视频失败",
  203. icon: "none"
  204. });
  205. }
  206. });
  207. },
  208. onClickPreview: function onClickPreview(event) {
  209. var index = event.currentTarget.dataset.index;
  210. var item = this.data.lists[index];
  211. this.$emit("click-preview", Object.assign(Object.assign({}, item), this.getDetail(index)));
  212. }
  213. }
  214. });