upImageWall.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <div class="pre-img">
  3. <div v-for="(img, index) in value" :key="index" class="pre-img-item">
  4. <el-image
  5. style="width: 100%; height: 100%"
  6. :src="img"
  7. fit="cover"
  8. :preview-src-list="value"
  9. />
  10. <!--img :src="img"-->
  11. <!--i class="el-alert__icon el-icon-error remove-img" @click="handleRemoveImage(img)"/-->
  12. <div v-if="edit" class="move-control">
  13. <span
  14. class="move-left"
  15. style="font-size: 10px"
  16. @click="moveLeft(index)"
  17. ><i
  18. class="el-icon-caret-left"
  19. /></span>
  20. <span
  21. class="move-left"
  22. style="font-size: 10px"
  23. @click="handleRemoveImage(img)"
  24. ><i
  25. class="el-icon-delete"
  26. /></span>
  27. <span
  28. class="move-right"
  29. style="font-size: 10px"
  30. @click="moveRight(index)"
  31. ><i
  32. class="el-icon-caret-right"
  33. /></span>
  34. </div>
  35. </div>
  36. <el-upload
  37. v-if="edit"
  38. v-loading="loading"
  39. action="https://upload.qiniup.com"
  40. :before-upload="beforeUpload"
  41. :data="upData"
  42. :on-remove="handleRemove"
  43. :on-success="handleSuccess"
  44. :on-error="handleError"
  45. :file-list="imageList"
  46. :show-file-list="false"
  47. element-loading-spinner="el-icon-loading"
  48. name="file"
  49. list-type="picture-card"
  50. multiple
  51. >
  52. <i class="el-icon-plus" />
  53. </el-upload>
  54. </div>
  55. </template>
  56. <script>
  57. import { getToken } from '@/api/qiniu'
  58. export default {
  59. name: 'UpImage',
  60. props: {
  61. value: {
  62. type: Array,
  63. default: function() {
  64. return []
  65. }
  66. },
  67. edit: {
  68. type: Boolean,
  69. default: true
  70. }
  71. },
  72. data() {
  73. return {
  74. upData: {
  75. token: ''
  76. },
  77. loading: false
  78. }
  79. },
  80. computed: {
  81. imageList() {
  82. return this.value
  83. ? this.value.map((v) => {
  84. return { name: 'x', url: v }
  85. })
  86. : {}
  87. }
  88. },
  89. methods: {
  90. beforeUpload(file) {
  91. if (file.size / 1024 / 1024 > 30) {
  92. this.$message.error('上传图片大小不能超过 30MB!')
  93. return false
  94. }
  95. let fileType = ''
  96. if (file.type === 'image/png') {
  97. fileType = 'png'
  98. } else if (file.type === 'image/jpeg') {
  99. fileType = 'jpg'
  100. } else {
  101. this.$message.error('上传图片只能是 JPG 或者 PNG 格式!')
  102. return false
  103. }
  104. const _self = this
  105. return new Promise((resolve, reject) => {
  106. getToken({ file_type: fileType })
  107. .then((response) => {
  108. const key = response.data.key
  109. const token = response.data.token
  110. _self._data.upData.token = token
  111. _self._data.upData.key = key
  112. resolve(true)
  113. })
  114. .catch((err) => {
  115. console.log(err)
  116. reject(false)
  117. })
  118. })
  119. },
  120. emitInput(val) {
  121. for (let i = 0; i < this.value.length; i++) {
  122. if (this.value[i] === val) {
  123. this.value.splice(i, 1)
  124. break
  125. }
  126. }
  127. this.$emit('input', this.value)
  128. },
  129. handleRemoveImage(file) {
  130. console.log(file)
  131. this.emitInput(file)
  132. },
  133. handleRemove(file, fileList) {
  134. this.emitInput(file)
  135. },
  136. handleSuccess(response, file, fileList) {
  137. console.log(fileList)
  138. this.loading = false
  139. this.fileList = fileList
  140. .map((file) =>
  141. file.response
  142. ? {
  143. url: this.$store.getters.setting.img_host + file.response.key
  144. }
  145. : file
  146. )
  147. var value = this.fileList.map((file) => file.url)
  148. console.log(value)
  149. this.$emit('input', value)
  150. },
  151. handleError(err, file, fileList) {
  152. console.log(err)
  153. this.loading = false
  154. this.$notify({
  155. title: '失败',
  156. message: '上传失败:[' + err + ']',
  157. type: 'error'
  158. })
  159. },
  160. moveLeft(index) {
  161. if (index === 0) return
  162. const tmp = this.value[index - 1]
  163. this.value.splice(index - 1, 1)
  164. this.value.splice(index, 0, tmp)
  165. this.$emit('input', this.value)
  166. },
  167. moveRight(index) {
  168. if (index === this.value.length - 1) return
  169. const tmp = this.value[index]
  170. this.value.splice(index, 1)
  171. this.value.splice(index + 1, 0, tmp)
  172. this.$emit('input', this.value)
  173. }
  174. }
  175. }
  176. </script>
  177. <style scoped>
  178. .pre-img {
  179. display: flex;
  180. flex-direction: row;
  181. justify-content: flex-start;
  182. flex-wrap: wrap;
  183. }
  184. .pre-img-item {
  185. position: relative;
  186. width: 120px;
  187. height: 120px;
  188. margin-right: 5px;
  189. /*margin-left: 5px;*/
  190. }
  191. .pre-img-item img {
  192. width: 100%;
  193. height: 100%;
  194. }
  195. .move-control {
  196. width: 0;
  197. height: 0;
  198. overflow: hidden;
  199. background-color: rgba(0, 0, 0, 0.5);
  200. color: white;
  201. font-size: 12px;
  202. position: absolute;
  203. bottom: -2px;
  204. display: flex;
  205. flex-direction: row;
  206. justify-content: space-around;
  207. padding: 0 10px;
  208. cursor: pointer;
  209. }
  210. .pre-img-item:hover .move-control {
  211. width: 100%;
  212. height: auto;
  213. }
  214. .el-upload--picture-card {
  215. width: 50px;
  216. }
  217. .remove-img {
  218. background-color: #fef0f0;
  219. color: #f56c6c;
  220. position: absolute;
  221. right: -3px;
  222. top: -5px;
  223. cursor: pointer;
  224. }
  225. </style>
  226. <style>
  227. .el-upload--picture-card {
  228. height: 120px;
  229. width: 120px;
  230. line-height: 120px;
  231. }
  232. .el-upload--picture-card i {
  233. font-size: 20px;
  234. color: #d4d4d4;
  235. }
  236. </style>