share.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <template>
  2. <el-dialog title="活动分享" width="500px" :visible.sync="visible" destroy-on-close>
  3. <template #title>
  4. <div class="modal-title">
  5. <span role="img" aria-label="share">✨</span>
  6. <span>活动分享</span>
  7. </div>
  8. </template>
  9. <div class="share-content">
  10. <div class="share-card">
  11. <div class="card-header">
  12. <div class="header-icon wechat-icon">
  13. <i class="el-icon-link"></i>
  14. </div>
  15. <span class="header-title">小程序链接分享</span>
  16. </div>
  17. <div class="card-body">
  18. <div class="link-input-wrapper">
  19. <el-input v-model="wechatPath" readonly>
  20. <i slot="suffix" class="el-icon-link action-icon copy-icon" :data-clipboard-text="wechatPath"></i>
  21. </el-input>
  22. <div id="share-qrcode" v-show="false"></div>
  23. </div>
  24. <div class="download-row">
  25. <el-link icon="el-icon-download" type="primary" @click="onDownload('miniCode')">下载小程序码</el-link>
  26. <el-link icon="el-icon-picture-outline" type="primary" @click="onDownload('poster')">下载活动海报</el-link>
  27. </div>
  28. </div>
  29. </div>
  30. <CompPreview ref="targetDom" :form-data="formData" />
  31. </div>
  32. </el-dialog>
  33. </template>
  34. <script>
  35. import QRCode from 'qrcodejs2';
  36. import html2canvas from 'html2canvas';
  37. import CompPreview from './CompPreview.vue';
  38. import {
  39. queryById,
  40. getWechatGroupFissionUrl
  41. } from '@/api/activity/index';
  42. export default {
  43. name: '',
  44. components: {
  45. CompPreview
  46. },
  47. data() {
  48. return {
  49. qrcode: null,
  50. clipboard: null,
  51. visible: false,
  52. wechatPath: '获取中...',
  53. formData: {
  54. timeRange: []
  55. }
  56. };
  57. },
  58. mounted() {
  59. // 复制功能
  60. this.clipboard = new this.ClipboardJS('.copy-icon');
  61. },
  62. destroyed() {
  63. this.clipboard && this.clipboard.destroy();
  64. },
  65. methods: {
  66. async show(record) {
  67. this.visible = true;
  68. if (record && record.id) {
  69. await this.getQRCode(record);
  70. this.getData(record);
  71. }
  72. },
  73. // 初始数据
  74. getData(record) {
  75. // 加载详情
  76. queryById({ id: record.id }).then(res => {
  77. if (res.code === 200) {
  78. this.formData = res.data;
  79. // 时间
  80. if (res.data.beginDate && res.data.endDate) {
  81. this.formData.timeRange = [res.data.beginDate, res.data.endDate];
  82. }
  83. // 阶梯数组
  84. if (res.data.weGroupFissionStepList) {
  85. this.formData.inviteTiers = res.data.weGroupFissionStepList.map((item) => ({
  86. reward: item.rewardAmount,
  87. count: item.invitationCount,
  88. }));
  89. }
  90. // 额外奖励数组
  91. if (res.data.weGroupFissionExtraList) {
  92. this.formData.inviteExtras = res.data.weGroupFissionExtraList.map((item) => {
  93. let nd = {
  94. tierIndex: item.sortNum - 1,
  95. rewardType: item.rewardType,
  96. rewardValue: undefined
  97. };
  98. if (item.rewardType == 1) {
  99. nd.rewardValue = item.rewardAmount;
  100. } else if (item.rewardType == 2) {
  101. nd.rewardName = item.rightInfoName;
  102. nd.rewardValue = item.rightInfoId;
  103. } else if (item.rewardType == 3) {
  104. nd.rewardName = item.lotteryActivityName;
  105. nd.rewardValue = item.lotteryActivityId;
  106. }
  107. return nd;
  108. });
  109. }
  110. this.$refs.targetDom.getQRCode(this.wechatPath);
  111. }
  112. });
  113. },
  114. // 初始化二维码
  115. async getQRCode(record) {
  116. await getWechatGroupFissionUrl({
  117. url: 'packageF/pages/activity/weGroupFission/poster',
  118. version: 'release',
  119. groupFissionId: record.id,
  120. }).then(res => {
  121. if (res.code === 200) {
  122. this.wechatPath = res.data;
  123. }
  124. });
  125. this.qrcode = new QRCode('share-qrcode', {
  126. text: this.wechatPath,
  127. width: 200,
  128. height: 200,
  129. colorDark: '#000000', // 二维码前景色
  130. colorLight: '#ffffff', // 二维码背景色
  131. correctLevel: QRCode.CorrectLevel.H // 容错级别:L/M/Q/H(H最高)
  132. });
  133. },
  134. // 下载功能
  135. async onDownload(type) {
  136. if (type === 'miniCode') {
  137. const qrImg = document.querySelector('#share-qrcode img') || document.querySelector('#share-qrcode canvas');
  138. if (!qrImg) {
  139. this.$message.error('二维码生成失败!');
  140. return;
  141. }
  142. // 创建下载链接
  143. const a = document.createElement('a');
  144. a.download = '小程序码-' + new Date().getTime() + '.png'; // 下载文件名
  145. // 处理 Canvas 或 Img 转 DataURL
  146. if (qrImg.tagName === 'CANVAS') {
  147. a.href = qrImg.toDataURL('image/png');
  148. } else {
  149. a.href = qrImg.src;
  150. }
  151. // 触发下载
  152. document.body.appendChild(a);
  153. a.click();
  154. document.body.removeChild(a);
  155. } else {
  156. try {
  157. // 1. 获取目标 DOM 元素
  158. const targetElement = this.$refs.targetDom.$el;
  159. if (!targetElement) {
  160. this.$message.error('未找到要截图的DOM元素!');
  161. return;
  162. }
  163. // 2. 配置 html2canvas 参数(关键:解决样式、跨域等问题)
  164. const canvas = await html2canvas(targetElement, {
  165. useCORS: true, // 允许跨域图片(必开,否则跨域图片会空白)
  166. allowTaint: true, // 允许污染画布
  167. scale: 2, // 缩放比例(2倍生成高清图,避免模糊)
  168. logging: false, // 关闭控制台日志
  169. backgroundColor: '#ffffff', // 背景色(默认透明,建议设置)
  170. // 可选:指定截图区域(x/y 偏移,width/height 尺寸)
  171. // x: 0,
  172. // y: 0,
  173. // width: targetElement.offsetWidth,
  174. // height: targetElement.offsetHeight
  175. });
  176. // 3. 转换 Canvas 为 Blob(比 Base64 更适合大文件)
  177. canvas.toBlob((blob) => {
  178. // 4. 创建下载链接
  179. const url = URL.createObjectURL(blob);
  180. const a = document.createElement('a');
  181. a.href = url;
  182. // 自定义下载文件名(时间戳避免重复)
  183. a.download = `活动海报-${new Date().getTime()}.png`;
  184. // 触发下载
  185. document.body.appendChild(a);
  186. a.click();
  187. // 5. 清理资源
  188. document.body.removeChild(a);
  189. URL.revokeObjectURL(url);
  190. this.$message.success('图片生成并下载成功!');
  191. }, 'image/png'); // 指定图片格式为 PNG
  192. } catch (error) {
  193. console.error('生成图片失败:', error);
  194. this.$message.error('图片生成失败,请重试!');
  195. }
  196. }
  197. },
  198. }
  199. }
  200. </script>
  201. <style lang="scss" scoped>
  202. .modal-title {
  203. font-size: 18px;
  204. font-weight: 600;
  205. color: #1f1f1f;
  206. display: flex;
  207. align-items: center;
  208. gap: 8px;
  209. }
  210. .share-content {
  211. .share-card {
  212. padding: 20px;
  213. background: #f8faff;
  214. border-radius: 12px;
  215. border: 1px solid #eff4f9;
  216. transition: all 0.3s;
  217. &:hover {
  218. border-color: #d9e4ff;
  219. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  220. }
  221. .card-header {
  222. display: flex;
  223. align-items: center;
  224. margin-bottom: 16px;
  225. .header-icon {
  226. width: 28px;
  227. height: 28px;
  228. display: flex;
  229. align-items: center;
  230. justify-content: center;
  231. border-radius: 6px;
  232. margin-right: 10px;
  233. font-size: 16px;
  234. &.wechat-icon {
  235. background: #e7f8eb;
  236. color: #07c160;
  237. }
  238. &.h5-icon {
  239. background: #e6f7ff;
  240. color: #1890ff;
  241. }
  242. }
  243. .header-title {
  244. font-size: 15px;
  245. font-weight: 600;
  246. color: #333;
  247. }
  248. }
  249. .card-body {
  250. .link-input-wrapper {
  251. margin-bottom: 12px;
  252. .action-icon {
  253. font-size: 22px;
  254. cursor: pointer;
  255. transition: all 0.2s;
  256. color: #1890ff;
  257. padding: 4px;
  258. border-radius: 4px;
  259. &:hover {
  260. color: #40a9ff;
  261. }
  262. &.copy-icon:active {
  263. color: #096dd9;
  264. }
  265. }
  266. }
  267. .download-row {
  268. display: flex;
  269. gap: 20px;
  270. }
  271. }
  272. }
  273. }
  274. .poster-preview {
  275. border: 0;
  276. border-radius: 0;
  277. position: fixed;
  278. top: -2000px;
  279. }
  280. </style>