UiFiles.vue 6.1 KB

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