com-collection-code.php 6.1 KB

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