index.vue 9.0 KB

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