com-qrcode.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <template id="com-qrcode">
  2. <div class="qrcode" :style="{'width':width, 'height':height}" @mouseenter="onMouseOver()" @mouseleave="onMouseOut()">
  3. <canvas :id="canvasId" :style="{'width':width, 'height':height}"></canvas>
  4. <transition name="fade">
  5. <div v-show="isShow" class="qrcode-options">
  6. <span @click="download">下载</span>
  7. <span @click="copyContent(copyInfo ? copyInfo : content)" class="qrcode-copy">复制</span>
  8. </div>
  9. </transition>
  10. </div>
  11. </template>
  12. <script src="<?= Yii::$app->request->baseUrl ?>/resources/js/lan.qrcode.js"></script>
  13. <script src="/resources/unpkg/clipboard@1.5.12/dist/clipboard.min.js"></script>
  14. <script>
  15. Vue.component("com-qrcode", {
  16. template: '#com-qrcode',
  17. props: {
  18. content: {
  19. type: String,
  20. default: function() {
  21. return 'this is a qrcode'
  22. }
  23. },
  24. width: {
  25. type: String,
  26. default: function() {
  27. return '160'
  28. }
  29. },
  30. height: {
  31. type: String,
  32. default: function() {
  33. return '160'
  34. }
  35. },
  36. downloadParam: { // 下载的参数
  37. type: Object,
  38. default () {
  39. return {
  40. width: 300,
  41. height: 300,
  42. name: "lan-qr-code" // 下载之后的文件名
  43. };
  44. }
  45. },
  46. copyInfo: {
  47. type: String,
  48. default: function() {
  49. return ''
  50. }
  51. },
  52. },
  53. data() {
  54. return {
  55. canvasId: "",
  56. isShow: false,
  57. }
  58. },
  59. created() {
  60. this.canvasId = this.getUUID();
  61. this.$nextTick(() => {
  62. this.init();
  63. });
  64. },
  65. methods: {
  66. init() {
  67. let width = this.width,
  68. height = this.height;
  69. LanQRCode.toCanvas(
  70. document.getElementById(this.canvasId),
  71. this.content, {
  72. width,
  73. height,
  74. toSJISFunc: LanQRCode.toSJIS
  75. },
  76. error => {}
  77. );
  78. },
  79. getUUID() {
  80. let d = new Date().getTime();
  81. let uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
  82. /[xy]/g,
  83. function(c) {
  84. let r = (d + Math.random() * 16) % 16 | 0;
  85. d = Math.floor(d / 16);
  86. return (c == "x" ? r : (r & 0x7) | 0x8).toString(16);
  87. }
  88. );
  89. return uuid;
  90. },
  91. // 下载二维码
  92. download() {
  93. const ele = document.createElement("canvas");
  94. ele.style.width = this.downloadParam.width || "300" + "px";
  95. ele.style.height = this.downloadParam.height || "300" + "px";
  96. LanQRCode.toCanvas(
  97. ele,
  98. this.content, {
  99. width: this.downloadParam.width || "300",
  100. height: this.downloadParam.height || "300",
  101. toSJISFunc: LanQRCode.toSJIS
  102. },
  103. error => {}
  104. );
  105. let [F, S, a] = [
  106. navigator.userAgent.indexOf("Firefox") > -1,
  107. ele.toDataURL("image/png"),
  108. document.createElement("a")
  109. ];
  110. [a.href, a.download] = [S, this.downloadParam.name];
  111. if (F) {
  112. let evt = document.createEvent("MouseEvents");
  113. evt.initEvent("click", true, true);
  114. a.dispatchEvent(evt);
  115. } else {
  116. a.click();
  117. }
  118. },
  119. // 鼠标移入
  120. onMouseOver() {
  121. this.isShow = true
  122. },
  123. // 鼠标移出
  124. onMouseOut() {
  125. this.isShow = false
  126. },
  127. copyContent(data) { //复制链接
  128. var clipboard = new ClipboardJS('.qrcode-copy', {
  129. text: function() {
  130. return data;
  131. }
  132. });
  133. clipboard.on('success', (e) => {
  134. this.$message.success('复制成功!');
  135. clipboard.destroy()
  136. });
  137. clipboard.on('error', (e) => {
  138. this.$message.error('复制失败!');
  139. this.clipboard.destroy()
  140. });
  141. },
  142. },
  143. watch: {
  144. content(val) {
  145. this.$nextTick(() => {
  146. this.init();
  147. });
  148. }
  149. }
  150. })
  151. </script>
  152. <style lang="scss" scoped>
  153. .qrcode {
  154. display: inline-block;
  155. position: relative;
  156. overflow: hidden;
  157. border: 1px solid black;
  158. }
  159. .qrcode .qrcode-options {
  160. display: flex;
  161. justify-content: center;
  162. width: 100%;
  163. text-align: center;
  164. font-size: 13px;
  165. line-height: 30px;
  166. height: 30px;
  167. color: #FFFFFF;
  168. }
  169. .qrcode .qrcode-options span {
  170. width: 50%;
  171. cursor: pointer;
  172. background-color: #DCDFE6;
  173. }
  174. .qrcode .qrcode-options span:nth-of-type(1) {
  175. margin-right: 1px;
  176. }
  177. .qrcode .qrcode-options span:nth-of-type(2) {
  178. margin-left: 1px;
  179. }
  180. .fade-enter-active,
  181. .fade-leave-active {
  182. transition: .75s ease;
  183. }
  184. .fade-enter,
  185. .fade-leave-to {
  186. transform: translateY(30px)
  187. }
  188. .fade-enter-to,
  189. .fade-leave {
  190. transform: translateX(0);
  191. }
  192. </style>