cwg-file-picker-wrapper.vue 24 KB

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