index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <view class="file-upload-container">
  3. <view class="file-upload-view" v-for="(item, index) in files" :key="index">
  4. <view class="file-upload-ckick" @click="imgView(index)">
  5. <image v-if="item.url" mode="aspectFit" :src="imageUrl + item.url" style="width: 100%; height: 100%"> </image>
  6. <template v-else>
  7. <image mode="aspectFit" :src="item.loUrl" style="width: 100%; height: 100%"> </image>
  8. <view class="mack" @tap.stop="() => {}"></view>
  9. <image @tap.stop="() => {}" class="uploadImg" mode="aspectFit" src="/static/upload/loading.png"> </image>
  10. </template>
  11. <template v-if="!disabled">
  12. <image class="delImg" mode="aspectFit" src="/static/upload/del.png" @tap.stop="del(index)"> </image>
  13. </template>
  14. </view>
  15. </view>
  16. <template v-if="!disabled">
  17. <view v-if="count === 0 ? true : count - files.length" class="file-upload-view" @click="upload">
  18. <view class="file-upload-ckick">
  19. <image class="uploadImg" mode="aspectFit" src="/static/upload/upload.png"> </image>
  20. </view>
  21. </view>
  22. </template>
  23. </view>
  24. </template>
  25. <script>
  26. // import { upload } from "@/common/http/upload-file.js";
  27. import api from '@/common/http/'
  28. import { uploadImgs } from '@/common/utils/util.js'
  29. import { b64_md5 } from '@/common/utils/md5.js'
  30. import loadingImg from './loading.png'
  31. export default {
  32. props: {
  33. disabled: {
  34. type: Boolean,
  35. default: false
  36. },
  37. count: {
  38. type: Number,
  39. default: 0
  40. },
  41. fileList: {
  42. type: Array,
  43. default: () => []
  44. }
  45. },
  46. data() {
  47. return {
  48. imageUrl: this.$imageUrl,
  49. files: this.setFileVal(this.fileList)
  50. }
  51. },
  52. watch: {
  53. fileList() {
  54. this.files = this.setFileVal(this.fileList)
  55. },
  56. files: {
  57. handler(newName, oldName) {
  58. if (newName.every(item => (item.url ? true : false))) {
  59. this.$emit(
  60. 'getFiles',
  61. newName.map(item => item.url)
  62. )
  63. }
  64. },
  65. deep: true
  66. }
  67. },
  68. methods: {
  69. // 检查文件类型
  70. checkFileType(url) {
  71. if (!url) return ''
  72. const fileSuffix = url.substring(url.lastIndexOf('.') + 1)
  73. if (['jpg', 'jpeg', 'png'].includes(fileSuffix)) {
  74. return 'image'
  75. } else if (['doc', 'docx', 'dot', 'wps', 'wpt'].includes(fileSuffix)) {
  76. return 'word'
  77. } else if (['xls', 'xlsx', 'xlt', 'et', 'ett'].includes(fileSuffix)) {
  78. return 'excel'
  79. } else if (['ppt', 'pptx', 'dps', 'dpt', 'pot', 'pps'].includes(fileSuffix)) {
  80. return 'ppt'
  81. } else if (['pdf'].includes(fileSuffix)) {
  82. return 'pdf'
  83. } else if (['zip', 'rar', 'gz', 'apk'].includes(fileSuffix)) {
  84. return 'file'
  85. }
  86. {
  87. return 'pdf'
  88. }
  89. },
  90. setFileVal(arr) {
  91. return arr.map(item => {
  92. if (typeof item == 'string') {
  93. return {
  94. url: item,
  95. key: b64_md5(item)
  96. }
  97. } else {
  98. return item
  99. }
  100. })
  101. },
  102. upload() {
  103. uni.chooseImage({
  104. // count: this.count === 0 ? 0 : this.count - this.files.length,
  105. count:1,
  106. success: async res => {
  107. let pathType = this.checkFileType(res.tempFiles[0].name)
  108. if (pathType !== 'image') {
  109. return this.$toast('当前格式仅支持jpg, jpeg, png,请检查')
  110. }
  111. // #ifdef H5
  112. uploadImgs(res.tempFiles[0]).then(res => {
  113. this.files.push({
  114. url: res.url
  115. })
  116. })
  117. // #endif
  118. // #ifdef MP-WEIXIN
  119. // #endif
  120. },
  121. fail: err => {
  122. console.log(err)
  123. }
  124. })
  125. },
  126. del(index) {
  127. this.files.splice(index, 1)
  128. },
  129. imgView(index) {
  130. var list = this.files.map(item => {
  131. if (item.url) {
  132. return this.imageUrl + item.url
  133. } else {
  134. return loadingImg
  135. }
  136. })
  137. uni.previewImage({
  138. current: index,
  139. urls: list
  140. })
  141. }
  142. }
  143. }
  144. </script>
  145. <style lang="scss" scoped>
  146. .file-upload-container {
  147. width: 100%;
  148. height: 100%;
  149. display: flex;
  150. flex-wrap: wrap;
  151. justify-content: space-between;
  152. .file-upload-view {
  153. display: inline-block;
  154. margin-bottom: 20rpx;
  155. &:nth-last-of-type(-n + 2) {
  156. margin-bottom: 0;
  157. }
  158. .file-upload-ckick {
  159. width: 300rpx;
  160. height: 190rpx;
  161. background: #ffffff;
  162. border-radius: 20rpx;
  163. opacity: 1;
  164. border: 1rpx solid #e4e4e4;
  165. box-sizing: border-box;
  166. padding: 10rpx;
  167. position: relative;
  168. display: flex;
  169. justify-content: center;
  170. align-items: center;
  171. .mack {
  172. width: 100%;
  173. height: 100%;
  174. position: absolute;
  175. top: 50%;
  176. left: 50%;
  177. transform: translate(-50%, -50%);
  178. z-index: 2;
  179. background: rgba(0, 0, 0, 0.2);
  180. }
  181. .uploadImg {
  182. width: 80rpx;
  183. height: 80rpx;
  184. position: absolute;
  185. top: 50%;
  186. left: 50%;
  187. transform: translate(-50%, -50%);
  188. z-index: 3;
  189. }
  190. .delImg {
  191. position: absolute;
  192. top: -16rpx;
  193. right: -16rpx;
  194. z-index: 2;
  195. width: 40rpx;
  196. height: 40rpx;
  197. }
  198. }
  199. }
  200. }
  201. </style>