index.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <div class="upload-control">
  3. <div class="section">
  4. <div class="upload" @click="chooseFile">
  5. <input type="file" v-show="false" ref="fileInput" :accept="accept" @change="onFileInputChange" />
  6. <span class="iconfont icon-add"></span>
  7. </div>
  8. <template v-if="showImage">
  9. <div class="remove"><span class="iconfont icon-delete"></span></div>
  10. <div class="preview"><img src="" alt="" /></div>
  11. </template>
  12. </div>
  13. <div class="tip" v-if="tipText" v-text="tipText"></div>
  14. </div>
  15. </template>
  16. <script>
  17. export default {
  18. props: {
  19. tipText: String,
  20. action: {
  21. type: String,
  22. default: () => process.env.BASE_URL + '/wx/upload/image',
  23. },
  24. limit: {
  25. type: Number,
  26. default: 1,
  27. },
  28. accept: {
  29. type: String,
  30. default: '.jpg,.png,.gif',
  31. },
  32. fileList: {
  33. type: Array,
  34. default: () => [],
  35. },
  36. },
  37. computed: {
  38. showImage() {
  39. return this.fileList.length > 0
  40. },
  41. },
  42. methods: {
  43. chooseFile() {
  44. this.$nextTick(() => {
  45. this.$refs.fileInput.click()
  46. })
  47. },
  48. onFileInputChange(e) {
  49. console.log(e.target.files)
  50. },
  51. },
  52. }
  53. </script>
  54. <style lang="scss" scoped>
  55. @media screen and (min-width: 768px) {
  56. .upload-control {
  57. .section {
  58. width: 180px;
  59. height: 180px;
  60. position: relative;
  61. margin: 0 auto;
  62. .remove {
  63. position: absolute;
  64. right: 0;
  65. top: 0;
  66. width: 28px;
  67. height: 28px;
  68. line-height: 28px;
  69. text-align: center;
  70. background: rgba(0, 0, 0, 0.4);
  71. z-index: 1;
  72. cursor: pointer;
  73. .icon-delete {
  74. font-size: 18px;
  75. color: #f1f1f1;
  76. }
  77. }
  78. .preview {
  79. position: absolute;
  80. width: 100%;
  81. height: 100%;
  82. left: 0;
  83. top: 0;
  84. img {
  85. display: block;
  86. width: 100%;
  87. height: 100%;
  88. object-fit: contain;
  89. }
  90. }
  91. .upload {
  92. width: 100%;
  93. height: 100%;
  94. border: 1px dashed #bec3cc;
  95. display: flex;
  96. justify-content: center;
  97. align-items: center;
  98. background: #fbfbfb;
  99. box-sizing: border-box;
  100. .icon-add {
  101. font-size: 36px;
  102. color: #bec3cc;
  103. }
  104. }
  105. }
  106. .tip {
  107. width: 266px;
  108. margin: 0 auto;
  109. margin-top: 36px;
  110. line-height: 24px;
  111. font-size: 14px;
  112. color: #b2b2b2;
  113. text-align: center;
  114. }
  115. }
  116. }
  117. @media screen and (max-width: 768px) {
  118. .upload-control {
  119. .section {
  120. width: 42vw;
  121. height: 42vw;
  122. position: relative;
  123. margin: 0 auto;
  124. .remove {
  125. position: absolute;
  126. right: 0;
  127. top: 0;
  128. width: 5vw;
  129. height: 5vw;
  130. line-height: 5vw;
  131. text-align: center;
  132. background: rgba(0, 0, 0, 0.4);
  133. z-index: 1;
  134. cursor: pointer;
  135. .icon-delete {
  136. font-size: 3.6vw;
  137. color: #f1f1f1;
  138. }
  139. }
  140. .preview {
  141. position: absolute;
  142. width: 100%;
  143. height: 100%;
  144. left: 0;
  145. top: 0;
  146. img {
  147. display: block;
  148. width: 100%;
  149. height: 100%;
  150. object-fit: contain;
  151. }
  152. }
  153. .upload {
  154. width: 100%;
  155. height: 100%;
  156. border: 0.1vw dashed #bec3cc;
  157. display: flex;
  158. justify-content: center;
  159. align-items: center;
  160. background: #fbfbfb;
  161. box-sizing: border-box;
  162. .icon-add {
  163. font-size: 7.2vw;
  164. color: #bec3cc;
  165. }
  166. }
  167. }
  168. .tip {
  169. width: 57vw;
  170. margin: 0 auto;
  171. margin-top: 4.8vw;
  172. line-height: 5.6vw;
  173. font-size: 3.2vw;
  174. color: #b2b2b2;
  175. text-align: center;
  176. }
  177. }
  178. }
  179. </style>