Spring774
2025-10-30 24d1f8782c262a20ead3e0d68a93eb28cac7d376
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { SdkDefault } from "./SdkDefault";
 
 
var rewardCallback = null;
export class SdkHuawei extends SdkDefault {
 
    private lastShowInterAdTime: number = 0;
 
    private interstitialAd = null;
    private rewardedVideoAd = null;
    private interstitialAdLoaded: boolean = false;
 
    /**
     * 开始游戏
     * @param cb 
     */
    startGame(cb?: Function): void {
        cc.log('SdkHuawei startGame.');
 
        qg.gameLoginWithReal({
            forceLogin: 1,
            appid: "115744405",
            success: (data) => {
                // 登录成功后,可以存储账号信息。             
                console.log(" game login with real success:" + JSON.stringify(data));
                cb && cb();
 
                this.getOAID();
 
                this.reLoadVideo();
            },
            fail: function (data, code) {
                console.log("game login with real fail:" + data + ", code:" + code);
                //根据状态码处理游戏的逻辑。
                //状态码为7004或者2012,表示玩家取消登录。
                //此时,建议返回游戏界面,可以让玩家重新进行登录操作。
                if (code == 7004 || code == 2012) {
                    console.log("玩家取消登录,返回游戏界面让玩家重新登录。");
                    cc.director.loadScene('logo');
                } else
                    //状态码为7021表示玩家取消实名认证。
                    //在中国大陆的情况下,此时需要禁止玩家进入游戏。
                    if (code == 7021) {
                        console.log("The player has canceled identity verification. Forbid the player from entering the game.")
                        cc.director.loadScene('logo');
                    } else {
                        cc.game.end();
                    }
            }
        });
    }
 
    getOAID() {
        "function" == typeof qg.getOAID && qg.getOAID({
            success: function (ret) {
                console.log("qg.getOAID success :" + JSON.stringify(ret));
            },
            fail: function (errMsg, errCode) {
                console.log("qg.getOAID fail " + errCode + ': ' + errMsg)
            },
            complete: function () {
                console.log("qg.getOAID complete")
            }
        })
    }
 
    //Ads
    showInterAd() {
        cc.log('show InterAd');
        // if (Date.now() - this.lastShowInterAdTime > 30000) {//最少间隔30秒
        //     this.lastShowInterAdTime = Date.now();
 
 
        // }
 
        this.loadInterAd();
 
    }
 
    loadInterAd() {
        if (this.interstitialAd) {
            this.interstitialAd.destroy();
        }
        this.interstitialAd = qg.createInterstitialAd({
            adUnitId:
            //    "g2tz5d4wkv" //测试ID
            "q5m5hlquus"//正式ID
        });
        this.interstitialAd.load({
            "channelExtras": {
                "customerChannel": "1"
            }
        });
        this.interstitialAd.onLoad((data) => {
            cc.log(
                "requestImageAdAndShow onLoad data= " + JSON.stringify(data)
            );
            this.interstitialAd.show();
        });
        this.interstitialAd.onError(err => {
            cc.log(" requestImageAdAndShow on error = " + JSON.stringify(err));
        });
        this.interstitialAd.onClose(() => {
            cc.log("requestImageAdAndShow interstitialAd closed");
            this.interstitialAd.destroy(() => {
                cc.log("destroy");
            });
        });
        this.interstitialAd.onClick(() => {
            cc.log("requestImageAdAndShow interstitialAd clicked");
        });
    }
 
    reLoadVideo() {
        this.rewardedVideoAd = qg.createRewardedVideoAd({
            adUnitId:
            //    "testx9dtjwj8hp" //测试ID
            "v1gtsb1ga8" //正式ID
            ,
            success: (code) => {
                cc.log("ad demo : loadAndShowVideoAd createRewardedVideoAd success");
            },
            fail: (data, code) => {
                cc.log("ad demo : loadAndShowVideoAd createRewardedVideoAd fail: " + data + "," + code);
            },
            complete: () => {
                cc.log("ad demo : loadAndShowVideoAd createRewardedVideoAd complete");
            }
        });
        this.rewardedVideoAd.onLoad(() => {
            cc.log('ad demo :ad loaded.');
        })
 
        this.rewardedVideoAd.onError((e) => {
            cc.error('load ad error:' + JSON.stringify(e));
            const errCode = e.errCode
            const errMsg = e.errMsg
 
            rewardCallback && rewardCallback(false);
        })
        this.rewardedVideoAd.onClose((res) => {
            // if(!this.rewardedVideoAd) return
            // this.rewardedVideoAd.offClose(null);
            cc.log('ad onClose: ' + res.isEnded)
            // this.rewardedVideoAd.offLoad(null);
            if (res && res.isEnded || res === undefined) {
                cc.log('播放激励视频结束,给予奖励');
                rewardCallback && rewardCallback(true);
            } else {
                cc.log('播放没结束,不给予奖励');
                rewardCallback && rewardCallback(false);
            }
        })
        this.rewardedVideoAd.load()
    }
 
    showVideoAd(cb?: (result: boolean) => void) {
        cc.log('show VideoAd');
        // cb && cb(true);
 
        rewardCallback = cb;
        this.rewardedVideoAd.show();
        this.rewardedVideoAd.load();
    }
 
    isPaymentReady(cb?: (result: boolean) => void) {
        cb && cb(false);
    }
 
}