g-upfile.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <view class="imglistbx">
  3. <view :class="['imglistItem',columnNum==3?'column3':'column4']" v-for="(item,index) in showList" :key='index' style="justify-content: center;text-align: center;">
  4. <image :src="item" class="itemImg" @click="previewImage(index)" mode="aspectFill" v-if="extensionNames[index]=='.jpg'||extensionNames[index]=='.gif'||extensionNames[index]=='.png'" ></image>
  5. <i class="fa fa-file-archive-o" aria-hidden="true" style="font-size:120px;" mode="aspectFill" v-else></i>
  6. <view class="font13" >{{files[index].name}}</view>
  7. <icon size="18" type="cancel" class="cancelBtn" @click="deleteImg(index)" v-if="deleteBtn"></icon>
  8. </view>
  9. <!-- 上传控件 -->
  10. <view :class="['imglistItem',columnNum==3?'column3':'column4']" @click="uploadImg" v-if="control&&showControl">
  11. <view class="itemImg uploadControl">+</view>
  12. </view>
  13. <view class="clear"></view>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. props: {
  19. //是否显示上传控件
  20. control: {
  21. type: Boolean,
  22. default: true
  23. },
  24. //是否显示上删除按钮
  25. deleteBtn: {
  26. type: Boolean,
  27. default: true
  28. },
  29. //行数量
  30. columnNum: {
  31. type: [Number, String],
  32. default: 4
  33. },
  34. //上传最大数量
  35. maxCount: {
  36. type: Number,
  37. default: 4
  38. },
  39. // //允许上传文件类型
  40. // allowFileType: {
  41. // type: [],
  42. // default: []
  43. // },
  44. //最大上传文件大小 Kb为单位,为0,则不限制上传文件大小
  45. maxFileSize: {
  46. type: Number,
  47. default: 0
  48. },
  49. //服务返回回调的图片数组--回填
  50. mode: {
  51. type: Array,
  52. default: function() {
  53. return []
  54. }
  55. }
  56. },
  57. data() {
  58. return {
  59. imgList: [],
  60. showList: [],
  61. showControl: true,
  62. files:[],
  63. extensionNames:[]
  64. }
  65. },
  66. watch: {
  67. mode(v) {
  68. this.init(v)
  69. },
  70. showList(){
  71. if (this.showList.length >= this.maxCount) {
  72. this.showControl = false
  73. return
  74. };
  75. this.showControl = true;
  76. }
  77. },
  78. created() {
  79. this.init(this.mode)
  80. },
  81. methods: {
  82. init(v) {
  83. if (this.mode.length != 0) {
  84. this.showList = v;
  85. return
  86. };
  87. this.showList = this.imgList;
  88. },
  89. // 上传图片
  90. uploadImg() {
  91. //console.info('chooseFiel',uni.chooseFile);
  92. uni.chooseFile({
  93. count: this.maxCount,
  94. extension:['.jpg','.png','.gif','.zip','.rar','.docx','.doc'],
  95. success: (chooseImageRes) => {
  96. let _this=this;
  97. console.info('chooseImageRes:',chooseImageRes);
  98. let tempFilePaths = chooseImageRes.tempFilePaths;
  99. let tempFiles=chooseImageRes.tempFiles;
  100. // console.info('upload start');
  101. // console.info('tempFilePaths',tempFilePaths);
  102. // console.info('tempFiles',tempFiles);
  103. let greaterArr=[];
  104. tempFiles.forEach((item,index)=>{
  105. if(_this.maxFileSize!=0){
  106. if((item.size/1024)>_this.maxFileSize){
  107. //console.info('item.name',item.name);
  108. greaterArr.push(item.name);
  109. }
  110. }
  111. });
  112. if(greaterArr.length>0){
  113. uni.showModal({
  114. title:'warning',
  115. content:'file:'+greaterArr.join()+' size greater than '+_this.maxFileSize+' kb',
  116. success:function(res){
  117. }
  118. });
  119. return;
  120. }
  121. tempFilePaths=tempFilePaths.slice(0,this.maxCount-this.showList.length);
  122. tempFiles.forEach((item,index)=>{
  123. _this.files.push(item);
  124. //console.info('文件大小',item.size);
  125. //获取文件扩展名
  126. let index1 = item.name.lastIndexOf(".");
  127. let suffix = item.name.substr(index1);
  128. //console.info('suffix',suffix);
  129. this.extensionNames.push(suffix);
  130. });
  131. //return;
  132. // console.info('pop window end');
  133. // console.info('tempFilePaths',tempFilePaths);
  134. // console.info('tempFiles',tempFiles);
  135. tempFilePaths.forEach((item) => {
  136. this.imgList.push(item);
  137. });
  138. this.$emit("chooseFile", this.imgList, tempFilePaths)
  139. }
  140. });
  141. },
  142. //删除图片
  143. deleteImg(eq) {
  144. let deleteImg = this.imgList;
  145. deleteImg.splice(eq, 1); //删除临时路径
  146. if (this.mode.length > 0) {
  147. let deleteImg = this.showList;
  148. deleteImg.splice(eq, 1); //删除服务那边的路径
  149. }
  150. this.$emit("imgDelete", this.handleImg(), eq)
  151. },
  152. // 预览图片
  153. previewImage(eq) {
  154. let getUrl = this.handleImg();
  155. uni.previewImage({
  156. current: getUrl[eq],
  157. urls: getUrl
  158. })
  159. },
  160. //返回需要操作的图片数组
  161. //如果是回调了则操作回填后的数组 否则操作临时路径的图片数组
  162. handleImg() {
  163. return this.mode.length > 0 ? this.showList : this.imgList
  164. },
  165. }
  166. }
  167. </script>
  168. <style scoped>
  169. /* 上传 str */
  170. .imglistbx {
  171. width: 100%;
  172. height: 100%;
  173. }
  174. .imglistItem {
  175. position: relative;
  176. float: left;
  177. margin-bottom: 20rpx;
  178. border-radius: 10rpx;
  179. }
  180. .column3 {
  181. width: 33.3333%;
  182. height: 160rpx;
  183. }
  184. .column4 {
  185. width: 25%;
  186. height: 130rpx;
  187. }
  188. .itemImg {
  189. width: 70%;
  190. height: 100%;
  191. margin: 0 auto;
  192. display: block;
  193. border-radius: 10rpx;
  194. }
  195. .cancelBtn {
  196. position: absolute;
  197. top: -10rpx;
  198. right: 10rpx;
  199. }
  200. /* 上传控件 */
  201. .uploadControl {
  202. font-size: 50rpx;
  203. color: #888;
  204. background-color: #EEEEEE;
  205. display: flex;
  206. justify-content: center;
  207. align-items: center;
  208. }
  209. /* 上传 str end*/
  210. .clear {
  211. clear: both;
  212. }
  213. </style>