g-upfile.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 iconSize" aria-hidden="true" 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: [Array],
  42. default: ()=>['.jpg','.png','.gif','.zip','.rar','.docx','.doc']
  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. invaliadFiles:[] //无效文件
  65. }
  66. },
  67. watch: {
  68. mode(v) {
  69. this.init(v)
  70. },
  71. showList(){
  72. if (this.showList.length >= this.maxCount) {
  73. this.showControl = false
  74. return
  75. };
  76. this.showControl = true;
  77. }
  78. },
  79. created() {
  80. this.init(this.mode)
  81. },
  82. methods: {
  83. init(v) {
  84. if (this.mode.length != 0) {
  85. this.showList = v;
  86. return
  87. };
  88. this.showList = this.imgList;
  89. },
  90. // 上传文件
  91. uploadImg() {
  92. //console.info('chooseFiel',uni.chooseFile);
  93. uni.chooseFile({
  94. count: this.maxCount,
  95. extension:this.allowFileType,
  96. success: (chooseImageRes) => {
  97. let _this=this;
  98. console.info('chooseImageRes:',chooseImageRes);
  99. let tempFilePaths = chooseImageRes.tempFilePaths;
  100. let tempFiles=chooseImageRes.tempFiles;
  101. // console.info('upload start');
  102. // console.info('tempFilePaths',tempFilePaths);
  103. // console.info('tempFiles',tempFiles);
  104. let greaterArr=[];
  105. let invalidFiles=[];
  106. tempFiles.forEach((item,index)=>{
  107. if(_this.maxFileSize!=0){
  108. if((item.size/1024)>_this.maxFileSize){
  109. //console.info('item.name',item.name);
  110. greaterArr.push(item.name);
  111. }
  112. }
  113. if(_this.allowFileType.length>0){
  114. //获取文件扩展名
  115. let index1 = item.name.lastIndexOf(".");
  116. let suffix = item.name.substr(index1);
  117. if(_this.allowFileType.indexOf(suffix)==-1){
  118. invalidFiles.push(item.name);
  119. }
  120. }
  121. });
  122. //不允许的文件类型提示
  123. if(invalidFiles.length>0){
  124. //限制文件类型列表传入事件
  125. _this.$emit('limitFileTypeList',invalidFiles,_this.allowFileType);
  126. return;
  127. }
  128. else{
  129. //限制文件类型列表传入事件
  130. _this.$emit('limitFileTypeList',invalidFiles,_this.allowFileType);
  131. }
  132. //文件大小的限制提示
  133. if(greaterArr.length>0){
  134. //限制文件大小列表传入事件
  135. _this.$emit('limitFileSizeList',greaterArr);
  136. return;
  137. }
  138. else{
  139. //限制文件大小列表传入事件
  140. _this.$emit('limitFileSizeList',greaterArr);
  141. }
  142. tempFilePaths=tempFilePaths.slice(0,this.maxCount-this.showList.length);
  143. tempFiles.forEach((item,index)=>{
  144. _this.files.push(item);
  145. //console.info('文件大小',item.size);
  146. //获取文件扩展名
  147. let index1 = item.name.lastIndexOf(".");
  148. let suffix = item.name.substr(index1);
  149. //console.info('suffix',suffix);
  150. this.extensionNames.push(suffix);
  151. });
  152. //return;
  153. // console.info('pop window end');
  154. // console.info('tempFilePaths',tempFilePaths);
  155. // console.info('tempFiles',tempFiles);
  156. tempFilePaths.forEach((item) => {
  157. this.imgList.push(item);
  158. });
  159. this.$emit("chooseFile", this.imgList, tempFilePaths)
  160. }
  161. });
  162. },
  163. //删除图片
  164. deleteImg(eq) {
  165. console.info("deleteImg",eq);
  166. console.info("this.imgList",this.imgList);
  167. console.info("this.files",this.files);
  168. console.info("this.extensionNames",this.extensionNames);
  169. let deleteImg = this.imgList;
  170. deleteImg.splice(eq, 1); //删除临时路径
  171. this.files.splice(eq,1);
  172. this.extensionNames.splice(eq,1);
  173. if (this.mode.length > 0) {
  174. let deleteImg = this.showList;
  175. deleteImg.splice(eq, 1); //删除服务那边的路径
  176. }
  177. this.$emit("imgDelete", this.handleImg(), eq)
  178. },
  179. // 预览图片
  180. previewImage(eq) {
  181. let getUrl = this.handleImg();
  182. uni.previewImage({
  183. current: getUrl[eq],
  184. urls: getUrl
  185. })
  186. },
  187. //返回需要操作的图片数组
  188. //如果是回调了则操作回填后的数组 否则操作临时路径的图片数组
  189. handleImg() {
  190. return this.mode.length > 0 ? this.showList : this.imgList
  191. },
  192. }
  193. }
  194. </script>
  195. <style scoped>
  196. /* 上传 str */
  197. .imglistbx {
  198. width: 100%;
  199. height: 100%;
  200. }
  201. .imglistItem {
  202. position:relative;
  203. float: left;
  204. margin-bottom: 20rpx;
  205. border-radius: 10rpx;
  206. }
  207. .column3 {
  208. width: 33.3333%;
  209. height: 200rpx;
  210. }
  211. .column4 {
  212. width: 25%;
  213. height: 220rpx;
  214. }
  215. .itemImg {
  216. width: 70%;
  217. height: 100%;
  218. margin: 0 auto;
  219. display: block;
  220. border-radius: 10rpx;
  221. }
  222. .cancelBtn {
  223. position: absolute;
  224. top: -10rpx;
  225. right: 10rpx;
  226. }
  227. /* 上传控件 */
  228. .uploadControl {
  229. font-size: 50rpx;
  230. color: #888;
  231. background-color: #EEEEEE;
  232. display: flex;
  233. justify-content: center;
  234. align-items: center;
  235. }
  236. /* 上传 str end*/
  237. .clear {
  238. clear: both;
  239. }
  240. .font13{
  241. font-size:12px;
  242. overflow: hidden;
  243. }
  244. .iconSize{
  245. font-size:90px;
  246. }
  247. </style>