cwg-file-picker-wrapper.vue 23 KB

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