Share.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <template>
  2. <div
  3. class="share"
  4. >
  5. <div
  6. id="capture"
  7. class="wrapper-1"
  8. :style="{
  9. width: initialWrapperWidth + 'px',
  10. height: initialWrapperHeight + 'px',
  11. transform: wrapperTransformCss,
  12. }"
  13. >
  14. <button
  15. v-show="!isSaving"
  16. class="close"
  17. @click="$router.go(-1)"
  18. />
  19. <div class="introduction-title-wrapper">
  20. <h1
  21. class="introduction-title"
  22. >
  23. 分享成绩
  24. </h1>
  25. </div>
  26. <p
  27. v-if="answerNumber !== undefined"
  28. class="main"
  29. >
  30. <span class="name">{{ userInfo.userName }}</span>在浙江省博物馆文澜阁学习系统中参与答题{{ answerNumber }}道{{ (isMobile && (badgeArchCurrent >= badgeArchGoal) || (badgeHistoryCurrent >= badgeHistoryGoal) || (badgeProtectorCurrent >= badgeProtectorGoal)) ? ',获得徽章:' : '' }}
  31. </p>
  32. <p
  33. v-if="!isMobile && ((badgeArchCurrent >= badgeArchGoal) || (badgeHistoryCurrent >= badgeHistoryGoal) || (badgeProtectorCurrent >= badgeProtectorGoal))"
  34. class="badge-num"
  35. >
  36. 获得徽章:
  37. </p>
  38. <ul
  39. v-if="(badgeArchCurrent >= badgeArchGoal) || (badgeHistoryCurrent >= badgeHistoryGoal) || (badgeProtectorCurrent >= badgeProtectorGoal)"
  40. class="sample-wrap"
  41. >
  42. <li v-if="badgeArchCurrent >= badgeArchGoal">
  43. <img
  44. class=""
  45. src="@/assets/images/badge-architecture.png"
  46. alt=""
  47. draggable="false"
  48. >
  49. <span>营造专家</span>
  50. </li>
  51. <li v-if="badgeHistoryCurrent >= badgeHistoryGoal">
  52. <img
  53. class=""
  54. src="@/assets/images/badge-hitstory.png"
  55. alt=""
  56. draggable="false"
  57. >
  58. <span>历史达人</span>
  59. </li>
  60. <li v-if="badgeProtectorCurrent >= badgeProtectorGoal">
  61. <img
  62. class=""
  63. src="@/assets/images/badge-protector.png"
  64. alt=""
  65. draggable="false"
  66. >
  67. <span>护书使者</span>
  68. </li>
  69. </ul>
  70. <div
  71. v-else
  72. class="no-badge"
  73. >
  74. 暂未获得勋章
  75. </div>
  76. <div class="splitter" />
  77. <div class="QRCode">
  78. <img
  79. v-show="qrcodeDataUrl"
  80. class=""
  81. :src="qrcodeDataUrl"
  82. alt=""
  83. draggable="false"
  84. >
  85. <p>{{ isMobile ? `长按` : `扫描` }}二维码</p>
  86. <p>云游西湖文澜阁</p>
  87. </div>
  88. <img
  89. v-if="(badgeArchCurrent >= badgeArchGoal) || (badgeHistoryCurrent >= badgeHistoryGoal) || (badgeProtectorCurrent >= badgeProtectorGoal)"
  90. src="@/assets/images/stamp.png"
  91. alt=""
  92. class="stamp"
  93. >
  94. <button
  95. v-show="!isSaving"
  96. class="save"
  97. @click="onClickSave"
  98. >
  99. <div class="text-wrapper">
  100. 保存图片
  101. </div>
  102. </button>
  103. </div>
  104. <a
  105. v-show="false"
  106. ref="for-download"
  107. :href="aDownloadHref"
  108. download="share.jpg"
  109. />
  110. </div>
  111. </template>
  112. <script>
  113. import html2canvas from "html2canvas"
  114. import QRCode from 'qrcode'
  115. import globalApi from "@/api"
  116. export default {
  117. data() {
  118. return {
  119. answerNumber: undefined,
  120. qrcodeDataUrl: null,
  121. isSaving: false,
  122. aDownloadHref: '',
  123. }
  124. },
  125. computed: {
  126. ...globalMapState([
  127. 'userInfo',
  128. 'loginStatus',
  129. 'badgeArchCurrent',
  130. 'badgeHistoryCurrent',
  131. 'badgeProtectorCurrent',
  132. 'badgeArchGoal',
  133. 'badgeHistoryGoal',
  134. 'badgeProtectorGoal',
  135. ]),
  136. initialWrapperWidth() {
  137. return this.isMobile ? 338 : 804
  138. },
  139. initialWrapperHeight() {
  140. return this.isMobile ? 596 : 793
  141. },
  142. wrapperTransformCss() {
  143. if (this.isMobile) {
  144. const WHRateViewport = window.innerWidth / window.innerHeight
  145. const WHRateComp = this.initialWrapperWidth / this.initialWrapperHeight
  146. if (WHRateViewport >= WHRateComp) { // 视口矮宽
  147. return `scale(${window.innerHeight / this.initialWrapperHeight * 0.9})`
  148. } else {
  149. return `scale(${window.innerWidth / this.initialWrapperWidth * 0.9})`
  150. }
  151. } else {
  152. return ''
  153. }
  154. }
  155. },
  156. mounted() {
  157. globalApi.getAnswerRecord().then((res) => {
  158. this.answerNumber = res.length
  159. })
  160. this.$nextTick(() => {
  161. QRCode.toDataURL(`${location.origin}${process.env.BASE_URL || '/'}index.html#/?m=768`).then((url) => {
  162. this.qrcodeDataUrl = url
  163. })
  164. })
  165. },
  166. methods: {
  167. onClickSave: globalUtils.throttle(function() {
  168. this.isSaving = true
  169. this.$nextTick(() => {
  170. // #capture 就是我们要获取截图对应的 DOM 元素选择器
  171. html2canvas(document.querySelector('#capture'), {
  172. useCORS: true, // 【重要】开启跨域配置
  173. scale: 1,
  174. allowTaint: true, // 允许跨域图片
  175. preserveDrawingBuffer: true,
  176. }).then((canvas) => {
  177. this.aDownloadHref = canvas.toDataURL('image/jpeg', 1.0)
  178. this.$nextTick(() => {
  179. this.$refs['for-download'].click()
  180. this.isSaving = false
  181. })
  182. })
  183. })
  184. }, 400)
  185. }
  186. }
  187. </script>
  188. <style lang="less" scoped>
  189. .share {
  190. position: fixed;
  191. left: 0;
  192. top: 0;
  193. width: 100%;
  194. height: 100%;
  195. background: rgba(46,32,19,0.55);
  196. backdrop-filter: blur(5px);
  197. z-index: 10000;
  198. font-family: Source Han Sans CN-Regular, Source Han Sans CN;
  199. display: flex;
  200. justify-content: center;
  201. align-items: center;
  202. > .wrapper-1 {
  203. position: relative;
  204. background-image: url(@/assets/images/share-bg.png);
  205. background-size: contain;
  206. background-repeat: no-repeat;
  207. background-position: center center;
  208. text-align: left;
  209. padding: 40px 100px 80px 100px;
  210. font-family: Source Han Sans CN-Regular, Source Han Sans CN;
  211. > button.close {
  212. position: absolute;
  213. top: 42px;
  214. right: 36px;
  215. width: 35px;
  216. height: 35px;
  217. background-image: url(@/assets/images/close.png);
  218. background-size: contain;
  219. background-repeat: no-repeat;
  220. background-position: center center;
  221. font-family: Source Han Sans CN-Regular, Source Han Sans CN;
  222. }
  223. > .introduction-title-wrapper {
  224. text-align: center;
  225. > h1.introduction-title {
  226. display: inline-block;
  227. background-image: url(@/assets/images/title-decorator-long.png);
  228. background-size: contain;
  229. background-repeat: no-repeat;
  230. background-position: center center;
  231. padding: 0 45px 15px 45px;
  232. font-size: 36px;
  233. font-family: LiSu-Regular, LiSu;
  234. font-weight: 400;
  235. color: #9A2D0A;
  236. }
  237. }
  238. > p.main {
  239. margin-top: 40px;
  240. font-size: 16px;
  241. font-family: Source Han Sans CN-Bold, Source Han Sans CN;
  242. color: #8F4831;
  243. line-height: 20px;
  244. letter-spacing: 3px;
  245. > .name {
  246. font-size: 36px;
  247. font-family: Source Han Sans CN-Bold, Source Han Sans CN;
  248. font-weight: bold;
  249. color: #8F4831;
  250. line-height: 40px;
  251. letter-spacing: 3px;
  252. text-decoration: underline;
  253. }
  254. }
  255. > p.badge-num {
  256. margin-top: 20px;
  257. text-align: left;
  258. font-size: 16px;
  259. font-family: Source Han Sans CN-Bold, Source Han Sans CN;
  260. color: #8F4831;
  261. line-height: 20px;
  262. letter-spacing: 3px;
  263. }
  264. > ul.sample-wrap {
  265. margin-top: 40px;
  266. display: flex;
  267. justify-content: center;
  268. align-items: center;
  269. font-family: Source Han Sans CN-Regular, Source Han Sans CN;
  270. > li {
  271. text-align: center;
  272. margin-right: 87px;
  273. font-family: Source Han Sans CN-Regular, Source Han Sans CN;
  274. &:last-of-type {
  275. margin-right: initial;
  276. }
  277. > img {
  278. display: block;
  279. width: 120px;
  280. height: 120px;
  281. margin-bottom: 7px;
  282. }
  283. > span {
  284. font-size: 16px;
  285. font-family: Source Han Sans CN-Regular, Source Han Sans CN;
  286. font-weight: 400;
  287. color: #693D2F;
  288. }
  289. }
  290. }
  291. > .no-badge {
  292. width: 100%;
  293. height: 238px;
  294. display: flex;
  295. justify-content: center;
  296. align-items: center;
  297. font-size: 32px;
  298. font-family: Source Han Sans CN-Regular, Source Han Sans CN;
  299. font-weight: 400;
  300. color: #8F4831;
  301. letter-spacing: 3px;
  302. opacity: 0.5;
  303. }
  304. > .splitter {
  305. margin-top: 32px;
  306. width: 100%;
  307. height: 2px;
  308. background: #8F4831;
  309. opacity: 0.5;
  310. font-family: Source Han Sans CN-Regular, Source Han Sans CN;
  311. }
  312. > .QRCode {
  313. margin-top: 22px;
  314. width: fit-content;
  315. font-family: Source Han Sans CN-Regular, Source Han Sans CN;
  316. > img {
  317. width: 143px;
  318. height: 143px;
  319. }
  320. > p {
  321. margin-top: 6px;
  322. width: 100%;
  323. text-align: center;
  324. font-size: 14px;
  325. font-family: Source Han Sans CN-Regular, Source Han Sans CN;
  326. font-weight: 400;
  327. color: #583329;
  328. }
  329. }
  330. > img.stamp {
  331. position: absolute;
  332. width: 300px;
  333. height: 300px;
  334. right: 25px;
  335. bottom: 60px;
  336. }
  337. > button.save {
  338. position: absolute;
  339. left: 50%;
  340. bottom: 75px;
  341. transform: translateX(-50%);
  342. background-image: url(@/assets/images/button-border-small-color.png);
  343. background-size: contain;
  344. background-repeat: no-repeat;
  345. background-position: center center;
  346. width: 149px;
  347. height: 40px;
  348. padding: 3px;
  349. font-size: 20px;
  350. font-family: LiSu-Regular, LiSu;
  351. font-weight: 400;
  352. color: #FFFFFF;
  353. }
  354. }
  355. }
  356. .mobile {
  357. .share {
  358. > .wrapper-1 {
  359. position: relative;
  360. background-image: url(@/assets/images/share-bg-mobile.png);
  361. padding: 40px 24px 20px 35px;
  362. > button.close {
  363. top: 40px;
  364. right: 25px;
  365. width: 21px;
  366. height: 21px;
  367. }
  368. > .introduction-title-wrapper {
  369. > h1.introduction-title {
  370. background-image: url(@/assets/images/title-decorator-mobile.png);
  371. font-size: 24px;
  372. padding: 0 30px 15px 30px;
  373. }
  374. }
  375. > p.main {
  376. margin-top: 20px;
  377. font-size: 12px;
  378. line-height: 3em;
  379. > .name {
  380. font-size: 30px;
  381. }
  382. }
  383. > ul.sample-wrap {
  384. margin-top: 21px;
  385. > li {
  386. margin-right: 8px;
  387. &:last-of-type {
  388. }
  389. > img {
  390. width: 85px;
  391. height: 85px;
  392. margin-bottom: 6px;
  393. }
  394. > span {
  395. font-size: 14px;
  396. }
  397. }
  398. }
  399. > .no-badge {
  400. height: 130px;
  401. font-size: 20px;
  402. letter-spacing: 2px;
  403. }
  404. > .splitter {
  405. margin-top: 34px;
  406. height: 1px;
  407. }
  408. > .QRCode {
  409. margin-top: 22px;
  410. > img {
  411. width: 100px;
  412. height: 100px;
  413. margin-bottom: 5px;
  414. }
  415. > p {
  416. margin-top: 0px;
  417. font-size: 12px;
  418. }
  419. }
  420. > img.stamp {
  421. width: 200px;
  422. height: 200px;
  423. right: -10px;
  424. bottom: 40px;
  425. }
  426. > button.save {
  427. bottom: 30px;
  428. width: 116px;
  429. height: 31px;
  430. font-size: 16px;
  431. }
  432. }
  433. }
  434. }
  435. </style>