UploadImage.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <template>
  2. <div
  3. class="com-upload-image"
  4. @click="onSelect"
  5. :style="{'background-image':(dataSRC?`url(${dataSRC})`:'')}"
  6. :class="{'no-border':showBorder== false}"
  7. >
  8. <div :class="{'has-image':dataSRC,hover:hover}">
  9. <upload ref="uploadFile" :limit="limit" media-type="image" :accept-type="acceptType" :to-url="toUrl" @file-change="onFileChange"></upload>
  10. <div class="btn-push" v-show="!dataSRC">
  11. <i class="iconfont icon-works_add"></i>
  12. <span>{{tips}}</span>
  13. </div>
  14. <div class="btn-change">
  15. <button class="ui-button submit">{{$t('common.change')}}</button>
  16. <a class="btn-clear" v-show="showClear" @click.stop="onClear">
  17. <i class="iconfont icon-close"></i>
  18. </a>
  19. </div>
  20. </div>
  21. </div>
  22. </template>
  23. <script>
  24. import Upload from "./Upload";
  25. export default {
  26. name: "UploadImage",
  27. components: {
  28. Upload
  29. },
  30. props: {
  31. src: String,
  32. tips: String,
  33. limit:Number,
  34. acceptType: String,
  35. hover: {
  36. type: Boolean,
  37. default: false
  38. },
  39. showClear: {
  40. type: Boolean,
  41. default: true
  42. },
  43. showBorder: {
  44. type: Boolean,
  45. default: true
  46. },
  47. showBackground: {
  48. type: Boolean,
  49. default: true
  50. },
  51. toUrl: {
  52. type: Boolean,
  53. default: true
  54. },
  55. },
  56. data() {
  57. return {
  58. dataURL: "",
  59. isClear: false
  60. };
  61. },
  62. computed: {
  63. dataSRC() {
  64. if (this.isClear || this.src == "") {
  65. return null;
  66. }
  67. return this.dataURL || this.src || null;
  68. }
  69. },
  70. methods: {
  71. onSelect() {
  72. this.$refs.uploadFile.click();
  73. },
  74. onClear() {
  75. this.$refs.uploadFile.clear();
  76. this.dataURL = "";
  77. this.isClear = true;
  78. this.$emit("file-change", null);
  79. },
  80. onFileChange(file, base64) {
  81. this.isClear = false;
  82. if (this.showBackground) {
  83. this.dataURL = file.dataURL;
  84. }
  85. this.$emit("file-change", file);
  86. }
  87. }
  88. };
  89. </script>
  90. <style lang="less" scoped>
  91. .com-upload-image {
  92. cursor: pointer;
  93. display: inline-block;
  94. min-width: 60px;
  95. min-height: 60px;
  96. width: 100%;
  97. height: 100%;
  98. border-radius: 2px;
  99. border: 1px solid #5d5d5d;
  100. position: relative;
  101. background: #414141;
  102. background-position: center center;
  103. background-size: cover;
  104. &.no-border {
  105. border: none;
  106. }
  107. > div {
  108. position: relative;
  109. display: flex;
  110. width: 100%;
  111. height: 100%;
  112. align-items: center;
  113. justify-content: center;
  114. flex-direction: column;
  115. font-size: 12px;
  116. color: @color;
  117. &.has-image {
  118. background-color: rgba(0, 0, 0, 0.5);
  119. .btn-change {
  120. display: block;
  121. }
  122. &.hover {
  123. background-color: transparent;
  124. opacity: 0;
  125. transition: opacity 0.2s ease-in;
  126. &:hover {
  127. opacity: 1;
  128. background-color: rgba(0, 0, 0, 0.5);
  129. .btn-change {
  130. display: block;
  131. }
  132. }
  133. .btn-change {
  134. display: none;
  135. }
  136. }
  137. }
  138. }
  139. .icon-works_add {
  140. font-size: 28px;
  141. }
  142. .icon-close {
  143. color: #fff;
  144. font-size: 12px;
  145. }
  146. .ui-button {
  147. width: 60px;
  148. }
  149. .btn-push {
  150. text-align: center;
  151. span {
  152. display: block;
  153. }
  154. }
  155. .btn-change {
  156. display: none;
  157. }
  158. .btn-clear {
  159. position: absolute;
  160. right: 0;
  161. top: 0;
  162. width: 20px;
  163. height: 20px;
  164. line-height: 20px;
  165. text-align: center;
  166. background-color: #c77a7a;
  167. border-radius: 50%;
  168. transform: translate(50%, -50%);
  169. &:hover {
  170. background-color: #e85353;
  171. }
  172. }
  173. }
  174. </style>