index.vue 11 KB

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