UiFile.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <el-row class="form-group">
  3. <el-col :span="3" style="padding-top:10px;">
  4. <label style="margin-left:10px;vertical-align: middle;" class="control-label">{{label ? label : '上传'}}</label>
  5. </el-col>
  6. <el-col :span="14">
  7. <div class="layui-input-block-my">
  8. <el-upload class="avatar-uploader upImage"
  9. :action="actiony"
  10. :data="data ? data : {}"
  11. :headers="headers"
  12. :accept="accept ? accept : '.jpg,.png,.jpeg,.gif,.bmp,.webp'"
  13. :size="size ? size : 1"
  14. :imageurl="imageurl ? imageurl : ''"
  15. :on-error="handleAvatarError"
  16. :on-success="handleAvatarSuccess"
  17. :before-upload="beforeAvatarUpload"
  18. :show-file-list="showFileList"
  19. name="file_image"
  20. drag
  21. :multiple="multiple">
  22. <img v-if="imageUrl" style="width:100%;height:100%" :src="imageUrl" class="avatar">
  23. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  24. </el-upload>
  25. <input type="text" style="width:0;height:0.5px;border: 0 solid rgba(255,255,255,0)" v-model="imageUrl" :name="name ? name : 'file'" value="imageUrl"/>
  26. </div>
  27. </el-col>
  28. <el-col :span="7">
  29. <div class="classJs">
  30. <div v-if="tips">
  31. <span class="red">*</span>&ensp;{{tips}}
  32. </div>
  33. </div>
  34. </el-col>
  35. </el-row>
  36. </template>
  37. <script>
  38. export default {
  39. props: ['label','action','data','name','accept','size','imageurl','tips'],
  40. data() {
  41. return {
  42. actiony: this._props.action ? this._props.action : parent.Domain+'/admin/oneImage',
  43. showFileList: false,
  44. multiple: false,
  45. imageUrl: this._props.imageurl ? this._props.imageurl : '',
  46. headers: {
  47. 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  48. }
  49. };
  50. },
  51. mounted() {
  52. $(document).find('.upImage .el-upload-dragger').css({width:180,height:180});
  53. var thumbHoverLayerEl;
  54. $(document).on('mouseover', '.upImage .el-upload-dragger img', function(event) {
  55. var evt = event || window.event;
  56. var th = $(evt.target);
  57. if( th.prop('src') ){
  58. thumbHoverLayerEl = layer.tips('<img src="'+th.prop('src')+'" style="width:40%;height:50%" />', th, {
  59. time : 0,
  60. tips: 4,
  61. maxWidth: '40%'
  62. });
  63. }
  64. });
  65. $(document).on('mouseout', '.upImage .el-upload-dragger img', function(event) {
  66. layer.close(thumbHoverLayerEl);
  67. });
  68. $(document).on('click', '.upImage .el-upload-dragger img', function(event) {
  69. layer.close(thumbHoverLayerEl);
  70. });
  71. },
  72. methods: {
  73. handleAvatarError(err, file, fileList) {
  74. this.$message.error('文件上传被中断 ... ');
  75. },
  76. handleAvatarSuccess(res, file) {
  77. if( res.success != 0 ){
  78. var _input = $(document).find('.upImage');
  79. if( $($(_input).find('input')[0]).val() ){
  80. this.imageUrl = $($(_input).find('input')[0]).val();
  81. }else{
  82. this.imageUrl = '';
  83. }
  84. this.$message.error(res.message);
  85. }else{
  86. //> 上传成功
  87. this.imageUrl = res.url;
  88. var _input = $(document).find('.upImage');
  89. $($(_input).find('input')[0]).prop('type','hidden');
  90. $($(_input).find('input')[0]).val(res.url);
  91. $($(_input).find('input')[0]).prop('type','file');
  92. this.$message.success('上传成功');
  93. }
  94. },
  95. beforeAvatarUpload(file) {
  96. var size = this._props.size;
  97. var accept = null;
  98. if( parseFloat(size) <= 0 || size == undefined ){
  99. size = 1;
  100. }
  101. const isLt1M = parseFloat(file.size / 1024 / 1024) < size;
  102. if( ! this._props.accept ){
  103. accept = '.jpg,.jpeg,.png';
  104. }else{
  105. accept = this._props.accept;
  106. }
  107. var accept_evt = accept.replace(/\./g,'image/');
  108. var cs = new RegExp(file.type);
  109. if( ! cs.test(accept_evt) ){
  110. this.$message.error('上传图片类型不匹配!当前支持上传后缀 "'+accept+'"');
  111. accept = false;
  112. }
  113. if (!isLt1M) {
  114. this.$message.error('上传图片大小不能超过 '+size+'MB!');
  115. }
  116. return isLt1M && accept;
  117. }
  118. }
  119. }
  120. </script>
  121. <style scoped>
  122. .avatar-uploader .el-upload {
  123. border: 1px dashed #d9d9d9;
  124. border-radius: 6px;
  125. cursor: pointer;
  126. position: relative;
  127. overflow: hidden;
  128. }
  129. .avatar-uploader .el-upload:hover {
  130. border-color: #20a0ff;
  131. }
  132. .avatar-uploader-icon {
  133. font-size: 28px;
  134. color: #8c939d;
  135. width: 178px;
  136. height: 178px;
  137. line-height: 178px;
  138. text-align: center;
  139. }
  140. .avatar {
  141. /*width: 178px;*/
  142. height: 178px;
  143. display: block;
  144. }
  145. .layui-input-block-my {
  146. margin-left:0;
  147. min-height: 42px;
  148. }
  149. .classJs {
  150. margin-left: 10px;
  151. vertical-align: middle;
  152. height:42px;
  153. line-height:42px;
  154. }
  155. .red{
  156. color: red;
  157. }
  158. </style>