index.vue 21 KB

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