Upload.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <div class="form-com-upload">
  3. <el-upload
  4. :file-list="fileList"
  5. v-bind="configs"
  6. :action="imgUploadUrl"
  7. :show-file-list="configs.multiple||false"
  8. :on-success="afterUploadPic"
  9. :on-remove="removePic"
  10. :on-error="onError"
  11. :on-preview="handlePictureCardPreview"
  12. :before-upload="beforeUploadPic">
  13. <div class="avatar-uploader" v-if="!configs.multiple" :class="configs.round? 'avatar-uploader-round' : ''">
  14. <img v-if="imgList&&imgList.length" :src="setImgUrl(imgList[0])" class="avatar">
  15. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  16. </div>
  17. <i v-else class="el-icon-plus"></i>
  18. </el-upload>
  19. <el-dialog :visible.sync="dialogVisible">
  20. <img width="100%" :src="dialogImageUrl" alt="">
  21. </el-dialog>
  22. </div>
  23. </template>
  24. <script>
  25. import {imgCompress} from '../../utils/imgCompress.js'
  26. import Tools from '../../utils/tool'
  27. export default {
  28. name: 'FormComUpload',
  29. props: {
  30. 'configs':{},
  31. value:{},
  32. },
  33. components:{
  34. },
  35. computed:{
  36. },
  37. watch:{
  38. value:{
  39. handler(n){
  40. if(this.isUploaded){
  41. return
  42. }
  43. if(!n){
  44. this.imgList = []
  45. this.fileList = []
  46. return
  47. }
  48. var list = []
  49. if(typeof n == 'string'){
  50. this.imgList = [n]
  51. list.push({
  52. name: '',
  53. url: this.setImgUrl(n),
  54. response: {
  55. obj: {
  56. fullUri: n
  57. }
  58. }
  59. })
  60. } else {
  61. this.imgList = n
  62. this.imgList.forEach(v=>{
  63. list.push({
  64. name: '',
  65. url: this.setImgUrl(v),
  66. response: {
  67. obj: {
  68. fullUri: v
  69. }
  70. }
  71. })
  72. })
  73. }
  74. this.fileList = list
  75. },
  76. immediate: true,
  77. deep: true
  78. }
  79. },
  80. data(){
  81. return {
  82. imgUploadUrl: Tools.upload_stream_url, // 图片服务器地址
  83. loading: "",
  84. imgList: [],
  85. dialogImageUrl: "",
  86. dialogVisible: false,
  87. fileList: [],
  88. isUploaded: false
  89. }
  90. },
  91. created(){
  92. },
  93. mounted() {
  94. },
  95. methods: {
  96. handlePictureCardPreview(file) {
  97. if(this.configs.multiple){
  98. this.dialogImageUrl = file.url;
  99. this.dialogVisible = true;
  100. }
  101. },
  102. beforeUploadPic(file) {
  103. if (file.size / 1024 / 1024 > 20) {
  104. this.$alert('上传图片不能超过20m', '提示')
  105. return false
  106. }
  107. this.loading = this.$loading({
  108. background: 'rgba(0, 0, 0, 0)',
  109. text: '上传中'
  110. })
  111. return imgCompress(file, this.loading)
  112. },
  113. afterUploadPic(response, file, fileList) {
  114. if (this.loading) {
  115. this.loading.close()
  116. }
  117. if(this.configs.multiple){
  118. this.imgList.push(response.obj.fullUri)
  119. this.$emit('input', this.imgList)
  120. } else {
  121. this.imgList = [response.obj.fullUri]
  122. this.$emit('input', this.imgList[0])
  123. }
  124. this.isUploaded = true
  125. },
  126. onError(){
  127. if (this.loading) {
  128. this.loading.close()
  129. }
  130. this.$message.error("上传失败")
  131. },
  132. removePic(file, fileList){
  133. var i = this.imgList.indexOf(file.response.obj.fullUri)
  134. this.imgList.splice(i, 1)
  135. console.log(this.imgList)
  136. },
  137. setImgUrl(url){
  138. return (Tools.formatImgUrl&&Tools.formatImgUrl(url)) || url
  139. }
  140. }
  141. }
  142. </script>
  143. <style lang="scss">
  144. .form-com-upload{
  145. line-height: 0;
  146. .avatar-uploader{
  147. width: 120px;
  148. height: 120px;
  149. display: -webkit-box;
  150. -webkit-box-align: center;
  151. -webkit-box-pack: center;
  152. overflow: hidden;
  153. border: 1px solid #ccc;
  154. img{
  155. width: 100%;
  156. height: 100%;
  157. display: block;
  158. }
  159. &.avatar-uploader-round{
  160. border-radius: 50%;
  161. }
  162. }
  163. }
  164. </style>