index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/images/upload/loading.png">
  10. </image>
  11. </template>
  12. <template v-if="!disabled">
  13. <text class="iconfont icon-guanbi1 delImg" @tap.stop="del(index)"></text>
  14. </template>
  15. </view>
  16. </view>
  17. <template v-if="!disabled">
  18. <view v-if="count === 0 ? true : count - files.length" class="file-upload-view" @click="upload">
  19. <view class="file-upload-ckick">
  20. <text class="iconfont icon-paizhao uploadImg"></text>
  21. </view>
  22. </view>
  23. </template>
  24. </view>
  25. </template>
  26. <script>
  27. import { uploadImgFull } from '@/common/utils/util.js'
  28. import { b64_md5 } from '@/utils/md5.js'
  29. import loadingImg from './loading.png'
  30. export default {
  31. props: {
  32. disabled: {
  33. type: Boolean,
  34. default: false
  35. },
  36. count: {
  37. type: Number,
  38. default: 0
  39. },
  40. fileList: {
  41. type: Array,
  42. default: () => []
  43. }
  44. },
  45. data() {
  46. return {
  47. imageUrl: this.$imageUrl,
  48. files: this.setFileVal(this.fileList)
  49. }
  50. },
  51. watch: {
  52. fileList() {
  53. this.files = this.setFileVal(this.fileList)
  54. },
  55. files: {
  56. handler(newName, oldName) {
  57. if (newName.every(item => (item.url ? true : false))) {
  58. this.$emit(
  59. 'getFiles',
  60. newName.map(item => item.url)
  61. )
  62. }
  63. },
  64. deep: true
  65. }
  66. },
  67. methods: {
  68. setFileVal(arr) {
  69. return arr.map(item => {
  70. if (typeof item == 'string') {
  71. return {
  72. url: item,
  73. key: b64_md5(item)
  74. }
  75. } else {
  76. return item
  77. }
  78. })
  79. },
  80. upload() {
  81. uni.chooseImage({
  82. count: this.count === 0 ? 0 : this.count - this.files.length,
  83. sizeType: ['original'],
  84. success: async res => {
  85. const leng = res.tempFiles.length
  86. for (var file of res.tempFiles) {
  87. this.files.push({
  88. url: false,
  89. key: b64_md5(file.path),
  90. loUrl: file.path
  91. })
  92. }
  93. for (var i = 0; i < leng; i++) {
  94. var data = await uploadImgFull(res.tempFiles[i])
  95. var obj = this.files.find(item => item.key === b64_md5(res.tempFiles[i].path))
  96. if (obj) {
  97. obj.url = data.url
  98. obj.key = b64_md5(data.url)
  99. delete obj.loUrl
  100. }
  101. }
  102. },
  103. fail: err => {
  104. console.log(err)
  105. }
  106. })
  107. },
  108. del(index) {
  109. this.files.splice(index, 1)
  110. },
  111. imgView(index) {
  112. var list = this.files.map(item => {
  113. if (item.url) {
  114. return this.imageUrl + item.url
  115. } else {
  116. return loadingImg
  117. }
  118. })
  119. uni.previewImage({
  120. current: index,
  121. urls: list
  122. })
  123. }
  124. }
  125. }
  126. </script>
  127. <style lang="scss" scoped>
  128. .file-upload-container {
  129. width: 100%;
  130. height: 100%;
  131. display: flex;
  132. flex-wrap: wrap;
  133. justify-content: space-between;
  134. .file-upload-view {
  135. display: inline-block;
  136. margin-bottom: 20rpx;
  137. &:nth-last-of-type(-n + 2) {
  138. margin-bottom: 0;
  139. }
  140. .file-upload-ckick {
  141. width: 300rpx;
  142. height: 190rpx;
  143. background: #ffffff;
  144. border-radius: 20rpx;
  145. opacity: 1;
  146. border: 1rpx solid #e4e4e4;
  147. box-sizing: border-box;
  148. padding: 10rpx;
  149. position: relative;
  150. display: flex;
  151. justify-content: center;
  152. align-items: center;
  153. .mack {
  154. width: 100%;
  155. height: 100%;
  156. position: absolute;
  157. top: 50%;
  158. left: 50%;
  159. transform: translate(-50%, -50%);
  160. z-index: 2;
  161. background: rgba(0, 0, 0, 0.2);
  162. }
  163. .icon-paizhao {
  164. background: $theme-color;
  165. color: #ffffff;
  166. font-size: 40rpx;
  167. border-radius: 50%;
  168. text-align: center;
  169. line-height: 80rpx;
  170. }
  171. .uploadImg {
  172. width: 80rpx;
  173. height: 80rpx;
  174. position: absolute;
  175. top: 50%;
  176. left: 50%;
  177. transform: translate(-50%, -50%);
  178. z-index: 3;
  179. }
  180. .delImg {
  181. position: absolute;
  182. top: -16rpx;
  183. right: -16rpx;
  184. z-index: 2;
  185. width: 40rpx;
  186. height: 40rpx;
  187. background: $minor-color;
  188. color: #ffffff;
  189. border-radius: 50%;
  190. text-align: center;
  191. line-height: 40rpx;
  192. font-size: 28rpx;
  193. }
  194. }
  195. }
  196. }
  197. </style>