TreasureShare.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <div class="treasure-share">
  3. <article ref="article">
  4. <button
  5. class="close"
  6. @click="$emit('close')"
  7. />
  8. <h2 title="万吨水压机">
  9. {{ info.name }}
  10. </h2>
  11. <img
  12. class="splitter"
  13. src="@/assets/images/splitter.png"
  14. alt=""
  15. draggable="false"
  16. >
  17. <img
  18. class="photo"
  19. :src="`${prefix}${info.thumb}`"
  20. alt=""
  21. draggable="false"
  22. >
  23. <div
  24. class="txt"
  25. v-html="info.description"
  26. />
  27. <div class="QRCode">
  28. <img
  29. v-show="qrcodeDataUrl"
  30. class=""
  31. :src="qrcodeDataUrl"
  32. alt=""
  33. draggable="false"
  34. >
  35. </div>
  36. <button
  37. v-if="!isSaving"
  38. class="download"
  39. @click="onClickDownload"
  40. >
  41. <img
  42. class="download"
  43. src="@/assets/images/download-from-cloud.png"
  44. alt=""
  45. draggable="false"
  46. >
  47. <span>保存图片</span>
  48. </button>
  49. </article>
  50. <a
  51. v-show="false"
  52. ref="forDownload"
  53. :href="aDownloadHref"
  54. download="share.jpg"
  55. />
  56. </div>
  57. </template>
  58. <script>
  59. import QRCode from 'qrcode'
  60. import { ref, nextTick } from "vue"
  61. import html2canvas from "html2canvas"
  62. export default {
  63. props: {
  64. info: {
  65. type: Object,
  66. required: true,
  67. }
  68. },
  69. setup(props) {
  70. const prefix = process.env.VUE_APP_API_ORIGIN
  71. const qrcodeDataUrl = ref(null)
  72. QRCode.toDataURL(location.href).then((url) => {
  73. qrcodeDataUrl.value = url
  74. })
  75. const article = ref(null)
  76. const forDownload = ref('')
  77. const aDownloadHref = ref(null)
  78. const isSaving = ref(false)
  79. const onClickDownload = utils.throttle(function() {
  80. isSaving.value = true
  81. nextTick(() => {
  82. // #capture 就是我们要获取截图对应的 DOM 元素选择器
  83. html2canvas(article.value, {
  84. useCORS: true, // 【重要】开启跨域配置
  85. scale: 1,
  86. allowTaint: true, // 允许跨域图片
  87. preserveDrawingBuffer: true,
  88. }).then((canvas) => {
  89. aDownloadHref.value = canvas.toDataURL('image/jpeg', 1.0)
  90. nextTick(() => {
  91. forDownload.value.click()
  92. isSaving.value = false
  93. })
  94. })
  95. })
  96. })
  97. return {
  98. prefix,
  99. qrcodeDataUrl,
  100. article,
  101. forDownload,
  102. aDownloadHref,
  103. isSaving,
  104. onClickDownload,
  105. }
  106. },
  107. }
  108. </script>
  109. <style lang="less" scoped>
  110. .treasure-share {
  111. position: absolute;
  112. left: 0;
  113. top: 0;
  114. width: 100%;
  115. height: 100%;
  116. background: rgba(0,0,0,0.5);
  117. backdrop-filter: blur(5px);
  118. >article {
  119. position: absolute;
  120. left: 50%;
  121. top: 50%;
  122. transform: translate(-50%, -50%);
  123. width: 717px;
  124. height: 83.7%;
  125. background-image: url(@/assets/images/general-article-bg.png);
  126. background-size: 100% auto;
  127. background-repeat: no-repeat;
  128. background-position: left top;
  129. padding: 32px 50px 50px 50px;
  130. display: flex;
  131. flex-direction: column;
  132. >button.close {
  133. position: absolute;
  134. top: 30px;
  135. right: 50px;
  136. width: 32px;
  137. height: 32px;
  138. background-image: url(@/assets/images/icon-close.png);
  139. background-size: cover;
  140. background-repeat: no-repeat;
  141. background-position: center center;
  142. }
  143. >h2 {
  144. flex: 0 0 auto;
  145. font-size: 30px;
  146. font-family: Source Han Sans CN-Bold, Source Han Sans CN;
  147. font-weight: bold;
  148. color: #FFFFFF;
  149. margin-bottom: 21px;
  150. overflow: hidden;
  151. white-space: pre;
  152. text-overflow: ellipsis;
  153. }
  154. >img.splitter {
  155. flex: 0 0 auto;
  156. width: 100%;
  157. margin-bottom: 37px;
  158. }
  159. >img.photo {
  160. flex: 0 0 auto;
  161. width: 100%;
  162. height: 43%;
  163. object-fit: contain;
  164. margin-bottom: 32px;
  165. }
  166. >.txt {
  167. flex: 1 0 1px;
  168. font-size: 20px;
  169. font-family: Source Han Sans CN-Regular, Source Han Sans CN;
  170. font-weight: 400;
  171. color: rgba(255, 255, 255, 0.8);
  172. line-height: 23px;
  173. overflow: auto;
  174. margin-right: -10px;
  175. padding-right: 10px;
  176. margin-bottom: 32px;
  177. text-indent: 2em;
  178. &::-webkit-scrollbar { background: transparent; width: 4px; } /*宽度是对垂直滚动条而言,高度是对水平滚动条而言*/
  179. &::-webkit-scrollbar-thumb {
  180. background: rgba(220, 231, 240, 0.2);
  181. border-radius: 2px;
  182. }
  183. }
  184. >.QRCode {
  185. height: 100px;
  186. >img {
  187. float: right;
  188. width: 100px;
  189. height: 100%;
  190. object-fit: contain;
  191. }
  192. }
  193. >button.download {
  194. position: absolute;
  195. bottom: 44px;
  196. right: calc(-46px - 149px);
  197. width: 149px;
  198. height: 46px;
  199. background: rgba(255,255,255,0.2);
  200. border: 1px solid #FFFFFF;
  201. border-radius: 23px;
  202. >img.download {
  203. width: 32px;
  204. margin-right: 8px;
  205. vertical-align: middle;
  206. }
  207. >span {
  208. font-size: 16px;
  209. color: #FFFFFF;
  210. vertical-align: middle;
  211. }
  212. }
  213. @media only screen and (max-width: 1110px) {
  214. >button.download {
  215. right: initial;
  216. left: 50px;
  217. }
  218. }
  219. }
  220. }
  221. </style>