UiFiles1.vue 5.5 KB

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