com-share-pop.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <!-- 分享二维码,链接,海报弹窗 -->
  2. <style>
  3. .canvas-div {
  4. position: fixed;
  5. left: -1000000px;
  6. top: -10000000px;
  7. }
  8. .share-name {
  9. width: 70px;
  10. display: inline-block;
  11. margin-right: 18px;
  12. text-align: end;
  13. }
  14. .share-pop-model {
  15. margin-top: 20px;
  16. }
  17. </style>
  18. <template id="com-share-pop">
  19. <div class="com-share-pop">
  20. <div id="qrcode-info" class="canvas-div">
  21. <!-- 默认海报 -->
  22. <canvas id="myDefCanvas" width="360" height="460" style="border:1px solid #c3c3c3;">
  23. <!-- 二维码 -->
  24. <div id="qrcode-pop" style="width:100px; height:100px; margin-top:15px;"></div>
  25. </div>
  26. <slot name="share-button" :show="showPop"></slot>
  27. <el-dialog title="分享" :visible.sync="show" :append-to-body="true" width="30%">
  28. <div v-if="showShareLink">
  29. <div class="share-name">分享链接</div>
  30. <el-input style="width: 50%;" v-model="url" placeholder="暂无链接" disabled>
  31. <template slot="append">
  32. <el-button circle size="mini" type="text" @click="copyLink(url)" class="copy_btn">复制</el-button>
  33. </template>
  34. </el-input>
  35. </div>
  36. <div style="padding-top: 10px" class="share-pop-model" flex>
  37. <div class="share-name">二维码</div>
  38. <div style="width: 180px; height: 180px;background: #F2F2F2;margin-right: 12px;" flex="main:center cross:center">
  39. <el-image id="code-img" style="width: 144px; height: 144px;" :src="qrCodeImg"></el-image>
  40. </div>
  41. <el-button flex type="text" v-if="showPoster" @click="getRechargeCode">下载海报</el-button>
  42. <el-button flex type="text" @click="downloadShareImg('qrcode')">下载二维码</el-button>
  43. </div>
  44. </el-dialog>
  45. </div>
  46. </template>
  47. <script>
  48. Vue.component('com-share-pop', {
  49. template: '#com-share-pop',
  50. props: {
  51. url: {
  52. type: String,
  53. default: "",
  54. },
  55. showPoster: {
  56. type: Boolean,
  57. default: false,
  58. },
  59. posterTilte: {
  60. type: String,
  61. default: '',
  62. },
  63. showShareLink: {
  64. type: Boolean,
  65. default: true,
  66. },
  67. },
  68. data() {
  69. return {
  70. qrCodeImg: "",
  71. show: false,
  72. }
  73. },
  74. created() {},
  75. methods: {
  76. async getData() {
  77. if (!this.url) return;
  78. this.qrCodeImg = await this.getQrCode(this.url);
  79. },
  80. copyLink(str) {
  81. var clipboard = new ClipboardJS('.copy_btn', {
  82. text: function() {
  83. return str;
  84. }
  85. });
  86. var self = this;
  87. clipboard.on('success', function(e) {
  88. // console.log("success")
  89. self.$message.success('复制成功');
  90. e.clearSelection();
  91. clipboard.destroy()
  92. });
  93. clipboard.on('error', function(e) {
  94. // console.log("error")
  95. self.$message.error('复制失败,请手动复制');
  96. });
  97. },
  98. // 默认海报生成
  99. getRechargeCode() {
  100. if (!this.url) return;
  101. var c = document.getElementById("myDefCanvas");
  102. var ctx = c.getContext("2d");
  103. ctx.fillStyle = "#1989FA";
  104. ctx.fillRect(0, 0, 360, 460);
  105. ctx.font = "40px Arial";
  106. ctx.fillStyle = "#FFF";
  107. ctx.fillText(this.posterTilte, 100, 124);
  108. ctx.fillStyle = "#FFF";
  109. ctx.fillRect(70, 156, 220, 220);
  110. let img = document.getElementById("code-img")
  111. ctx.drawImage(img, 78, 164, 204, 204);
  112. var a = document.getElementById("myPoster");
  113. let srcInfo = c.toDataURL('image/png');
  114. this.downloadShareImg("poster", srcInfo);
  115. },
  116. // 生成二维码
  117. getQrCode(url) {
  118. console.log(111111);
  119. let qrcodeInfo = document.getElementById("qrcode-info");
  120. document.getElementById("qrcode-pop").remove(); //删除生成的二维码
  121. let addCodeDom = document.createElement("div")
  122. addCodeDom.setAttribute("id", "qrcode-pop");
  123. addCodeDom.setAttribute("style", "width:300px; height:300px;");
  124. qrcodeInfo.appendChild(addCodeDom) //添加标签
  125. let qrCodeDom = document.getElementById("qrcode-pop")
  126. var qrcode = new QRCode(qrCodeDom, {
  127. width: 300,
  128. height: 300
  129. });
  130. let urlObject = new URL(url);
  131. let encodedUrl = encodeURI(urlObject.href);
  132. qrcode.makeCode(encodedUrl);
  133. // 获取二维码生成的图片信息
  134. let src = "";
  135. return new Promise((req, rej) => {
  136. setTimeout(() => {
  137. src = qrCodeDom.getElementsByTagName("img")[0].src;
  138. req(src)
  139. }, 200);
  140. })
  141. },
  142. // 图片下载
  143. downloadShareImg(type, img) {
  144. let img_url = img;
  145. if ('qrcode' == type) {
  146. if (!this.url) return;
  147. img_url = this.qrCodeImg;
  148. }
  149. fetch(img_url).then(res => res.blob().then(blob => {
  150. var a = document.createElement('a');
  151. var url = window.URL.createObjectURL(blob);
  152. a.href = url;
  153. a.download = "";
  154. a.click();
  155. window.URL.revokeObjectURL(url);
  156. }))
  157. },
  158. showPop() {
  159. console.log(this.showShareLink, 'showShareLink ==================');
  160. if (!this.url) {
  161. this.$message({
  162. message: '暂无分享链接',
  163. type: 'warning'
  164. });
  165. return;
  166. }
  167. this.show = true;
  168. let that = this;
  169. this.$nextTick(v => {
  170. that.getData();
  171. })
  172. console.info("test");
  173. }
  174. }
  175. });
  176. </script>