pretty-uploadFile.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <template>
  2. <view>
  3. <!-- 上传文件 begin -->
  4. <view class="display_flex">
  5. <view class="img_list" v-for="(item, index) in fileList" :key="index">
  6. <image class="img_item" :src="item.url" @click="preview(item.url)" mode="aspectFill" v-if="item.book == 'img'"></image>
  7. <view class="upload" @click="prePdf(item.id)" v-else><text class="iconfont iconpdf" style="color:#FF6969;font-size:44px"></text></view>
  8. <view class="iconfont deleteicon iconshanchu2" @click.stop="delteFile(index)"></view>
  9. </view>
  10. <view class="upload img_list" @click="upload('img')">
  11. <view class="iconfont iconziyuan" style="font-size:20px;"></view>
  12. 上传照片
  13. </view>
  14. </view>
  15. <!-- 上传文件 end -->
  16. </view>
  17. </template>
  18. <script>
  19. import { pathToBase64, base64ToPath } from '@/js/pathto64.js'
  20. export default {
  21. name: 'upload',
  22. model: {
  23. prop: 'showPop',
  24. event: 'change'
  25. },
  26. props: {
  27. limitnumber: {
  28. type: Number,
  29. default: 10
  30. },
  31. },
  32. data() {
  33. return {
  34. primary:this.$theme.primary,
  35. settingFile:getApp().globalData.siteinfo,
  36. fileList: []
  37. };
  38. },
  39. methods: {
  40. /**
  41. * @description 上传文件
  42. */
  43. upload(book) {
  44. let that = this;
  45. if (this.fileList.length == this.$props.limitnumber) {
  46. uni.showToast({
  47. title: '文件上传已上限',
  48. icon: 'none'
  49. });
  50. return;
  51. }
  52. if (book == 'img') {
  53. uni.chooseImage({
  54. count: 1,
  55. sizeType: ['compressed'],
  56. success: function(res) {
  57. pathToBase64(res.tempFilePaths[0]).then(base64=>{
  58. // console.log(base64)
  59. // let base64 = 'data:image/jpeg;base64,' + v.data; // 返回的是没有 'data:image/jpeg;base64,'头的数据, 有需要可自行追加上
  60. that.$emit('upload', base64,);
  61. that.fileList.push({
  62. url: base64,
  63. id: res.uploadId,
  64. book: 'img'
  65. });
  66. })
  67. // .then(v => {
  68. // let base64 = 'data:image/jpeg;base64,' + v; // 返回的是没有 'data:image/jpeg;base64,'头的数据, 有需要可自行追加上
  69. // console.log(base64)
  70. // that.$emit('upload', base64,);
  71. // that.fileList.push({
  72. // url: base64,
  73. // id: res.uploadId,
  74. // book: 'img'
  75. // });
  76. // console.log(that.fileList);
  77. // })
  78. // .catch(error => {
  79. // console.error(error)
  80. // })
  81. // const arrayBuffer = new Uint8Array(res.tempFilePaths[0]); //先将本地图片路径转换成array类型
  82. // const base64Img = uni.arrayBufferToBase64(arrayBuffer); //再转换成base64类型
  83. // uni.getFileSystemManager().readFile({
  84. // filePath: res.tempFilePaths[0], //选择图片返回的相对路径
  85. // encoding: 'base64', //编码格式
  86. // success: v => {
  87. // //成功的回调
  88. // let base64 = 'data:image/jpeg;base64,' + v.data; // 返回的是没有 'data:image/jpeg;base64,'头的数据, 有需要可自行追加上
  89. // that.$emit('upload', base64,);
  90. // that.fileList.push({
  91. // url: base64,
  92. // id: res.uploadId,
  93. // book: 'img'
  94. // });
  95. // }
  96. // });
  97. }
  98. // count: 6, //默认9
  99. // sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  100. // sourceType: ['album'], //从相册选择
  101. // success: function (res) {
  102. // // console.log(res)
  103. // console.log(JSON.stringify(res.tempFilePaths));
  104. // }
  105. });
  106. return;
  107. }
  108. },
  109. /**
  110. * @description 预览图片
  111. */
  112. preview(url) {
  113. uni.previewImage({
  114. urls: [url],
  115. current: url
  116. });
  117. },
  118. /**
  119. * @description 预览pdf
  120. */
  121. prePdf(e) {
  122. let id = `/api/v1/consumer/download/${e}`; // 下载pdf文件的借口地址
  123. previepdf(id);
  124. },
  125. /**
  126. * @description 删除上传的文件
  127. */
  128. delteFile(index) {
  129. this.fileList.splice(index, 1);
  130. this.$emit('delteFile', index);
  131. }
  132. }
  133. };
  134. </script>
  135. <style>
  136. @import './pretty-uploadFile.css';
  137. .display_flex {
  138. margin-left: 36rpx;
  139. display: flex;
  140. flex-wrap: wrap;
  141. width: 100%;
  142. }
  143. .fileicon {
  144. font-size: 25px;
  145. }
  146. /* 上传图片 */
  147. .img_list {
  148. width: 70px;
  149. height: 70px;
  150. border-radius: 5px;
  151. margin: 10px 5px;
  152. position: relative;
  153. }
  154. .img > image {
  155. width: 70px;
  156. height: 70px;
  157. border-radius: 5px;
  158. }
  159. .img_item {
  160. width: 70px;
  161. height: 70px;
  162. border-radius: 5px;
  163. }
  164. .upload {
  165. height: 70px;
  166. width: 70px;
  167. background-color: #f7f7f7;
  168. border: 1px dashed rgb(224, 223, 223);
  169. display: flex;
  170. flex-direction: column;
  171. justify-content: center;
  172. align-items: center;
  173. font-size: 14px;
  174. color: #aaa;
  175. word-break: break-all;
  176. overflow: hidden;
  177. }
  178. .deleteicon {
  179. position: absolute;
  180. font-size: 20px;
  181. color: #f93313;
  182. top: -10px;
  183. right: -6px;
  184. background-color: #fff;
  185. border-radius: 50%;
  186. }
  187. </style>