Files
18xchat/assets/Scripts/Sub/UI/PreloadUI.ts
T

334 lines
12 KiB
TypeScript
Raw Normal View History

2025-08-12 19:49:50 +08:00
import {
_decorator,
AssetManager,
AudioClip,
2025-08-15 09:41:24 +08:00
BufferAsset,
2025-08-12 19:49:50 +08:00
Button,
Component,
ImageAsset,
JsonAsset,
Label,
Material,
Node,
Prefab,
ProgressBar,
Sprite,
SpriteFrame,
} from "cc";
import { GButton } from "../../Main/Common/GButton";
import ResManager from "../../Main/Manager/ResManager";
import Resource from "../../Main/Config/Resource";
import { SDKManager } from "../../Main/Channel/SDKManager";
import HttpUnit from "../../Main/Common/HttpUnit";
import GlobalValue from "../../Main/Common/GlobalValue";
import Utils from "../../Main/Common/Utils";
import { InnerMsgCode } from "../../Main/Config/InnerMsgCode";
import { promptItem } from "../Item/promptItem";
import LanguageUtils, { LanguageType } from "../../Main/Common/LanguageUtils";
2025-08-16 16:14:10 +08:00
import { DataManager, DataId } from "../../chat18x/data/DataManager";
import { LoginData } from "../../chat18x/data/LoginData";
2025-08-25 16:27:18 +08:00
import { PlayerData } from "../../chat18x/data/PlayerData";
2025-08-16 16:14:10 +08:00
import { AccountData } from "../../chat18x/data/AccountData";
import { MetaData } from "../../chat18x/data/MetaData";
import { NetTimeData } from "../../chat18x/data/NetTimeData";
2025-08-15 15:24:31 +08:00
import ConfigManager from "../../chat18x/manager/ConfigManager";
2025-08-15 16:19:23 +08:00
import { AppConfig } from "db://assets/Scripts/chat18x/config/env/appConfig";
import DeviceIdService from "db://assets/Scripts/chat18x/foundation/identity/DeviceIdService";
2025-08-18 10:58:38 +08:00
import MachineInfoService from "db://assets/Scripts/chat18x/foundation/identity/MachineInfoService";
2025-08-18 15:22:54 +08:00
import { AuthService } from "db://assets/Scripts/chat18x/network/services/AuthService";
import { PlayerDataService } from "db://assets/Scripts/chat18x/network/services/PlayerDataService";
import { ApiCode } from "db://assets/Scripts/chat18x/network/client/types";
2025-08-25 11:30:50 +08:00
import proto from 'db://assets/Scripts/proto/proto.pb.js';
2025-08-18 10:58:38 +08:00
2025-07-17 17:18:21 +08:00
const { ccclass, property } = _decorator;
2025-08-12 19:49:50 +08:00
@ccclass("PreloadUI")
2025-07-17 17:18:21 +08:00
export class PreloadUI extends Component {
2025-08-12 19:49:50 +08:00
private checkFinish = true; //检查完成
private preloadFinish = false; //资源加载完成
private loginFinish = false; //登录完成
2025-07-17 17:18:21 +08:00
2025-08-12 19:49:50 +08:00
private jinduBar: Node = null;
private jinduFilled: Sprite = null;
private LabProgress: Label = null;
2025-08-16 01:07:42 +08:00
private loginNode: Node = null;
private btnLogin: Node = null; //登录按钮
2025-07-17 17:18:21 +08:00
2025-08-12 19:49:50 +08:00
start() {
//Utils.addInnerEL(InnerMsgCode.UI_ShowPrompt, this, this.resiveShowPrompt)
2025-07-17 17:18:21 +08:00
2025-08-12 19:49:50 +08:00
this.jinduBar = this.node.getChildByName("jinduBar");
this.jinduFilled = this.jinduBar
.getChildByName("jinduFilled")
.getComponent(Sprite);
this.LabProgress = this.node
.getChildByName("LabProgress")
.getComponent(Label);
2025-08-16 01:07:42 +08:00
this.loginNode = this.node.getChildByName("loginNode");
this.btnLogin = this.loginNode.getChildByName("btnLogin");
2025-07-17 17:18:21 +08:00
2025-08-12 19:49:50 +08:00
//适配背景图
let BG = this.node.getChildByName("BG");
Utils.adjustBgPixelRatio(BG, 1);
2025-07-17 17:18:21 +08:00
2025-08-12 19:49:50 +08:00
this.jinduFilled.fillRange = 0;
2025-08-16 01:07:42 +08:00
// 注册监听
this.registerListener();
2025-08-15 09:41:24 +08:00
2025-08-18 10:22:46 +08:00
// 初始化配置
ConfigManager.I.init();
//console.log(ConfigManager.tables.TbGlobalConfig.GameName);
2025-08-15 16:19:23 +08:00
// 初始化环境配置
AppConfig.I;
2025-08-14 16:30:08 +08:00
// 初始化数据层
2025-08-15 14:59:08 +08:00
DataManager.I.init();
2025-08-15 16:19:23 +08:00
// 设备号
DeviceIdService.I.init();
2025-08-15 14:59:08 +08:00
2025-08-12 19:49:50 +08:00
this.preloadFiles();
LanguageUtils.setLanguage(LanguageType.CN);
}
protected onDestroy(): void {
Utils.removeInnerEL(
InnerMsgCode.UI_ShowPrompt,
this,
this.resiveShowPrompt
);
}
update(deltaTime: number) {
if (this.checkFinish) {
if (this.preloadFinish && this.loginFinish) {
this.checkFinish = false;
this.enterGame();
}
2025-07-17 17:18:21 +08:00
}
2025-08-12 19:49:50 +08:00
}
2025-07-17 17:18:21 +08:00
2025-08-12 19:49:50 +08:00
/**收到飘字消息 */
resiveShowPrompt(data: any) {
let msg = data.text;
let self = this;
ResManager.I.loadSubpackagePrefab("item/promptItem", (n_node: Node) => {
self.node.addChild(n_node);
let pItem = n_node.getComponent(promptItem);
pItem.setLabel(msg);
});
}
2025-07-17 17:18:21 +08:00
2025-08-12 19:49:50 +08:00
//预加载文件
preloadFiles() {
let preloadTab = [
{ path: "ChatPanel", bundleName: "Chat18x", ftype: Prefab },
{ path: "GirlDetailPanel", bundleName: "Chat18x", ftype: Prefab },
{ path: "GirlListPanel", bundleName: "Chat18x", ftype: Prefab },
// {path:"Music/music_interface", bundleName: "Audio", ftype: AudioClip},
// {path:"bg/mengli_stage_bg", bundleName: "Raw", ftype: ImageAsset},
// {path:"cg0/mengli_0_cg", bundleName: "Raw", ftype: ImageAsset},
// {path:"cg1/mengli_1_cg", bundleName: "Raw", ftype: ImageAsset},
// {path:"cg2/mengli_2_cg", bundleName: "Raw", ftype: ImageAsset},
// {path:"cg3/mengli_3_cg", bundleName: "Raw", ftype: ImageAsset},
// {path:"emo_1/mengli_shy", bundleName: "Raw", ftype: ImageAsset},
// {path:"emo_2/mengli_laugh", bundleName: "Raw", ftype: ImageAsset},
// {path:"emo_3/mengli_disgust", bundleName: "Raw", ftype: ImageAsset},
// {path:"role/mengli_default", bundleName: "Raw", ftype: ImageAsset},
// {path:"Zjm/raw1", bundleName: "Raw", ftype: ImageAsset},
// {path:"touming", bundleName: "common", ftype: ImageAsset},
// // {path:"chanpin_1_0", bundleName: "daoju", ftype: ImageAsset}, //有合图加载失败
// {path:"zjm_1", bundleName: "Zhujiemian", ftype: ImageAsset},
// {path:"zjm_19", bundleName: "Zhujiemian", ftype: ImageAsset},
// {path:"zjm_30", bundleName: "Zhujiemian", ftype: ImageAsset},
// {path:"mohu", bundleName: "Materials", ftype: Material},
// {path:"UI/Cross/Cross_UI", bundleName: "PB", ftype: Prefab},
// {path:"UI/StartUI/Start_UI", bundleName: "PB", ftype: Prefab},
2025-08-15 09:41:24 +08:00
{ path: "tblanguage", bundleName: "DataTable", ftype: BufferAsset },
2025-08-12 19:49:50 +08:00
];
let num_prefab = 0; //已加载数量
let num_succeed = 0; //加载成功数量
let preloadFunc = () => {
if (num_prefab < preloadTab.length) {
let cfg = preloadTab[num_prefab];
ResManager.I.preLoadSubpackageFile(
cfg.path,
cfg.bundleName,
cfg.ftype,
(ret) => {
num_prefab++;
num_succeed++;
let percent = 0.45 * (num_prefab / preloadTab.length);
this.showLoadProgress(0.55 + percent);
console.log("加载成功:", cfg.path);
preloadFunc();
},
(err) => {
num_prefab++;
console.log("加载失败:", err);
}
);
} else {
if (num_succeed >= num_prefab) {
this.loadDataTable();
2025-07-17 17:18:21 +08:00
}
2025-08-12 19:49:50 +08:00
}
};
preloadFunc();
}
2025-07-17 17:18:21 +08:00
2025-08-12 19:49:50 +08:00
//加载配置表
loadDataTable() {
let dataArr = [
//"commonGlobal",
// "levelHole",
//"Music",
//"Sound",
];
2025-07-17 17:18:21 +08:00
2025-08-12 19:49:50 +08:00
//配置表使用方法: let cfgs = Resource.getConfig("Sound")
let num_prefab = 0;
let preloadFunc = () => {
if (num_prefab < dataArr.length) {
let cfgname = dataArr[num_prefab];
ResManager.I.loadSubpackageDataTable(cfgname, (ret: JsonAsset) => {
num_prefab++;
Resource.addSubJsonConfig(cfgname, ret.json);
preloadFunc();
});
} else {
this.preloadFinish = true;
2025-08-16 01:07:42 +08:00
this.enterLoginModule();
2025-08-12 19:49:50 +08:00
}
};
preloadFunc();
}
2025-07-17 17:18:21 +08:00
2025-08-12 19:49:50 +08:00
//显示加载进度
showLoadProgress(percent) {
this.jinduFilled.fillRange = percent;
}
2025-07-17 17:18:21 +08:00
2025-08-16 01:07:42 +08:00
// 进入登录模块
private enterLoginModule() {
this.LabProgress.node.active = false;
this.jinduBar.active = false;
this.loginNode.active = true;
}
2025-08-12 19:49:50 +08:00
//登录
2025-08-16 01:07:42 +08:00
async onLogin() {
2025-08-12 19:49:50 +08:00
//初始化SDK信息
// SDKManager.init(() => {
// //检测是否有授权,有授权直接登录,没有授权就先进主界面,到主界面获取权限
// SDKManager.checkAutoSetting("userInfo", (ishave) => {
// if (ishave) {
// console.log("有授权,直接登录")
// HttpUnit.ins.login((loginData) => {
// if (loginData == null) {
// //登录失败,这里处理重复登录的逻辑
// } else {
// this.loginFinish = true
// }
// })
// } else {
// console.log("没有授权,等待授权后登录")
// // this.loginFinish = true
// this.jinduBar.active = false
// this.LabProgress.node.active = false
// //登录按钮
// this.btnLogin.node.active = true
// let loginPath = ""
// if (SDKManager.isWenxin()) {
// loginPath = "login1"
// } else if (SDKManager.isByteDance()) {
// loginPath = "login2"
// } else if (SDKManager.isKuaishou()) {
// loginPath = "login3"
// }
// ResManager.I.changeResourceSpriteFrame(this.btnLogin, loginPath)
//
// HttpUnit.ins.login((loginData) => {
// if (loginData == null) {
// //登录失败,这里处理重复登录的逻辑
// } else {
// this.loginFinish = true
// }
// })
// }
// })
// })
2025-08-16 01:07:42 +08:00
2025-08-18 10:22:46 +08:00
// 游客登录
const deviceId = DeviceIdService.I.id;
2025-08-18 10:58:38 +08:00
const machineIdentifier = MachineInfoService.I.getMachineIdentifier();
2025-08-16 01:07:42 +08:00
const reqData = {
2025-08-25 11:30:50 +08:00
platType: 'guset',
userId: deviceId,
device: deviceId,
os: 3,
2025-08-16 01:07:42 +08:00
};
2025-08-18 10:58:38 +08:00
console.log("登录请求数据:", reqData);
2025-08-16 01:07:42 +08:00
let res = await AuthService.I.login(reqData);
console.log("登录响应数据:", res);
2025-08-25 11:30:50 +08:00
if (res && res.code === proto.cs.EnmRetCode.SUCCESS) {
2025-08-16 16:14:10 +08:00
const resData = res.data;
// 保存数据
const loginData = DataManager.I.getDataById<LoginData>(DataId.Login);
2025-08-25 11:30:50 +08:00
loginData.token = resData.token;
2025-08-25 16:27:18 +08:00
loginData.refreshToken = resData.refreshToken;
loginData.expire = resData.expire;
const playerData = DataManager.I.getDataById<PlayerData>(DataId.Player);
playerData.name = resData.name;
2025-08-25 11:30:50 +08:00
// const accountData = DataManager.I.getDataById<AccountData>(DataId.Account);
// accountData.identifier = resData.Identifier;
// accountData.identityType = resData.IdentityType;
// accountData.uid = resData.Uid;
// accountData.isNewGuide = resData.IsNewGuide;
// accountData.isNewRegist = resData.IsNewRegist;
// const metaData = DataManager.I.getDataById<MetaData>(DataId.Meta);
// metaData.gameName = resData.GameName;
// const netTimeData = DataManager.I.getDataById<NetTimeData>(DataId.NetTime);
// netTimeData.serverTime = resData.ServerTime;
// netTimeData.timezoneOffset = resData.TimezoneOffset;
2025-08-16 16:14:10 +08:00
2025-08-16 01:07:42 +08:00
this.loginFinish = true;
2025-08-18 15:22:54 +08:00
2025-08-25 16:27:18 +08:00
// this.reqMyInfo();
2025-08-16 01:07:42 +08:00
};
2025-08-12 19:49:50 +08:00
}
2025-07-17 17:18:21 +08:00
2025-08-25 16:27:18 +08:00
// 请求个人信息
private async reqMyInfo() {
2025-08-18 15:22:54 +08:00
const reqData = {
2025-08-25 16:27:18 +08:00
2025-08-18 15:22:54 +08:00
};
console.log("玩家数据请求数据:", reqData);
2025-08-25 16:27:18 +08:00
let res = await PlayerDataService.I.reqMyInfo(reqData);
2025-08-18 15:22:54 +08:00
console.log("玩家数据响应数据:", res);
2025-08-25 16:27:18 +08:00
if (res && res.code === proto.cs.EnmRetCode.SUCCESS) {
2025-08-18 15:22:54 +08:00
}
}
2025-08-12 19:49:50 +08:00
enterGame() {
//let islogin = HttpUnit.IsLogin();
//console.log("进入游戏,登录状态:", islogin);
this.jinduBar.active = false;
this.LabProgress.node.active = false;
//this.btnLogin.node.active = false
// if (islogin) {
// } else {
// //没登录成功,先进主界面,在主界面里进行登录
// }
2025-08-15 15:24:31 +08:00
2025-08-12 19:49:50 +08:00
ResManager.I.goMainScene(); //进入下一个场景
// ResManager.I.goCustomScene("Test2DScene"); //进入自定义场景
}
2025-08-16 01:07:42 +08:00
private registerListener() {
GButton.BandClick(this.btnLogin, this.onLogin, this);
}
2025-07-17 17:18:21 +08:00
}