index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <template>
  2. <view class="pdf-viewer-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">{{ t('pdfViewer.title') }} ({{ t('pdfViewer.totalPages', { count: pdfList.length }) }})</text>
  10. <view class="placeholder"></view>
  11. </view>
  12. </view>
  13. <!-- PDF列表 -->
  14. <scroll-view scroll-y class="pdf-list">
  15. <view
  16. v-for="(item, index) in pdfList"
  17. :key="item.id"
  18. class="pdf-item"
  19. >
  20. <view class="pdf-icon-wrapper">
  21. <image src="/static/icon/pdf.svg" mode="aspectFit" class="pdf-icon" />
  22. </view>
  23. <view class="pdf-info" @click="handlePreviewPdf(item.path, index)" @longpress="handleEditName(index)">
  24. <text class="pdf-name">{{ item.name || t('pdfViewer.document', { index: index + 1 }) }}</text>
  25. <text class="pdf-tip">{{ t('pdfViewer.previewTip') }}</text>
  26. </view>
  27. <!-- 操作按钮 -->
  28. <view class="pdf-actions">
  29. <view
  30. v-if="index > 0"
  31. class="action-icon"
  32. @click="handleMoveUp(index)"
  33. >
  34. <text class="icon-text">↑</text>
  35. </view>
  36. <view
  37. v-if="index < pdfList.length - 1"
  38. class="action-icon"
  39. @click="handleMoveDown(index)"
  40. >
  41. <text class="icon-text">↓</text>
  42. </view>
  43. <view class="action-icon delete" @click="handleDelete(index)">
  44. <text class="icon-text">×</text>
  45. </view>
  46. </view>
  47. </view>
  48. <view v-if="pdfList.length === 0" class="empty-tip">
  49. <text class="empty-text">{{ t('pdfViewer.empty') }}</text>
  50. </view>
  51. </scroll-view>
  52. <!-- 底部提交按钮 -->
  53. <view class="submit-footer">
  54. <view class="submit-btn" @click="handleSubmit">
  55. <text class="submit-text">{{ t('pdfViewer.submit') }}</text>
  56. </view>
  57. </view>
  58. </view>
  59. </template>
  60. <script setup>
  61. import { ref, onMounted, computed } from 'vue'
  62. import { useI18n } from 'vue-i18n'
  63. import { scanUploadBatch } from '@/apis/scan'
  64. const { t } = useI18n()
  65. // 状态栏高度
  66. const statusBarHeight = ref(0)
  67. // PDF列表(包含路径和base64)
  68. const pdfList = ref([])
  69. // 生成唯一ID
  70. let idCounter = 0
  71. const generateId = () => {
  72. return `pdf_${Date.now()}_${idCounter++}`
  73. }
  74. onMounted(() => {
  75. // 获取系统信息
  76. const windowInfo = uni.getWindowInfo()
  77. statusBarHeight.value = windowInfo.statusBarHeight || 0
  78. // 从全局数据获取PDF路径和base64
  79. const app = getApp()
  80. if (app.globalData && app.globalData.pdfData) {
  81. pdfList.value = app.globalData.pdfData.map(item => ({
  82. id: generateId(),
  83. path: item.path,
  84. base64: item.base64,
  85. name: item.name || t('pdfViewer.document', { index: Date.now() })
  86. }))
  87. }
  88. })
  89. // 返回上一页
  90. const handleBack = () => {
  91. uni.navigateBack()
  92. }
  93. // 预览PDF
  94. const handlePreviewPdf = (path, index) => {
  95. uni.openDocument({
  96. filePath: path,
  97. fileType: 'pdf',
  98. showMenu: true,
  99. success: () => {
  100. // 预览成功
  101. },
  102. fail: (err) => {
  103. console.error('打开文档失败:', err)
  104. uni.showToast({
  105. title: t('pdfViewer.message.openFailed'),
  106. icon: 'none'
  107. })
  108. }
  109. })
  110. }
  111. // 上移
  112. const handleMoveUp = (index) => {
  113. if (index === 0) return
  114. const temp = pdfList.value[index]
  115. pdfList.value[index] = pdfList.value[index - 1]
  116. pdfList.value[index - 1] = temp
  117. // 触发响应式更新
  118. pdfList.value = [...pdfList.value]
  119. uni.showToast({
  120. title: t('pdfViewer.message.movedUp'),
  121. icon: 'success',
  122. duration: 1000
  123. })
  124. }
  125. // 下移
  126. const handleMoveDown = (index) => {
  127. if (index === pdfList.value.length - 1) return
  128. const temp = pdfList.value[index]
  129. pdfList.value[index] = pdfList.value[index + 1]
  130. pdfList.value[index + 1] = temp
  131. // 触发响应式更新
  132. pdfList.value = [...pdfList.value]
  133. uni.showToast({
  134. title: t('pdfViewer.message.movedDown'),
  135. icon: 'success',
  136. duration: 1000
  137. })
  138. }
  139. // 删除
  140. const handleDelete = (index) => {
  141. const itemName = pdfList.value[index].name || t('pdfViewer.document', { index: index + 1 })
  142. uni.showModal({
  143. title: t('pdfViewer.dialog.deleteTitle'),
  144. content: t('pdfViewer.dialog.deleteContent', { name: itemName }),
  145. confirmText: t('pdfViewer.dialog.deleteConfirm'),
  146. cancelText: t('pdfViewer.dialog.deleteCancel'),
  147. success: (res) => {
  148. if (res.confirm) {
  149. pdfList.value.splice(index, 1)
  150. uni.showToast({
  151. title: t('pdfViewer.message.deleted'),
  152. icon: 'success',
  153. duration: 1000
  154. })
  155. }
  156. }
  157. })
  158. }
  159. // 编辑文件名
  160. const handleEditName = (index) => {
  161. const currentName = pdfList.value[index].name || t('pdfViewer.document', { index: index + 1 })
  162. uni.showModal({
  163. title: t('pdfViewer.dialog.editTitle'),
  164. content: currentName,
  165. editable: true,
  166. placeholderText: t('pdfViewer.dialog.editPlaceholder'),
  167. success: (res) => {
  168. if (res.confirm && res.content && res.content.trim()) {
  169. pdfList.value[index].name = res.content.trim()
  170. uni.showToast({
  171. title: t('pdfViewer.message.editSuccess'),
  172. icon: 'success',
  173. duration: 1000
  174. })
  175. }
  176. }
  177. })
  178. }
  179. // 提交 - 跳转到文件选择页面
  180. const handleSubmit = () => {
  181. if (pdfList.value.length === 0) {
  182. uni.showToast({
  183. title: t('pdfViewer.message.addPdf'),
  184. icon: 'none'
  185. })
  186. return
  187. }
  188. // 按顺序提取base64数组
  189. const base64Array = pdfList.value.map(item => item.base64)
  190. // 将base64数组存储到全局数据中
  191. getApp().globalData.scannedFileBase64List = base64Array
  192. // 跳转到文件选择页面
  193. uni.navigateTo({
  194. url: '/pages/scan/fileSelect/index'
  195. })
  196. }
  197. // 上传所有PDF
  198. const handleUploadAll = async () => {
  199. if (pdfList.value.length === 0) {
  200. uni.showToast({
  201. title: t('pdfViewer.message.addPdf'),
  202. icon: 'none'
  203. })
  204. return
  205. }
  206. uni.showModal({
  207. title: t('pdfViewer.dialog.uploadTitle'),
  208. content: t('pdfViewer.dialog.uploadContent', { count: pdfList.value.length }),
  209. confirmText: t('pdfViewer.dialog.uploadConfirm'),
  210. cancelText: t('pdfViewer.dialog.uploadCancel'),
  211. success: async (res) => {
  212. if (res.confirm) {
  213. try {
  214. uni.showLoading({
  215. title: t('pdfViewer.message.uploading'),
  216. mask: true
  217. })
  218. // 按顺序提取base64数组
  219. const base64Array = pdfList.value.map(item => item.base64)
  220. // 调用批量上传接口
  221. const response = await scanUploadBatch({
  222. files: base64Array
  223. })
  224. uni.hideLoading()
  225. if (response.code === 200 && response.data) {
  226. uni.showToast({
  227. title: t('pdfViewer.message.uploadSuccess'),
  228. icon: 'success',
  229. duration: 2000
  230. })
  231. // 延迟返回上一页
  232. setTimeout(() => {
  233. uni.navigateBack()
  234. }, 2000)
  235. } else {
  236. throw new Error(response.msg || t('pdfViewer.message.uploadFailed'))
  237. }
  238. } catch (error) {
  239. uni.hideLoading()
  240. console.error('上传失败:', error)
  241. uni.showToast({
  242. title: error.message || t('pdfViewer.message.uploadFailed'),
  243. icon: 'none'
  244. })
  245. }
  246. }
  247. }
  248. })
  249. }
  250. </script>
  251. <style lang="scss" scoped>
  252. .pdf-viewer-page {
  253. width: 100%;
  254. height: 100vh;
  255. display: flex;
  256. flex-direction: column;
  257. background-color: #f5f5f5;
  258. // 顶部导航栏
  259. .header-bg {
  260. background: linear-gradient(180deg, #1ec9c9 0%, #1eb8b8 100%);
  261. position: relative;
  262. z-index: 100;
  263. .header-content {
  264. height: 88rpx;
  265. display: flex;
  266. align-items: center;
  267. justify-content: space-between;
  268. padding: 0 24rpx;
  269. .back-btn {
  270. width: 60rpx;
  271. height: 60rpx;
  272. display: flex;
  273. align-items: center;
  274. justify-content: flex-start;
  275. .back-icon {
  276. font-size: 56rpx;
  277. color: #ffffff;
  278. font-weight: 300;
  279. }
  280. }
  281. .header-title {
  282. flex: 1;
  283. text-align: center;
  284. font-size: 32rpx;
  285. font-weight: 600;
  286. color: #ffffff;
  287. }
  288. .placeholder {
  289. width: 60rpx;
  290. }
  291. }
  292. }
  293. // PDF列表
  294. .pdf-list {
  295. flex: 1;
  296. padding: 24rpx;
  297. .pdf-item {
  298. background: #ffffff;
  299. border-radius: 16rpx;
  300. padding: 24rpx;
  301. margin-bottom: 16rpx;
  302. display: flex;
  303. align-items: center;
  304. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  305. &:active {
  306. background: #f8f8f8;
  307. }
  308. .pdf-icon-wrapper {
  309. width: 80rpx;
  310. height: 80rpx;
  311. background: #f5f5f5;
  312. border-radius: 12rpx;
  313. display: flex;
  314. align-items: center;
  315. justify-content: center;
  316. margin-right: 24rpx;
  317. .pdf-icon {
  318. width: 48rpx;
  319. height: 48rpx;
  320. }
  321. }
  322. .pdf-info {
  323. flex: 1;
  324. display: flex;
  325. flex-direction: column;
  326. gap: 8rpx;
  327. .pdf-name {
  328. font-size: 32rpx;
  329. font-weight: 600;
  330. color: #333333;
  331. }
  332. .pdf-tip {
  333. font-size: 24rpx;
  334. color: #999999;
  335. }
  336. }
  337. .pdf-actions {
  338. display: flex;
  339. align-items: center;
  340. gap: 12rpx;
  341. .action-icon {
  342. width: 56rpx;
  343. height: 56rpx;
  344. border-radius: 50%;
  345. background: #f5f5f5;
  346. display: flex;
  347. align-items: center;
  348. justify-content: center;
  349. &:active {
  350. background: #e0e0e0;
  351. }
  352. &.delete {
  353. background: #ffe5e5;
  354. &:active {
  355. background: #ffcccc;
  356. }
  357. .icon-text {
  358. color: #ff4444;
  359. font-size: 40rpx;
  360. }
  361. }
  362. .icon-text {
  363. font-size: 32rpx;
  364. color: #666666;
  365. font-weight: 600;
  366. line-height: 1;
  367. }
  368. }
  369. }
  370. .arrow-icon {
  371. font-size: 48rpx;
  372. color: #cccccc;
  373. font-weight: 300;
  374. }
  375. }
  376. .empty-tip {
  377. padding: 200rpx 0;
  378. text-align: center;
  379. .empty-text {
  380. font-size: 28rpx;
  381. color: #999999;
  382. }
  383. }
  384. }
  385. // 底部提交按钮
  386. .submit-footer {
  387. background: #ffffff;
  388. padding: 24rpx 32rpx;
  389. padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
  390. box-shadow: 0 -2rpx 8rpx rgba(0, 0, 0, 0.05);
  391. .submit-btn {
  392. width: 100%;
  393. height: 88rpx;
  394. background: linear-gradient(135deg, #1ec9c9 0%, #1eb8b8 100%);
  395. border-radius: 44rpx;
  396. display: flex;
  397. align-items: center;
  398. justify-content: center;
  399. box-shadow: 0 4rpx 16rpx rgba(30, 201, 201, 0.3);
  400. &:active {
  401. transform: scale(0.98);
  402. opacity: 0.9;
  403. }
  404. .submit-text {
  405. font-size: 32rpx;
  406. font-weight: 600;
  407. color: #ffffff;
  408. }
  409. }
  410. }
  411. }
  412. </style>