share.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <div class="share">
  3. <div class="imgBox" v-if="isOk">
  4. <img :src="isOk" alt="" />
  5. <p>{{ isMobile ? "可长按图片保存" : "可点击鼠标右键保存图片" }}</p>
  6. </div>
  7. <div class="main" id="canvsBox" v-else>
  8. <!-- 数字 -->
  9. <div class="txt">
  10. 第&nbsp;&nbsp;<span>{{ num }}</span
  11. >&nbsp;&nbsp;位参与者
  12. </div>
  13. <!-- 二维码 -->
  14. <div class="code">
  15. <img src="code.png" alt="" />
  16. </div>
  17. </div>
  18. <!-- 加载中 -->
  19. <div
  20. class="logingBox"
  21. v-loading="!isOk"
  22. element-loading-text="生成海报中"
  23. element-loading-background="rgba(0, 0, 0, 0.8)"
  24. ></div>
  25. <div class="close" @click="$emit('close')">
  26. <img src="../../../assets/img/close.png" alt="" />
  27. </div>
  28. </div>
  29. </template>
  30. <script>
  31. import html2canvas from "html2canvas";
  32. import { getVisitAPI } from "../../../utils/api";
  33. export default {
  34. components: {},
  35. props: ["openInd"],
  36. data() {
  37. return {
  38. num: 0,
  39. // 生成截图成功
  40. isOk: "",
  41. };
  42. },
  43. computed: {},
  44. watch: {
  45. openInd(val) {
  46. if (val) {
  47. if (!this.isOk) this.toImgFu();
  48. }
  49. },
  50. },
  51. methods: {
  52. // 生成截图
  53. toImgFu() {
  54. window.setTimeout(() => {
  55. const dom = document.querySelector("#canvsBox");
  56. html2canvas(dom, {
  57. // backgroundColor: "transparent",
  58. allowTaint: true,
  59. useCORS: true,
  60. }).then((canvas) => {
  61. const link = canvas.toDataURL("image/jpeg");
  62. this.isOk = link;
  63. });
  64. }, 100);
  65. },
  66. },
  67. async created() {
  68. try {
  69. const res = await getVisitAPI();
  70. this.num = res.data.pcsShow + res.data.pcsVisit;
  71. } catch (e) {
  72. console.log(e);
  73. }
  74. },
  75. mounted() {},
  76. beforeCreate() {}, //生命周期 - 创建之前
  77. beforeMount() {}, //生命周期 - 挂载之前
  78. beforeUpdate() {}, //生命周期 - 更新之前
  79. updated() {}, //生命周期 - 更新之后
  80. beforeDestroy() {}, //生命周期 - 销毁之前
  81. destroyed() {}, //生命周期 - 销毁完成
  82. activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
  83. };
  84. </script>
  85. <style lang='less' scoped>
  86. .share {
  87. position: absolute;
  88. top: 50%;
  89. left: 50%;
  90. transform: translate(-50%, -50%);
  91. width: 542px;
  92. height: 820px;
  93. .imgBox {
  94. width: 100%;
  95. height: 100%;
  96. position: relative;
  97. & > img {
  98. width: 100%;
  99. height: 100%;
  100. }
  101. & > p {
  102. position: absolute;
  103. bottom: 10px;
  104. right: 34px;
  105. bottom: 20px;
  106. width: 80px;
  107. font-size: 12px;
  108. text-align: center;
  109. }
  110. }
  111. .logingBox {
  112. position: absolute;
  113. top: 0;
  114. left: 0;
  115. width: 100%;
  116. height: 100%;
  117. z-index: 99;
  118. }
  119. .main {
  120. width: 100%;
  121. height: 100%;
  122. background-image: url("../../../assets/img/open/bg-share.jpg");
  123. background-size: 100% 100%;
  124. position: relative;
  125. .txt {
  126. position: absolute;
  127. bottom: 70px;
  128. left: 34px;
  129. color: #d6b970;
  130. & > span {
  131. font-size: 36px;
  132. font-weight: 700;
  133. }
  134. }
  135. .code {
  136. position: absolute;
  137. bottom: 60px;
  138. right: 34px;
  139. width: 80px;
  140. & > img {
  141. border-radius: 6px;
  142. width: 80px;
  143. }
  144. }
  145. }
  146. .close {
  147. z-index: 99;
  148. position: absolute;
  149. right: -40px;
  150. top: 10px;
  151. cursor: pointer;
  152. }
  153. }
  154. // 移动端
  155. @media screen and (max-width: 1000px) {
  156. .share {
  157. width: 90%;
  158. height: 90%;
  159. .imgBox {
  160. & > p {
  161. font-size: 12px;
  162. bottom: 10px;
  163. width: 84px;
  164. right: 14px;
  165. }
  166. }
  167. .main {
  168. background-image: url("../../../assets/img/open/bg-shareM.jpg");
  169. background-size: 100% 100%;
  170. .txt {
  171. bottom: 30px;
  172. left: 16px;
  173. & > span {
  174. font-size: 30px;
  175. }
  176. }
  177. .code {
  178. width: 84px;
  179. bottom: 30px;
  180. right: 14px;
  181. & > img {
  182. width: 84px;
  183. }
  184. }
  185. }
  186. .close {
  187. position: absolute;
  188. right: -14px;
  189. top: -14px;
  190. cursor: pointer;
  191. }
  192. }
  193. }
  194. </style>