imgUpLoader1.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <div class="img-container" v-loading="loading">
  3. <div class="wrapper" style="position: relative" v-if="url">
  4. <img :src="url" class="img" />
  5. <svg class="closeIcon" aria-hidden="true" @click="deletePic()">
  6. <use xlink:href="#icon-photo-close"></use>
  7. </svg>
  8. </div>
  9. <div class="btn-add" v-else>
  10. <input
  11. type="file"
  12. class="add"
  13. @change="addImg"
  14. ref="inputImg"
  15. accept="image/gif, image/jpeg, image/png, image/jpg"
  16. />
  17. <span v-if="text">{{ text }}</span>
  18. <svg class="icon" v-else aria-hidden="true">
  19. <use xlink:href="#icon-add"></use>
  20. </svg>
  21. </div>
  22. </div>
  23. </template>
  24. <script type="text/ecmascript-6">
  25. export default {
  26. name: 'img-uploader',
  27. data() {
  28. return {
  29. value: [], // 累计上传的轮播图
  30. loading: false, // 页面加载
  31. picList: [], // 上传oss图片列表
  32. url: this.imgUrl || ''
  33. };
  34. },
  35. props: {
  36. imgUrl: {
  37. type: String
  38. },
  39. text: {
  40. type: String
  41. },
  42. index: {
  43. type: Number
  44. }
  45. },
  46. created() {},
  47. mounted() {},
  48. computed: {},
  49. methods: {
  50. addImg: function (e) {
  51. // 鉴别上传重复图片
  52. let value = this.$refs.inputImg.value;
  53. // if (this.value.includes(value)) {
  54. // this.$message({
  55. // message: '图片上传重复,请重新选择!',
  56. // type: 'warning'
  57. // });
  58. // return;
  59. // }
  60. let file = e.target.files[0];
  61. let max = this.maxPicNum;
  62. if (!file) {
  63. return;
  64. }
  65. // 商品相关上传图片
  66. this.loading = true;
  67. this.httpUploadImg({ prefix: 'goods', file: file }).then(
  68. (res) => {
  69. // this.imgList.push(res);
  70. this.$emit('getPics', this.index, res);
  71. this.loading = false;
  72. // 将当前的value赋给this.value,将value置空,避免input重复上传图片不触发change事件
  73. this.value.push(value);
  74. this.$refs.inputImg.value = '';
  75. },
  76. (res) => {}
  77. );
  78. },
  79. deletePic(index) {
  80. this.$emit('getPics', this.index, '');
  81. }
  82. },
  83. watch: {
  84. imgUrl(val) {
  85. this.url = val;
  86. }
  87. }
  88. };
  89. </script>
  90. <style lang="stylus" rel="stylesheet/stylus" scoped>
  91. @import '~assets/main.styl';
  92. .title {
  93. flex-center();
  94. justify-content: flex-start;
  95. word-main();
  96. height: cell-height;
  97. padding-left: 10px;
  98. }
  99. .img-container {
  100. flex-x();
  101. background: white;
  102. justify-content: flex-start;
  103. flex-wrap: wrap;
  104. box-sizing: border-box;
  105. // width: 92%;
  106. margin-right 10px;
  107. .wrapper {
  108. width: 80px;
  109. height: 80px;
  110. margin: 10px 20px 10px 0;
  111. text-align center
  112. flex-x(center, center);
  113. .img {
  114. width: 80px;
  115. height auto;
  116. border-radius: 5px;
  117. }
  118. }
  119. .btn-add {
  120. flex-y();
  121. width: 80px;
  122. height: 80px;
  123. border: 1px solid border-color;
  124. font-size: 10px;
  125. color: gray9;
  126. position: relative;
  127. background: #E8E8E8;
  128. .add {
  129. width: 100%;
  130. height: 100%;
  131. opacity: 0;
  132. position: absolute;
  133. }
  134. }
  135. }
  136. .icon {
  137. fill: border-color;
  138. width: 20px;
  139. height: 20px;
  140. }
  141. .closeIcon {
  142. fill: gray6;
  143. background-color: white;
  144. border-radius: 50%;
  145. width: 20px;
  146. height: 20px;
  147. position: absolute;
  148. top: -10px;
  149. right: -10px;
  150. cursor: pointer;
  151. }
  152. </style>