Files
18xchat/assets/Scripts/Main/Channel/SDKManager.ts
T

332 lines
8.3 KiB
TypeScript
Raw Normal View History

2025-07-17 17:18:21 +08:00
import { _decorator, Component, sys, utils } from "cc";
import Utils from "../Common/Utils";
import { WXSDK } from "./wxSDK";
import { ByteDanceSDK } from "./bdSDK";
import { WebSDK } from "./webSDK";
import { KSSDK } from "./ksSDK";
import AudioManager from "../Manager/AudioManager";
import GlobalValue, { VideoRoleType } from "../Common/GlobalValue";
const { ccclass, property } = _decorator;
/**运行渠道 */
export enum E_SDK_Channel {
/**微信 */
WX,
/**字节跳动 (抖音、头条) */
BD,
/**网页 */
WEB,
/**快手 */
KS,
}
/**玩家SDK信息 */
export interface I_SDK_UserInfo {
/**昵称 */
nickName: string;
/**头像url */
avatarUrl: string;
}
/**玩家SDK信息回调 */
export interface I_SDK_UserInfo_Callback {
(info: I_SDK_UserInfo): void;
}
//渠道管理器
@ccclass('SDKManager')
export class SDKManager { //extends Component
private static _I: SDKManager = null;
public static get I(): SDKManager {
if (!SDKManager._I) {
SDKManager._I = new SDKManager();
SDKManager._I.init();
}
return SDKManager._I;
}
private init(){
}
initEmpty(){
}
//玩家头像
private static _avatarUrl: string = "";
public static get avatarUrl(): string {
return SDKManager._avatarUrl;
}
public static set avatarUrl(value: string) {
if (value == null) {
value = "";
}
SDKManager._avatarUrl = value;
}
//玩家昵称
private static _nickName: string = "";
public static get nickName(): string {
return SDKManager._nickName;
}
public static set nickName(value: string) {
if (value == null) {
value = "游客";
}
SDKManager._nickName = value;
}
private static sdk_channel:E_SDK_Channel = E_SDK_Channel.WEB //当前SDK渠道
private static video_reward_map:any
private static share_reward_map:any
private static sdk:any
//初始化SDK
static init(cb: Function = null) {
this.video_reward_map = {}
this.share_reward_map = {}
console.log("平台值 wx:",window["wx"], typeof window["wx"])
console.log("平台值 ks:",window["ks"], typeof window["ks"])
console.log("平台值 tt:",window["tt"], typeof window["tt"])
if (typeof window["ks"] !== 'undefined') {
this.sdk_channel = E_SDK_Channel.KS
this.sdk = new KSSDK() //快手
}else if (sys.platform === sys.Platform.WECHAT_GAME && typeof window["wx"] !== 'undefined') {
this.sdk_channel = E_SDK_Channel.WX
this.sdk = new WXSDK() //微信
} else if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME && typeof window["tt"] !== 'undefined') {
this.sdk_channel = E_SDK_Channel.BD
this.sdk = new ByteDanceSDK() //抖音(头条)
} else {
this.sdk_channel = E_SDK_Channel.WEB
this.sdk = new WebSDK()
}
console.log("当前平台:",sys.platform, this.sdk_channel)
if (GlobalValue.NpcVideoMod) {
GlobalValue.NpcVideoMod = this.sdk_channel === E_SDK_Channel.WX || this.sdk_channel === E_SDK_Channel.WEB
}
this.sdk.init(cb)
SDKManager.umaInit();
}
/**是否是微信平台 */
static isWenxin() {
return this.sdk_channel === E_SDK_Channel.WX
}
/**是否是抖音平台 */
static isByteDance() {
return this.sdk_channel === E_SDK_Channel.BD
}
/**是否是快手平台 */
static isKuaishou() {
return this.sdk_channel === E_SDK_Channel.KS
}
/**是否是web平台 */
static isWeb() {
return this.sdk_channel === E_SDK_Channel.WEB
}
//SDK登录,并返回code
static login(cb: Function = null) {
this.sdk.login(cb)
}
//检查玩家权限
//autoKey 授权类型 userInfo=用户信息
static checkAutoSetting(autoKey:string, cb:Function = null) {
this.sdk.checkAutoSetting(autoKey, cb)
}
//获取用户信息
static getUserInfo(cb:Function = null) {
this.sdk.getUserInfo(cb)
}
//注册视频广告回调
static register_video_reward(uuid:string, func:Function) {
let video_reward_map = this.video_reward_map || {};
video_reward_map[uuid] = func
this.video_reward_map = video_reward_map;
}
//注册分享回调
static register_share_reward(uuid:string, func:Function) {
let share_reward_map = this.share_reward_map || {};
share_reward_map[uuid] = func
this.share_reward_map = share_reward_map;
}
//执行视频广告回调
private static dispatch_video_reward(uuid:string, tag:number) {
let video_reward_map = this.video_reward_map || {};
let register_func:Function = video_reward_map[uuid];
if (register_func) {
register_func(tag)
}
}
//执行分享回调
private static dispatch_share_reward(uuid:string, tag:number) {
let share_reward_map = this.share_reward_map || {};
let register_func:Function = share_reward_map[uuid];
if (register_func) {
register_func(tag)
}
}
//视频广告
static show_reward_video_ad(uuid:string, tag: number) {
SDKManager.video_ad_pre();
this.sdk.show_video(()=>{
this.dispatch_video_reward(uuid,tag)
});
}
//看视频广告前
static video_ad_pre() {
console.log("SDKManager 视频广告前")
AudioManager.I.PauseMusic();
}
//看视频广告后返回游戏
static video_ad_back() {
console.log("SDKManager 视频广告后")
AudioManager.I.ResumeMusic();
}
//插屏广告
static show_interstitial_ad() {
this.sdk.show_splash();
}
//展示BANNER
static show_banner_ad() {
this.sdk.show_banner();
}
//隐藏BANNBER
static hide_banner_ad() {
this.sdk.hide_banner();
}
//主动拉起分享
static show_share() {
this.sdk.show_share();
}
//分享得奖励
static show_reward_share(uuid:string, tag: number) {
this.sdk.show_share(()=>{
this.dispatch_share_reward(uuid,tag)
});
}
//获取设备分辨率
static getWindowSize() {
return this.sdk.getWindowSize();
}
/**显示游戏圈
* @param leftRatio 左边距占屏幕宽度的比例
* @param topRatio 顶部边距占屏幕高度的比例
* @param widthRatio 游戏圈宽度占屏幕宽度的比例
* @param heightRatio 游戏圈高度占屏幕高度的比例
*/
static show_gameClub(leftRatio, topRatio, widthRatio, heightRatio) {
this.sdk.show_gameClub(leftRatio, topRatio, widthRatio, heightRatio);
}
//隐藏游戏圈
static hide_gameClub() {
this.sdk.hide_gameClub();
}
/**检查是否支持base64音频播放*/
static checkSupportBase64Audio() {
return this.sdk.checkSupportBase64Audio();
}
/**播放base64音频 */
static playBase64Audio(base64: string, loop: boolean = false) {
this.sdk.playBase64Audio(base64, loop);
}
//记录GM日志
private static _gmLogString: string = "";
private static _gmLogCount: number = 0; //记录日志条数
static get GMLogString(): string {
return SDKManager._gmLogString;
}
static ShowGMLog(log: string){
Utils.Log(log);
if (SDKManager._gmLogCount > 100){
SDKManager.ClearGMLog();
}
SDKManager._gmLogString += log + "\n";
SDKManager._gmLogCount++;
}
static ClearGMLog(){
SDKManager._gmLogString = "";
SDKManager._gmLogCount = 0;
}
/**是否支持客服功能 */
static KefuSupport() {
return this.sdk.kefuSupport();
}
/**打开客服 */
static OpenKefu() {
this.sdk.openKefu();
}
//region 播放背景视频 cbMap = {cbReady, cbEnded}
static PlayBgAudio(remoteUrl:string, vtype:VideoRoleType, loop:boolean = false, extra:any = {}) {
this.sdk.playBgAudio(remoteUrl, vtype, loop, extra);
}
//添加要播放的表情视频
static AddEmoAudio(remoteUrl:string, vtype:VideoRoleType, loop:boolean = false, extra:any = {}) {
this.sdk.addEmoAudio(remoteUrl, vtype, loop, extra);
}
//region 语音转文字
static startRecordRecognition() {
if (this.sdk.startRecordRecognition) {
this.sdk.startRecordRecognition();
} else {
Utils.Log("sdk not support startRecordRecognition");
}
}
static stopRecordRecognition() {
if (this.sdk.stopRecordRecognition) {
this.sdk.stopRecordRecognition();
} else {
Utils.Log("sdk not support stopRecordRecognition");
}
}
//region 友盟+
static Uma_Event_TalkRound = "_um.talk.round"; //聊天轮次
static Uma_Event_LvUnlock = "_um.lv.unlock"; //解锁新关卡
/**初始化友盟 */
static umaInit() {
this.sdk.umaInit();
}
/**友盟功能是否生效 */
static umaSwt() {
return this.sdk.umaSwt();
}
/**友盟事件上报 */
static umaEvent(eventName: string, value: any = {}) {
this.sdk.umaEvent(eventName, value);
}
}