| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- import { _decorator, Component, Node, Label, RichText, EventHandler } from 'cc';
- import { Http } from '../core/net/Http';
- import { OpenWindow } from '../core/ui/window/OpenWindow';
- import { WindowManager } from '../core/ui/window/WindowManager';
- import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
- import { Utils } from '../core/utils/Utils';
- import { MultipleType } from '../Data/ADData';
- import { g } from '../Data/g';
- import { LoginWaitWin } from '../Windows/LoginWaitWin';
- const { ccclass, property } = _decorator;
- @ccclass('RedBoxItem')
- export class RedBoxItem extends Component {
- @property({ tooltip: "红包等级", type: Node }) public redLv: Node;
- @property({ tooltip: "红包id", type: Node }) public redID: Node;
- @property({ tooltip: "倒计时", type: Node }) public redTime: Node;
- @property({ tooltip: "http服务", type: Http }) public http: Http;
- @property({ tooltip: "领取神石回调", type: EventHandler }) public getDiamondBackFun: EventHandler;
- @property({ tooltip: "红点", type: Node }) public redDot: Node;
- public redType = 0;//红包类型0:免费红包,1:切磋红包
- public opentime = 0;//红包开启时间
- public redId = 0;//红包id
- start() {
- g.gameData.isPlayVideo = false;
- }
- public setData(data: any) {
- this.redType = data.type;
- this.opentime = data.canOpenTime;
- this.redId = data.id;
- if (data.type == 0) {
- this.redLv.getComponent(Label).string = g.userData.lv + "级红包";
- this.redID.getComponent(Label).string = "免费红包";
- this.redTime.getComponent(RichText).string = "<color=#55a87e>看视频领取</color>";
- } else {
- this.redLv.getComponent(Label).string = data.lv + "级红包";
- this.redID.getComponent(Label).string = "" + data.enemyName;
- let _time = this.opentime * 1000 - Date.now();
- this.redTime.getComponent(RichText).string = _time <= 0 ? "<color=#55a87e>可领取</color>" :
- "<color=#ff2905>" + Utils.formatCountDown(_time) + "后可领取</color>";
- if (_time > 0) {
- this.schedule(this.scheduleCallback, 1);
- } else {
- this.redDot.active = true;
- }
- }
- }
- //定时回调
- public scheduleCallback() {
- let _time = this.opentime * 1000 - Date.now();
- this.redTime.getComponent(RichText).string = _time <= 0 ? "<color=#55a87e>可领取</color>" :
- "<color=#ff2905>" + Utils.formatCountDown(_time) + "后可领取</color>";
- if (_time <= 0) {
- this.redDot.active = true;
- this.unschedule(this.scheduleCallback);
- }
- }
- public isSend = false;
- public async getRedBox() {
- if (g.gameData.isPlayVideo) {
- return;
- }
- g.gameData.isPlayVideo = true;
- if (this.redType == 0) {
- let data = await this.http.send("/api/ad/WatchVideoAD");
- switch (data.code) {
- case 0:
- //播放视频
- let adData = await g.showRewardVideo();
- if (adData) {
- this.videoBack();
- } else {
- //视频观看失败
- WindowManager.showTips("视频观看时间不足");
- }
- break;
- case 108:
- WindowManager.showTips("今日视频次数已用完,请明日再继续");
- g.gameData.isPlayVideo = false;
- break;
- default:
- // WindowManager.showTips(g.CodeMsg[data.code]);
- WindowManager.showTips("视频准备中,请稍后再试");
- g.gameData.isPlayVideo = false;
- break;
- }
- } else {
- if (this.opentime - (Date.now() / 1000) <= 0) {
- this.openRedBox();
- } else {
- WindowManager.showTips("倒计时结束后可抢红包");
- g.gameData.isPlayVideo = false;
- }
- }
- }
- //视频结束回调
- public async videoBack() {
- //#region 打开加载等待界面
- // let _window = await WindowManager.open("Prefabs/Windows/加载等待窗口", WindowOpenMode.NotCloseAndCover);
- //#endregion
- let data = await this.http.send("/api/wealth/AddDiamondForVideo", { multipleType: MultipleType.Default, adData: g.adData });
- // _window.getComponent(LoginWaitWin).close();//关闭加载等待界面
- if (data.code == 0) {
- if (data.data.addDiamond > 0) {
- this.getComponent(OpenWindow).prefabPath = "Prefabs/Windows/神石领取界面";
- let _window = this.getComponent(OpenWindow);
- _window.open([data.data.addDiamond, -1, null, this.getDiamondBackFun]);
- } else {
- this.getDiamondBackFun.emit([]);
- }
- } else {
- WindowManager.showTips(g.CodeMsg[data.code]);
- g.gameData.isPlayVideo = false;
- }
- }
- public async openRedBox() {
- //#region 打开加载等待界面
- let _window = await WindowManager.open("Prefabs/Windows/加载等待窗口", WindowOpenMode.NotCloseAndCover);
- //#endregion
- let data = await this.http.send("/api/redPacket/OpenRedPacket", { redPacketID: this.redId });
- _window.getComponent(LoginWaitWin).close();//关闭加载等待界面
- if (data.code == 0) {
- let _window = this.getComponent(OpenWindow);
- if (data.data.openID == g.userData.id) {//打开红包
- _window.prefabPath = "Prefabs/Windows/切磋红包奖品";
- _window.open([data.data.prizes]);
- } else {//红包被抢
- _window.prefabPath = "Prefabs/Windows/红包被抢弹窗";
- _window.open({ name: data.data.uerName, avator: data.data.userAvator, time: data.data.openTime });
- }
- for (let i = 0; i < g.gameData.myRedPacketDatas.length; i++) {
- if (this.redId == g.gameData.myRedPacketDatas[i].id) {
- g.gameData.myRedPacketDatas.splice(i, 1);
- break;
- }
- }
- this.close();
- } else {
- g.gameData.isPlayVideo = false;
- WindowManager.showTips(g.CodeMsg[data.code]);
- }
- }
- public close() {
- this.node.removeFromParent();
- this.destroy();
- }
- onDestroy() {
- g.gameData.isPlayVideo = false;
- }
- }
|