TreasureShare.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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(10px);
  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. background-color: rgba(40, 47, 58, 0.7);
  133. backdrop-filter: blur(50px);
  134. >button.close {
  135. position: absolute;
  136. top: 30px;
  137. right: 50px;
  138. width: 32px;
  139. height: 32px;
  140. background-image: url(@/assets/images/icon-close.png);
  141. background-size: cover;
  142. background-repeat: no-repeat;
  143. background-position: center center;
  144. }
  145. >h2 {
  146. flex: 0 0 auto;
  147. font-size: 30px;
  148. font-family: Source Han Sans CN-Bold, Source Han Sans CN;
  149. font-weight: bold;
  150. color: #FFFFFF;
  151. margin-bottom: 21px;
  152. overflow: hidden;
  153. white-space: pre;
  154. text-overflow: ellipsis;
  155. }
  156. >img.splitter {
  157. flex: 0 0 auto;
  158. width: 100%;
  159. margin-bottom: 37px;
  160. }
  161. >img.photo {
  162. flex: 0 0 auto;
  163. width: 100%;
  164. height: 43%;
  165. object-fit: contain;
  166. margin-bottom: 32px;
  167. }
  168. >.txt {
  169. flex: 1 0 1px;
  170. font-size: 20px;
  171. font-family: Source Han Sans CN-Light, Source Han Sans CN;
  172. font-weight: 400;
  173. color: rgba(255, 255, 255, 0.8);
  174. line-height: 1.5;
  175. overflow: auto;
  176. margin-right: -10px;
  177. padding-right: 10px;
  178. margin-bottom: 32px;
  179. text-indent: 2em;
  180. &::-webkit-scrollbar { background: transparent; width: 4px; } /*宽度是对垂直滚动条而言,高度是对水平滚动条而言*/
  181. &::-webkit-scrollbar-thumb {
  182. background: rgba(220, 231, 240, 0.2);
  183. border-radius: 2px;
  184. }
  185. }
  186. >.QRCode {
  187. height: 100px;
  188. >img {
  189. float: right;
  190. width: 100px;
  191. height: 100%;
  192. object-fit: contain;
  193. }
  194. }
  195. >button.download {
  196. position: absolute;
  197. bottom: 44px;
  198. right: calc(-46px - 149px);
  199. width: 149px;
  200. height: 46px;
  201. background: rgba(255,255,255,0.2);
  202. border: 1px solid #FFFFFF;
  203. border-radius: 23px;
  204. >img.download {
  205. width: 32px;
  206. margin-right: 8px;
  207. vertical-align: middle;
  208. }
  209. >span {
  210. font-size: 16px;
  211. color: #FFFFFF;
  212. vertical-align: middle;
  213. }
  214. }
  215. @media only screen and (max-width: 1110px) {
  216. >button.download {
  217. right: initial;
  218. left: 50px;
  219. }
  220. }
  221. }
  222. }
  223. </style>