This commit is contained in:
2025-07-17 17:18:21 +08:00
commit 1ffc1b5cf0
5309 changed files with 1150310 additions and 0 deletions
@@ -0,0 +1,315 @@
import { _decorator, sys } from "cc";
import { SDKManager } from "../Channel/SDKManager";
import GlobalValue from "./GlobalValue";
import li_EventManager from "./li_EventManager";
import Utils from "./Utils";
import { InnerMsgCode } from "../Config/InnerMsgCode";
// import { EventManager, EventNameEnum } from "../manager/eventManager";
// import GlobalValue from "../manager/GlobalValue";
// import { DataManager } from "../manager/dataManager";
// import { SDKManager } from "../manager/sdkManager";
// import GlobalValue from "../global/GlobalValue";
const { ccclass, property } = _decorator;
/**
* http通讯
*/
@ccclass
export default class HttpUnit {
// static readonly ServerHttpHost = "https://bls.vip.hnhxzkj.com/";
static readonly ServerHttpHost = "https://xqmnyx.vip.hnhxzkj.com/api/";
static PlayerToken = ""; //登录时获取的token
static LoginState = 0; //登录状态 0未登录 1登录中 2已登录
static UserInfo:any = {}; //用户数据
static SerSetting:any = {}; //后台设置
//单例
private static I: HttpUnit;
public static get ins(): HttpUnit {
if (this.I == null) {
this.I = new HttpUnit();
}
return this.I;
}
public constructor() {
}
public jsonToQueryString(json: any) {
var str = '';
if (typeof json == "object") {
str = "?";
for (var k in json) {
if (str != "?") {
str += "&";
}
str += k + "=" + json[k];
}
}
return str;
}
private objectToFormData(obj: Object): FormData {
const formData = new FormData();
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const value = obj[key];
if (Array.isArray(value)) {
for (const item of value) {
formData.append(key, item);
}
} else {
formData.append(key, value);
}
}
}
return formData;
}
private async quest(xhr: XMLHttpRequest, method: "GET" | "POST" | "PUT" = "GET", data: Object, formData: boolean = false) {
return new Promise((resolve, reason) => {
xhr.onreadystatechange = () => {
if (xhr.readyState == 4) {
if (xhr.status >= 200 && xhr.status < 300) {
// console.log("[Http] 返回", xhr.response);
resolve(JSON.parse(xhr.response));
} else {
reason(xhr.status)
}
}
};
xhr.onerror = (error) => {
console.error("http request onerror");
reason(error)
};
xhr.ontimeout = (e) => {
console.error("http request timeout");
reason(e)
}
//根据POST和GET方式,选择是否发送msg数据
if (formData) {
xhr.send(this.objectToFormData(data));
} else {
if (method == 'POST') {
xhr.send(JSON.stringify(data));
} else if (method == 'PUT') {
xhr.send(JSON.stringify(data));
} else {
xhr.send();
}
}
});
}
/**是否已登录成功 */
public static IsLogin(): boolean {
return HttpUnit.LoginState == 2;
}
//登录
private loginCB: Function = null;
public login(cb: Function = null) {
this.loginCB = cb;
SDKManager.login((_loginData) => {
console.log("SDK登录成功", _loginData)
SDKManager.getUserInfo((userInfo) => {
console.log("SDK获取用户信息成功", userInfo)
// let serparam = {
// platform: _loginData.platform,
// code: _loginData.code,
// user_info: {nickName:userInfo.nickName, avatarUrl:userInfo.avatarUrl}
// }
if (GlobalValue.IsTest) {
console.log("登录测试环境")
this.loginSer({platform:0, code:12345}, this.initData.bind(this))
} else {
if (_loginData.code == null) {
let uuid = sys.localStorage.getItem('uuid');
if (!uuid) {
uuid = Date.now(); //当前时间戳
sys.localStorage.setItem('uuid', uuid);
}
_loginData.code = uuid;
}
console.log("登录正式环境 loginData.code: ", _loginData.code)
this.loginSer({platform:1, code:_loginData.code}, this.initData.bind(this))
}
})
})
}
private initData(playerData) {
playerData = playerData || {};
this.loginCB && this.loginCB(playerData);
}
//登录服务器
private async loginSer(msg, cb: Function = null) {
let data: any = await HttpUnit.ins.api("login", msg, "POST", true);
console.log("login:", msg, data)
if (data.code == 1) {
HttpUnit.PlayerToken = data.data.token
HttpUnit.LoginState = 2
console.log("HttpUnit.PlayerToken:", HttpUnit.PlayerToken)
//获取玩家信息
// this.getUserInfo(cb);
// this.getCommSetting();
HttpUnit.UserInfo = data.data.user_info;
cb && cb(data.data.user_info);
}else{
console.log("登陆失败:"+ data.msg);
cb && cb(null);
}
}
//获取关卡列表数据
public async getLevelList(cb: Function = null) {
let data: any = await HttpUnit.ins.api("game/levels", {}, "POST", true);
console.log("关卡数据:", data)
if (data.code == 1) {
cb && cb(data.data);
}else{
console.log("获取关卡数据失败:"+ data.msg);
cb && cb(null);
}
}
/**开始关卡*/
public async levelStart(msg:any, cb: Function = null) {
let data: any = await HttpUnit.ins.api("game/start", msg, "POST", true);
console.log("开始关卡:", msg, data)
if (data.code == 1) {
cb && cb(data.data);
}else{
console.log("开始关卡失败:"+ data.msg);
cb && cb(null);
}
}
/**推进关卡*/
public async sendUserTalk(msg:any, cb: Function = null) {
let data: any = await HttpUnit.ins.api("game/propel", msg, "POST", true);
console.log("推进关卡:", msg, data)
if (data.code == 1) {
cb && cb(data.data);
}else{
console.log("推进关卡失败:"+ data.msg);
cb && cb(null);
}
}
/**结束关卡*/
public async sendLevelFinish(msg:any, cb: Function = null) {
let data: any = await HttpUnit.ins.api("game/end", msg, "POST", true);
console.log("结束关卡:", msg, data)
if (data.code == 1) {
cb && cb(data.data);
}else{
console.log("结束关卡失败:"+ data.msg);
cb && cb(null);
}
}
/**增加次数,更新关卡*/
public async addTalkCnt(msg:any, cb: Function = null) {
let data: any = await HttpUnit.ins.api("game/update", msg, "POST", true);
console.log("更新关卡:", msg, data)
if (data.code == 1) {
cb && cb(data.data);
}else{
console.log("更新关卡失败:"+ data.msg);
cb && cb(null);
}
}
/**获取AI语音*/
public async getAIVoice(msg:any, cb: Function = null) {
let data: any = await HttpUnit.ins.api("game/voice", msg, "POST", true);
console.log("获取AI语音:", msg, data)
if (data.code == 1) {
cb && cb(data.data);
}else{
console.log("获取AI语音失败:"+ data.msg);
cb && cb(null);
}
}
public async api(path: string, data: any, method: "GET" | "POST" | "PUT" = "GET", bLock: boolean = false, isFormData: boolean = false) {
let url = HttpUnit.ServerHttpHost + path;
// console.log("[Http] 请求Url:",url, " 方式:", method, " Authorization:", GlobalValue.PlayerToken, " 数据:", JSON.stringify(data) );
bLock && this.lock();
var xhr = new XMLHttpRequest();
xhr.timeout = 10000;
// xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// if (cc.sys.isNative) {
// xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// }
// xhr.setRequestHeader('authorization', Config.TOKEN);
//发送数据
if (method == 'GET') {
url = url + encodeURI(this.jsonToQueryString(data));
xhr.open(method, url, true);
if (sys.isNative) {
xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
xhr.setRequestHeader("content-type", "text/html;charset=utf-8");
}
} else {
xhr.open(method, url, true);
xhr.setRequestHeader("Content-type", "application/json");
}
xhr.setRequestHeader('Authorization', "Bearer " + HttpUnit.PlayerToken);
let self = this;
return new Promise((resolve, reason) => {
self.quest(xhr, method, data, isFormData).then(value => {
bLock && self.unlock();
resolve(value);
}).catch(err => {
bLock && self.unlock();
resolve(null);
});
});
}
/**
* 发送请求
* @param url 接口地址
* @param data 消息字符串 (json格式)
* @param method 请求方式
* @param bLock 是否锁定屏幕
* @param formData 是否formData
* @returns
*/
public async send(url: string, data: any, method: "GET" | "POST" | "PUT" = "GET", bLock: boolean = false, formData: boolean = false) {
// console.log("[Http] 请求Url:",url, " 方式:", method, " 数据:", data);
bLock && this.lock();
var xhr = new XMLHttpRequest();
xhr.timeout = 10000;
if (method == 'GET') {
url = url + encodeURI(this.jsonToQueryString(data));
xhr.open(method, url, true);
if (sys.isNative) {
xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
xhr.setRequestHeader("content-type", "text/html;charset=utf-8");
}
} else {
xhr.open(method, url, true);
xhr.setRequestHeader("Content-type", "application/json");
}
let self = this;
return new Promise((resolve, reason) => {
self.quest(xhr, method, data, formData).then(value => {
bLock && self.unlock();
resolve(value);
}).catch(err => {
bLock && self.unlock();
resolve(null);
});
});
}
/**解锁屏幕 */
private unlock() {
// LockScreenUI.ins.unShowloading();
// Config.loading.hide2();
}
/**锁屏 */
private lock() {
// Config.loading.show2();
// LockScreenUI.ins.Showloading('网络加载中请稍后...');
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 398 KiB

@@ -0,0 +1,134 @@
{
"ver": "1.0.27",
"importer": "image",
"imported": true,
"uuid": "08d35734-2dd6-4012-815e-83514c2e1229",
"files": [
".json",
".png"
],
"subMetas": {
"6c48a": {
"importer": "texture",
"uuid": "08d35734-2dd6-4012-815e-83514c2e1229@6c48a",
"displayName": "mengxia_disgust",
"id": "6c48a",
"name": "texture",
"userData": {
"wrapModeS": "clamp-to-edge",
"wrapModeT": "clamp-to-edge",
"imageUuidOrDatabaseUri": "08d35734-2dd6-4012-815e-83514c2e1229",
"isUuid": true,
"visible": false,
"minfilter": "linear",
"magfilter": "linear",
"mipfilter": "none",
"anisotropy": 0
},
"ver": "1.0.22",
"imported": true,
"files": [
".json"
],
"subMetas": {}
},
"f9941": {
"importer": "sprite-frame",
"uuid": "08d35734-2dd6-4012-815e-83514c2e1229@f9941",
"displayName": "mengxia_disgust",
"id": "f9941",
"name": "spriteFrame",
"userData": {
"trimType": "none",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": 0,
"trimX": 0,
"trimY": 0,
"width": 925,
"height": 1189,
"rawWidth": 925,
"rawHeight": 1189,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"packable": true,
"pixelsToUnit": 100,
"pivotX": 0.5,
"pivotY": 0.5,
"meshType": 0,
"vertices": {
"rawPosition": [
-462.5,
-594.5,
0,
462.5,
-594.5,
0,
-462.5,
594.5,
0,
462.5,
594.5,
0
],
"indexes": [
0,
1,
2,
2,
1,
3
],
"uv": [
0,
1189,
925,
1189,
0,
0,
925,
0
],
"nuv": [
0,
0,
1,
0,
0,
1,
1,
1
],
"minPos": [
-462.5,
-594.5,
0
],
"maxPos": [
462.5,
594.5,
0
]
},
"isUuid": true,
"imageUuidOrDatabaseUri": "08d35734-2dd6-4012-815e-83514c2e1229@6c48a",
"atlasUuid": ""
},
"ver": "1.0.12",
"imported": true,
"files": [
".json"
],
"subMetas": {}
}
},
"userData": {
"type": "sprite-frame",
"hasAlpha": true,
"fixAlphaTransparencyArtifacts": false,
"redirect": "08d35734-2dd6-4012-815e-83514c2e1229@6c48a"
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 363 KiB

@@ -0,0 +1,134 @@
{
"ver": "1.0.27",
"importer": "image",
"imported": true,
"uuid": "717e9fa1-65d2-4f43-9277-7c97f2332651",
"files": [
".json",
".png"
],
"subMetas": {
"6c48a": {
"importer": "texture",
"uuid": "717e9fa1-65d2-4f43-9277-7c97f2332651@6c48a",
"displayName": "setting_1",
"id": "6c48a",
"name": "texture",
"userData": {
"wrapModeS": "clamp-to-edge",
"wrapModeT": "clamp-to-edge",
"imageUuidOrDatabaseUri": "717e9fa1-65d2-4f43-9277-7c97f2332651",
"isUuid": true,
"visible": false,
"minfilter": "linear",
"magfilter": "linear",
"mipfilter": "none",
"anisotropy": 0
},
"ver": "1.0.22",
"imported": true,
"files": [
".json"
],
"subMetas": {}
},
"f9941": {
"importer": "sprite-frame",
"uuid": "717e9fa1-65d2-4f43-9277-7c97f2332651@f9941",
"displayName": "setting_1",
"id": "f9941",
"name": "spriteFrame",
"userData": {
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": -2.5,
"trimX": 0,
"trimY": 5,
"width": 814,
"height": 624,
"rawWidth": 814,
"rawHeight": 629,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"packable": true,
"pixelsToUnit": 100,
"pivotX": 0.5,
"pivotY": 0.5,
"meshType": 0,
"vertices": {
"rawPosition": [
-407,
-312,
0,
407,
-312,
0,
-407,
312,
0,
407,
312,
0
],
"indexes": [
0,
1,
2,
2,
1,
3
],
"uv": [
0,
624,
814,
624,
0,
0,
814,
0
],
"nuv": [
0,
0,
1,
0,
0,
0.9920508744038156,
1,
0.9920508744038156
],
"minPos": [
-407,
-312,
0
],
"maxPos": [
407,
312,
0
]
},
"isUuid": true,
"imageUuidOrDatabaseUri": "717e9fa1-65d2-4f43-9277-7c97f2332651@6c48a",
"atlasUuid": ""
},
"ver": "1.0.12",
"imported": true,
"files": [
".json"
],
"subMetas": {}
}
},
"userData": {
"type": "sprite-frame",
"hasAlpha": true,
"fixAlphaTransparencyArtifacts": false,
"redirect": "717e9fa1-65d2-4f43-9277-7c97f2332651@6c48a"
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 636 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

@@ -0,0 +1,14 @@
{
"ver": "1.2.0",
"importer": "directory",
"imported": true,
"uuid": "89f25312-b72d-499a-91f2-052f6e729c3c",
"files": [],
"subMetas": {},
"userData": {
"isBundle": true,
"bundleConfigID": "default",
"bundleName": "resources",
"priority": 8
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 143 KiB

@@ -0,0 +1,134 @@
{
"ver": "1.0.27",
"importer": "image",
"imported": true,
"uuid": "cd003667-6ab0-48c9-b832-500afa5254d1",
"files": [
".json",
".png"
],
"subMetas": {
"6c48a": {
"importer": "texture",
"uuid": "cd003667-6ab0-48c9-b832-500afa5254d1@6c48a",
"displayName": "hanako_stage_icon",
"id": "6c48a",
"name": "texture",
"userData": {
"wrapModeS": "clamp-to-edge",
"wrapModeT": "clamp-to-edge",
"imageUuidOrDatabaseUri": "cd003667-6ab0-48c9-b832-500afa5254d1",
"isUuid": true,
"visible": false,
"minfilter": "linear",
"magfilter": "linear",
"mipfilter": "none",
"anisotropy": 0
},
"ver": "1.0.22",
"imported": true,
"files": [
".json"
],
"subMetas": {}
},
"f9941": {
"importer": "sprite-frame",
"uuid": "cd003667-6ab0-48c9-b832-500afa5254d1@f9941",
"displayName": "hanako_stage_icon",
"id": "f9941",
"name": "spriteFrame",
"userData": {
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": 0,
"trimX": 0,
"trimY": 0,
"width": 138,
"height": 138,
"rawWidth": 138,
"rawHeight": 138,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"packable": true,
"pixelsToUnit": 100,
"pivotX": 0.5,
"pivotY": 0.5,
"meshType": 0,
"vertices": {
"rawPosition": [
-69,
-69,
0,
69,
-69,
0,
-69,
69,
0,
69,
69,
0
],
"indexes": [
0,
1,
2,
2,
1,
3
],
"uv": [
0,
138,
138,
138,
0,
0,
138,
0
],
"nuv": [
0,
0,
1,
0,
0,
1,
1,
1
],
"minPos": [
-69,
-69,
0
],
"maxPos": [
69,
69,
0
]
},
"isUuid": true,
"imageUuidOrDatabaseUri": "cd003667-6ab0-48c9-b832-500afa5254d1@6c48a",
"atlasUuid": ""
},
"ver": "1.0.12",
"imported": true,
"files": [
".json"
],
"subMetas": {}
}
},
"userData": {
"type": "sprite-frame",
"hasAlpha": true,
"fixAlphaTransparencyArtifacts": false,
"redirect": "cd003667-6ab0-48c9-b832-500afa5254d1@6c48a"
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 343 B