cwg-file-picker-wrapper.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. <template>
  2. <view class="file-picker-wrapper">
  3. <!-- 只读模式:仅展示 -->
  4. <view v-if="readonly" class="file-list readonly-list">
  5. <view v-for="(file, index) in innerFileList" :key="index" class="file-item readonly-item"
  6. @click="previewFile(file, index)">
  7. <image v-if="isImage(file)" :src="file.url || file.path" mode="aspectFill" class="file-thumb"
  8. :style="imgStyle" />
  9. <view v-else class="file-icon" :class="getFileClass(file.name)">
  10. <text class="file-icon-text">{{ getFileIcon(file.name) }}</text>
  11. </view>
  12. <text class="file-name">{{ file.name }}</text>
  13. </view>
  14. <view v-if="!innerFileList?.length" class="empty-text">暂无文件</view>
  15. </view>
  16. <!-- 正常模式:宫格上传(完全对齐官方 upload-image 样式) -->
  17. <view v-else class="uni-file-picker__container">
  18. <view class="file-picker__box" v-for="(item, index) in innerFileList" :key="index"
  19. :style="typeof boxStyle === 'object' ? boxStyle : { cssText: boxStyle }" >
  20. <view class="file-picker__box-content " :style="borderStyle" >
  21. <!-- 图片 -->
  22. <image v-if="isImage(item)" class="file-image cursor-pointer" :data-tooltip="tooltipText(item)" :src="item.url || item.path" mode="aspectFill"
  23. :style="imgStyle" @click.stop="previewFile(item, index)" />
  24. <!-- 视频 → 显示第一帧 + 播放图标 -->
  25. <view v-else-if="isVideo(item)" class="file-cover video-box cursor-pointer" :data-tooltip="tooltipText(item)" @click.stop="previewFile(item, index)">
  26. <image :src="item.url || item.path" class="file-image" mode="aspectFill" :style="imgStyle" />
  27. <view class="video-play-icon">▶</view>
  28. </view>
  29. <!-- 其他文件(PDF/Word/Excel) -->
  30. <view v-else class="file-cover file-box cursor-pointer" :data-tooltip="tooltipText(item)" :class="getFileClass(item.name)"
  31. @click.stop="previewFile(item, index)">
  32. <text class="file-big-icon">{{ getFileIcon(item.name) }}</text>
  33. <text class="file-ext-name">{{ getFileExt(item.name) }}</text>
  34. </view>
  35. <!-- 删除 -->
  36. <view v-if="delIcon" class="icon-del-box cursor-pointer" :data-tooltip="t('vu.tooltip.t17')" @click.stop="deleteFile(index)">
  37. <view class="icon-del"></view>
  38. <view class="icon-del rotate"></view>
  39. </view>
  40. <!-- 进度 -->
  41. <view v-if="item.status === 'uploading'" class="file-picker__progress">
  42. <progress class="file-picker__progress-item" :percent="item.progress" stroke-width="4"
  43. backgroundColor="#EBEBEB" />
  44. </view>
  45. <!-- 失败重试 -->
  46. <view v-if="item.status === 'error'" class="file-picker__mask" @click.stop="reUploadFile(index)">
  47. 点击重试
  48. </view>
  49. </view>
  50. </view>
  51. <!-- 添加按钮 -->
  52. <view v-if="innerFileList?.length < limit" class="file-picker__box cursor-pointer"
  53. :style="typeof boxStyle === 'object' ? boxStyle : { cssText: boxStyle }" :data-tooltip="t('vu.tooltip.t10')" data-placement="top">
  54. <view class="file-picker__box-content is-add" :style="borderStyle" @click="handleChoose">
  55. <cwg-icon name="icon_add" class="upload-icon" :size="24" />
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. <cop-chooseFile :trigger="triggerFlag" accept="*" @receiveRenderFile="handleFile">
  61. </cop-chooseFile>
  62. </template>
  63. <script setup>
  64. import { ref, watch, nextTick, computed } from 'vue'
  65. import config from '@/config'
  66. import { useI18n } from 'vue-i18n';
  67. import { userToken } from '@/composables/config'
  68. import { whenDomainReady } from '@/utils/dynamicDomain'
  69. import copChooseFile from '@/uni_modules/cop-chooseFile/components/cop-chooseFile/cop-chooseFile.vue'
  70. const { t, locale } = useI18n();
  71. // === Vue3 v-model 标准写法 + 多类型兼容 ===
  72. const props = defineProps({
  73. modelValue: {
  74. type: [Array, String, Object],
  75. default: () => []
  76. },
  77. readonly: {
  78. type: Boolean,
  79. default: false
  80. },
  81. disabled: {
  82. type: Boolean,
  83. default: false
  84. },
  85. limit: {
  86. type: Number,
  87. default: 9
  88. },
  89. fileMediatype: {
  90. type: String,
  91. default: 'all'
  92. },
  93. delIcon: {
  94. type: Boolean,
  95. default: true
  96. },
  97. disablePreview: {
  98. type: Boolean,
  99. default: false
  100. },
  101. autoUpload: {
  102. type: Boolean,
  103. default: true
  104. },
  105. action: {
  106. type: String,
  107. default: ''
  108. },
  109. imageWidth: {
  110. type: [String,Number],
  111. default: ''
  112. },
  113. imageHeight: {
  114. type: [String,Number],
  115. default: ''
  116. },
  117. uploadUrl: {
  118. type: String,
  119. default: '/custom/bank/upload'
  120. },
  121. uploadHeaders: {
  122. type: Object,
  123. default: () => ({})
  124. },
  125. uploadName: {
  126. type: String,
  127. default: 'file'
  128. },
  129. uploadData: {
  130. type: Object,
  131. default: () => ({})
  132. },
  133. responseHandler: {
  134. type: Function,
  135. default: (res) => {
  136. try {
  137. const data = typeof res === 'string' ? JSON.parse(res) : res
  138. return {
  139. success: data.code === 200,
  140. path: data.data?.path || data.data,
  141. message: data.msg || '上传成功'
  142. }
  143. } catch (e) {
  144. return { success: false, message: '解析失败' }
  145. }
  146. }
  147. }
  148. })
  149. const emit = defineEmits([
  150. 'update:modelValue',
  151. 'change',
  152. 'delete',
  153. 'success',
  154. 'fail',
  155. 'progress',
  156. 'select'
  157. ])
  158. const triggerFlag = ref(0)
  159. // 内部数据
  160. const innerFileList = ref([])
  161. const tempFileQueue = ref([])
  162. const originalType = ref('array') // 'string' | 'object' | 'array'
  163. const borderStyle = 'border:1px #eee solid;border-radius:5px;'
  164. const boxStyle = computed(() => {
  165. if (props.imageWidth || props.imageHeight) {
  166. const width = typeof props.imageWidth === 'number' ? `${props.imageWidth}px` : props.imageWidth || 'auto'
  167. const height = typeof props.imageHeight === 'number' ? `${props.imageHeight}px` : props.imageHeight || 'auto'
  168. return { width, height }
  169. }
  170. return 'width:33.3%;height:0;'
  171. })
  172. const tooltipText = (file)=>{
  173. return (props.disablePreview || file.status === 'uploading' || file.status === 'error')?'':t('vu.tooltip.t16')
  174. }
  175. // ==============================================
  176. // 统一格式化函数(修复递归核心)
  177. // ==============================================
  178. function formatValue(val) {
  179. if (!val) return []
  180. // 字符串
  181. if (typeof val === 'string') {
  182. return [{
  183. path: val,
  184. url: val.startsWith('http') ? val : config.Host05 + val,
  185. name: val.split('/').pop(),
  186. status: 'success'
  187. }]
  188. }
  189. // 对象
  190. if (typeof val === 'object' && !Array.isArray(val)) {
  191. const path = val.path || val.url || ''
  192. return [{
  193. ...val,
  194. path,
  195. url: val.url || (path.startsWith('http') ? path : config.Host05 + path),
  196. name: val.name || path.split('/').pop(),
  197. status: 'success'
  198. }]
  199. }
  200. // 数组
  201. if (Array.isArray(val)) {
  202. return val.map(item => {
  203. if (typeof item === 'string') {
  204. return {
  205. path: item,
  206. url: item.startsWith('http') ? item : config.Host05 + item,
  207. name: item.split('/').pop(),
  208. status: 'success'
  209. }
  210. } else {
  211. const path = item?.path || item?.url || ''
  212. return {
  213. ...item,
  214. path,
  215. url: item?.url || (path.startsWith('http') ? path : config.Host05 + path),
  216. name: item?.name || path.split('/').pop(),
  217. status: 'success'
  218. }
  219. }
  220. })
  221. }
  222. return []
  223. }
  224. // ==============================================
  225. // 监听外部传入:防重复赋值 → 无递归
  226. // ==============================================
  227. watch(
  228. () => props.modelValue,
  229. (val) => {
  230. const formatted = formatValue(val)
  231. // 值相同不更新,防止死循环
  232. if (JSON.stringify(innerFileList.value) === JSON.stringify(formatted)) return
  233. if (!val) {
  234. // innerFileList.value = []
  235. // originalType.value = 'array'
  236. return
  237. }
  238. // 记录原始类型
  239. if (typeof val === 'string') originalType.value = 'string'
  240. else if (typeof val === 'object' && !Array.isArray(val)) originalType.value = 'object'
  241. else originalType.value = 'array'
  242. innerFileList.value = formatted
  243. },
  244. { immediate: true, deep: true }
  245. )
  246. // ==============================================
  247. // 内部变化同步外部:nextTick → 无递归
  248. // ==============================================
  249. watch(
  250. innerFileList,
  251. (list) => {
  252. nextTick(() => {
  253. let returnValue = []
  254. if (!list || list.length === 0) {
  255. returnValue = originalType.value === 'string' ? '' :
  256. originalType.value === 'object' ? {} : []
  257. emitUpdate(returnValue)
  258. return
  259. }
  260. // 只返回干净的 path 数据
  261. const cleanList = list.map(item => item.path || item.url || '')
  262. // 按原始类型返回
  263. if (props.limit === 1) {
  264. returnValue = cleanList[0] || ''
  265. } else if (originalType.value === 'string') returnValue = cleanList[0] || ''
  266. else if (originalType.value === 'object') returnValue = { path: cleanList[0] || '' }
  267. else returnValue = cleanList
  268. emitUpdate(returnValue)
  269. })
  270. },
  271. { deep: true }
  272. )
  273. // 统一触发更新
  274. function emitUpdate(val) {
  275. emit('update:modelValue', val)
  276. emit('change', val)
  277. }
  278. // ==============================================
  279. // 文件类型判断
  280. // ==============================================
  281. const isImage = (file) => {
  282. const ext = (file.path || file.name || '').split('.').pop()?.toLowerCase()
  283. return ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'svg'].includes(ext)
  284. }
  285. const isVideo = (file) => {
  286. const ext = (file.path || file.name || '').split('.').pop()?.toLowerCase()
  287. return ['mp4', 'mov', 'avi', 'flv', 'wmv', 'rmvb'].includes(ext)
  288. }
  289. const getFileExt = (name) => {
  290. if (!name) return ''
  291. return name.split('.').pop()?.toUpperCase()
  292. }
  293. const getFileIcon = (name) => {
  294. const ext = getFileExt(name)
  295. if (ext === 'PDF') return 'PDF'
  296. if (['DOC', 'DOCX'].includes(ext)) return 'WORD'
  297. if (['XLS', 'XLSX'].includes(ext)) return 'EXCEL'
  298. if (isVideo({ name })) return '▶'
  299. if (isImage({ name })) return 'IMG'
  300. return 'FILE'
  301. }
  302. const getFileClass = (name) => {
  303. const ext = getFileExt(name)
  304. if (ext === 'PDF') return 'file-pdf'
  305. if (['DOC', 'DOCX'].includes(ext)) return 'file-word'
  306. if (['XLS', 'XLSX'].includes(ext)) return 'file-excel'
  307. if (isVideo({ name })) return 'file-video'
  308. return 'file-other'
  309. }
  310. // ==============================================
  311. // 预览
  312. // ==============================================
  313. const previewFile = (file, index) => {
  314. if (props.disablePreview || file.status === 'uploading' || file.status === 'error') return
  315. const url = file.url || file.path
  316. const name = file.name || '文件'
  317. if (isImage(file)) {
  318. const successImages = innerFileList.value.filter(item => isImage(item) && item.status === 'success')
  319. const realIndex = successImages.findIndex(item => item.url === file.url && item.path === file.path)
  320. const urls = successImages.map(item => item.url || item.path)
  321. uni.previewImage({ current: realIndex, urls, loop: true })
  322. return
  323. }
  324. uni.navigateTo({
  325. url: `/pages/common/webview?url=${encodeURIComponent(url)}&title=${encodeURIComponent(name)}`
  326. })
  327. }
  328. // ==============================================
  329. // 上传 / 删除 / 重试
  330. // ==============================================
  331. const deleteFile = (index) => {
  332. const item = innerFileList.value[index]
  333. innerFileList.value.splice(index, 1)
  334. emit('delete', item, index)
  335. }
  336. const handleChoose = () => {
  337. if (props.disabled || props.readonly) return
  338. const count = props.limit - innerFileList.value.length
  339. // #ifdef H5
  340. uni.chooseFile({
  341. type: props.fileMediatype,
  342. count,
  343. success: (res) => {
  344. const files = res.tempFiles || res.tempFilePaths.map((p, i) => ({
  345. path: p, name: `file_${i}`
  346. }))
  347. emit('select', { tempFiles: files })
  348. tempFileQueue.value = files
  349. if (props.autoUpload) startUpload()
  350. }
  351. })
  352. // #endif
  353. // #ifdef APP-PLUS
  354. triggerFlag.value = Date.now()
  355. // chooseFileFromModule({
  356. // complete: (res) => {
  357. // console.log(res)
  358. // let path = res.path
  359. // let name = res.name
  360. // let fileType = ''
  361. // // 如果没有name,默认为:截取最后一个/之后的内容
  362. // if (!name) {
  363. // let lastIndex = path.lastIndexOf('/');
  364. // if (lastIndex !== -1) {
  365. // name = path.substring(lastIndex + 1);
  366. // } else {
  367. // name = Math.random().toString(36).substr(2) + Date.now();
  368. // }
  369. // }
  370. // // 使用 lastIndexOf 方法找到最后一个 . 的位置
  371. // let lastDotIndex = name.lastIndexOf('.');
  372. // if (lastDotIndex !== -1) {
  373. // fileType = name.substring(lastDotIndex + 1);
  374. // } else {
  375. // console.log('文件路径中没有 .');
  376. // }
  377. // let file = {
  378. // size: res.size,
  379. // path,
  380. // fileType,
  381. // name
  382. // }
  383. // console.log('根据需求构造的数据', file)
  384. // }
  385. // })
  386. // #endif
  387. }
  388. // 处理选择的文件
  389. const handleFile = async (fileData) => {
  390. try {
  391. const tempPath = await base64ToTempFile(
  392. fileData.filePath,
  393. fileData.name
  394. )
  395. const file = {
  396. name: fileData.name,
  397. size: fileData.size,
  398. type: fileData.type,
  399. // 关键
  400. path: tempPath,
  401. status: 'ready'
  402. }
  403. console.log(file, 121212);
  404. tempFileQueue.value = [file]
  405. emit('select', {
  406. tempFiles: [file]
  407. })
  408. if (props.autoUpload) {
  409. startUpload()
  410. }
  411. } catch (e) {
  412. const errMsg = e?.message || (typeof e === 'string' ? e : '文件处理失败')
  413. console.error('handleFile failed:', e)
  414. uni.showToast({
  415. title: errMsg,
  416. icon: 'none'
  417. })
  418. }
  419. }
  420. const startUpload = async () => {
  421. const files = tempFileQueue.value
  422. tempFileQueue.value = []
  423. for (const file of files) {
  424. // APP base64
  425. if (file.base64) {
  426. await uploadBase64(file)
  427. }
  428. // H5 正常文件
  429. else {
  430. await uploadFile(file)
  431. }
  432. }
  433. }
  434. const uploadFile = async (fileItem) => {
  435. await whenDomainReady()
  436. return new Promise((resolve) => {
  437. innerFileList.value.push({ ...fileItem, status: 'uploading', progress: 0 })
  438. console.log(innerFileList.value,'upload231')
  439. const index = innerFileList.value.length - 1
  440. const url = props.action || config.Host80 + props.uploadUrl
  441. console.log({
  442. url,
  443. filePath: fileItem.path,
  444. name: props.uploadName,
  445. header: { 'Access-Token': userToken.value, ...props.uploadHeaders },
  446. formData: props.uploadData
  447. }, 100000);
  448. const task = uni.uploadFile({
  449. url,
  450. filePath: fileItem.path,
  451. name: props.uploadName,
  452. header: { 'Access-Token': userToken.value, ...props.uploadHeaders },
  453. formData: props.uploadData,
  454. success: (res) => {
  455. const result = typeof props.responseHandler === 'function'
  456. ? props.responseHandler(res.data)
  457. : {
  458. success: typeof res.data === 'string'
  459. ? JSON.parse(res.data).code === 200
  460. : res.data.code === 200,
  461. path: typeof res.data === 'string'
  462. ? (JSON.parse(res.data).data?.path || JSON.parse(res.data).data)
  463. : (res.data.data?.path || res.data.data),
  464. message: typeof res.data === 'string'
  465. ? JSON.parse(res.data).msg
  466. : res.data.msg
  467. }
  468. console.log(innerFileList.value,'file',result)
  469. if (result.success) {
  470. innerFileList.value[index].progress = 100
  471. innerFileList.value[index].status = 'success'
  472. innerFileList.value[index].url = config.Host05 + result.path
  473. innerFileList.value[index].path = result.path
  474. emit('success', innerFileList.value[index])
  475. } else {
  476. innerFileList.value[index].status = 'error'
  477. uni.showToast({ title: result.message || '上传失败', icon: 'error' })
  478. emit('fail', result.message || '上传失败')
  479. }
  480. resolve(result)
  481. },
  482. fail: () => {
  483. innerFileList.value[index].status = 'error'
  484. uni.showToast({ title: '上传失败', icon: 'error' })
  485. emit('fail', '网络异常')
  486. resolve(null)
  487. }
  488. })
  489. task.onProgressUpdate((p) => {
  490. innerFileList.value[index].progress = p.progress
  491. emit('progress', p, index)
  492. })
  493. })
  494. }
  495. const uploadBase64 = async (fileItem) => {
  496. await whenDomainReady()
  497. return new Promise((resolve) => {
  498. innerFileList.value.push({
  499. ...fileItem,
  500. status: 'uploading',
  501. progress: 0
  502. })
  503. const index =
  504. innerFileList.value.length - 1
  505. const url =
  506. props.action ||
  507. config.Host80 + props.uploadUrl
  508. uni.request({
  509. url,
  510. method: 'POST',
  511. header: {
  512. 'Content-Type': 'application/json',
  513. 'Access-Token': userToken.value,
  514. ...props.uploadHeaders
  515. },
  516. data: {
  517. file: fileItem.base64,
  518. fileName: fileItem.name,
  519. fileType: fileItem.type,
  520. ...props.uploadData
  521. },
  522. success: (res) => {
  523. const result =
  524. typeof props.responseHandler === 'function'
  525. ? props.responseHandler(res.data)
  526. : {
  527. success: res.data.code === 200,
  528. path: res.data.data?.path || res.data.data,
  529. message: res.data.msg
  530. }
  531. if (result.success) {
  532. innerFileList.value[index].progress = 100
  533. innerFileList.value[index].status = 'success'
  534. // 这里继续用后端返回URL
  535. innerFileList.value[index].url =
  536. config.Host05 + result.path
  537. innerFileList.value[index].path =
  538. result.path
  539. emit(
  540. 'success',
  541. innerFileList.value[index]
  542. )
  543. } else {
  544. innerFileList.value[index].status = 'error'
  545. uni.showToast({
  546. title: result.message || '上传失败',
  547. icon: 'none'
  548. })
  549. emit('fail', result.message)
  550. }
  551. resolve(result)
  552. },
  553. fail: () => {
  554. innerFileList.value[index].status = 'error'
  555. uni.showToast({
  556. title: '上传失败',
  557. icon: 'none'
  558. })
  559. emit('fail', '网络异常')
  560. resolve(null)
  561. }
  562. })
  563. })
  564. }
  565. /** 从 data URL 中取出 base64 正文,避免对大字符串做正则匹配 */
  566. const getBase64Body = (dataUrl) => {
  567. if (!dataUrl || typeof dataUrl !== 'string') return ''
  568. const marker = 'base64,'
  569. const markerIdx = dataUrl.indexOf(marker)
  570. if (markerIdx !== -1) {
  571. return dataUrl.substring(markerIdx + marker.length)
  572. }
  573. const commaIdx = dataUrl.indexOf(',')
  574. return commaIdx !== -1 ? dataUrl.substring(commaIdx + 1) : dataUrl
  575. }
  576. const base64ToTempFile = (base64, fileName = 'file.png') => {
  577. return new Promise((resolve, reject) => {
  578. // #ifndef APP-PLUS
  579. reject(new Error('仅 APP 环境支持'))
  580. return
  581. // #endif
  582. const base64Data = getBase64Body(base64)
  583. if (!base64Data) {
  584. reject(new Error('base64格式错误'))
  585. return
  586. }
  587. const safeName = `${Date.now()}_${(fileName || 'file').replace(/[/\\]/g, '_')}`
  588. const dirName = 'cwg_upload'
  589. plus.io.resolveLocalFileSystemURL('_doc', (docEntry) => {
  590. docEntry.getDirectory(dirName, { create: true, exclusive: false }, (dirEntry) => {
  591. dirEntry.getFile(safeName, { create: true, exclusive: false }, (fileEntry) => {
  592. fileEntry.createWriter((writer) => {
  593. writer.onwrite = () => {
  594. resolve(fileEntry.toLocalURL())
  595. }
  596. writer.onerror = (err) => {
  597. reject(err || new Error('写入临时文件失败'))
  598. }
  599. writer.seek(0)
  600. writer.writeAsBinary(base64Data)
  601. }, reject)
  602. }, reject)
  603. }, reject)
  604. }, reject)
  605. })
  606. }
  607. const reUploadFile = (index) => {
  608. const file = innerFileList.value[index]
  609. uploadFile(file)
  610. }
  611. const imgStyle = computed(() => {
  612. let style = {}
  613. if (props.imageWidth) {
  614. style.width = typeof props.imageWidth === 'number' ? `${props.imageWidth}px` : `${props.imageWidth}`
  615. }
  616. if (props.imageHeight) {
  617. style.height = typeof props.imageHeight === 'number' ? `${props.imageHeight}px` : `${props.imageHeight}`
  618. }
  619. console.log(style)
  620. return style
  621. })
  622. </script>
  623. <style scoped lang="scss">
  624. .file-picker-wrapper{
  625. }
  626. /* 布局 */
  627. .uni-file-picker__container {
  628. display: flex;
  629. flex-wrap: wrap;
  630. //margin: -5px;
  631. justify-content: center;
  632. align-self: center;
  633. gap: px2rpx(5);
  634. width: 100%;
  635. }
  636. .file-picker__box {
  637. position: relative;
  638. width: 33.3%;
  639. height: 0;
  640. //padding-top: 33.3%;
  641. box-sizing: border-box;
  642. }
  643. .file-picker__box-content {
  644. position: absolute;
  645. top: 0;
  646. right: 0;
  647. bottom: 0;
  648. left: 0;
  649. //margin: 5px;
  650. border-radius: 5px;
  651. overflow: hidden;
  652. background: #f7f7f7;
  653. }
  654. /* 图片 */
  655. .file-image {
  656. width: 100%;
  657. height: 100%;
  658. }
  659. /* 文件封面 */
  660. .file-cover {
  661. width: 100%;
  662. height: 100%;
  663. display: flex;
  664. align-items: center;
  665. justify-content: center;
  666. flex-direction: column;
  667. color: var(--bs-emphasis-color);
  668. }
  669. /* 视频 */
  670. .video-box {
  671. position: relative;
  672. }
  673. .video-play-icon {
  674. position: absolute;
  675. font-size: 30px;
  676. color: var(--bs-emphasis-color);
  677. background: rgba(0, 0, 0, 0.5);
  678. width: 50px;
  679. height: 50px;
  680. border-radius: 50%;
  681. display: flex;
  682. align-items: center;
  683. justify-content: center;
  684. }
  685. /* 文件类型颜色 */
  686. .file-pdf {
  687. background: #ff4f4f;
  688. }
  689. .file-word {
  690. background: #3a7ff5;
  691. }
  692. .file-excel {
  693. background: #24a148;
  694. }
  695. .file-video {
  696. background: #000;
  697. }
  698. .file-other {
  699. background: #f7f7f7;
  700. }
  701. .file-big-icon {
  702. font-size: 16px;
  703. font-weight: bold;
  704. margin-bottom: 4px;
  705. }
  706. .file-ext-name {
  707. font-size: 12px;
  708. opacity: 0.9;
  709. }
  710. /* 添加按钮 */
  711. .is-add {
  712. display: flex;
  713. align-items: center;
  714. justify-content: center;
  715. background: var(--bs-body-bg);
  716. }
  717. .upload-icon {
  718. color: var(--bs-heading-color);
  719. }
  720. /* 删除按钮 */
  721. .icon-del-box {
  722. position: absolute;
  723. top: 3px;
  724. right: 3px;
  725. width: 26px;
  726. height: 26px;
  727. border-radius: 50%;
  728. background: rgba(0, 0, 0, 0.5);
  729. display: flex;
  730. align-items: center;
  731. justify-content: center;
  732. z-index: 10;
  733. transform: rotate(-45deg);
  734. }
  735. .icon-del {
  736. width: 15px;
  737. height: 2px;
  738. background: #fff;
  739. border-radius: 2px;
  740. }
  741. .rotate {
  742. position: absolute;
  743. transform: rotate(90deg);
  744. }
  745. /* 进度 */
  746. .file-picker__progress {
  747. position: absolute;
  748. bottom: 0;
  749. left: 0;
  750. right: 0;
  751. z-index: 2;
  752. }
  753. /* 失败遮罩 */
  754. .file-picker__mask {
  755. position: absolute;
  756. top: 0;
  757. left: 0;
  758. right: 0;
  759. bottom: 0;
  760. background: rgba(0, 0, 0, 0.4);
  761. color: var(--bs-emphasis-color);
  762. display: flex;
  763. align-items: center;
  764. justify-content: center;
  765. font-size: 12px;
  766. }
  767. /* 只读模式 */
  768. .file-list {
  769. padding: 5px;
  770. }
  771. .file-item {
  772. display: flex;
  773. align-items: center;
  774. gap: 10px;
  775. padding: 10px;
  776. background: #f8f8f8;
  777. border-radius: 8px;
  778. margin-bottom: 10px;
  779. }
  780. .file-thumb,
  781. .file-icon {
  782. width: 60px;
  783. height: 60px;
  784. border-radius: 6px;
  785. }
  786. .file-icon {
  787. background: #eee;
  788. display: flex;
  789. align-items: center;
  790. justify-content: center;
  791. }
  792. .file-icon-text {
  793. font-size: 12px;
  794. font-weight: bold;
  795. }
  796. .file-name {
  797. flex: 1;
  798. font-size: 14px;
  799. color: var(--bs-heading-color);
  800. }
  801. .empty-text {
  802. text-align: center;
  803. color: var(--bs-heading-color);
  804. padding: 20px 0;
  805. }
  806. </style>