UiImgUpload.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. :on-error="handleAvatarError"
  15. :on-success="handleAvatarSuccess"
  16. :before-upload="beforeAvatarUpload"
  17. name="file_image"
  18. drag
  19. :multiple="multiple"
  20. :list-type="listType?listType:'picture-card'"
  21. :show-file-list="showFileList"
  22. :file-list="fileList"
  23. @change="selectChange">
  24. <el-button v-if="listType==='text'" size="small" type="primary">点击上传</el-button>
  25. <i v-else slot="default" class="el-icon-plus"></i>
  26. </el-upload>
  27. </div>
  28. </el-col>
  29. <el-col :span="7">
  30. <div class="classJs">
  31. <div v-if="tips">
  32. <span class="red">*</span>&ensp;{{tips}}
  33. </div>
  34. </div>
  35. </el-col>
  36. </el-row>
  37. </template>
  38. <script>
  39. export default {
  40. props: ['label','action','data','name','accept','size','multiple','imgNum','imageurl','tips','imgtype','value','listType'],
  41. data() {
  42. return {
  43. actiony: this._props.action ? this._props.action : parent.Domain+'/admin/oneImage',
  44. showFileList: true,
  45. fileList: [],
  46. imageUrl: [],
  47. headers: {
  48. 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  49. }
  50. };
  51. },
  52. mounted() {
  53. console.log('imgNum------------------------'+this.imgNum)
  54. console.log('imgtype------------------------'+this.imgtype)
  55. console.log('size------------------------'+this.size)
  56. $(document).find('.upImage .el-upload-dragger').css({width:160,height:160});
  57. var thumbHoverLayerEl;
  58. $(document).on('mouseover', '.upImage .el-upload-dragger img', function(event) {
  59. var evt = event || window.event;
  60. var th = $(evt.target);
  61. if( th.prop('src') ){
  62. thumbHoverLayerEl = layer.tips('<img src="'+th.prop('src')+'" style="width:40%;height:50%" />', th, {
  63. time : 0,
  64. tips: 4,
  65. maxWidth: '40%'
  66. });
  67. }
  68. });
  69. $(document).on('mouseout', '.upImage .el-upload-dragger img', function(event) {
  70. layer.close(thumbHoverLayerEl);
  71. });
  72. $(document).on('click', '.upImage .el-upload-dragger img', function(event) {
  73. layer.close(thumbHoverLayerEl);
  74. });
  75. if(this.value){
  76. }
  77. },
  78. watch:{
  79. value(val) {
  80. // if (val) {
  81. // if (typeof val === 'string') {
  82. // let list = val.split(';');
  83. // list.forEach((url) => {
  84. // this.setFileUrl(url);
  85. // })
  86. // } else {
  87. // val.forEach((url) => {
  88. // if (typeof url === 'string') {
  89. // this.setFileUrl(url);
  90. // } else {
  91. // this.setFileUrl(url.url);
  92. // }
  93. // })
  94. // }
  95. // }
  96. },
  97. },
  98. methods: {
  99. setValue(){
  100. let val=this.value
  101. if (val) {
  102. if (typeof val === 'string') {
  103. let list = val.split(';');
  104. list.forEach((url) => {
  105. this.setFileUrl(url);
  106. })
  107. } else {
  108. val.forEach((url) => {
  109. if (typeof url === 'string') {
  110. this.setFileUrl(url);
  111. } else {
  112. this.setFileUrl(url.url);
  113. }
  114. })
  115. }
  116. }
  117. },
  118. setFileUrl(){
  119. let isAdd = true;
  120. this.fileList.forEach((v, k) => {
  121. if (v.url === url) {
  122. isAdd = false;
  123. }
  124. })
  125. if (isAdd) {
  126. this.fileList.push({
  127. name: this.getFileName(url),
  128. url: url
  129. });
  130. }
  131. console.log(this.fileList);
  132. },
  133. selectChange(value) {
  134. this.$emit('set-keys',value);
  135. },
  136. handleAvatarError(err, file, fileList) {
  137. this.$message.error('文件上传被中断 ... ');
  138. },
  139. handleAvatarSuccess(res, file) {
  140. if( res.success != 0 ){
  141. this.$message.error(res.message);
  142. }else{
  143. //> 上传成功
  144. file.url = res.url;
  145. this.imageUrl.push(res.url)
  146. console.log(this.imgType+'-----------------------------')
  147. if (this.imgtype*1 !== 1) {
  148. this.$emit('input', this.imageUrl)
  149. } else {
  150. this.$emit('input', this.imageUrl.join(';'))
  151. }
  152. this.$message.success('上传成功');
  153. }
  154. },
  155. beforeAvatarUpload(file) {
  156. var size = this._props.size;
  157. var accept = null;
  158. if( parseFloat(size) <= 0 || size == undefined ){
  159. size = 1;
  160. }
  161. const isLt1M = parseFloat(file.size / 1024 / 1024) < size;
  162. if( ! this._props.accept ){
  163. accept = '.jpg,.jpeg,.png';
  164. }else{
  165. accept = this._props.accept;
  166. }
  167. var accept_evt = accept.replace(/\./g,'image/');
  168. var cs = new RegExp(file.type);
  169. if( ! cs.test(accept_evt) ){
  170. this.$message.error('上传图片类型不匹配!当前支持上传后缀 "'+accept+'"');
  171. accept = false;
  172. }
  173. if (!isLt1M) {
  174. this.$message.error('上传图片大小不能超过 '+size+'MB!');
  175. }
  176. return isLt1M && accept;
  177. }
  178. }
  179. }
  180. </script>
  181. <style scoped>
  182. .control-label{
  183. color: #606266;
  184. }
  185. .avatar-uploader .el-upload {
  186. border: 1px dashed #d9d9d9;
  187. border-radius: 6px;
  188. cursor: pointer;
  189. position: relative;
  190. overflow: hidden;
  191. }
  192. .avatar-uploader .el-upload:hover {
  193. border-color: #20a0ff;
  194. }
  195. .avatar-uploader-icon {
  196. font-size: 28px;
  197. color: #8c939d;
  198. width: 178px;
  199. height: 178px;
  200. line-height: 178px;
  201. text-align: center;
  202. }
  203. .avatar {
  204. /*width: 178px;*/
  205. height: 178px;
  206. display: block;
  207. }
  208. .layui-input-block-my {
  209. margin-left:0;
  210. min-height: 42px;
  211. }
  212. .classJs {
  213. margin-left: 10px;
  214. vertical-align: middle;
  215. height:42px;
  216. line-height:42px;
  217. }
  218. .red{
  219. color: red;
  220. }
  221. </style>