index.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. <template>
  2. <view class="upload-edit-page">
  3. <!-- 顶部导航栏 -->
  4. <view class="header-bg" :style="{ paddingTop: statusBarHeight + 'px' }">
  5. <view class="header-content">
  6. <view class="back-btn" @click="handleBack">
  7. <text class="back-icon">‹</text>
  8. </view>
  9. <text class="header-title">编辑上传文件</text>
  10. <view class="placeholder"></view>
  11. </view>
  12. </view>
  13. <!-- 页面内容 -->
  14. <scroll-view scroll-y class="page-content">
  15. <!-- 文件夹信息卡片 -->
  16. <view class="info-card">
  17. <view class="info-row">
  18. <text class="info-label">文件夹路径:</text>
  19. <text class="info-value">{{ folderPath }}</text>
  20. </view>
  21. </view>
  22. <!-- 文件列表 -->
  23. <view class="file-list-section">
  24. <view class="section-title">文件列表 ({{ fileList.length }})</view>
  25. <view class="file-list">
  26. <view
  27. v-for="(file, index) in fileList"
  28. :key="index"
  29. class="file-item"
  30. >
  31. <view class="file-main" @click="handlePreview(file, index)">
  32. <view class="file-icon-wrapper">
  33. <image src="/static/icon/pdf.svg" mode="aspectFit" class="file-icon" />
  34. </view>
  35. <view class="file-info">
  36. <text class="file-name">{{ file.name }}</text>
  37. <text class="file-tip">点击预览</text>
  38. </view>
  39. </view>
  40. <!-- 操作按钮 -->
  41. <view class="file-actions">
  42. <view
  43. v-if="index > 0"
  44. class="action-icon"
  45. @click="handleMoveUp(index)"
  46. >
  47. <text class="icon-text">↑</text>
  48. </view>
  49. <view
  50. v-if="index < fileList.length - 1"
  51. class="action-icon"
  52. @click="handleMoveDown(index)"
  53. >
  54. <text class="icon-text">↓</text>
  55. </view>
  56. <view class="action-icon delete" @click="handleDelete(index)">
  57. <text class="icon-text">×</text>
  58. </view>
  59. </view>
  60. </view>
  61. </view>
  62. </view>
  63. </scroll-view>
  64. <!-- 底部操作按钮 -->
  65. <view class="footer-actions">
  66. <view class="action-btn secondary" @click="handleCancel">
  67. <text class="action-text">取消</text>
  68. </view>
  69. <view class="action-btn primary" @click="handleUpload">
  70. <text class="action-text">上传</text>
  71. </view>
  72. </view>
  73. <!-- 上传弹出框 -->
  74. <view v-if="showUploadDialog" class="dialog-overlay" @click="closeDialog">
  75. <view class="dialog-content" @click.stop>
  76. <view class="dialog-header">
  77. <text class="dialog-title">上传文件</text>
  78. <view class="dialog-close" @click="closeDialog">
  79. <text class="close-icon">×</text>
  80. </view>
  81. </view>
  82. <view class="dialog-body">
  83. <view class="form-item">
  84. <text class="form-label">文件名</text>
  85. <input
  86. class="form-input"
  87. v-model="uploadForm.fileName"
  88. placeholder="请输入文件名"
  89. maxlength="100"
  90. />
  91. </view>
  92. <view class="form-item">
  93. <text class="form-label">备注</text>
  94. <textarea
  95. class="form-textarea"
  96. v-model="uploadForm.note"
  97. placeholder="请输入备注(可选)"
  98. maxlength="500"
  99. :auto-height="true"
  100. />
  101. </view>
  102. </view>
  103. <view class="dialog-footer">
  104. <view class="dialog-btn cancel" @click="closeDialog">
  105. <text class="btn-text">取消</text>
  106. </view>
  107. <view class="dialog-btn confirm" @click="confirmUpload">
  108. <text class="btn-text">确认</text>
  109. </view>
  110. </view>
  111. </view>
  112. </view>
  113. </view>
  114. </template>
  115. <script>
  116. import { uploadNew } from '@/apis/scan.js'
  117. export default {
  118. data() {
  119. return {
  120. statusBarHeight: 0,
  121. folderId: 0,
  122. projectId: 0,
  123. folderPath: '',
  124. fileList: [], // 文件列表,每个元素包含 { base64, name, path }
  125. showUploadDialog: false,
  126. uploadForm: {
  127. fileName: '',
  128. note: ''
  129. }
  130. }
  131. },
  132. onLoad(options) {
  133. this.folderId = parseInt(options.folderId) || 0
  134. this.projectId = parseInt(options.projectId) || 0
  135. this.folderPath = decodeURIComponent(options.folderPath || '')
  136. },
  137. onReady() {
  138. // 获取系统信息
  139. const windowInfo = uni.getWindowInfo()
  140. this.statusBarHeight = windowInfo.statusBarHeight || 0
  141. // 从全局数据获取文件列表
  142. const fileBase64List = getApp().globalData.scannedFileBase64List
  143. const pdfData = getApp().globalData.pdfData
  144. if (fileBase64List && fileBase64List.length > 0) {
  145. // 如果有 pdfData(包含文件名和路径),使用它
  146. if (pdfData && pdfData.length > 0) {
  147. this.fileList = pdfData.map((item, index) => ({
  148. base64: item.base64 || fileBase64List[index],
  149. name: item.name || `文件_${index + 1}`,
  150. path: item.path
  151. }))
  152. } else {
  153. // 否则只有 base64 数据
  154. this.fileList = fileBase64List.map((base64, index) => ({
  155. base64: base64,
  156. name: `文件_${index + 1}`,
  157. path: null
  158. }))
  159. }
  160. } else {
  161. console.error('未找到文件列表')
  162. uni.showToast({
  163. title: '未找到文件',
  164. icon: 'none'
  165. })
  166. }
  167. },
  168. methods: {
  169. // 返回上一页
  170. handleBack() {
  171. uni.navigateBack()
  172. },
  173. // 取消
  174. handleCancel() {
  175. uni.showModal({
  176. title: '提示',
  177. content: '确定要取消上传吗?',
  178. confirmText: '确定',
  179. cancelText: '继续编辑',
  180. success: (res) => {
  181. if (res.confirm) {
  182. // 清除全局数据
  183. getApp().globalData.scannedFileBase64List = null
  184. getApp().globalData.selectedFolder = null
  185. // 返回首页
  186. uni.reLaunch({
  187. url: '/pages/home/index'
  188. })
  189. }
  190. }
  191. })
  192. },
  193. // 上传
  194. handleUpload() {
  195. if (this.fileList.length === 0) {
  196. uni.showToast({
  197. title: '没有可上传的文件',
  198. icon: 'none'
  199. })
  200. return
  201. }
  202. // 显示上传弹出框
  203. this.showUploadDialog = true
  204. },
  205. // 关闭弹出框
  206. closeDialog() {
  207. this.showUploadDialog = false
  208. this.uploadForm = {
  209. fileName: '',
  210. note: ''
  211. }
  212. },
  213. // 确认上传
  214. async confirmUpload() {
  215. // 验证文件名
  216. if (!this.uploadForm.fileName.trim()) {
  217. uni.showToast({
  218. title: '请输入文件名',
  219. icon: 'none'
  220. })
  221. return
  222. }
  223. // 准备上传数据(在关闭弹出框之前)
  224. const uploadData = {
  225. folderId: this.folderId,
  226. fileName: this.uploadForm.fileName.trim(),
  227. projectId: this.projectId,
  228. note: this.uploadForm.note.trim(),
  229. files: this.fileList.map(item => item.base64)
  230. }
  231. // 关闭弹出框
  232. this.closeDialog()
  233. // 显示加载提示
  234. uni.showLoading({
  235. title: '上传中...',
  236. mask: true
  237. })
  238. try {
  239. // 调用上传接口
  240. await uploadNew(uploadData)
  241. uni.hideLoading()
  242. uni.showToast({
  243. title: '上传成功',
  244. icon: 'success',
  245. duration: 2000
  246. })
  247. // 清除全局数据
  248. getApp().globalData.scannedFileBase64List = null
  249. getApp().globalData.selectedFolder = null
  250. getApp().globalData.pdfData = null
  251. // 返回首页
  252. setTimeout(() => {
  253. uni.reLaunch({
  254. url: '/pages/home/index'
  255. })
  256. }, 2000)
  257. } catch (error) {
  258. console.error('上传失败:', error)
  259. uni.hideLoading()
  260. uni.showToast({
  261. title: error.msg || '上传失败',
  262. icon: 'none',
  263. duration: 2000
  264. })
  265. }
  266. },
  267. // 预览文件
  268. async handlePreview(file, index) {
  269. if (!file.base64) {
  270. uni.showToast({
  271. title: '文件数据异常',
  272. icon: 'none'
  273. })
  274. return
  275. }
  276. uni.showLoading({
  277. title: '加载中...',
  278. mask: true
  279. })
  280. try {
  281. // 将 base64 转换为临时文件
  282. const fs = uni.getFileSystemManager()
  283. const fileName = `${Date.now()}_${file.name}.pdf`
  284. const filePath = `${wx.env.USER_DATA_PATH}/${fileName}`
  285. // 将 base64 写入文件
  286. fs.writeFileSync(
  287. filePath,
  288. file.base64,
  289. 'base64'
  290. )
  291. uni.hideLoading()
  292. // 使用 uni.openDocument 打开文件
  293. uni.openDocument({
  294. filePath: filePath,
  295. fileType: 'pdf',
  296. showMenu: true,
  297. success: () => {
  298. // 文档打开成功
  299. },
  300. fail: (err) => {
  301. console.error('文档打开失败:', err)
  302. uni.showToast({
  303. title: '文档打开失败',
  304. icon: 'none'
  305. })
  306. }
  307. })
  308. } catch (error) {
  309. uni.hideLoading()
  310. console.error('预览失败:', error)
  311. uni.showToast({
  312. title: '预览失败',
  313. icon: 'none'
  314. })
  315. }
  316. },
  317. // 上移文件
  318. handleMoveUp(index) {
  319. if (index === 0) return
  320. const temp = this.fileList[index]
  321. this.fileList[index] = this.fileList[index - 1]
  322. this.fileList[index - 1] = temp
  323. // 触发响应式更新
  324. this.fileList = [...this.fileList]
  325. uni.showToast({
  326. title: '已上移',
  327. icon: 'success',
  328. duration: 1000
  329. })
  330. },
  331. // 下移文件
  332. handleMoveDown(index) {
  333. if (index === this.fileList.length - 1) return
  334. const temp = this.fileList[index]
  335. this.fileList[index] = this.fileList[index + 1]
  336. this.fileList[index + 1] = temp
  337. // 触发响应式更新
  338. this.fileList = [...this.fileList]
  339. uni.showToast({
  340. title: '已下移',
  341. icon: 'success',
  342. duration: 1000
  343. })
  344. },
  345. // 删除文件
  346. handleDelete(index) {
  347. // 不允许删除最后一个文件
  348. if (this.fileList.length <= 1) {
  349. uni.showToast({
  350. title: '至少保留一个文件',
  351. icon: 'none',
  352. duration: 2000
  353. })
  354. return
  355. }
  356. uni.showModal({
  357. title: '确认删除',
  358. content: `确定要删除"${this.fileList[index].name}"吗?`,
  359. confirmText: '删除',
  360. cancelText: '取消',
  361. success: (res) => {
  362. if (res.confirm) {
  363. this.fileList.splice(index, 1)
  364. uni.showToast({
  365. title: '已删除',
  366. icon: 'success',
  367. duration: 1000
  368. })
  369. }
  370. }
  371. })
  372. },
  373. // 格式化文件大小
  374. formatFileSize(bytes) {
  375. if (!bytes) return '0 B'
  376. const k = 1024
  377. const sizes = ['B', 'KB', 'MB', 'GB']
  378. const i = Math.floor(Math.log(bytes) / Math.log(k))
  379. return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i]
  380. }
  381. }
  382. }
  383. </script>
  384. <style lang="scss" scoped>
  385. .upload-edit-page {
  386. width: 100%;
  387. height: 100vh;
  388. display: flex;
  389. flex-direction: column;
  390. background-color: #f5f5f5;
  391. // 顶部导航栏
  392. .header-bg {
  393. background: linear-gradient(180deg, #1ec9c9 0%, #1eb8b8 100%);
  394. position: relative;
  395. z-index: 100;
  396. .header-content {
  397. height: 88rpx;
  398. display: flex;
  399. align-items: center;
  400. justify-content: space-between;
  401. padding: 0 24rpx;
  402. .back-btn {
  403. width: 60rpx;
  404. height: 60rpx;
  405. display: flex;
  406. align-items: center;
  407. justify-content: flex-start;
  408. .back-icon {
  409. font-size: 56rpx;
  410. color: #ffffff;
  411. font-weight: 300;
  412. }
  413. }
  414. .header-title {
  415. flex: 1;
  416. text-align: center;
  417. font-size: 32rpx;
  418. font-weight: 600;
  419. color: #ffffff;
  420. }
  421. .placeholder {
  422. width: 60rpx;
  423. }
  424. }
  425. }
  426. // 页面内容
  427. .page-content {
  428. flex: 1;
  429. padding: 24rpx;
  430. // 信息卡片
  431. .info-card {
  432. background: #ffffff;
  433. border-radius: 16rpx;
  434. padding: 32rpx 24rpx;
  435. margin-bottom: 24rpx;
  436. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.08);
  437. .info-row {
  438. display: flex;
  439. align-items: flex-start;
  440. margin-bottom: 20rpx;
  441. &:last-child {
  442. margin-bottom: 0;
  443. }
  444. .info-label {
  445. font-size: 28rpx;
  446. color: #666666;
  447. min-width: 180rpx;
  448. }
  449. .info-value {
  450. flex: 1;
  451. font-size: 28rpx;
  452. color: #333333;
  453. font-weight: 500;
  454. word-break: break-all;
  455. }
  456. }
  457. }
  458. // 文件列表区域
  459. .file-list-section {
  460. .section-title {
  461. font-size: 30rpx;
  462. font-weight: 600;
  463. color: #333333;
  464. margin-bottom: 16rpx;
  465. }
  466. .file-list {
  467. background: #ffffff;
  468. border-radius: 16rpx;
  469. overflow: hidden;
  470. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.08);
  471. .file-item {
  472. display: flex;
  473. align-items: center;
  474. justify-content: space-between;
  475. padding: 24rpx;
  476. border-bottom: 1rpx solid #f5f5f5;
  477. &:last-child {
  478. border-bottom: none;
  479. }
  480. .file-main {
  481. flex: 1;
  482. display: flex;
  483. align-items: center;
  484. &:active {
  485. opacity: 0.7;
  486. }
  487. .file-icon-wrapper {
  488. width: 64rpx;
  489. height: 64rpx;
  490. background: #f5f5f5;
  491. border-radius: 12rpx;
  492. display: flex;
  493. align-items: center;
  494. justify-content: center;
  495. margin-right: 16rpx;
  496. .file-icon {
  497. width: 40rpx;
  498. height: 40rpx;
  499. }
  500. }
  501. .file-info {
  502. flex: 1;
  503. display: flex;
  504. flex-direction: column;
  505. gap: 8rpx;
  506. .file-name {
  507. font-size: 28rpx;
  508. color: #333333;
  509. font-weight: 500;
  510. }
  511. .file-tip {
  512. font-size: 24rpx;
  513. color: #999999;
  514. }
  515. }
  516. }
  517. .file-actions {
  518. display: flex;
  519. align-items: center;
  520. gap: 12rpx;
  521. margin-left: 16rpx;
  522. .action-icon {
  523. width: 56rpx;
  524. height: 56rpx;
  525. border-radius: 50%;
  526. background: #f5f5f5;
  527. display: flex;
  528. align-items: center;
  529. justify-content: center;
  530. &:active {
  531. background: #e0e0e0;
  532. }
  533. &.delete {
  534. background: #ffe5e5;
  535. &:active {
  536. background: #ffcccc;
  537. }
  538. .icon-text {
  539. color: #ff4444;
  540. font-size: 40rpx;
  541. }
  542. }
  543. .icon-text {
  544. font-size: 32rpx;
  545. color: #666666;
  546. font-weight: 600;
  547. line-height: 1;
  548. }
  549. }
  550. }
  551. }
  552. }
  553. }
  554. }
  555. // 底部操作按钮
  556. .footer-actions {
  557. background: #ffffff;
  558. padding: 24rpx 32rpx;
  559. padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
  560. box-shadow: 0 -2rpx 8rpx rgba(0, 0, 0, 0.05);
  561. display: flex;
  562. gap: 16rpx;
  563. .action-btn {
  564. flex: 1;
  565. height: 88rpx;
  566. border-radius: 44rpx;
  567. display: flex;
  568. align-items: center;
  569. justify-content: center;
  570. &:active {
  571. transform: scale(0.98);
  572. opacity: 0.9;
  573. }
  574. &.secondary {
  575. background: #ffffff;
  576. border: 2rpx solid #1ec9c9;
  577. .action-text {
  578. color: #1ec9c9;
  579. }
  580. }
  581. &.primary {
  582. background: linear-gradient(135deg, #1ec9c9 0%, #1eb8b8 100%);
  583. box-shadow: 0 4rpx 16rpx rgba(30, 201, 201, 0.3);
  584. .action-text {
  585. color: #ffffff;
  586. }
  587. }
  588. .action-text {
  589. font-size: 32rpx;
  590. font-weight: 600;
  591. }
  592. }
  593. }
  594. // 弹出框遮罩
  595. .dialog-overlay {
  596. position: fixed;
  597. top: 0;
  598. left: 0;
  599. right: 0;
  600. bottom: 0;
  601. background: rgba(0, 0, 0, 0.5);
  602. display: flex;
  603. align-items: center;
  604. justify-content: center;
  605. z-index: 1000;
  606. .dialog-content {
  607. width: 600rpx;
  608. background: #ffffff;
  609. border-radius: 24rpx;
  610. overflow: hidden;
  611. .dialog-header {
  612. padding: 32rpx 32rpx 24rpx;
  613. display: flex;
  614. align-items: center;
  615. justify-content: space-between;
  616. border-bottom: 1rpx solid #f5f5f5;
  617. .dialog-title {
  618. font-size: 32rpx;
  619. font-weight: 600;
  620. color: #333333;
  621. }
  622. .dialog-close {
  623. width: 48rpx;
  624. height: 48rpx;
  625. display: flex;
  626. align-items: center;
  627. justify-content: center;
  628. &:active {
  629. opacity: 0.7;
  630. }
  631. .close-icon {
  632. font-size: 48rpx;
  633. color: #999999;
  634. line-height: 1;
  635. }
  636. }
  637. }
  638. .dialog-body {
  639. padding: 32rpx;
  640. .form-item {
  641. margin-bottom: 32rpx;
  642. &:last-child {
  643. margin-bottom: 0;
  644. }
  645. .form-label {
  646. display: block;
  647. font-size: 28rpx;
  648. color: #333333;
  649. margin-bottom: 16rpx;
  650. font-weight: 500;
  651. }
  652. .form-input {
  653. width: 100%;
  654. height: 80rpx;
  655. padding: 0 24rpx;
  656. background: #f5f5f5;
  657. border-radius: 12rpx;
  658. font-size: 28rpx;
  659. color: #333333;
  660. box-sizing: border-box;
  661. }
  662. .form-textarea {
  663. width: 100%;
  664. min-height: 120rpx;
  665. padding: 16rpx 24rpx;
  666. background: #f5f5f5;
  667. border-radius: 12rpx;
  668. font-size: 28rpx;
  669. color: #333333;
  670. box-sizing: border-box;
  671. }
  672. }
  673. }
  674. .dialog-footer {
  675. padding: 24rpx 32rpx 32rpx;
  676. display: flex;
  677. gap: 16rpx;
  678. .dialog-btn {
  679. flex: 1;
  680. height: 80rpx;
  681. border-radius: 40rpx;
  682. display: flex;
  683. align-items: center;
  684. justify-content: center;
  685. &:active {
  686. transform: scale(0.98);
  687. opacity: 0.9;
  688. }
  689. &.cancel {
  690. background: #f5f5f5;
  691. .btn-text {
  692. color: #666666;
  693. }
  694. }
  695. &.confirm {
  696. background: linear-gradient(135deg, #1ec9c9 0%, #1eb8b8 100%);
  697. .btn-text {
  698. color: #ffffff;
  699. }
  700. }
  701. .btn-text {
  702. font-size: 30rpx;
  703. font-weight: 600;
  704. }
  705. }
  706. }
  707. }
  708. }
  709. }
  710. </style>