SkuImgUploader.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.default = void 0;
  5. var _utils = require("../../utils");
  6. var _uploader = _interopRequireDefault(require("../../uploader"));
  7. // Utils
  8. // Components
  9. var namespace = (0, _utils.createNamespace)('sku-img-uploader');
  10. var createComponent = namespace[0];
  11. var t = namespace[2];
  12. var _default = createComponent({
  13. props: {
  14. value: String,
  15. uploadImg: Function,
  16. customUpload: Function,
  17. maxSize: {
  18. type: Number,
  19. default: 6
  20. }
  21. },
  22. data: function data() {
  23. return {
  24. fileList: []
  25. };
  26. },
  27. watch: {
  28. value: function value(val) {
  29. if (val) {
  30. this.fileList = [{
  31. url: val,
  32. isImage: true
  33. }];
  34. } else {
  35. this.fileList = [];
  36. }
  37. }
  38. },
  39. methods: {
  40. afterReadFile: function afterReadFile(file) {
  41. var _this = this;
  42. file.status = 'uploading';
  43. file.message = t('uploading');
  44. this.uploadImg(file.file, file.content).then(function (img) {
  45. file.status = 'done';
  46. _this.$emit('input', img);
  47. }).catch(function () {
  48. file.status = 'failed';
  49. file.message = t('fail');
  50. });
  51. },
  52. onOversize: function onOversize() {
  53. this.$toast(t('oversize', this.maxSize));
  54. },
  55. onDelete: function onDelete() {
  56. this.$emit('input', '');
  57. },
  58. onClickUpload: function onClickUpload() {
  59. var _this2 = this;
  60. if (this.customUpload) {
  61. this.customUpload().then(function (url) {
  62. _this2.fileList.push({
  63. url: url
  64. });
  65. _this2.$emit('input', url);
  66. });
  67. }
  68. }
  69. },
  70. render: function render() {
  71. var _this3 = this;
  72. var h = arguments[0];
  73. return h(_uploader.default, {
  74. "attrs": {
  75. "maxCount": 1,
  76. "readonly": !!this.customUpload,
  77. "maxSize": this.maxSize * 1024 * 1024,
  78. "afterRead": this.afterReadFile
  79. },
  80. "on": {
  81. "oversize": this.onOversize,
  82. "delete": this.onDelete,
  83. "click-upload": this.onClickUpload
  84. },
  85. "model": {
  86. value: _this3.fileList,
  87. callback: function callback($$v) {
  88. _this3.fileList = $$v;
  89. }
  90. }
  91. });
  92. }
  93. });
  94. exports.default = _default;