You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
5.7 KiB

3 weeks ago
  1. <template>
  2. <view class="uni-file-picker__container">
  3. <view class="file-picker__box" v-for="(item,index) in filesList" :key="index" :style="boxStyle">
  4. <view class="file-picker__box-content" :style="borderStyle">
  5. <image class="file-image" :src="item.url" mode="aspectFill" @click.stop="prviewImage(item,index)"></image>
  6. <view v-if="delIcon && !readonly" class="icon-del-box" @click.stop="delFile(index)">
  7. <view class="icon-del"></view>
  8. <view class="icon-del rotate"></view>
  9. </view>
  10. <view v-if="(item.progress && item.progress !== 100) ||item.progress===0 " class="file-picker__progress">
  11. <progress class="file-picker__progress-item" :percent="item.progress === -1?0:item.progress" stroke-width="4"
  12. :backgroundColor="item.errMsg?'#ff5a5f':'#EBEBEB'" />
  13. </view>
  14. <view v-if="item.errMsg" class="file-picker__mask" @click.stop="uploadFiles(item,index)">
  15. 点击重试
  16. </view>
  17. </view>
  18. </view>
  19. <view v-if="filesList.length < limit && !readonly" class="file-picker__box" :style="boxStyle">
  20. <view class="file-picker__box-content is-add" :style="borderStyle" @click="choose">
  21. <slot></slot>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. name: "uploadImage",
  29. emits:['uploadFiles','choose','delFile'],
  30. props: {
  31. filesList: {
  32. type: Array,
  33. default () {
  34. return []
  35. }
  36. },
  37. disabled:{
  38. type: Boolean,
  39. default: false
  40. },
  41. disablePreview: {
  42. type: Boolean,
  43. default: false
  44. },
  45. limit: {
  46. type: [Number, String],
  47. default: 9
  48. },
  49. imageStyles: {
  50. type: Object,
  51. default () {
  52. return {
  53. width: 'auto',
  54. height: 'auto',
  55. border: {}
  56. }
  57. }
  58. },
  59. delIcon: {
  60. type: Boolean,
  61. default: true
  62. },
  63. readonly:{
  64. type:Boolean,
  65. default:false
  66. }
  67. },
  68. computed: {
  69. styles() {
  70. let styles = {
  71. width: 'auto',
  72. height: 'auto',
  73. border: {}
  74. }
  75. return Object.assign(styles, this.imageStyles)
  76. },
  77. boxStyle() {
  78. const {
  79. width = 'auto',
  80. height = 'auto'
  81. } = this.styles
  82. let obj = {}
  83. if (height === 'auto') {
  84. if (width !== 'auto') {
  85. obj.height = this.value2px(width)
  86. obj['padding-top'] = 0
  87. } else {
  88. obj.height = 0
  89. }
  90. } else {
  91. obj.height = this.value2px(height)
  92. obj['padding-top'] = 0
  93. }
  94. if (width === 'auto') {
  95. if (height !== 'auto') {
  96. obj.width = this.value2px(height)
  97. } else {
  98. obj.width = '33.3%'
  99. }
  100. } else {
  101. obj.width = this.value2px(width)
  102. }
  103. let classles = ''
  104. for(let i in obj){
  105. classles+= `${i}:${obj[i]};`
  106. }
  107. return classles
  108. },
  109. borderStyle() {
  110. let {
  111. border
  112. } = this.styles
  113. let obj = {}
  114. const widthDefaultValue = 1
  115. const radiusDefaultValue = 3
  116. if (typeof border === 'boolean') {
  117. obj.border = border ? '1px #eee solid' : 'none'
  118. } else {
  119. let width = (border && border.width) || widthDefaultValue
  120. width = this.value2px(width)
  121. let radius = (border && border.radius) || radiusDefaultValue
  122. radius = this.value2px(radius)
  123. obj = {
  124. 'border-width': width,
  125. 'border-style': (border && border.style) || 'solid',
  126. 'border-color': (border && border.color) || '#eee',
  127. 'border-radius': radius
  128. }
  129. }
  130. let classles = ''
  131. for(let i in obj){
  132. classles+= `${i}:${obj[i]};`
  133. }
  134. return classles
  135. }
  136. },
  137. methods: {
  138. uploadFiles(item, index) {
  139. this.$emit("uploadFiles", item)
  140. },
  141. choose() {
  142. this.$emit("choose")
  143. },
  144. delFile(index) {
  145. this.$emit('delFile', index)
  146. },
  147. prviewImage(img, index) {
  148. let urls = []
  149. if(Number(this.limit) === 1&&this.disablePreview&&!this.disabled){
  150. this.$emit("choose")
  151. }
  152. if(this.disablePreview) return
  153. this.filesList.forEach(i => {
  154. urls.push(i.url)
  155. })
  156. uni.previewImage({
  157. urls: urls,
  158. current: index
  159. });
  160. },
  161. value2px(value) {
  162. if (typeof value === 'number') {
  163. value += 'px'
  164. } else {
  165. if (value.indexOf('%') === -1) {
  166. value = value.indexOf('px') !== -1 ? value : value + 'px'
  167. }
  168. }
  169. return value
  170. }
  171. }
  172. }
  173. </script>
  174. <style lang="scss">
  175. .uni-file-picker__container {
  176. /* #ifndef APP-NVUE */
  177. display: flex;
  178. box-sizing: border-box;
  179. /* #endif */
  180. flex-wrap: wrap;
  181. margin: -5px;
  182. }
  183. .file-picker__box {
  184. position: relative;
  185. // flex: 0 0 33.3%;
  186. width: 33.3%;
  187. height: 0;
  188. padding-top: 33.33%;
  189. /* #ifndef APP-NVUE */
  190. box-sizing: border-box;
  191. /* #endif */
  192. }
  193. .file-picker__box-content {
  194. position: absolute;
  195. top: 0;
  196. right: 0;
  197. bottom: 0;
  198. left: 0;
  199. margin: 5px;
  200. border: 1px #eee solid;
  201. border-radius: 5px;
  202. overflow: hidden;
  203. }
  204. .file-picker__progress {
  205. position: absolute;
  206. bottom: 0;
  207. left: 0;
  208. right: 0;
  209. /* border: 1px red solid; */
  210. z-index: 2;
  211. }
  212. .file-picker__progress-item {
  213. width: 100%;
  214. }
  215. .file-picker__mask {
  216. /* #ifndef APP-NVUE */
  217. display: flex;
  218. /* #endif */
  219. justify-content: center;
  220. align-items: center;
  221. position: absolute;
  222. right: 0;
  223. top: 0;
  224. bottom: 0;
  225. left: 0;
  226. color: #fff;
  227. font-size: 12px;
  228. background-color: rgba(0, 0, 0, 0.4);
  229. }
  230. .file-image {
  231. width: 100%;
  232. height: 100%;
  233. }
  234. .is-add {
  235. /* #ifndef APP-NVUE */
  236. display: flex;
  237. /* #endif */
  238. align-items: center;
  239. justify-content: center;
  240. }
  241. .rotate {
  242. position: absolute;
  243. transform: rotate(90deg);
  244. }
  245. .icon-del-box {
  246. /* #ifndef APP-NVUE */
  247. display: flex;
  248. /* #endif */
  249. align-items: center;
  250. justify-content: center;
  251. position: absolute;
  252. top: 3px;
  253. right: 3px;
  254. height: 26px;
  255. width: 26px;
  256. border-radius: 50%;
  257. background-color: rgba(0, 0, 0, 0.5);
  258. z-index: 2;
  259. transform: rotate(-45deg);
  260. }
  261. .icon-del {
  262. width: 15px;
  263. height: 2px;
  264. background-color: #fff;
  265. border-radius: 2px;
  266. }
  267. </style>