upload-image.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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.path" 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>
  22. <view class="icon-add"></view>
  23. <view class="icon-add rotate"></view>
  24. </slot>
  25. </view>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. export default {
  31. name: "uploadImage",
  32. props: {
  33. filesList: {
  34. type: Array,
  35. default () {
  36. return []
  37. }
  38. },
  39. disabled:{
  40. type: Boolean,
  41. default: false
  42. },
  43. disablePreview: {
  44. type: Boolean,
  45. default: false
  46. },
  47. limit: {
  48. type: [Number, String],
  49. default: 9
  50. },
  51. imageStyles: {
  52. type: Object,
  53. default () {
  54. return {
  55. width: 'auto',
  56. height: 'auto',
  57. border: {}
  58. }
  59. }
  60. },
  61. delIcon: {
  62. type: Boolean,
  63. default: true
  64. },
  65. readonly:{
  66. type:Boolean,
  67. default:false
  68. }
  69. },
  70. computed: {
  71. styles() {
  72. let styles = {
  73. width: 'auto',
  74. height: 'auto',
  75. border: {}
  76. }
  77. return Object.assign(styles, this.imageStyles)
  78. },
  79. boxStyle() {
  80. const {
  81. width = 'auto',
  82. height = 'auto'
  83. } = this.styles
  84. let obj = {}
  85. if (height === 'auto') {
  86. if (width !== 'auto') {
  87. obj.height = this.value2px(width)
  88. obj['padding-top'] = 0
  89. } else {
  90. obj.height = 0
  91. }
  92. } else {
  93. obj.height = this.value2px(height)
  94. obj['padding-top'] = 0
  95. }
  96. if (width === 'auto') {
  97. if (height !== 'auto') {
  98. obj.width = this.value2px(height)
  99. } else {
  100. obj.width = '33.3%'
  101. }
  102. } else {
  103. obj.width = this.value2px(width)
  104. }
  105. let classles = ''
  106. for(let i in obj){
  107. classles+= `${i}:${obj[i]};`
  108. }
  109. return classles
  110. },
  111. borderStyle() {
  112. let {
  113. border
  114. } = this.styles
  115. let obj = {}
  116. if (typeof border === 'boolean') {
  117. obj.border = border ? '1px #eee solid' : 'none'
  118. } else {
  119. let width = (border && border.width) || 1
  120. width = this.value2px(width)
  121. let radius = (border && border.radius) || 5
  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.path)
  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: 8px;
  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. .icon-add {
  242. width: 50px;
  243. height: 5px;
  244. background-color: #f1f1f1;
  245. border-radius: 2px;
  246. }
  247. .rotate {
  248. position: absolute;
  249. transform: rotate(90deg);
  250. }
  251. .icon-del-box {
  252. /* #ifndef APP-NVUE */
  253. display: flex;
  254. /* #endif */
  255. align-items: center;
  256. justify-content: center;
  257. position: absolute;
  258. top: 5px;
  259. right: 5px;
  260. height: 26px;
  261. width: 26px;
  262. border-radius: 50%;
  263. background-color: rgba(0, 0, 0, 0.5);
  264. z-index: 2;
  265. transform: rotate(-45deg);
  266. }
  267. .icon-del {
  268. width: 15px;
  269. height: 2px;
  270. background-color: #fff;
  271. border-radius: 2px;
  272. }
  273. </style>