albumCarouselItem.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <template>
  2. <div class="carousel-container">
  3. <!-- 图片列表 -->
  4. <div class="carousel-wrapper" :style="carouselStyle">
  5. <!-- <photo-provider v-if="images" :photo-closable="true" @visibleChange="handleVisibleChange">
  6. <template v-for="(photo, index) in images"
  7. :key="photo.id">
  8. <album-draw-box :isShowNum="0" :farmId="766" :photo="photo" :current="currentIndex" :index="index" :length="images.length"
  9. ></album-draw-box>
  10. </template>
  11. </photo-provider> -->
  12. <div
  13. class="carousel-img"
  14. v-for="(photo, index) in images"
  15. :key="index"
  16. >
  17. <div class="label-text" v-if="labelText">{{ labelText }}</div>
  18. <img class="img-dom" :index="index" @click="clickPhoto(photo)" :src="getPhotoSrc(photo)" alt="" />
  19. <div class="carousel-img-mask" v-if="!isAchievementImgs">
  20. <div class="mask-content">
  21. <div class="mask-line line-top">
  22. <span class="date-text">{{ imgData?.executeDate }}</span>
  23. <span class="line-separator">|</span>
  24. <span class="executor-text">执行人:{{ imgData?.executeName }}</span>
  25. </div>
  26. <div class="mask-line line-middle">
  27. <span class="work-name">{{ imgData?.farmWorkName }}</span>
  28. <span class="line-separator">|</span>
  29. <span class="location-text">
  30. <img src="@/assets/watermark/address.png" alt="location" class="location-icon" />
  31. {{ imgData?.farmName }}
  32. </span>
  33. </div>
  34. <div class="mask-line line-bottom">
  35. <span class="prescription-text">药物处方:{{ prescriptionText }}</span>
  36. </div>
  37. </div>
  38. </div>
  39. <div class="carousel-img-mask" v-if="isAchievementImgs && imgData?.reCheckText">
  40. <div class="mask-content">
  41. <div class="review-effect-text">复核成效</div>
  42. <div class="review-effect-content">
  43. <span class="review-effect-content-text">{{ imgData?.reCheckText }}</span>
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. </div>
  49. <!-- 左右箭头 -->
  50. <div @click.stop="prev" v-if="currentIndex !== 0" class="arrow left-arrow">
  51. <el-icon color="#F0D09C"><ArrowLeftBold /></el-icon>
  52. </div>
  53. <div @click.stop="next" v-if="images && currentIndex !== images.length - 1" class="arrow right-arrow">
  54. <el-icon color="#F0D09C"><ArrowRightBold /></el-icon>
  55. </div>
  56. <div class="curren-img" v-if="currentPhoto">
  57. <div ref="currentImgRef" class="carousel-img" :class="{ noFit: isAchievementImgs }">
  58. <img class="img-dom" :src="currentPhoto" alt="" />
  59. <img src="@/assets/img/home/qrcode.png" alt="" class="code-icon" />
  60. <div class="carousel-img-mask" v-if="!isAchievementImgs">
  61. <div class="mask-content">
  62. <div class="mask-line line-top">
  63. <span class="date-text">{{ imgData?.executeDate }}</span>
  64. <span class="line-separator">|</span>
  65. <span class="executor-text">执行人:{{ imgData?.executeName }}</span>
  66. </div>
  67. <div class="mask-line line-middle">
  68. <span class="work-name">{{ imgData?.farmWorkName }}</span>
  69. <span class="line-separator">|</span>
  70. <span class="location-text">
  71. <img src="@/assets/watermark/address.png" alt="location" class="location-icon" />
  72. {{ imgData?.farmName }}
  73. </span>
  74. </div>
  75. <div class="mask-line line-bottom">
  76. <span class="prescription-text">药物处方:{{ prescriptionText }}</span>
  77. </div>
  78. </div>
  79. </div>
  80. <div class="carousel-img-mask" v-if="isAchievementImgs && imgData?.reCheckText">
  81. <div class="mask-content">
  82. <div class="review-effect-text">复核成效</div>
  83. <div class="review-effect-content">
  84. <span class="review-effect-content-text">{{ imgData?.reCheckText }}</span>
  85. </div>
  86. </div>
  87. </div>
  88. </div>
  89. </div>
  90. <popup class="cavans-popup" v-model:show="showPopup">
  91. <div class="cavans-content">
  92. <img class="current-img" :src="previewCanvas" alt="" />
  93. </div>
  94. <!-- 底部操作按钮 -->
  95. <div class="bottom-actions" @click.stop="showPopup = false">
  96. <div class="action-buttons">
  97. <div class="action-btn green-btn" @click.stop="handleWechat">
  98. <div class="icon-circle">
  99. <img src="@/assets/img/home/wechat.png" alt="" />
  100. </div>
  101. <span class="btn-label">微信</span>
  102. </div>
  103. <div class="action-btn orange-btn" @click.stop="handleSaveImage">
  104. <div class="icon-circle">
  105. <el-icon :size="24"><Download /></el-icon>
  106. </div>
  107. <span class="btn-label">保存图片</span>
  108. </div>
  109. </div>
  110. <div class="cancel-btn" @click="handleCancel">取消</div>
  111. </div>
  112. </popup>
  113. </div>
  114. </template>
  115. <script setup>
  116. import { Popup } from "vant";
  117. import { toRefs, ref, computed, onMounted, onUnmounted, nextTick, watch } from "vue";
  118. import AlbumDrawBox from "./albumDrawBox.vue";
  119. import html2canvas from "html2canvas";
  120. import { base_img_url2 } from "@/api/config";
  121. import "./cacheImg.js";
  122. const props = defineProps({
  123. images: {
  124. type: Array,
  125. required: true,
  126. },
  127. labelText: {
  128. type: String,
  129. default: "",
  130. },
  131. isAchievementImgs: {
  132. type: Boolean,
  133. default: false,
  134. },
  135. imgData: {
  136. type: Object,
  137. default: () => {},
  138. },
  139. });
  140. const { images, labelText, isAchievementImgs } = toRefs(props);
  141. let timer = null;
  142. const currentIndex = ref(0);
  143. onMounted(() => {
  144. updateImagePosition();
  145. clearAndRestartTimer();
  146. prescriptionText.value = buildPrescriptionText(props.imgData.prescriptionList);
  147. });
  148. onUnmounted(() => {
  149. clearInterval(timer);
  150. });
  151. watch(() => props.imgData, (newVal) => {
  152. if (newVal && newVal.prescriptionList) {
  153. prescriptionText.value = buildPrescriptionText(newVal.prescriptionList);
  154. }
  155. });
  156. const prescriptionText = ref("");
  157. function buildPrescriptionText(list) {
  158. try {
  159. return list
  160. .map((group) =>
  161. (group.pesticideFertilizerList || [])
  162. .map((p) => p.defaultName || p.pesticideFertilizerName || "")
  163. .filter(Boolean)
  164. .join("+")
  165. )
  166. .filter(Boolean)
  167. .join("+");
  168. } catch {
  169. return "";
  170. }
  171. }
  172. const updateImagePosition = () => {
  173. carouselStyle.value.transform = `translateX(-${currentIndex.value * 100}%)`;
  174. };
  175. const clickPhotoShow = () => {
  176. if (timer) {
  177. clearInterval(timer);
  178. }
  179. };
  180. // 图片显隐切换回调
  181. const handleVisibleChange = ({ visible }) => {
  182. if (visible.value) {
  183. if (timer) {
  184. clearInterval(timer);
  185. }
  186. } else {
  187. clearAndRestartTimer();
  188. }
  189. };
  190. // 计算轮播图样式
  191. const carouselStyle = computed(() => {
  192. return {
  193. transform: `translateX(-${currentIndex.value * 100}%)`,
  194. };
  195. });
  196. // 下一张图片
  197. const next = () => {
  198. // 图片总数
  199. const totalImages = images.value.length;
  200. currentIndex.value = (currentIndex.value + 1) % totalImages;
  201. updateImagePosition();
  202. clearAndRestartTimer();
  203. };
  204. // 上一张图片
  205. const prev = () => {
  206. // 图片总数
  207. const totalImages = images.value.length;
  208. currentIndex.value = (currentIndex.value - 1 + totalImages) % totalImages;
  209. updateImagePosition();
  210. clearAndRestartTimer();
  211. };
  212. const clearAndRestartTimer = () => {
  213. if (timer) {
  214. clearInterval(timer);
  215. }
  216. // timer = setInterval(next, 5000);
  217. };
  218. const showPopup = ref(false);
  219. const currentPhoto = ref(null);
  220. const previewCanvas = ref(null);
  221. const currentImgRef = ref(null);
  222. const clickPhoto = (photo) => {
  223. currentPhoto.value = getPhotoSrc(photo);
  224. nextTick(async () => {
  225. const canvas = await html2canvas(currentImgRef.value, {
  226. backgroundColor: "#ffffff00",
  227. scrollY: -window.scrollY, // 处理滚动条位置
  228. allowTaint: true, // 允许跨域图片
  229. useCORS: true, // 使用CORS
  230. scale: 2, // 提高分辨率(2倍)
  231. height: currentImgRef.value.scrollHeight, // 设置完整高度
  232. width: currentImgRef.value.scrollWidth, // 设置完整宽度
  233. logging: true, // 开启日志(调试用)
  234. });
  235. // 转换为图片并下载
  236. const image = canvas.toDataURL("image/png");
  237. setTimeout(() => {
  238. previewCanvas.value = image;
  239. showPopup.value = true;
  240. }, 100);
  241. });
  242. };
  243. const handleSaveImage = () => {
  244. downloadImage(previewCanvas.value, '执行照片');
  245. };
  246. function downloadImage(dataUrl, filename) {
  247. const link = document.createElement("a");
  248. link.href = dataUrl;
  249. link.download = filename;
  250. document.body.appendChild(link);
  251. link.click();
  252. document.body.removeChild(link);
  253. }
  254. const getPhotoSrc = (photo) => {
  255. // if (isAchievementImgs.value) {
  256. // return photo;
  257. // }
  258. return base_img_url2 + (photo.cloudFilename ? photo.cloudFilename : photo);
  259. };
  260. </script>
  261. <style lang="scss" scoped>
  262. @import "src/styles/index";
  263. .carousel-container {
  264. position: relative;
  265. width: 100%;
  266. overflow: hidden;
  267. margin: 0 auto;
  268. .curren-img {
  269. position: fixed;
  270. bottom: 0;
  271. left: 0;
  272. right: 0;
  273. width: 80%;
  274. height: 100%;
  275. margin: 0 auto;
  276. z-index: -1;
  277. pointer-events: none;
  278. .carousel-img {
  279. width: 100%;
  280. position: relative;
  281. overflow: hidden;
  282. }
  283. .img-dom {
  284. width: 100%;
  285. height: 100%;
  286. object-fit: cover;
  287. border-radius: 8px;
  288. }
  289. }
  290. .carousel-wrapper {
  291. display: flex;
  292. transition: transform 0.5s ease;
  293. width: 100%;
  294. .carousel-img {
  295. width: calc(100vw - 48px);
  296. min-width: calc(100vw - 48px);
  297. // min-width: 312px;
  298. height: 255px;
  299. object-fit: cover;
  300. position: relative;
  301. overflow: hidden;
  302. &.noFit {
  303. height: auto;
  304. object-fit: contain;
  305. }
  306. .img-dom {
  307. width: 100%;
  308. height: 100%;
  309. object-fit: cover;
  310. border-radius: 8px;
  311. }
  312. &.noFit {
  313. .img-dom {
  314. height: auto;
  315. object-fit: contain;
  316. }
  317. }
  318. }
  319. }
  320. .code-icon {
  321. position: absolute;
  322. right: 12px;
  323. top: 12px;
  324. width: 40px;
  325. }
  326. .carousel-img-mask {
  327. position: absolute;
  328. bottom: -2px;
  329. left: -2px;
  330. width: calc(100% + 2px);
  331. height: 80%;
  332. background: linear-gradient(360deg, rgba(0, 0, 0, 0.7) 0%, rgba(0, 0, 0, 0.4) 25%, rgba(0, 0, 0, 0) 40%);
  333. z-index: 1;
  334. pointer-events: none;
  335. display: flex;
  336. align-items: flex-end;
  337. padding: 8px 12px;
  338. box-sizing: border-box;
  339. border-radius: 8px 8px 12px 12px;
  340. .mask-content {
  341. width: 100%;
  342. color: #ffffff;
  343. font-size: 10px;
  344. line-height: 15px;
  345. }
  346. .review-effect-text {
  347. font-family: "PangMenZhengDao";
  348. font-size: 16px;
  349. margin-bottom: 4px;
  350. }
  351. .review-effect-content {
  352. font-size: 10px;
  353. line-height: 15px;
  354. }
  355. .mask-line {
  356. display: flex;
  357. align-items: center;
  358. flex-wrap: wrap;
  359. }
  360. .prescription-text {
  361. display: flex;
  362. .prescription-text-label {
  363. flex: none;
  364. }
  365. }
  366. .line-middle {
  367. margin-top: 4px;
  368. .work-name {
  369. font-family: "PangMenZhengDao", system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial,
  370. sans-serif;
  371. font-size: 17px;
  372. }
  373. }
  374. .line-bottom {
  375. margin-top: 4px;
  376. }
  377. .date-text {
  378. font-family: "PangMenZhengDao";
  379. font-size: 12px;
  380. }
  381. .date-text,
  382. .executor-text,
  383. .location-text {
  384. white-space: nowrap;
  385. }
  386. .location-icon {
  387. width: 9px;
  388. height: 10px;
  389. margin-right: 2px;
  390. }
  391. .line-separator {
  392. margin: 0 6px;
  393. }
  394. }
  395. .label-text {
  396. position: absolute;
  397. top: 0;
  398. left: 0;
  399. padding: 4px 10px;
  400. background: rgba(54, 52, 52, 0.8);
  401. color: #fff;
  402. font-size: 12px;
  403. border-radius: 8px 0 8px 0;
  404. z-index: 1;
  405. }
  406. .blur-bg {
  407. position: absolute;
  408. top: 0;
  409. width: 100%;
  410. height: 100%;
  411. backdrop-filter: blur(1.4px);
  412. .blur-content {
  413. border-radius: 8px;
  414. background: rgba(0, 0, 0, 0.5);
  415. width: 100%;
  416. height: 100%;
  417. display: flex;
  418. flex-direction: column;
  419. align-items: center;
  420. justify-content: center;
  421. font-size: 12px;
  422. color: #fff;
  423. .blur-img {
  424. img {
  425. width: 54px;
  426. position: relative;
  427. left: 4px;
  428. top: 4px;
  429. }
  430. }
  431. .blur-text {
  432. padding: 8px 0;
  433. text-align: center;
  434. line-height: 1.5;
  435. }
  436. .blur-btn {
  437. padding: 0 40px;
  438. box-shadow: 0 -2px 2px #86c9ff;
  439. height: 28px;
  440. line-height: 28px;
  441. border-radius: 50px;
  442. background: rgba(33, 153, 248, 0.7);
  443. // background: linear-gradient(#86C9FF, rgba(255, 255, 255, 0));
  444. }
  445. }
  446. }
  447. .arrow {
  448. position: absolute;
  449. top: 50%;
  450. transform: translateY(-50%);
  451. background: rgba(0, 0, 0, 0.5);
  452. width: rpx(72);
  453. height: rpx(72);
  454. border-radius: 50%;
  455. display: inline-flex;
  456. align-items: center;
  457. justify-content: center;
  458. cursor: pointer;
  459. pointer-events: all;
  460. }
  461. .left-arrow {
  462. left: rpx(32);
  463. }
  464. .right-arrow {
  465. right: rpx(32);
  466. }
  467. }
  468. .cavans-popup {
  469. width: 100%;
  470. max-width: 100%;
  471. max-height: 90vh;
  472. background: none;
  473. border-radius: 12px;
  474. overflow: auto;
  475. display: flex;
  476. flex-direction: column;
  477. backdrop-filter: 4px;
  478. .cavans-content {
  479. text-align: center;
  480. padding: 16px;
  481. .current-img {
  482. width: 100%;
  483. }
  484. }
  485. // 底部操作按钮
  486. .bottom-actions {
  487. flex-shrink: 0;
  488. .action-buttons {
  489. padding: 16px;
  490. display: flex;
  491. justify-content: space-around;
  492. .action-btn {
  493. display: flex;
  494. flex-direction: column;
  495. align-items: center;
  496. cursor: pointer;
  497. .icon-circle {
  498. width: 48px;
  499. height: 48px;
  500. border-radius: 50%;
  501. display: flex;
  502. align-items: center;
  503. justify-content: center;
  504. color: #fff;
  505. margin-bottom: 4px;
  506. .el-icon {
  507. color: #fff;
  508. }
  509. img {
  510. width: 50px;
  511. }
  512. }
  513. &.blue-btn .icon-circle {
  514. background: #2199f8;
  515. }
  516. &.green-btn .icon-circle {
  517. background: #07c160;
  518. }
  519. &.orange-btn .icon-circle {
  520. background: #ff790b;
  521. }
  522. .btn-label {
  523. font-size: 12px;
  524. color: #fff;
  525. }
  526. }
  527. }
  528. .cancel-btn {
  529. text-align: center;
  530. font-size: 18px;
  531. color: #fff;
  532. cursor: pointer;
  533. }
  534. }
  535. }
  536. </style>