Attachment.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import {copyObj, rand} from "../util.js";
  2. const template=`
  3. <component is="style">
  4. .imgbox{
  5. display: flex;
  6. align-items: center;
  7. flex-wrap: wrap;
  8. }
  9. .imgbox .imgli{
  10. width: 100px;
  11. height: 100px;
  12. margin-right: 10px;
  13. overflow: hidden;
  14. position: relative;
  15. border: 1px dashed #ccc;
  16. cursor: pointer;
  17. }
  18. .imgbox .imgli .fa-times{
  19. position: absolute;
  20. right: 0px;
  21. top: 0px;
  22. background: var(--el-color-danger);
  23. color: #fff;
  24. padding:2px 4px;
  25. border-radius: 50%;
  26. cursor: pointer;
  27. }
  28. .imgbox .add{
  29. border: 1px dashed #ccc;
  30. display: flex;
  31. justify-content: center;
  32. align-items: center;
  33. cursor: pointer;
  34. }
  35. .imgbox .add span{
  36. margin-left: 5px;
  37. }
  38. </component>
  39. <div class="imgbox" :id="'imgbox-'+id">
  40. <div class="imgli" v-for="(item,index) in imgs" @click="preview(item)">
  41. <img :src="item" style="width: 100px;"/>
  42. <i class="fa fa-times" @click.stop="removeFile(index)"></i>
  43. </div>
  44. <div class="imgli add" @click="openLayer" v-if="limit-imgs.length>0">
  45. <i class="fa fa-plus"></i>
  46. <span>添加图片</span>
  47. </div>
  48. </div>
  49. `;
  50. function moveRow(arr, oldIndex, newIndex){
  51. let value=arr[oldIndex];
  52. arr.splice(oldIndex,1);
  53. arr.splice(newIndex,0,value);
  54. }
  55. export default {
  56. name: "Attachment",
  57. data: function () {
  58. return {
  59. id:'',
  60. imgs:[]
  61. }
  62. },
  63. props:{
  64. limit:{
  65. type:Number,
  66. default:5
  67. },
  68. field:{
  69. type:String,
  70. default: ''
  71. },
  72. value:''
  73. },
  74. created:function (){
  75. this.id=rand(10000,99999);
  76. },
  77. mounted:function (){
  78. if(this.value && typeof this.value=='string'){
  79. this.imgs=this.value.split(',');
  80. }
  81. if(this.value && this.value instanceof Array){
  82. this.imgs=this.value;
  83. }
  84. this.rowDrop();
  85. },
  86. emits:['change'],
  87. template:template,
  88. methods:{
  89. rowDrop:function(){
  90. const tbody = document.querySelector('#imgbox-'+this.id);
  91. const _this = this;
  92. Sortable.create(tbody, {
  93. draggable: ".imgli",
  94. animation: 100,
  95. handle:'.imgli',
  96. onEnd ({ newIndex, oldIndex }) {
  97. //移动图片所在数组中的位置
  98. let imgs=copyObj(_this.imgs);
  99. _this.imgs=[];
  100. moveRow(imgs,oldIndex,newIndex);
  101. Vue.nextTick(()=>{
  102. if(_this.field){
  103. _this.$emit('change',{field: _this.field,value:imgs.join(',')});
  104. _this.imgs=imgs;
  105. }else{
  106. _this.$emit('change',imgs.join(','));
  107. }
  108. });
  109. }
  110. });
  111. },
  112. preview:function (url){
  113. Yunqi.api.previewImg(url);
  114. },
  115. removeFile:function (index){
  116. this.imgs.splice(index,1);
  117. if(this.field){
  118. this.$emit('change',{field: this.field,value:this.imgs.join(',')});
  119. }else{
  120. this.$emit('change',this.imgs.join(','));
  121. }
  122. },
  123. openLayer:function (){
  124. let that=this;
  125. let number=this.limit-this.imgs.length;
  126. if(number<=0){
  127. Yunqi.message.error('最多选择'+this.limit+'张图片');
  128. return;
  129. }
  130. Yunqi.api.open({
  131. url:'general/attachment/select?limit='+number,
  132. title:'选择图片',
  133. icon:'fa fa-image',
  134. width:1000,
  135. height:550,
  136. close:function (e){
  137. if(!e){
  138. return;
  139. }
  140. that.imgs=that.imgs.concat(e);
  141. if(that.field){
  142. that.$emit('change',{field: that.field,value:that.imgs.join(',')});
  143. }else{
  144. that.$emit('change',that.imgs.join(','));
  145. }
  146. }
  147. });
  148. }
  149. }
  150. };