cwg-file-picker-wrapper.vue 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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. uni.showToast({
  431. title: 'Loading...',
  432. icon: 'loading',
  433. duration: 10000000,
  434. mask: true
  435. })
  436. const files = tempFileQueue.value
  437. tempFileQueue.value = []
  438. for (const file of files) {
  439. // APP base64
  440. if (file.base64) {
  441. await uploadBase64(file)
  442. }
  443. else {
  444. // H5 正常文件
  445. await uploadFile(file)
  446. }
  447. }
  448. uni.hideToast()
  449. }
  450. const uploadFile = async (fileItem) => {
  451. await whenDomainReady()
  452. return new Promise((resolve) => {
  453. innerFileList.value.push({ ...fileItem, status: 'uploading', progress: 0 })
  454. console.log(innerFileList.value,'upload231')
  455. const index = innerFileList.value.length - 1
  456. const url = props.action || config.Host80 + props.uploadUrl
  457. console.log({
  458. url,
  459. filePath: fileItem.path,
  460. name: props.uploadName,
  461. header: { 'Access-Token': userToken.value, ...props.uploadHeaders },
  462. formData: props.uploadData
  463. }, 100000);
  464. const task = uni.uploadFile({
  465. url,
  466. filePath: fileItem.path,
  467. name: props.uploadName,
  468. header: { 'Access-Token': userToken.value, ...props.uploadHeaders },
  469. formData: props.uploadData,
  470. success: (res) => {
  471. const result = typeof props.responseHandler === 'function'
  472. ? props.responseHandler(res.data)
  473. : {
  474. success: typeof res.data === 'string'
  475. ? JSON.parse(res.data).code === 200
  476. : res.data.code === 200,
  477. path: typeof res.data === 'string'
  478. ? (JSON.parse(res.data).data?.path || JSON.parse(res.data).data)
  479. : (res.data.data?.path || res.data.data),
  480. message: typeof res.data === 'string'
  481. ? JSON.parse(res.data).msg
  482. : res.data.msg
  483. }
  484. console.log(innerFileList.value,'file',result)
  485. if (result.success) {
  486. innerFileList.value[index].progress = 100
  487. innerFileList.value[index].status = 'success'
  488. innerFileList.value[index].url = config.Host05 + result.path
  489. innerFileList.value[index].path = result.path
  490. emit('success', innerFileList.value[index])
  491. } else {
  492. innerFileList.value[index].status = 'error'
  493. uni.showToast({ title: result.message || '上传失败', icon: 'error' })
  494. emit('fail', result.message || '上传失败')
  495. }
  496. resolve(result)
  497. },
  498. fail: () => {
  499. innerFileList.value[index].status = 'error'
  500. uni.showToast({ title: '上传失败', icon: 'error' })
  501. emit('fail', '网络异常')
  502. resolve(null)
  503. }
  504. })
  505. task.onProgressUpdate((p) => {
  506. innerFileList.value[index].progress = p.progress
  507. emit('progress', p, index)
  508. })
  509. })
  510. }
  511. const uploadBase64 = async (fileItem) => {
  512. await whenDomainReady()
  513. return new Promise((resolve) => {
  514. innerFileList.value.push({
  515. ...fileItem,
  516. status: 'uploading',
  517. progress: 0
  518. })
  519. const index =
  520. innerFileList.value.length - 1
  521. const url =
  522. props.action ||
  523. config.Host80 + props.uploadUrl
  524. uni.request({
  525. url,
  526. method: 'POST',
  527. header: {
  528. 'Content-Type': 'application/json',
  529. 'Access-Token': userToken.value,
  530. ...props.uploadHeaders
  531. },
  532. data: {
  533. file: fileItem.base64,
  534. fileName: fileItem.name,
  535. fileType: fileItem.type,
  536. ...props.uploadData
  537. },
  538. success: (res) => {
  539. const result =
  540. typeof props.responseHandler === 'function'
  541. ? props.responseHandler(res.data)
  542. : {
  543. success: res.data.code === 200,
  544. path: res.data.data?.path || res.data.data,
  545. message: res.data.msg
  546. }
  547. if (result.success) {
  548. innerFileList.value[index].progress = 100
  549. innerFileList.value[index].status = 'success'
  550. // 这里继续用后端返回URL
  551. innerFileList.value[index].url =
  552. config.Host05 + result.path
  553. innerFileList.value[index].path =
  554. result.path
  555. emit(
  556. 'success',
  557. innerFileList.value[index]
  558. )
  559. } else {
  560. innerFileList.value[index].status = 'error'
  561. uni.showToast({
  562. title: result.message || '上传失败',
  563. icon: 'none'
  564. })
  565. emit('fail', result.message)
  566. }
  567. resolve(result)
  568. },
  569. fail: () => {
  570. innerFileList.value[index].status = 'error'
  571. uni.showToast({
  572. title: '上传失败',
  573. icon: 'none'
  574. })
  575. emit('fail', '网络异常')
  576. resolve(null)
  577. }
  578. })
  579. })
  580. }
  581. /** 从 data URL 中取出 base64 正文,避免对大字符串做正则匹配 */
  582. const getBase64Body = (dataUrl) => {
  583. if (!dataUrl || typeof dataUrl !== 'string') return ''
  584. const marker = 'base64,'
  585. const markerIdx = dataUrl.indexOf(marker)
  586. if (markerIdx !== -1) {
  587. return dataUrl.substring(markerIdx + marker.length)
  588. }
  589. const commaIdx = dataUrl.indexOf(',')
  590. return commaIdx !== -1 ? dataUrl.substring(commaIdx + 1) : dataUrl
  591. }
  592. const base64ToTempFile = (base64, fileName = 'file.png') => {
  593. return new Promise((resolve, reject) => {
  594. // #ifndef APP-PLUS
  595. reject(new Error('仅 APP 环境支持'))
  596. return
  597. // #endif
  598. const base64Data = getBase64Body(base64)
  599. if (!base64Data) {
  600. reject(new Error('base64格式错误'))
  601. return
  602. }
  603. const safeName = `${Date.now()}_${(fileName || 'file').replace(/[/\\]/g, '_')}`
  604. const dirName = 'cwg_upload'
  605. plus.io.resolveLocalFileSystemURL('_doc', (docEntry) => {
  606. docEntry.getDirectory(dirName, { create: true, exclusive: false }, (dirEntry) => {
  607. dirEntry.getFile(safeName, { create: true, exclusive: false }, (fileEntry) => {
  608. fileEntry.createWriter((writer) => {
  609. writer.onwrite = () => {
  610. resolve(fileEntry.toLocalURL())
  611. }
  612. writer.onerror = (err) => {
  613. reject(err || new Error('写入临时文件失败'))
  614. }
  615. writer.seek(0)
  616. writer.writeAsBinary(base64Data)
  617. }, reject)
  618. }, reject)
  619. }, reject)
  620. }, reject)
  621. })
  622. }
  623. const reUploadFile = (index) => {
  624. const file = innerFileList.value[index]
  625. uploadFile(file)
  626. }
  627. const imgStyle = computed(() => {
  628. let style = {}
  629. if (props.imageWidth) {
  630. style.width = typeof props.imageWidth === 'number' ? `${props.imageWidth}px` : `${props.imageWidth}`
  631. }
  632. if (props.imageHeight) {
  633. style.height = typeof props.imageHeight === 'number' ? `${props.imageHeight}px` : `${props.imageHeight}`
  634. }
  635. console.log(style)
  636. return style
  637. })
  638. </script>
  639. <style scoped lang="scss">
  640. .file-picker-wrapper{
  641. }
  642. /* 布局 */
  643. .uni-file-picker__container {
  644. display: flex;
  645. flex-wrap: wrap;
  646. //margin: -5px;
  647. justify-content: center;
  648. align-self: center;
  649. gap: px2rpx(5);
  650. width: 100%;
  651. }
  652. .file-picker__box {
  653. position: relative;
  654. width: 33.3%;
  655. height: 0;
  656. //padding-top: 33.3%;
  657. box-sizing: border-box;
  658. }
  659. .file-picker__box-content {
  660. position: absolute;
  661. top: 0;
  662. right: 0;
  663. bottom: 0;
  664. left: 0;
  665. //margin: 5px;
  666. border-radius: 5px;
  667. overflow: hidden;
  668. background: #f7f7f7;
  669. }
  670. /* 图片 */
  671. .file-image {
  672. width: 100%;
  673. height: 100%;
  674. }
  675. /* 文件封面 */
  676. .file-cover {
  677. width: 100%;
  678. height: 100%;
  679. display: flex;
  680. align-items: center;
  681. justify-content: center;
  682. flex-direction: column;
  683. color: var(--bs-emphasis-color);
  684. }
  685. /* 视频 */
  686. .video-box {
  687. position: relative;
  688. }
  689. .video-play-icon {
  690. position: absolute;
  691. font-size: 30px;
  692. color: var(--bs-emphasis-color);
  693. background: rgba(0, 0, 0, 0.5);
  694. width: 50px;
  695. height: 50px;
  696. border-radius: 50%;
  697. display: flex;
  698. align-items: center;
  699. justify-content: center;
  700. }
  701. /* 文件类型颜色 */
  702. .file-pdf {
  703. background: #ff4f4f;
  704. }
  705. .file-word {
  706. background: #3a7ff5;
  707. }
  708. .file-excel {
  709. background: #24a148;
  710. }
  711. .file-video {
  712. background: #000;
  713. }
  714. .file-other {
  715. background: #f7f7f7;
  716. }
  717. .file-big-icon {
  718. font-size: 16px;
  719. font-weight: bold;
  720. margin-bottom: 4px;
  721. }
  722. .file-ext-name {
  723. font-size: 12px;
  724. opacity: 0.9;
  725. }
  726. /* 添加按钮 */
  727. .is-add {
  728. display: flex;
  729. align-items: center;
  730. justify-content: center;
  731. background: var(--bs-body-bg);
  732. }
  733. .upload-icon {
  734. color: var(--bs-heading-color);
  735. }
  736. /* 删除按钮 */
  737. .icon-del-box {
  738. position: absolute;
  739. top: 3px;
  740. right: 3px;
  741. width: 26px;
  742. height: 26px;
  743. border-radius: 50%;
  744. background: rgba(0, 0, 0, 0.5);
  745. display: flex;
  746. align-items: center;
  747. justify-content: center;
  748. z-index: 10;
  749. transform: rotate(-45deg);
  750. }
  751. .icon-del {
  752. width: 15px;
  753. height: 2px;
  754. background: #fff;
  755. border-radius: 2px;
  756. }
  757. .rotate {
  758. position: absolute;
  759. transform: rotate(90deg);
  760. }
  761. /* 进度 */
  762. .file-picker__progress {
  763. position: absolute;
  764. bottom: 0;
  765. left: 0;
  766. right: 0;
  767. z-index: 2;
  768. }
  769. /* 失败遮罩 */
  770. .file-picker__mask {
  771. position: absolute;
  772. top: 0;
  773. left: 0;
  774. right: 0;
  775. bottom: 0;
  776. background: rgba(0, 0, 0, 0.4);
  777. color: var(--bs-emphasis-color);
  778. display: flex;
  779. align-items: center;
  780. justify-content: center;
  781. font-size: 12px;
  782. }
  783. /* 只读模式 */
  784. .file-list {
  785. padding: 5px;
  786. }
  787. .file-item {
  788. display: flex;
  789. align-items: center;
  790. gap: 10px;
  791. padding: 10px;
  792. background: #f8f8f8;
  793. border-radius: 8px;
  794. margin-bottom: 10px;
  795. }
  796. .file-thumb,
  797. .file-icon {
  798. width: 60px;
  799. height: 60px;
  800. border-radius: 6px;
  801. }
  802. .file-icon {
  803. background: #eee;
  804. display: flex;
  805. align-items: center;
  806. justify-content: center;
  807. }
  808. .file-icon-text {
  809. font-size: 12px;
  810. font-weight: bold;
  811. }
  812. .file-name {
  813. flex: 1;
  814. font-size: 14px;
  815. color: var(--bs-heading-color);
  816. }
  817. .empty-text {
  818. text-align: center;
  819. color: var(--bs-heading-color);
  820. padding: 20px 0;
  821. }
  822. </style>