gift-modal.component.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { FormsModule } from '@angular/forms';
  4. import { AiChatService } from '../../../services/aichart.service';
  5. import { LiveService } from '../../../services/live.service';
  6. import * as Parse from 'parse';
  7. import {
  8. ionicStandaloneModules,
  9. AlertController,
  10. LoadingController,
  11. } from '../../../modules/ionic-standalone.modules';
  12. import { AccountService } from '../../../services/account.service';
  13. import { MessageService } from '../../../services/message.service';
  14. // import Vap from 'video-animation-player'
  15. declare const Vap: any;
  16. @Component({
  17. selector: 'app-gift-modal',
  18. templateUrl: './gift-modal.component.html',
  19. styleUrls: ['./gift-modal.component.scss'],
  20. standalone: true,
  21. imports: [...ionicStandaloneModules, CommonModule, FormsModule],
  22. })
  23. export class GiftModalComponent implements OnInit {
  24. @Input('toUid') toUid!: string; // 接收礼物的uid
  25. isOpenGift: boolean = false; // 是否显示礼物弹窗
  26. giftList: Array<any> = [];
  27. tabs: Array<any> = [
  28. {
  29. name: '礼物',
  30. val: 'all',
  31. },
  32. {
  33. name: '专属礼物',
  34. val: 'vip',
  35. },
  36. {
  37. name: '守护',
  38. val: 'guard',
  39. },
  40. ];
  41. activeTab: string = 'all';
  42. currentGift: any; //当前礼物
  43. giftCount: number = 1;
  44. wallet: any = { balance: 0 };
  45. isShowGiftModal: boolean = false; // 是否显示礼物动效弹窗
  46. times: any;
  47. @Output() sendEmit: EventEmitter<any> = new EventEmitter<any>();
  48. constructor(
  49. private aiServ: AiChatService,
  50. private alertController: AlertController,
  51. public liveService: LiveService,
  52. private loadingCtrl: LoadingController,
  53. public accServ: AccountService,
  54. public msgSerrvice: MessageService
  55. ) {
  56. }
  57. async ngOnInit() {
  58. this.giftList = this.msgSerrvice.giftList;
  59. }
  60. async openModal() {
  61. let uid: any = Parse.User.current()?.id;
  62. this.wallet = await this.aiServ.getWallet(uid);
  63. this.isOpenGift = true;
  64. }
  65. async selectTab(val: string) {
  66. if (this.activeTab === val) return;
  67. this.giftList = await this.aiServ.getGift(val == 'all' ? '' : val);
  68. this.activeTab = val;
  69. this.currentGift = null;
  70. }
  71. onChange() {
  72. this.giftCount = this.giftCount < 1 ? 1 : Math.floor(this.giftCount);
  73. console.log(this.giftCount);
  74. }
  75. async sendGift() {
  76. let _this = this;
  77. if (!this.currentGift?.id || this.giftCount < 1) return;
  78. const alert = await this.alertController.create({
  79. cssClass: 'my-custom-class',
  80. header: '送出礼物',
  81. message: `确定送出${this.giftCount}件${this.currentGift.name}吗`,
  82. buttons: [
  83. {
  84. text: '取消',
  85. role: 'cancel',
  86. cssClass: 'secondary',
  87. handler: (blah) => {},
  88. },
  89. {
  90. text: '确定',
  91. handler: async () => {
  92. const loading = await _this.loadingCtrl.create({
  93. message: '送出礼物中...',
  94. });
  95. loading.present();
  96. _this.aiServ
  97. .putGift(_this.toUid, _this.currentGift.id, _this.giftCount)
  98. .then((data) => {
  99. console.log(data);
  100. // _this.liveService.get_duration();
  101. loading.dismiss();
  102. _this.sendEmit.emit(); //触发父组件的sendEmit事件
  103. _this.isOpenGift = false;
  104. // _this.isShowGiftModal = true;
  105. _this.publishGiftMessage(_this.currentGift.id, _this.giftCount);
  106. // setTimeout(() => {
  107. // _this.isShowGiftModal = false;
  108. // console.log(_this.isShowGiftModal);
  109. // console.log('5s后关闭');
  110. // }, 5000);
  111. setTimeout(() => {
  112. this.initPlayer()
  113. }, 0);
  114. })
  115. .catch((err) => {
  116. _this.isOpenGift = false;
  117. loading.dismiss();
  118. console.error(err);
  119. });
  120. },
  121. },
  122. ],
  123. });
  124. await alert.present();
  125. }
  126. initPlayer(){
  127. console.log('emit 送出礼物');
  128. let json = this.currentGift.config
  129. let dom = document.getElementById('vap-warp');
  130. let vapPlayer = new Vap.default({
  131. container: dom, // 要渲染的载体,dom元素
  132. src: this.currentGift.video, // vap动画地址
  133. config: json, // 播放vap动画需要的 json文件。必填
  134. width: window.innerWidth, // 容器宽度
  135. height: window.innerHeight, // 容器高度
  136. fps: 30, // 帧数,json文件中有这个视频的帧数的,可以看一下,
  137. mute: false, // 静音
  138. type: 2, // 组件基于type字段做了实例化缓存,不同的VAP实例应该使用不同的type值(如0、1、2等)
  139. loop: false, // 循环
  140. precache: true, // 预加载视频,下载完再播。小动画建议边下边播,大动画还是先下后播吧,因为太大了或者网络不好,会一卡一卡的。
  141. beginPoint: 0, // 起始播放时间点(单位秒),在一些浏览器中可能无效
  142. accurate: true, // 是否启用精准模式(使用requestVideoFrameCallback提升融合效果,浏览器不兼容时自动降级)
  143. });
  144. vapPlayer.play(); // 开始播放
  145. vapPlayer.on('ended', () => {
  146. // 监听播放完成的事件
  147. vapPlayer.destroy();
  148. vapPlayer = null;
  149. console.log('play end');
  150. });
  151. vapPlayer.on('playering', function() { console.log('playering'); })
  152. }
  153. onCloseGiftModal() {
  154. if (!this.isShowGiftModal) this.isShowGiftModal = false;
  155. }
  156. publishGiftMessage(gid: string, count: number) {
  157. this.msgSerrvice.subscribeMessage(this.toUid, {
  158. message: true,
  159. presence: true,
  160. }); //触发礼物消息
  161. this.msgSerrvice.publishMessage(
  162. `ONUSERSENDGIFT_${gid}_${count}`,
  163. this.toUid
  164. );
  165. }
  166. }