upload.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <div>
  3. <div class="flex">
  4. <el-upload
  5. class="avatar-uploader"
  6. list-type="picture-card"
  7. :action="action"
  8. :on-remove="handleRemove"
  9. :accept="acceptType"
  10. :headers="myHeaders"
  11. :multiple="limitNum == 1?false:true"
  12. :limit='limitNum'
  13. :on-exceed="handleExceed"
  14. :on-change='handleChangeUpload'
  15. :on-success="handleSuccess"
  16. :on-preview="handlePictureCardPreview"
  17. :file-list="fileList"
  18. :show-file-list="acceptType =='.jpg, .jpeg, .png'?false:true"
  19. :disabled="disabled"
  20. :auto-upload="autoUpload"
  21. >
  22. <i class="el-icon-plus avatar-uploader-icon"></i>
  23. </el-upload>
  24. </div>
  25. <span class="tip" v-if="warning">{{warning}}</span>
  26. <el-dialog
  27. title="查看"
  28. :visible.sync="dialogVisible"
  29. width="500px"
  30. :close-on-click-modal="false"
  31. >
  32. <img :src="dialogImageUrl" style="width:100%;"/>
  33. <span slot="footer" class="dialog-footer">
  34. <el-button type="info" @click="dialogVisible = false">关闭</el-button>
  35. </span>
  36. </el-dialog>
  37. </div>
  38. </template>
  39. <script>
  40. import store from '@/store'
  41. export default {
  42. name: "index",
  43. components: {
  44. },
  45. props: {
  46. warning: {
  47. type: String,
  48. default: ''
  49. },
  50. file: {
  51. type: Array,
  52. default: () => []
  53. },
  54. disabled: {
  55. type: Boolean,
  56. default: false
  57. },
  58. acceptType: {
  59. type: String,
  60. default: ''
  61. },
  62. limitNum: {
  63. type: Number,
  64. default: 3
  65. },
  66. uploadUrl: {
  67. type: String,
  68. default: '/file/img/upload'
  69. },
  70. autoUpload: {
  71. type: Boolean,
  72. default: true
  73. }
  74. },
  75. data() {
  76. return {
  77. fileList: [],
  78. dialogVisible: false,
  79. imgUrl: '',
  80. baseUrl: '',
  81. dialogImageUrl:''
  82. };
  83. },
  84. computed: {
  85. action(){
  86. return global_config.BASE_URL + this.uploadUrl;
  87. },
  88. myHeaders(){
  89. return {Authorization: 'Bearer ' + store.getters.getToken, 'X-Token':store.getters.getToken};
  90. }
  91. },
  92. created() {
  93. this.fileList = this.file;
  94. },
  95. methods: {
  96. // 手动上传
  97. handleChangeUpload(fileData){
  98. if(!this.autoUpload){
  99. let event = event || window.event;
  100. let file = event.target.files[0];
  101. let reader = new FileReader();
  102. let that = this;
  103. reader.onload = function(e) {
  104. that.baseUrl = e.target.result;
  105. that.$emit('changeUpload', fileData, that.baseUrl);
  106. }
  107. reader.readAsDataURL(file);
  108. }
  109. },
  110. // 上传成功
  111. handleSuccess(response, file, fileList){
  112. this.fileList.push({
  113. name: response.fileName,
  114. url: response.message
  115. });
  116. },
  117. // 删除
  118. handleRemove(file, fileList) {
  119. this.fileList = fileList;
  120. },
  121. // 上传超过限制
  122. handleExceed(files, fileList) {
  123. if(fileList.length >= this.limitNum){
  124. this.$message.closeAll();
  125. this.$message.warning(`当前限制选择${this.limitNum}个文件`);
  126. }
  127. },
  128. handlePictureCardPreview(file){
  129. this.dialogImageUrl = file.url;
  130. this.dialogVisible = true;
  131. },
  132. view(index){
  133. this.dialogVisible = true;
  134. this.imgUrl = this.fileList[index].url;
  135. },
  136. del(index){
  137. this.fileList.splice(index, 1);
  138. }
  139. }
  140. };
  141. </script>
  142. <style lang="less" scoped>
  143. .file-list{
  144. margin-right: 5px;
  145. position: relative;
  146. width: 150px;
  147. height: 150px;
  148. display: flex;
  149. justify-content: center;
  150. align-items: center;
  151. border: 1px dashed #d9d9d9;
  152. border-radius: 6px;
  153. &:hover{
  154. .mask{
  155. opacity: 1;
  156. }
  157. }
  158. .mask{
  159. opacity: 0;
  160. position: absolute;
  161. top: 0;
  162. bottom: 0;
  163. width: 100%;
  164. background: rgba(0, 0, 0, 0.4);
  165. border-radius: 6px;
  166. color: #fff;
  167. display: flex;
  168. align-items: center;
  169. justify-content: center;
  170. font-size: 18px;
  171. i{
  172. cursor:pointer;
  173. margin: 0 10px;
  174. }
  175. }
  176. img{
  177. max-width: 150px;
  178. max-height: 150px;
  179. display: block;
  180. }
  181. }
  182. // .avatar-uploader{
  183. // line-height: initial;
  184. // width: 150px;
  185. // }
  186. .avatar-uploader /deep/.el-upload {
  187. border: 1px dashed #d9d9d9;
  188. border-radius: 6px;
  189. cursor: pointer;
  190. position: relative;
  191. overflow: hidden;
  192. }
  193. .avatar-uploader /deep/.el-upload:hover {
  194. border-color: #409EFF;
  195. }
  196. .avatar-uploader-icon {
  197. font-size: 28px;
  198. color: #8c939d;
  199. width: 148px;
  200. height: 148px;
  201. line-height: 148px;
  202. text-align: center;
  203. }
  204. .avatar {
  205. width: 148px;
  206. height: 148px;
  207. display: block;
  208. }
  209. .tip{
  210. color:#f56c6c;
  211. font-size:14px;
  212. }
  213. </style>