call-modal.component.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { Component, Input, OnInit } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import {
  4. AlertController,
  5. ToastController,
  6. } from '../../../modules/ionic-standalone.modules';
  7. import { AiChatService } from '../../../services/aichart.service';
  8. import { ConnectTaskService } from '../../../services/connectTask.service';
  9. import { MessageService } from '../../../services/message.service';
  10. import * as Parse from 'parse';
  11. import { Subject } from 'rxjs';
  12. import { takeUntil } from 'rxjs/operators';
  13. @Component({
  14. selector: 'app-call-modal',
  15. templateUrl: './call-modal.component.html',
  16. styleUrls: ['./call-modal.component.scss'],
  17. standalone: true,
  18. imports: [],
  19. })
  20. export class CallModalComponent implements OnInit {
  21. @Input('profile') profile?: Parse.Object;
  22. @Input('userStatus') userStatus: string = 'OFFLINE';
  23. room?: Parse.Object;
  24. uid?: string; //主播频道&uid
  25. currentUser?: Parse.Object = Parse.User.current(); //当前登录用户
  26. iscall: boolean = false;
  27. isLiveing: boolean = false; // 是否在直播通话中
  28. private ngUnsubscribe = new Subject<void>(); // 用于取消订阅
  29. constructor(
  30. private router: Router,
  31. private toastController: ToastController,
  32. private alertController: AlertController,
  33. private connectTask: ConnectTaskService,
  34. private msgSer: MessageService,
  35. private aiChatServ: AiChatService
  36. ) {
  37. this.msgSer.event$
  38. .pipe(takeUntil(this.ngUnsubscribe)) // 使用 takeUntil 取消订阅
  39. .subscribe((data) => {
  40. this.inviteCallback(data);
  41. });
  42. }
  43. ngOnInit() {
  44. this.uid = this.profile?.get('user').id;
  45. this.refresh();
  46. }
  47. ngOnDestroy(): void {
  48. this.ngUnsubscribe.next();
  49. this.ngUnsubscribe.complete();
  50. if (!this.isLiveing && this.uid !== this.currentUser?.id) {
  51. console.log('断开连接');
  52. this.msgSer?.unsubscribeMessage(this.uid!);
  53. }
  54. }
  55. refresh() {
  56. this.getRoom();
  57. }
  58. async getRoom() {
  59. let query = new Parse.Query('Room');
  60. query.equalTo('profile', this.profile?.id);
  61. query.notEqualTo('isDeleted', true);
  62. this.room = await query.first();
  63. // console.log(this.room);
  64. if (!this.room?.id) return;
  65. this.userStatus = await this.connectTask.getState(
  66. this.profile?.get('user').id,
  67. this.profile?.get('user').id
  68. );
  69. if (this.profile?.get('isCheck') && this.userStatus == 'ONLINE') {
  70. this.userStatus = 'REFUSE';
  71. }
  72. }
  73. async toLiveContact() {
  74. if (this.userStatus == 'REFUSE') {
  75. const toast = await this.toastController.create({
  76. message: '对方已设置免打扰状态',
  77. color: 'warning',
  78. duration: 1500,
  79. });
  80. toast.present();
  81. return;
  82. }
  83. this.userStatus = await this.connectTask.getState(this.uid!, this.uid!);
  84. if (this.userStatus !== 'ONLINE') {
  85. const toast = await this.toastController.create({
  86. message: '对方不在线或忙线中',
  87. color: 'warning',
  88. duration: 1500,
  89. });
  90. toast.present();
  91. return;
  92. }
  93. const alert = await this.alertController.create({
  94. cssClass: 'my-custom-class',
  95. header: '邀请通话',
  96. backdropDismiss: false,
  97. message: '你将与对方发起私聊通话',
  98. buttons: [
  99. {
  100. text: '取消',
  101. role: 'cancel',
  102. handler: (blah) => {},
  103. },
  104. {
  105. text: '确定',
  106. cssClass: 'secondary',
  107. handler: () => {
  108. this.sendVideoCallInvite();
  109. },
  110. },
  111. ],
  112. });
  113. await alert.present();
  114. }
  115. timer: any; // 定时器
  116. inviteCallback(event: boolean) {
  117. console.log(event);
  118. if (event == undefined) return;
  119. this.timer && clearTimeout(this.timer);
  120. this.timer = setTimeout(async () => {
  121. this.iscall = false;
  122. const toast = await this.toastController.create({
  123. message: `对方${event ? '已接受' : '拒绝'}通话邀请`,
  124. color: event ? 'success' : 'warning',
  125. duration: 1500,
  126. });
  127. toast.present();
  128. if (event) {
  129. this.isLiveing = true;
  130. let path = location.pathname;
  131. if (path.indexOf('/live/link-room/') != -1) return;
  132. this.router.navigate(['live/link-room/' + this.room?.id]);
  133. }
  134. }, 100);
  135. }
  136. async sendVideoCallInvite() {
  137. let second = await this.aiChatServ.get_duration(
  138. this.room?.id!,
  139. this.currentUser?.id!
  140. );
  141. if (second < 120) {
  142. const alert = await this.alertController.create({
  143. cssClass: 'my-custom-class',
  144. header: '钻石余额不足',
  145. backdropDismiss: false,
  146. message: '通话时长不足2分钟,请充值后再试通',
  147. buttons: [
  148. {
  149. text: '取消',
  150. role: 'cancel',
  151. handler: (blah) => {},
  152. },
  153. {
  154. text: '去充值',
  155. cssClass: 'secondary',
  156. handler: () => {
  157. this.router.navigate(['/account/recharge']);
  158. },
  159. },
  160. ],
  161. });
  162. await alert.present();
  163. // const toast = await this.toastController.create({
  164. // message: '通话时长不足2分钟,请充值后再试通',
  165. // color: 'warning',
  166. // duration: 1500,
  167. // });
  168. // toast.present();
  169. return;
  170. }
  171. this.iscall = true;
  172. // this.router.navigate(['live/link-room/' + this.room?.id]);
  173. await this.msgSer.subscribeMessage(this.uid!, {
  174. message: true,
  175. presence: true,
  176. }); //进入对方主播频道发送聊天邀请
  177. this.msgSer.publishMessage('USERCALLINVITATION', this.uid!);
  178. }
  179. async onCloseCall() {
  180. this.timer && clearTimeout(this.timer);
  181. this.iscall = false;
  182. const toast = await this.toastController.create({
  183. message: '已取消视频通话邀请',
  184. color: 'warning',
  185. duration: 1500,
  186. });
  187. toast.present();
  188. this.msgSer.publishMessage('CLOASEINVITATION', this.uid!);
  189. }
  190. }