index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. <template>
  2. <div class="container">
  3. <div class="processing-photo-task">
  4. <div class="top">
  5. <div class="nav-bar">
  6. <van-nav-bar
  7. title="加工拍照任务"
  8. left-arrow
  9. @click-left="goBack"
  10. @click-right="onRefresh"
  11. >
  12. <template #left>
  13. <van-icon name="arrow-left" size="25" />
  14. <div
  15. style="
  16. color: #fff;
  17. height: 46px;
  18. padding-right: 20px;
  19. line-height: 46px;
  20. "
  21. >
  22. 返回
  23. </div>
  24. </template>
  25. <template #right>
  26. <div class="nav-right" style="color: #fff">重置</div>
  27. </template>
  28. </van-nav-bar>
  29. </div>
  30. <div class="context">
  31. <!-- 筛选区域 -->
  32. <div class="filter-section">
  33. <van-row gutter="10">
  34. <van-col span="8">
  35. <van-field
  36. v-model="filterForm.owners"
  37. placeholder="请选择货主"
  38. readonly
  39. clickable
  40. clearable
  41. clear-trigger="always"
  42. @click="showOwnerDialog"
  43. @clear="onOwnerClear"
  44. />
  45. </van-col>
  46. <van-col span="8">
  47. <van-field
  48. v-model="filterForm.status"
  49. placeholder="请选择状态"
  50. readonly
  51. clickable
  52. @click="showStatusPicker = true"
  53. />
  54. </van-col>
  55. <van-col span="8">
  56. <van-button type="primary" size="small" block @click="onFilterSearch">
  57. 搜索
  58. </van-button>
  59. </van-col>
  60. </van-row>
  61. </div>
  62. <van-pull-refresh
  63. v-model="loading"
  64. @refresh="onRefresh"
  65. :style="{
  66. 'max-height': computedMaxHeight,
  67. 'min-height': computedMaxHeight,
  68. overflow: 'auto',
  69. }"
  70. >
  71. <table class="photo-task-table">
  72. <thead>
  73. <tr>
  74. <th style="width: 25%">货主</th>
  75. <th style="width: 35%">加工单号</th>
  76. <th style="width: 15%">状态</th>
  77. <th style="width: 12.5%">查看</th>
  78. <th style="width: 12.5%">操作</th>
  79. </tr>
  80. </thead>
  81. <tbody>
  82. <tr v-for="(row, rowIndex) in photoTasks" :key="rowIndex">
  83. <td>
  84. <span class="text-compact">{{ row.ownerName }}</span>
  85. </td>
  86. <td>
  87. <span class="text-sm code-link" @click="showNotes(row)">{{ row.code }}</span>
  88. </td>
  89. <td>
  90. <span class="text-compact">{{ row.status }}</span>
  91. </td>
  92. <td>
  93. <van-button type="primary" plain size="small" @click="showPhotos(row)" class="compact-button"
  94. >查看
  95. </van-button>
  96. </td>
  97. <td>
  98. <van-button v-if="row.status !== '完成'" type="primary" size="small" @click="toPhoto(row)" class="compact-button"
  99. >拍摄
  100. </van-button>
  101. </td>
  102. </tr>
  103. </tbody>
  104. </table>
  105. <!-- 分页控件 -->
  106. <van-pagination
  107. v-model="paginate.page"
  108. :page-size="paginate.size"
  109. :total-items="paginate.total"
  110. :page-count="paginate.pages"
  111. :show-page-size="5"
  112. model="simple"
  113. forced="true"
  114. @change="onPageChange"
  115. class="pagination"
  116. />
  117. </van-pull-refresh>
  118. </div>
  119. </div>
  120. <!-- 货主选择器弹窗 -->
  121. <Owner
  122. ref="ownerRef"
  123. @onOwner="onOwnerSelect"
  124. />
  125. <!-- 状态选择器弹窗 -->
  126. <van-popup v-model:show="showStatusPicker" position="bottom">
  127. <van-picker
  128. :columns="statusOptions"
  129. @confirm="onStatusConfirm"
  130. @cancel="showStatusPicker = false"
  131. />
  132. </van-popup>
  133. <!-- 图片上传弹窗 -->
  134. <van-popup v-model:show="showUpload" position="bottom" :style="{ height: '80%' }">
  135. <div class="upload-popup">
  136. <van-nav-bar
  137. title="图片上传"
  138. left-arrow
  139. @click-left="showUpload = false"
  140. >
  141. <template #left>
  142. <van-icon name="arrow-left" size="25" />
  143. <div style="color: #fff; height: 46px; padding-right: 20px; line-height: 46px;">
  144. 返回
  145. </div>
  146. </template>
  147. </van-nav-bar>
  148. <div class="upload-content">
  149. <van-uploader
  150. v-model="uploadImages"
  151. :max-count="9"
  152. :max-size="10 * 1024 * 1024"
  153. :before-read="beforeReadImage"
  154. :after-read="afterReadImage"
  155. multiple
  156. preview-full-image
  157. />
  158. <div class="upload-footer">
  159. <van-button
  160. type="primary"
  161. block
  162. :loading="uploading"
  163. @click="submitUpload"
  164. >
  165. {{ uploading ? '上传中...' : '提交上传' }}
  166. </van-button>
  167. </div>
  168. </div>
  169. </div>
  170. </van-popup>
  171. </div>
  172. </div>
  173. </template>
  174. <script setup>
  175. import { ref, computed, onMounted } from 'vue'
  176. import { useRoute } from 'vue-router'
  177. import { showNotify, showImagePreview, showFailToast, showLoadingToast, closeToast, showDialog } from 'vant'
  178. import {
  179. getProcessingPhotoTask,
  180. processingPhotoTaskItems,
  181. uploadPhoto,status
  182. } from '@/api/processing/index'
  183. import { getHeader, goBack } from '@/utils/android'
  184. import { compressImage } from '@/utils/imageCompression'
  185. import Owner from '@/components/Owner.vue'
  186. import { useStore } from '@/store/modules/user'
  187. try {
  188. getHeader()
  189. } catch (error) {
  190. console.log(error)
  191. }
  192. const store = useStore()
  193. const warehouse = store.warehouse
  194. const route = useRoute()
  195. const loading = ref(false)
  196. const photoTasks = ref([])
  197. const showUpload = ref(false)
  198. const uploadImages = ref([])
  199. const uploading = ref(false)
  200. const currentTaskId = ref(null)
  201. const ownerRef = ref(null)
  202. const paginate = ref({
  203. page: 1,
  204. size: 20,
  205. total: 0,
  206. pages: 0,
  207. })
  208. const reviewImages = ref([])
  209. // 筛选相关数据
  210. const filterForm = ref({
  211. owners: '',
  212. ownerCode: '',
  213. status: '',
  214. statusCode: ''
  215. })
  216. const showStatusPicker = ref(false)
  217. const statusOptions = ref([])
  218. // 计算最大高度适配手持设备
  219. const computedMaxHeight = computed(() => {
  220. // 减去导航栏高度(46px)和筛选区域高度(约80px)
  221. return window.innerHeight - 46 - 80 + 'px'
  222. })
  223. // 获取从其他页面传递的参数
  224. onMounted(() => {
  225. // 可以从route.query或route.params中获取参数
  226. if (route.query.id) {
  227. // 如果有传入id参数,可以做特殊处理
  228. }
  229. status().then(res=>{
  230. if (res.data) {
  231. statusOptions.value = res.data.map(item=>{
  232. return {text:item.name,value:item.code}
  233. })
  234. }
  235. })
  236. })
  237. // 显示notes信息 - 改为弹框方式
  238. function showNotes(row) {
  239. const notes = row.notes || '暂无备注信息'
  240. showDialog({
  241. title: '任务备注',
  242. message: notes,
  243. confirmButtonText: '确定',
  244. messageAlign: 'left'
  245. })
  246. }
  247. function onRefresh() {
  248. // 刷新时重置到第一页
  249. paginate.value.page = 1
  250. filterForm.value.owners = null
  251. filterForm.value.ownerCode = null
  252. filterForm.value.status = null
  253. filterForm.value.statusCode = null
  254. listProcessingPhoto()
  255. }
  256. function listProcessingPhoto() {
  257. loading.value = true
  258. // 合并筛选条件和分页参数,使用statusCode而不是status
  259. const params = {
  260. ...paginate.value,
  261. warehouses: warehouse,
  262. owners: filterForm.value.ownerCode,
  263. status: filterForm.value.statusCode
  264. }
  265. getProcessingPhotoTask(params).then((res) => {
  266. photoTasks.value = res?.data?.records || []
  267. // 更新分页信息
  268. if (res?.data) {
  269. paginate.value.size = res.data.size || 0
  270. paginate.value.total = res.data.total || 0
  271. paginate.value.page = res.data.current || 0
  272. paginate.value.pages = res.data.pages || 0
  273. }
  274. }).finally(() => {
  275. loading.value = false
  276. })
  277. }
  278. // 分页变更处理
  279. function onPageChange(page) {
  280. paginate.value.page = page
  281. listProcessingPhoto()
  282. }
  283. function showPhotos(row) {
  284. processingPhotoTaskItems(row.id).then((res) => {
  285. const items = res.data
  286. if (!items || items.length === 0) {
  287. showNotify({ type: 'warning', message: '没有对应的拍摄' })
  288. return
  289. }
  290. const baseUrl = import.meta.env.VITE_APP_BASE_API + 'statics/storage/'
  291. const paths = baseUrl.split('.')
  292. paths[1] = 'swms'
  293. const base = paths.join('.')
  294. // 解构图片url
  295. reviewImages.value = items.map(item => {
  296. const {path,module} = item
  297. return `${base}${module}/${path}`
  298. }).filter(url => url)
  299. // 预览图片
  300. if (reviewImages.value.length > 0) {
  301. showImagePreview({
  302. images: reviewImages.value,
  303. closeable: true,
  304. })
  305. } else {
  306. showNotify({ type: 'warning', message: '没有可预览的图片' })
  307. }
  308. }).catch(err => {
  309. showNotify({ type: 'danger', message: '获取图片失败' })
  310. })
  311. }
  312. function toPhoto(row) {
  313. currentTaskId.value = row.id
  314. uploadImages.value = []
  315. showUpload.value = true
  316. }
  317. // 检查图片格式和大小
  318. const beforeReadImage = (file) => {
  319. const files = Array.isArray(file) ? file : [file]
  320. for (const f of files) {
  321. // 检查文件格式
  322. if (f.name.toLowerCase().endsWith('.heic') || f.name.toLowerCase().endsWith('.heif')) {
  323. showFailToast('不支持HEIC/HEIF格式的图片')
  324. return false
  325. }
  326. // 检查文件类型
  327. const isImage = /^image\//.test(f.type)
  328. if (!isImage) {
  329. showFailToast('请上传图片文件')
  330. return false
  331. }
  332. // 检查文件大小 (10MB)
  333. if (f.size > 10 * 1024 * 1024) {
  334. showFailToast('图片大小不能超过10MB')
  335. return false
  336. }
  337. }
  338. return true
  339. }
  340. // 图片读取完成后处理
  341. const afterReadImage = async (file) => {
  342. try {
  343. // 处理单个文件或多个文件
  344. const files = Array.isArray(file) ? file : [file]
  345. for (let i = 0; i < files.length; i++) {
  346. const fileItem = files[i]
  347. const originalFile = fileItem.file
  348. if (originalFile) {
  349. // 检查文件大小,如果大于1MB则进行压缩
  350. if (originalFile.size > 1 * 1024 * 1024) {
  351. showNotify({
  352. type: 'primary',
  353. message: `正在压缩图片: ${originalFile.name} (${(originalFile.size / 1024 / 1024).toFixed(2)}MB)`
  354. })
  355. try {
  356. // 压缩图片到1MB以内
  357. const compressedFile = await compressImage(originalFile, 1 * 1024 * 1024)
  358. // 更新文件对象
  359. fileItem.file = compressedFile
  360. fileItem.content = URL.createObjectURL(compressedFile)
  361. // 显示压缩结果
  362. const compressionRatio = ((originalFile.size - compressedFile.size) / originalFile.size * 100).toFixed(1)
  363. showNotify({
  364. type: 'success',
  365. message: `图片压缩完成: ${(compressedFile.size / 1024 / 1024).toFixed(2)}MB (压缩${compressionRatio}%)`
  366. })
  367. } catch (error) {
  368. console.error('图片压缩失败:', error)
  369. showNotify({
  370. type: 'warning',
  371. message: `图片压缩失败,将使用原图上传: ${error.message}`
  372. })
  373. // 压缩失败时保持原文件
  374. }
  375. } else {
  376. console.log(`图片大小符合要求,无需压缩: ${(originalFile.size / 1024).toFixed(2)}KB`)
  377. }
  378. }
  379. }
  380. } catch (error) {
  381. console.error('处理图片时发生错误:', error)
  382. showNotify({
  383. type: 'danger',
  384. message: '处理图片时发生错误: ' + error.message
  385. })
  386. }
  387. }
  388. // 提交上传
  389. const submitUpload = async () => {
  390. if (!uploadImages.value.length) {
  391. showFailToast('请先选择图片')
  392. return
  393. }
  394. if (!currentTaskId.value) {
  395. showFailToast('任务ID无效')
  396. return
  397. }
  398. uploading.value = true
  399. const toast = showLoadingToast('上传中...')
  400. try {
  401. const totalImages = uploadImages.value.length
  402. let successCount = 0
  403. let failedImages = []
  404. // 逐个上传图片
  405. const failedMessages = []
  406. for (let i = 0; i < uploadImages.value.length; i++) {
  407. const image = uploadImages.value[i]
  408. try {
  409. const formData = new FormData()
  410. formData.append('file', image.file)
  411. // 调用上传API
  412. await uploadPhoto(currentTaskId.value, formData)
  413. successCount++
  414. } catch (err) {
  415. failedMessages.push(`${i+1}/${uploadImages.value.length}张图片上传失败:${err.message}`)
  416. console.error(`第${i + 1}张图片上传失败:`, err.message)
  417. failedImages.push({
  418. index: i,
  419. image: image,
  420. error: err.message || '上传失败'
  421. })
  422. }
  423. }
  424. closeToast()
  425. if (failedMessages.length !== 0) {
  426. showNotify({
  427. type: 'danger',
  428. message: failedMessages.join("\\t\\n")
  429. })
  430. }
  431. if (successCount === totalImages) {
  432. // 全部上传成功
  433. showNotify({ type: 'success', message: '全部图片上传成功' })
  434. // 关闭弹窗并刷新列表
  435. showUpload.value = false
  436. uploadImages.value = []
  437. onRefresh()
  438. } else if (successCount > 0) {
  439. // 部分上传成功
  440. showNotify({
  441. type: 'warning',
  442. message: `${successCount}/${totalImages}张图片上传成功,${failedImages.length}张失败`
  443. })
  444. // 移除上传成功的图片,保留失败的图片
  445. uploadImages.value = failedImages.map(item => item.image)
  446. }
  447. } catch (err) {
  448. closeToast()
  449. showNotify({ type: 'danger', message: '上传过程发生异常: ' + (err.message || '未知错误') })
  450. // 发生异常时保留所有图片,让用户可以重试
  451. } finally {
  452. uploading.value = false
  453. }
  454. }
  455. function onFilterSearch() {
  456. // 搜索时重置到第一页
  457. paginate.value.page = 1
  458. listProcessingPhoto()
  459. }
  460. // 显示货主选择弹窗
  461. function showOwnerDialog() {
  462. if (ownerRef.value) {
  463. ownerRef.value.show('filter')
  464. }
  465. }
  466. // 处理货主选择
  467. function onOwnerSelect(item, type) {
  468. filterForm.value.owners = item.name
  469. filterForm.value.ownerCode = item.code
  470. // 选择货主后自动搜索item
  471. onFilterSearch()
  472. }
  473. // 处理货主清空
  474. function onOwnerClear() {
  475. filterForm.value.owners = ''
  476. filterForm.value.ownerCode = ''
  477. // 清空货主后自动搜索
  478. onFilterSearch()
  479. }
  480. function onStatusConfirm({ selectedOptions }) {
  481. filterForm.value.status = selectedOptions[0].text
  482. filterForm.value.statusCode = selectedOptions[0].value
  483. showStatusPicker.value = false
  484. // 选择状态后自动搜索
  485. onFilterSearch()
  486. }
  487. // 初始化加载数据
  488. onRefresh()
  489. </script>
  490. <style scoped lang="scss">
  491. // 固定顶部区域样式
  492. .top {
  493. position: fixed;
  494. top: 0;
  495. left: 0;
  496. right: 0;
  497. z-index: 1000;
  498. background: #fff;
  499. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); // 添加阴影效果
  500. }
  501. .nav-bar {
  502. position: relative;
  503. z-index: 1001;
  504. }
  505. .context {
  506. position: relative;
  507. z-index: 999;
  508. }
  509. // 为整个容器添加上边距,避免内容被固定区域遮挡
  510. .container {
  511. padding-top: 126px; // 导航栏46px + 筛选区域约80px
  512. }
  513. // 确保van-field的清空按钮样式正确显示
  514. :deep(.van-field__clear) {
  515. display: flex !important;
  516. align-items: center;
  517. justify-content: center;
  518. color: #c8c9cc;
  519. font-size: 16px;
  520. cursor: pointer;
  521. &:active {
  522. color: #969799;
  523. }
  524. }
  525. // 确保van-field右侧区域有足够空间显示清空按钮
  526. :deep(.van-field__right-icon) {
  527. display: flex !important;
  528. align-items: center;
  529. }
  530. .photo-task-table {
  531. width: 100%;
  532. border-collapse: collapse;
  533. text-align: center;
  534. table-layout: fixed;
  535. font-size: 12px;
  536. thead {
  537. tr {
  538. background-color: #f5f5f5;
  539. th {
  540. padding: 6px 2px;
  541. font-weight: 500;
  542. }
  543. }
  544. }
  545. tbody {
  546. tr {
  547. height: 36px;
  548. border-bottom: 1px solid #e0e0e0;
  549. &:last-child {
  550. border-bottom: none;
  551. }
  552. td {
  553. padding: 3px 2px;
  554. overflow: hidden;
  555. text-overflow: ellipsis;
  556. white-space: nowrap;
  557. .text-compact {
  558. font-size: 12px;
  559. line-height: 1.2;
  560. display: block;
  561. white-space: nowrap;
  562. overflow: hidden;
  563. text-overflow: ellipsis;
  564. }
  565. .text-sm {
  566. font-size: 11px;
  567. line-height: 1.2;
  568. display: block;
  569. white-space: nowrap;
  570. overflow: hidden;
  571. text-overflow: ellipsis;
  572. }
  573. .compact-button {
  574. height: 24px;
  575. line-height: 22px;
  576. padding: 0 6px;
  577. font-size: 11px;
  578. min-width: 36px;
  579. }
  580. }
  581. }
  582. }
  583. }
  584. .pagination {
  585. padding: 10px 0;
  586. display: flex;
  587. justify-content: center;
  588. }
  589. .upload-popup {
  590. height: 100%;
  591. display: flex;
  592. flex-direction: column;
  593. .upload-content {
  594. flex: 1;
  595. padding: 10px;
  596. overflow-y: auto;
  597. .upload-footer {
  598. padding: 10px 0;
  599. }
  600. }
  601. }
  602. .filter-section {
  603. padding: 10px;
  604. background: #fff;
  605. border-bottom: 1px solid #e0e0e0;
  606. }
  607. // 移除之前的padding-top,因为已经在.container中设置了
  608. :deep(.van-pull-refresh) {
  609. // 调整滚动区域的高度计算,减去固定的导航栏和筛选区域高度
  610. max-height: calc(100vh - 126px) !important; // 总共减去126px
  611. min-height: calc(100vh - 126px) !important;
  612. overflow: auto !important;
  613. }
  614. .code-link {
  615. color: #1989fa;
  616. cursor: pointer;
  617. text-decoration: underline;
  618. &:hover {
  619. color: #0570db;
  620. }
  621. }
  622. </style>