share.component.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { HttpClient } from '@angular/common/http';
  2. import { Component, Input, OnInit } from '@angular/core';
  3. import { ToastController } from '@ionic/angular';
  4. import { NavComponent } from '../../../app/components/nav/nav.component';
  5. import * as Parse from 'parse';
  6. import { ionicStandaloneModules } from '../../ionic-standalone.modules';
  7. @Component({
  8. selector: 'app-share',
  9. templateUrl: './share.component.html',
  10. styleUrls: ['./share.component.scss'],
  11. standalone: true,
  12. imports: [...ionicStandaloneModules, NavComponent],
  13. })
  14. export class ShareComponent implements OnInit {
  15. @Input() path: string = '/chat/home';
  16. imgUrl: string = '';
  17. //海报
  18. bannerUrl: string =
  19. 'https://file-cloud.fmode.cn/Qje9D4bqol/20241220/qvj1bm054428527.png';
  20. codeUrl: string = '';
  21. constructor(
  22. private toastController: ToastController,
  23. private http: HttpClient
  24. ) {}
  25. ngOnInit() {
  26. this.getCode()
  27. }
  28. ngOnChanges() {
  29. this.getCode();
  30. }
  31. getCode() {
  32. console.log('getcode');
  33. let uid = Parse.User.current()?.id;
  34. let qrCodeUrl = `https://chat.gdchat.cn?invite=${uid}`;
  35. let params = {
  36. qrCode: qrCodeUrl,
  37. darkColor: '#ffffff',
  38. lightColor: '#000000',
  39. };
  40. this.http
  41. .get('https://server.fmode.cn/api/common/qrcode', { params })
  42. .subscribe((res: any) => {
  43. // console.log(res);
  44. this.codeUrl = res.data;
  45. this.drawPoster();
  46. });
  47. }
  48. //绘画海报
  49. async drawPoster() {
  50. let canvas: any = document.createElement('canvas');
  51. canvas.height = '480';
  52. canvas.width = '300';
  53. let ctx = canvas.getContext('2d');
  54. //定义图片
  55. ctx.drawImage(await this.compileImage(this.bannerUrl), 0, 0, 340, 540);
  56. //绘制二维码
  57. ctx.drawImage(await this.compileImage(this.codeUrl), 80, 150, 148, 148);
  58. let tempSrc = canvas.toDataURL('image/png');
  59. this.imgUrl = tempSrc;
  60. document.body.removeChild(canvas);
  61. }
  62. compileImage(url: string): Promise<any> {
  63. return new Promise((res) => {
  64. let img = new Image();
  65. img.src = url;
  66. img.setAttribute('crossOrigin', 'anonymous');
  67. img.onload = function () {
  68. res(img);
  69. };
  70. });
  71. }
  72. /**
  73. * 返回当前元素的文本内容
  74. */
  75. async copyText() {
  76. let uid = Parse.User.current()?.id;
  77. let text = `https://chat.gdchat.cn?invite=${uid}`;
  78. // let oInput = document.createElement("input");
  79. // oInput.value = text;
  80. // document.body.appendChild(oInput);
  81. // oInput.select(); // 选择对象
  82. // oInput.setAttribute("readonly", "readonly"); //设置只读属性防止手机上弹出软键盘
  83. // document.execCommand("Copy"); // 执行浏览器复制命令
  84. // oInput.style.display = "none";
  85. // document.body.removeChild(oInput); //移除DOM元素
  86. navigator.clipboard.writeText(text);
  87. console.log(text);
  88. console.log('复制成功');
  89. const toast = await this.toastController.create({
  90. message: '复制成功',
  91. color: 'success',
  92. duration: 1000,
  93. });
  94. toast.present();
  95. window.location.href = 'weixin://';
  96. }
  97. /* 下载海报 */
  98. downImg() {
  99. let dlLink: any = document.createElement('a');
  100. if ('download' in dlLink) {
  101. dlLink.style.visibility = 'hidden';
  102. dlLink.href = this.imgUrl;
  103. dlLink.download = '分享海报';
  104. document.body.appendChild(dlLink);
  105. dlLink.click();
  106. document.body.removeChild(dlLink);
  107. } else {
  108. location.href = this.imgUrl;
  109. }
  110. }
  111. }