index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. <template>
  2. <view class="background-files">
  3. <view class="background-files-head adfacjb">
  4. <view>
  5. <view class="background-files-title">团队背景资料</view>
  6. <view class="background-files-count">已上传 {{ count }} 个文档</view>
  7. </view>
  8. <view v-if="editable" class="background-files-add" @click="chooseWechatFiles">从微信会话选择</view>
  9. </view>
  10. <template v-if="editable">
  11. <view class="background-files-pc" @click="usePcUpload">使用电脑上传</view>
  12. <view v-if="files.length" class="file-list">
  13. <view v-for="file in files" :key="file.id" class="file-row adfacjb">
  14. <view class="file-main">
  15. <view class="file-name">{{ file.fileName || file.name || '未命名文件' }}</view>
  16. <view class="file-meta">{{ formatFileSize(file.fileSize || file.size) }}</view>
  17. </view>
  18. <view class="file-actions adfac">
  19. <view class="file-action" @click="previewFile(file)">{{ previewingId === file.id ? '打开中' : '查看' }}</view>
  20. <view class="file-action danger" @click="removeFile(file)">停用</view>
  21. </view>
  22. </view>
  23. </view>
  24. <view v-if="queue.length" class="queue-list">
  25. <view class="queue-title">本次选择</view>
  26. <view v-for="item in queue" :key="item.id" class="queue-row">
  27. <view class="queue-line adfacjb">
  28. <view class="queue-name">{{ item.name }}</view>
  29. <view class="queue-status" :class="item.status">{{ queueStatusText(item) }}</view>
  30. </view>
  31. <view v-if="item.status === 'uploading'" class="queue-progress">
  32. <view class="queue-progress-current" :style="{ width: item.progress + '%' }"></view>
  33. </view>
  34. <view v-if="item.error" class="queue-error">{{ item.error }}</view>
  35. <view v-if="item.status === 'failed' && !item.validationError" class="queue-retry" @click="retryItem(item)">重试</view>
  36. </view>
  37. </view>
  38. </template>
  39. <view v-if="pcDialogVisible" class="overlay adffcacjc">
  40. <view class="pc-dialog">
  41. <view class="dialog-title">使用电脑上传</view>
  42. <view class="dialog-desc" v-if="remainingSeconds > 0">在电脑打开链接或输入验证码,有效期剩余 {{ remainingText }}</view>
  43. <view class="dialog-expired" v-else>本次上传会话已过期,请重新生成</view>
  44. <view class="session-expiry">失效时间:{{ session && session.expiresAt || '-' }}</view>
  45. <view class="session-code">{{ session && session.code || '------' }}</view>
  46. <view class="session-link">{{ session && session.uploadUrl || '' }}</view>
  47. <view class="session-actions adfacjb">
  48. <view class="session-button secondary" :class="{ disabled: remainingSeconds === 0 }" @click="copyUploadUrl">复制链接</view>
  49. <view class="session-button secondary" @click="refreshPcSession">重新生成</view>
  50. </view>
  51. <view class="session-actions adfacjb">
  52. <view class="session-button primary" @click="finishDeferredPcUpload">完成并刷新</view>
  53. <view class="session-button secondary" @click="skipDeferredPcUpload">暂不上传</view>
  54. </view>
  55. </view>
  56. </view>
  57. <view v-if="textPreview.visible" class="overlay text-overlay adffcacjc">
  58. <view class="text-dialog adffc">
  59. <view class="dialog-title text-title">{{ textPreview.fileName }}</view>
  60. <view v-if="textPreview.truncated" class="truncated-tip">文件较大,仅展示前 256KB 内容</view>
  61. <scroll-view class="text-content" scroll-y>
  62. <text selectable>{{ textPreview.content }}</text>
  63. </scroll-view>
  64. <view class="text-close" @click="closeTextPreview">关闭</view>
  65. </view>
  66. </view>
  67. </view>
  68. </template>
  69. <script>
  70. import {
  71. createTeamBackgroundSession,
  72. disableTeamBackgroundFile,
  73. downloadTeamBackgroundFile,
  74. listTeamBackgroundFiles,
  75. uploadTeamBackgroundFile
  76. } from '@/http/teamBackgroundFile.js'
  77. import teamBackgroundRules from '@/utils/teamBackgroundFile.js'
  78. const {
  79. createBackgroundQueueItem,
  80. createEmptyTextPreviewState,
  81. createTextPreviewState,
  82. getSessionRemainingSeconds,
  83. getUploadableBackgroundFiles
  84. } = teamBackgroundRules
  85. export default {
  86. props: {
  87. teamId: { type: [String, Number], default: '' },
  88. initialCount: { type: Number, default: 0 },
  89. editable: { type: Boolean, default: true }
  90. },
  91. data() {
  92. return {
  93. boundTeamId: '',
  94. session: null,
  95. pcDialogVisible: false,
  96. pcUploadDeferred: false,
  97. deferredPcResolve: null,
  98. files: [],
  99. count: this.initialCount || 0,
  100. queue: [],
  101. remainingSeconds: 0,
  102. sessionTimer: null,
  103. sessionRequestId: 0,
  104. refreshRequestId: 0,
  105. previewRequestId: 0,
  106. previewingId: '',
  107. uploadPromise: null,
  108. hasLoadedFiles: false,
  109. componentDestroyed: false,
  110. textPreview: createEmptyTextPreviewState()
  111. }
  112. },
  113. computed: {
  114. remainingText() {
  115. const minutes = Math.floor(this.remainingSeconds / 60)
  116. const seconds = this.remainingSeconds % 60
  117. return `${minutes}分${String(seconds).padStart(2, '0')}秒`
  118. }
  119. },
  120. watch: {
  121. teamId: {
  122. immediate: true,
  123. handler(value) {
  124. this.boundTeamId = value || ''
  125. this.hasLoadedFiles = false
  126. this.refreshRequestId += 1
  127. if (value && this.editable) {
  128. this.refreshFiles(value).catch(error => this.showError(error, '资料列表加载失败'))
  129. }
  130. }
  131. },
  132. initialCount(value) {
  133. if (!this.hasLoadedFiles) this.count = Number(value) || 0
  134. }
  135. },
  136. beforeDestroy() {
  137. this.componentDestroyed = true
  138. this.sessionRequestId += 1
  139. this.refreshRequestId += 1
  140. this.previewRequestId += 1
  141. this.clearSessionTimer()
  142. this.closeTextPreview()
  143. this.resolveDeferredPcUpload()
  144. },
  145. methods: {
  146. activeTeamId(teamId) {
  147. return teamId || this.teamId || this.boundTeamId
  148. },
  149. showError(error, fallback) {
  150. if (this.componentDestroyed) return
  151. this.$showToast(error && (error.message || error.msg) || fallback)
  152. },
  153. queueStatusText(item) {
  154. const labels = {
  155. pending: '待上传',
  156. uploading: `${item.progress || 0}%`,
  157. success: '已上传',
  158. failed: '失败'
  159. }
  160. return labels[item.status] || ''
  161. },
  162. formatFileSize(bytes) {
  163. const size = Number(bytes) || 0
  164. if (size < 1024) return `${size} B`
  165. if (size < 1024 * 1024) return `${Number((size / 1024).toFixed(1))} KB`
  166. return `${Number((size / 1024 / 1024).toFixed(1))} MB`
  167. },
  168. chooseWechatFiles() {
  169. if (typeof wx === 'undefined' || typeof wx.chooseMessageFile !== 'function') {
  170. this.$showToast('请在微信小程序中选择文件')
  171. return
  172. }
  173. wx.chooseMessageFile({
  174. count: 10,
  175. type: 'file',
  176. success: result => {
  177. const selectedAt = Date.now()
  178. ;(result.tempFiles || []).forEach((file, index) => {
  179. this.queue.push(createBackgroundQueueItem(file, `${selectedAt}-${index}-${file.path}`))
  180. })
  181. const teamId = this.activeTeamId()
  182. if (teamId) this.uploadPending(teamId).catch(error => this.showError(error, '上传失败,请重试'))
  183. },
  184. fail: error => {
  185. if (error && !String(error.errMsg || '').includes('cancel')) this.showError(error, '文件选择失败')
  186. }
  187. })
  188. },
  189. uploadPending(teamId) {
  190. return this.flushPendingFiles(teamId)
  191. },
  192. async flushPendingFiles(teamId) {
  193. const targetTeamId = this.activeTeamId(teamId)
  194. if (!targetTeamId) return { success: 0, failed: 0 }
  195. this.boundTeamId = targetTeamId
  196. if (this.uploadPromise) return this.uploadPromise
  197. const upload = this.performPendingUploads(targetTeamId)
  198. this.uploadPromise = upload
  199. let result
  200. try {
  201. result = await upload
  202. } finally {
  203. if (this.uploadPromise === upload) this.uploadPromise = null
  204. }
  205. if (!this.componentDestroyed && this.queue.some(item => item.status === 'pending')) {
  206. const laterResult = await this.flushPendingFiles(targetTeamId)
  207. return {
  208. success: result.success + laterResult.success,
  209. failed: result.failed + laterResult.failed
  210. }
  211. }
  212. return result
  213. },
  214. async performPendingUploads(teamId) {
  215. let success = 0
  216. let failed = 0
  217. for (const item of getUploadableBackgroundFiles(this.queue)) {
  218. if (this.componentDestroyed) break
  219. item.status = 'uploading'
  220. item.error = ''
  221. try {
  222. await uploadTeamBackgroundFile(teamId, item.path, progress => {
  223. if (!this.componentDestroyed) item.progress = progress
  224. })
  225. if (this.componentDestroyed) break
  226. item.status = 'success'
  227. item.progress = 100
  228. success += 1
  229. } catch (error) {
  230. if (this.componentDestroyed) break
  231. item.status = 'failed'
  232. item.error = error && (error.message || error.msg) || '上传失败,请重试'
  233. failed += 1
  234. }
  235. }
  236. if (!this.componentDestroyed) await this.refreshFiles(teamId)
  237. return { success, failed }
  238. },
  239. retryItem(item) {
  240. if (item.validationError || item.status === 'uploading') return
  241. item.status = 'pending'
  242. item.error = ''
  243. item.progress = 0
  244. const teamId = this.activeTeamId()
  245. if (teamId) this.uploadPending(teamId).catch(error => this.showError(error, '上传失败,请重试'))
  246. },
  247. async refreshFiles(teamId = this.activeTeamId()) {
  248. if (!teamId || !this.editable) return []
  249. const requestId = ++this.refreshRequestId
  250. let files
  251. try {
  252. files = await listTeamBackgroundFiles(teamId)
  253. } catch (error) {
  254. if (this.componentDestroyed || requestId !== this.refreshRequestId) return this.files
  255. throw error
  256. }
  257. if (this.componentDestroyed || requestId !== this.refreshRequestId
  258. || String(teamId) !== String(this.activeTeamId())) return this.files
  259. this.files = Array.isArray(files) ? files : []
  260. this.count = this.files.length
  261. this.hasLoadedFiles = true
  262. this.$emit('countChange', this.count)
  263. return this.files
  264. },
  265. async previewFile(file) {
  266. const teamId = this.activeTeamId()
  267. if (!teamId || !file || file.id === undefined || file.id === null) return
  268. const requestId = ++this.previewRequestId
  269. this.previewingId = file.id
  270. try {
  271. const result = await downloadTeamBackgroundFile(teamId, file.id)
  272. if (this.componentDestroyed || requestId !== this.previewRequestId) return
  273. if (result && result.mode === 'text') {
  274. this.textPreview = createTextPreviewState(result, file.fileName || file.name)
  275. }
  276. } catch (error) {
  277. if (requestId === this.previewRequestId) this.showError(error, '文件打开失败')
  278. } finally {
  279. if (requestId === this.previewRequestId) this.previewingId = ''
  280. }
  281. },
  282. closeTextPreview() {
  283. this.previewRequestId += 1
  284. this.previewingId = ''
  285. this.textPreview = createEmptyTextPreviewState()
  286. },
  287. removeFile(file) {
  288. const teamId = this.activeTeamId()
  289. if (!teamId || !file || file.id === undefined || file.id === null) return
  290. uni.showModal({
  291. title: '停用资料',
  292. content: `确定停用“${file.fileName || file.name || '该文件'}”吗?`,
  293. confirmColor: '#199C9C',
  294. success: async result => {
  295. if (!result.confirm) return
  296. try {
  297. await disableTeamBackgroundFile(teamId, file.id)
  298. await this.refreshFiles(teamId)
  299. if (!this.componentDestroyed) this.$showToast('已停用')
  300. } catch (error) {
  301. this.showError(error, '停用失败,请重试')
  302. }
  303. }
  304. })
  305. },
  306. usePcUpload() {
  307. const teamId = this.activeTeamId()
  308. if (!teamId) {
  309. this.pcUploadDeferred = true
  310. this.$showToast('将先保存团队,再生成电脑上传方式')
  311. return
  312. }
  313. this.pcUploadDeferred = false
  314. this.openPcSession(teamId).catch(error => this.showError(error, '上传会话创建失败'))
  315. },
  316. async openPcSession(teamId) {
  317. const requestId = ++this.sessionRequestId
  318. this.clearSessionTimer()
  319. let session
  320. try {
  321. session = await createTeamBackgroundSession(teamId)
  322. } catch (error) {
  323. if (this.componentDestroyed || requestId !== this.sessionRequestId) return null
  324. throw error
  325. }
  326. if (this.componentDestroyed || requestId !== this.sessionRequestId) return null
  327. this.boundTeamId = teamId
  328. this.session = session
  329. this.pcDialogVisible = true
  330. this.startSessionTimer()
  331. return session
  332. },
  333. startSessionTimer() {
  334. this.clearSessionTimer()
  335. this.updateRemainingSeconds()
  336. if (this.remainingSeconds === 0) return
  337. this.sessionTimer = setInterval(() => this.updateRemainingSeconds(), 1000)
  338. },
  339. updateRemainingSeconds() {
  340. this.remainingSeconds = getSessionRemainingSeconds(this.session && this.session.expiresAt)
  341. if (this.remainingSeconds === 0) this.clearSessionTimer()
  342. },
  343. clearSessionTimer() {
  344. if (this.sessionTimer) clearInterval(this.sessionTimer)
  345. this.sessionTimer = null
  346. },
  347. copyUploadUrl() {
  348. if (this.remainingSeconds === 0 || !this.session || !this.session.uploadUrl) return
  349. uni.setClipboardData({
  350. data: this.session.uploadUrl,
  351. success: () => this.$showToast('链接已复制'),
  352. fail: error => this.showError(error, '复制失败')
  353. })
  354. },
  355. refreshPcSession() {
  356. const teamId = this.activeTeamId()
  357. if (!teamId) return
  358. this.openPcSession(teamId).catch(error => this.showError(error, '上传会话创建失败'))
  359. },
  360. hasDeferredPcUpload() {
  361. return this.pcUploadDeferred
  362. },
  363. async waitForDeferredPcUpload(teamId) {
  364. this.resolveDeferredPcUpload()
  365. const session = await this.openPcSession(teamId)
  366. if (!session || this.componentDestroyed) return
  367. this.pcUploadDeferred = true
  368. return new Promise(resolve => {
  369. this.deferredPcResolve = resolve
  370. })
  371. },
  372. resolveDeferredPcUpload() {
  373. if (this.deferredPcResolve) this.deferredPcResolve()
  374. this.deferredPcResolve = null
  375. this.pcUploadDeferred = false
  376. },
  377. closePcDialog() {
  378. this.sessionRequestId += 1
  379. this.clearSessionTimer()
  380. this.pcDialogVisible = false
  381. this.session = null
  382. this.remainingSeconds = 0
  383. },
  384. finishDeferredPcUpload() {
  385. const teamId = this.activeTeamId()
  386. this.closePcDialog()
  387. this.resolveDeferredPcUpload()
  388. if (teamId) this.refreshFiles(teamId).catch(error => this.showError(error, '资料列表刷新失败'))
  389. },
  390. skipDeferredPcUpload() {
  391. this.closePcDialog()
  392. this.resolveDeferredPcUpload()
  393. }
  394. }
  395. }
  396. </script>
  397. <style scoped lang="scss">
  398. .background-files {
  399. background: #ffffff;
  400. border-radius: 24rpx;
  401. margin-top: 20rpx;
  402. padding: 28rpx 24rpx;
  403. box-sizing: border-box;
  404. }
  405. .background-files-title, .queue-title, .dialog-title {
  406. font-size: 30rpx;
  407. color: #002846;
  408. line-height: 42rpx;
  409. }
  410. .background-files-count, .file-meta, .dialog-desc {
  411. font-size: 24rpx;
  412. color: #95a5b1;
  413. line-height: 34rpx;
  414. margin-top: 8rpx;
  415. }
  416. .background-files-add {
  417. padding: 18rpx 22rpx;
  418. border-radius: 36rpx;
  419. background: linear-gradient(90deg, #33a7a7 0%, #64bbbb 100%);
  420. font-size: 24rpx;
  421. color: #ffffff;
  422. }
  423. .background-files-pc {
  424. font-size: 26rpx;
  425. color: #199c9c;
  426. line-height: 38rpx;
  427. text-align: right;
  428. margin-top: 20rpx;
  429. }
  430. .file-list, .queue-list {
  431. margin-top: 24rpx;
  432. border-top: 1rpx solid #efefef;
  433. }
  434. .file-row {
  435. padding: 22rpx 0;
  436. border-bottom: 1rpx solid #efefef;
  437. }
  438. .file-main { width: calc(100% - 190rpx); }
  439. .file-name, .queue-name {
  440. font-size: 26rpx;
  441. color: #667e90;
  442. line-height: 36rpx;
  443. word-break: break-all;
  444. }
  445. .file-action {
  446. font-size: 24rpx;
  447. color: #199c9c;
  448. margin-left: 20rpx;
  449. }
  450. .file-action.danger { color: #fd4f66; }
  451. .queue-title { margin-top: 22rpx; }
  452. .queue-row {
  453. padding: 20rpx 0;
  454. border-bottom: 1rpx solid #efefef;
  455. position: relative;
  456. }
  457. .queue-name { width: calc(100% - 130rpx); }
  458. .queue-status { font-size: 24rpx; color: #95a5b1; }
  459. .queue-status.success { color: #199c9c; }
  460. .queue-status.failed, .queue-error { color: #fd4f66; }
  461. .queue-progress {
  462. height: 8rpx;
  463. background: #f0f2f8;
  464. border-radius: 4rpx;
  465. margin-top: 12rpx;
  466. overflow: hidden;
  467. }
  468. .queue-progress-current { height: 100%; background: #4db2b2; }
  469. .queue-error { font-size: 22rpx; line-height: 32rpx; margin-top: 8rpx; }
  470. .queue-retry { font-size: 24rpx; color: #199c9c; margin-top: 10rpx; }
  471. .overlay {
  472. position: fixed;
  473. left: 0;
  474. right: 0;
  475. top: 0;
  476. bottom: 0;
  477. z-index: 1200;
  478. background: rgba(0, 0, 0, .55);
  479. padding: 40rpx;
  480. box-sizing: border-box;
  481. }
  482. .pc-dialog, .text-dialog {
  483. width: 100%;
  484. background: #ffffff;
  485. border-radius: 28rpx;
  486. padding: 40rpx 30rpx;
  487. box-sizing: border-box;
  488. }
  489. .dialog-title { font-weight: bold; text-align: center; }
  490. .dialog-desc, .dialog-expired { text-align: center; margin-top: 24rpx; }
  491. .dialog-expired { font-size: 24rpx; color: #fd4f66; }
  492. .session-expiry { font-size: 22rpx; color: #95a5b1; text-align: center; margin-top: 12rpx; }
  493. .session-code {
  494. font-size: 56rpx;
  495. font-weight: bold;
  496. letter-spacing: 12rpx;
  497. color: #002846;
  498. text-align: center;
  499. margin-top: 30rpx;
  500. }
  501. .session-link {
  502. font-size: 22rpx;
  503. color: #667e90;
  504. line-height: 32rpx;
  505. word-break: break-all;
  506. background: #f7f8fa;
  507. padding: 20rpx;
  508. border-radius: 16rpx;
  509. margin-top: 26rpx;
  510. }
  511. .session-actions { margin-top: 26rpx; }
  512. .session-button {
  513. width: calc(50% - 12rpx);
  514. height: 72rpx;
  515. line-height: 72rpx;
  516. border-radius: 36rpx;
  517. font-size: 26rpx;
  518. text-align: center;
  519. box-sizing: border-box;
  520. }
  521. .session-button.primary { color: #ffffff; background: #33a7a7; }
  522. .session-button.secondary { color: #199c9c; border: 1rpx solid #199c9c; }
  523. .session-button.disabled { color: #b3bfc8; border-color: #b3bfc8; }
  524. .text-dialog { height: 80vh; }
  525. .text-title {
  526. width: 100%;
  527. white-space: nowrap;
  528. overflow: hidden;
  529. text-overflow: ellipsis;
  530. }
  531. .truncated-tip {
  532. font-size: 22rpx;
  533. color: #b06a00;
  534. line-height: 32rpx;
  535. text-align: center;
  536. margin-top: 16rpx;
  537. }
  538. .text-content {
  539. width: 100%;
  540. flex: 1;
  541. background: #f7f8fa;
  542. border-radius: 16rpx;
  543. box-sizing: border-box;
  544. padding: 24rpx;
  545. margin-top: 22rpx;
  546. font-size: 26rpx;
  547. color: #334b5c;
  548. line-height: 40rpx;
  549. white-space: pre-wrap;
  550. word-break: break-all;
  551. }
  552. .text-close {
  553. width: 100%;
  554. height: 76rpx;
  555. line-height: 76rpx;
  556. border-radius: 38rpx;
  557. background: #33a7a7;
  558. color: #ffffff;
  559. font-size: 28rpx;
  560. text-align: center;
  561. margin-top: 24rpx;
  562. }
  563. </style>