MixRedPacket.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { _decorator, Component, Node, ImageAsset, SpriteFrame, Texture2D, Label, Sprite } from 'cc';
  2. import { JSB } from 'cc/env';
  3. import { Http } from '../../core/net/Http';
  4. import { ResourcesUtils } from '../../core/resourceManager/ResourcesUtils';
  5. import { OpenWindow } from '../../core/ui/window/OpenWindow';
  6. import { Window } from '../../core/ui/window/Window';
  7. import { WindowManager } from '../../core/ui/window/WindowManager';
  8. import { WindowOpenMode } from '../../core/ui/window/WindowOpenMode';
  9. import { g } from '../../Data/g';
  10. const { ccclass, property } = _decorator;
  11. /**合成红包 */
  12. @ccclass('MixRedPacket')
  13. export class MixRedPacket extends Component {
  14. @property({ type: Label, tooltip: "名字文本" }) titleLabel: Label;
  15. @property({ type: Sprite, tooltip: "图标" }) icon: Sprite;
  16. @property({ tooltip: "http服务", type: Http }) http: Http;
  17. async start() {
  18. }
  19. private async onOpenHander() {
  20. let data = g.downLoadData.recommendData[Math.floor(Math.random() * g.downLoadData.recommendData.length)];
  21. this.titleLabel.string = data.title;
  22. if (JSB) {
  23. let img = await ResourcesUtils.loadRemote<ImageAsset>(data.icon, { ext: ".png" }, this);
  24. const spriteFrame = new SpriteFrame();
  25. const texture = new Texture2D();
  26. texture.image = img;
  27. spriteFrame.texture = texture;
  28. this.icon.spriteFrame = spriteFrame;
  29. }
  30. }
  31. /**领取合成红包 */
  32. private async onGet() {
  33. let result = await this.http.send("/api/ad/watchVideoAD");
  34. if (result.code == 0) {
  35. //播放视频
  36. let adData = await g.showRewardVideo();
  37. if (adData) {
  38. this.videoBack();
  39. } else {
  40. //视频观看失败
  41. WindowManager.showTips("视频观看时间不足");
  42. }
  43. } else if (result.code == 108) {
  44. WindowManager.showTips("今日视频次数已用完,请明日再继续");
  45. } else {
  46. WindowManager.showTips("视频准备中,请稍后再试");
  47. }
  48. }
  49. //视频结束回调,合成红包
  50. public async videoBack() {
  51. let data = await this.http.send("/api/wealth/AddDiamondForVideo", { multipleType: 1, adData: g.adData });
  52. if (data.code == 0) {//视频观看成功
  53. if (data.data.addDiamond > 0) {
  54. this.getComponent(OpenWindow).prefabPath = "Prefabs/Windows/神石领取界面";
  55. let _window = this.getComponent(OpenWindow);
  56. _window.open([data.data.addDiamond, 1]);
  57. this.getComponent(Window).close();
  58. }
  59. } else {
  60. g.gameData.isPlayVideo = false;
  61. WindowManager.showTips(g.CodeMsg[data.code]);
  62. }
  63. }
  64. }