upload-file.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <template>
  2. <view class="uni-file-picker__files">
  3. <view v-if="!readonly" class="files-button" @click="choose">
  4. <slot></slot>
  5. </view>
  6. <!-- :class="{'is-text-box':showType === 'list'}" -->
  7. <view
  8. v-if="list.length > 0"
  9. class="uni-file-picker__lists is-text-box"
  10. :style="borderStyle"
  11. >
  12. <!-- ,'is-list-card':showType === 'list-card' -->
  13. <view
  14. class="uni-file-picker__lists-box"
  15. v-for="(item, index) in list"
  16. :key="index"
  17. :class="{
  18. 'files-border': index !== 0 && styles.dividline
  19. }"
  20. :style="index !== 0 && styles.dividline && borderLineStyle"
  21. >
  22. <view class="uni-file-picker__item">
  23. <!-- :class="{'is-text-image':showType === 'list'}" -->
  24. <!-- <view class="files__image is-text-image">
  25. <image class="header-image" :src="item.logo" mode="aspectFit"></image>
  26. </view> -->
  27. <view class="files__name">{{ item.name }}</view>
  28. <view
  29. v-if="delIcon && !readonly"
  30. class="icon-del-box icon-files"
  31. @click="delFile(index)"
  32. >
  33. <view class="icon-del icon-files"></view>
  34. <view class="icon-del rotate"></view>
  35. </view>
  36. </view>
  37. <view
  38. v-if="(item.progress && item.progress !== 100) || item.progress === 0"
  39. class="file-picker__progress"
  40. >
  41. <progress
  42. class="file-picker__progress-item"
  43. :percent="item.progress === -1 ? 0 : item.progress"
  44. stroke-width="4"
  45. :backgroundColor="item.errMsg ? '#ff5a5f' : '#EBEBEB'"
  46. />
  47. </view>
  48. <view
  49. v-if="item.status === 'error'"
  50. class="file-picker__mask"
  51. @click.stop="uploadFiles(item, index)"
  52. >
  53. 点击重试
  54. </view>
  55. </view>
  56. </view>
  57. </view>
  58. </template>
  59. <script>
  60. export default {
  61. name: 'uploadFile',
  62. emits: ['uploadFiles', 'choose', 'delFile'],
  63. props: {
  64. filesList: {
  65. type: Array,
  66. default () {
  67. return []
  68. }
  69. },
  70. delIcon: {
  71. type: Boolean,
  72. default: true
  73. },
  74. limit: {
  75. type: [Number, String],
  76. default: 9
  77. },
  78. showType: {
  79. type: String,
  80. default: ''
  81. },
  82. listStyles: {
  83. type: Object,
  84. default () {
  85. return {
  86. // 是否显示边框
  87. border: true,
  88. // 是否显示分隔线
  89. dividline: true,
  90. // 线条样式
  91. borderStyle: {}
  92. }
  93. }
  94. },
  95. readonly: {
  96. type: Boolean,
  97. default: false
  98. }
  99. },
  100. computed: {
  101. list () {
  102. let files = []
  103. this.filesList.forEach(v => {
  104. files.push(v)
  105. })
  106. return files
  107. },
  108. styles () {
  109. let styles = {
  110. border: true,
  111. dividline: true,
  112. 'border-style': {}
  113. }
  114. return Object.assign(styles, this.listStyles)
  115. },
  116. borderStyle () {
  117. let { borderStyle, border } = this.styles
  118. let obj = {}
  119. if (!border) {
  120. obj.border = 'none'
  121. } else {
  122. let width = (borderStyle && borderStyle.width) || 1
  123. width = this.value2px(width)
  124. let radius = (borderStyle && borderStyle.radius) || 5
  125. radius = this.value2px(radius)
  126. obj = {
  127. 'border-width': width,
  128. 'border-style': (borderStyle && borderStyle.style) || 'solid',
  129. 'border-color': (borderStyle && borderStyle.color) || '#eee',
  130. 'border-radius': radius
  131. }
  132. }
  133. let classles = ''
  134. for (let i in obj) {
  135. classles += `${i}:${obj[i]};`
  136. }
  137. return classles
  138. },
  139. borderLineStyle () {
  140. let obj = {}
  141. let { borderStyle } = this.styles
  142. if (borderStyle && borderStyle.color) {
  143. obj['border-color'] = borderStyle.color
  144. }
  145. if (borderStyle && borderStyle.width) {
  146. let width = (borderStyle && borderStyle.width) || 1
  147. let style = (borderStyle && borderStyle.style) || 0
  148. if (typeof width === 'number') {
  149. width += 'px'
  150. } else {
  151. width = width.indexOf('px') ? width : width + 'px'
  152. }
  153. obj['border-width'] = width
  154. if (typeof style === 'number') {
  155. style += 'px'
  156. } else {
  157. style = style.indexOf('px') ? style : style + 'px'
  158. }
  159. obj['border-top-style'] = style
  160. }
  161. let classles = ''
  162. for (let i in obj) {
  163. classles += `${i}:${obj[i]};`
  164. }
  165. return classles
  166. }
  167. },
  168. methods: {
  169. uploadFiles (item, index) {
  170. this.$emit('uploadFiles', {
  171. item,
  172. index
  173. })
  174. },
  175. choose () {
  176. this.$emit('choose')
  177. },
  178. delFile (index) {
  179. this.$emit('delFile', index)
  180. },
  181. value2px (value) {
  182. if (typeof value === 'number') {
  183. value += 'px'
  184. } else {
  185. value = value.indexOf('px') !== -1 ? value : value + 'px'
  186. }
  187. return value
  188. }
  189. }
  190. }
  191. </script>
  192. <style lang="scss">
  193. .uni-file-picker__files {
  194. /* #ifndef APP-NVUE */
  195. display: flex;
  196. /* #endif */
  197. flex-direction: column;
  198. justify-content: flex-start;
  199. }
  200. .files-button {
  201. // border: 1px red solid;
  202. }
  203. .uni-file-picker__lists {
  204. position: relative;
  205. margin-top: 5px;
  206. overflow: hidden;
  207. }
  208. .file-picker__mask {
  209. /* #ifndef APP-NVUE */
  210. display: flex;
  211. /* #endif */
  212. justify-content: center;
  213. align-items: center;
  214. position: absolute;
  215. right: 0;
  216. top: 0;
  217. bottom: 0;
  218. left: 0;
  219. color: #fff;
  220. font-size: 32rpx;
  221. background-color: rgba(0, 0, 0, 0.4);
  222. }
  223. .uni-file-picker__lists-box {
  224. position: relative;
  225. }
  226. .uni-file-picker__item {
  227. /* #ifndef APP-NVUE */
  228. display: flex;
  229. /* #endif */
  230. align-items: center;
  231. padding: 8px 10px;
  232. padding-right: 5px;
  233. padding-left: 10px;
  234. }
  235. .files-border {
  236. border-top: 1px #eee solid;
  237. }
  238. .files__name {
  239. flex: 1;
  240. font-size: 32rpx;
  241. color: #666;
  242. margin-right: 25px;
  243. /* #ifndef APP-NVUE */
  244. word-break: break-all;
  245. word-wrap: break-word;
  246. /* #endif */
  247. }
  248. .icon-files {
  249. /* #ifndef APP-NVUE */
  250. position: static;
  251. background-color: initial;
  252. /* #endif */
  253. }
  254. // .icon-files .icon-del {
  255. // background-color: #333;
  256. // width: 12px;
  257. // height: 1px;
  258. // }
  259. .is-list-card {
  260. border: 1px #eee solid;
  261. margin-bottom: 5px;
  262. border-radius: 5px;
  263. box-shadow: 0 0 2px 0px rgba(0, 0, 0, 0.1);
  264. padding: 5px;
  265. }
  266. .files__image {
  267. width: 40px;
  268. height: 40px;
  269. margin-right: 10px;
  270. }
  271. .header-image {
  272. width: 100%;
  273. height: 100%;
  274. }
  275. .is-text-box {
  276. border: 1px #eee solid;
  277. border-radius: 5px;
  278. }
  279. .is-text-image {
  280. width: 25px;
  281. height: 25px;
  282. margin-left: 5px;
  283. }
  284. .rotate {
  285. position: absolute;
  286. transform: rotate(90deg);
  287. }
  288. .icon-del-box {
  289. /* #ifndef APP-NVUE */
  290. display: flex;
  291. margin: auto 0;
  292. /* #endif */
  293. align-items: center;
  294. justify-content: center;
  295. position: absolute;
  296. top: 0px;
  297. bottom: 0;
  298. right: 5px;
  299. height: 26px;
  300. width: 26px;
  301. // border-radius: 50%;
  302. // background-color: rgba(0, 0, 0, 0.5);
  303. z-index: 2;
  304. transform: rotate(-45deg);
  305. }
  306. .icon-del {
  307. width: 15px;
  308. height: 1px;
  309. background-color: #333;
  310. // border-radius: 1px;
  311. }
  312. /* #ifdef H5 */
  313. @media all and (min-width: 768px) {
  314. .uni-file-picker__files {
  315. max-width: 375px;
  316. }
  317. }
  318. /* #endif */
  319. </style>