重做登录

This commit is contained in:
COMPUTER\EDY 2025-10-11 16:17:56 +08:00
parent 9367d57c7e
commit 92974027cc
3 changed files with 193 additions and 226 deletions

View File

@ -176,129 +176,118 @@ export default class GameManager extends cc.Component {
//#region 微信登录,以及读取用户信息
/** 微信登录,以及读取用户信息 */
readUserData(retryCount = 0) {
this.load3 = true;
this.load4 = true;
this.load5 = true;
this.load6 = true;
let levelInfo = cc.fx.StorageMessage.getStorage("level");
//如果本地缓存没有关卡信息,默认为服务器信息为主
if (levelInfo == undefined || levelInfo == "" || levelInfo == null) {
// this.getUserDataToServer(data.data);
const timestamp = Date.now();
cc.fx.GameConfig.GM_INFO.level = 0;
let levelInfo = { "level": cc.fx.GameConfig.GM_INFO.level, "timestamp": timestamp };
cc.fx.StorageMessage.setStorage("level", levelInfo);
cc.fx.GameConfig.GM_INFO.first = true;
// this.oldReadData(retryCount);
}
else {
cc.fx.GameConfig.GM_INFO.level = levelInfo.level;
}
cc.fx.GameTool.getHealth(null);
//@ts-ignore
// if (typeof wx !== 'undefined' && wx !== null) {
// MiniGameSdk.API.shushu_Init();
// this.nowTime = Date.now();
// // 最大重试次数
// const MAX_RETRIES = 30;
// // 延迟时间数组,按照 1 秒 3 次、2 秒 5 次、5 秒 6 次、15 秒 5 次的规则生成
// const delays = [
// ...Array(3).fill(2000),
// ...Array(5).fill(3000),
// ...Array(6).fill(5000),
// ...Array(5).fill(15000)
// ];
// //cc.fx.GameConfig.GM_INFO.shushu_DistinctId
// const attemptUserInfo = () => {
// Utils.getUserInfo((data) => {
// if (data.code == 1) { // 假设返回数据中有 success 字段表示成功
// console.log("登錄", data);
// if (data.data.openid) {
// cc.fx.GameConfig.GM_INFO.openid = data.data.openid;
// cc.fx.StorageMessage.setStorage("openid", cc.fx.GameConfig.GM_INFO.openid);
// }
// if (data.data._id) {
// cc.fx.GameConfig.GM_INFO.uid = data.data._id;
// cc.fx.StorageMessage.setStorage("uid", data.data._id);
// }
// if (data.data.onlyId) {
// cc.fx.GameConfig.GM_INFO.userId = data.data.onlyId;
// }
// if (data.data.outTradeNo.length > 0) {
// cc.fx.GameConfig.GM_INFO.allOutTradeNo = [];
// cc.fx.GameConfig.GM_INFO.allOutTradeNo = data.data.outTradeNo;
// }
// if (data.data.shareLv) {
// if (data.data.shareLv.length > 0) {
// cc.fx.GameConfig.GM_INFO.helpLevel = data.data.shareLv[0].lv;
// }
// }
// if (data.data.username) {
// cc.fx.GameConfig.GM_INFO.username = data.data.username;
// }
// if (data.data.useravatar) {
// cc.fx.GameConfig.GM_INFO.useravatarIcon = data.data.useravatar;
// if (cc.fx.GameConfig.GM_INFO.useravatarIcon.length < 10)
// cc.fx.GameConfig.GM_INFO.useravatarIcon = "icon_" + cc.fx.GameConfig.GM_INFO.useravatarIcon
// }
// if (data.data.useravatarIcon) {
// cc.fx.GameConfig.GM_INFO.useravaterkuang = data.data.useravatarIcon;
// cc.fx.GameConfig.GM_INFO.useravaterkuang = "kuang_" + (parseInt(cc.fx.GameConfig.GM_INFO.useravaterkuang) + 1);
// }
// if (data.data.task) {
// let task = JSON.parse(data.data.task);
// cc.fx.GameConfig.GM_INFO.tasks.levelPass = task["levelPass"];
// cc.fx.GameConfig.GM_INFO.tasks.share = task["share"];
// cc.fx.GameConfig.GM_INFO.tasks.useEnergy = task["useEnergy"];
// cc.fx.GameConfig.GM_INFO.tasks.useProp = task["useProp"];
// }
// this.setUserPower(data);
// this.setmonth(data);
// this.setRevive(data);
// let levelInfo = cc.fx.StorageMessage.getStorage("level");
// //如果本地缓存没有关卡信息,默认为服务器信息为主
// if (levelInfo == undefined || levelInfo == "" || levelInfo == null) {
// this.getUserDataToServer(data.data);
// // this.oldReadData(retryCount);
// }
// //新的读取数据设置方法,以本地为主
// else {
// this.getUserDataToLocal(data.data);
// // this.newReadData();
// }
// cc.fx.GameTool.getHealth((data) => {
// this.load5 = true;
// });
// this.load6 = true;
// }
// else {
// if (retryCount < MAX_RETRIES && retryCount < delays.length) {
// const delay = delays[retryCount];
// console.error(`获取用户信息失败,第 ${retryCount + 1} 次重试,将在 ${delay / 1000} 秒后重试`);
// setTimeout(() => {
// retryCount++;
// attemptUserInfo();
// }, delay);
// } else {
// console.error('获取用户信息失败,达到最大重试次数,退出游戏');
// // 退出游戏
// cc.game.end();
// }
// }
// });
// };
// attemptUserInfo();
// this.load3 = true;
// this.load4 = true;
// this.load5 = true;
// this.load6 = true;
// let levelInfo = cc.fx.StorageMessage.getStorage("level");
// //如果本地缓存没有关卡信息,默认为服务器信息为主
// if (levelInfo == undefined || levelInfo == "" || levelInfo == null) {
// // this.getUserDataToServer(data.data);
// const timestamp = Date.now();
// cc.fx.GameConfig.GM_INFO.level = 0;
// let levelInfo = { "level": cc.fx.GameConfig.GM_INFO.level, "timestamp": timestamp };
// cc.fx.StorageMessage.setStorage("level", levelInfo);
// cc.fx.GameConfig.GM_INFO.first = true;
// // this.oldReadData(retryCount);
// }
// else {
// this.load3 = true;
// this.load4 = true;
// this.load5 = true;
// this.load6 = true;
// cc.fx.GameTool.getHealth(null);
// cc.fx.GameConfig.GM_INFO.level = levelInfo.level;
// }
// cc.fx.GameTool.getHealth(null);
//@ts-ignore
if (typeof wx !== 'undefined' && wx !== null) {
MiniGameSdk.API.shushu_Init();
this.nowTime = Date.now();
// 最大重试次数
const MAX_RETRIES = 30;
// 延迟时间数组,按照 1 秒 3 次、2 秒 5 次、5 秒 6 次、15 秒 5 次的规则生成
const delays = [
...Array(3).fill(2000),
...Array(5).fill(3000),
...Array(6).fill(5000),
...Array(5).fill(15000)
];
//cc.fx.GameConfig.GM_INFO.shushu_DistinctId
const attemptUserInfo = () => {
Utils.getUserInfo((data) => {
if (data.code == 1) { // 假设返回数据中有 success 字段表示成功
console.log("登錄", data);
if (data.data.openid) {
cc.fx.GameConfig.GM_INFO.openid = data.data.openid;
cc.fx.StorageMessage.setStorage("openid", cc.fx.GameConfig.GM_INFO.openid);
}
if (data.data._id) {
cc.fx.GameConfig.GM_INFO.uid = data.data._id;
cc.fx.StorageMessage.setStorage("uid", data.data._id);
}
if (data.data.onlyId) {
cc.fx.GameConfig.GM_INFO.userId = data.data.onlyId;
}
if (data.data.shareLv) {
console.log("分享数据", data.data.shareLv);
if (data.data.shareLv.length > 0) {
cc.fx.GameConfig.GM_INFO.helpLevel = data.data.shareLv[0].lv;
}
}
if (data.data.username) {
cc.fx.GameConfig.GM_INFO.username = data.data.username;
}
// if (data.data.useravatar) {
// cc.fx.GameConfig.GM_INFO.useravatarIcon = data.data.useravatar;
// if (cc.fx.GameConfig.GM_INFO.useravatarIcon.length < 10)
// cc.fx.GameConfig.GM_INFO.useravatarIcon = "icon_" + cc.fx.GameConfig.GM_INFO.useravatarIcon
// }
// if (data.data.useravatarIcon) {
// cc.fx.GameConfig.GM_INFO.useravaterkuang = data.data.useravatarIcon;
// cc.fx.GameConfig.GM_INFO.useravaterkuang = "kuang_" + (parseInt(cc.fx.GameConfig.GM_INFO.useravaterkuang) + 1);
// }
// this.setUserPower(data);
// this.setmonth(data);
// this.setRevive(data);
let levelInfo = cc.fx.StorageMessage.getStorage("level");
//如果本地缓存没有关卡信息,默认为服务器信息为主
if (levelInfo == undefined || levelInfo == "" || levelInfo == null) {
this.getUserDataToServer(data.data);
// this.oldReadData(retryCount);
}
//新的读取数据设置方法,以本地为主
else {
this.getUserDataToLocal(data.data);
// this.newReadData();
}
cc.fx.GameTool.getHealth((data) => {
this.load5 = true;
});
this.load6 = true;
}
else {
if (retryCount < MAX_RETRIES && retryCount < delays.length) {
const delay = delays[retryCount];
console.error(`获取用户信息失败,第 ${retryCount + 1} 次重试,将在 ${delay / 1000} 秒后重试`);
setTimeout(() => {
retryCount++;
attemptUserInfo();
}, delay);
} else {
console.error('获取用户信息失败,达到最大重试次数,退出游戏');
// 退出游戏
cc.game.end();
}
}
});
};
attemptUserInfo();
}
else {
this.load3 = true;
this.load4 = true;
this.load5 = true;
this.load6 = true;
cc.fx.GameTool.getHealth(null);
}
// 存储用户数据
}
//#region 读取音乐配置
@ -465,69 +454,69 @@ export default class GameManager extends cc.Component {
//#region 设置无限体力信息
/** 设置无限体力信息 */
setUserPower(data) {
let nowTime = Math.floor(Date.now() / 1000);
let powerTime = cc.fx.StorageMessage.getStorage("userPowerTime");
//本地没缓存
if (powerTime == "undifend" || powerTime == null || powerTime == "" || powerTime == 0) {
//本地没缓存,但是服务器有缓存,判断服务器的无限体力时间是否过期
if (data.data.userPowerTime && data.data.userPowerTime != 0) {
powerTime = data.data.userPowerTime;
if (powerTime > nowTime) {
cc.fx.GameConfig.GM_INFO.userPowerTime = powerTime;
cc.fx.StorageMessage.setStorage("userPowerTime", powerTime);
}
else {
cc.fx.GameConfig.GM_INFO.userPowerTime = 0;
cc.fx.StorageMessage.setStorage("userPowerTime", 0);
}
}
//本地没缓存,服务器也没缓存
else {
powerTime = 0;
cc.fx.GameConfig.GM_INFO.userPowerTime = 0;
cc.fx.StorageMessage.setStorage("userPowerTime", 0);
}
}
//本地有缓存
else {
//本地有缓存,且服务器有缓存
if (data.data.userPowerTime && data.data.userPowerTime != 0) {
//服务器缓存大于本地缓存,以服务器缓存为主
if (data.data.userPowerTime > powerTime) {
powerTime = data.data.userPowerTime;
if (powerTime > nowTime) {
cc.fx.GameConfig.GM_INFO.userPowerTime = powerTime;
cc.fx.StorageMessage.setStorage("userPowerTime", powerTime);
}
else {
cc.fx.GameConfig.GM_INFO.userPowerTime = 0;
cc.fx.StorageMessage.setStorage("userPowerTime", 0);
}
}
//服务器缓存小于本地缓存,以本地缓存为主
else {
if (powerTime > nowTime) {
cc.fx.GameConfig.GM_INFO.userPowerTime = powerTime;
cc.fx.StorageMessage.setStorage("userPowerTime", powerTime);
}
else {
cc.fx.GameConfig.GM_INFO.userPowerTime = 0;
cc.fx.StorageMessage.setStorage("userPowerTime", 0);
}
}
}
//本地有缓存,服务器没缓存
else {
if (powerTime > nowTime) {
cc.fx.GameConfig.GM_INFO.userPowerTime = powerTime;
cc.fx.StorageMessage.setStorage("userPowerTime", powerTime);
}
else {
cc.fx.GameConfig.GM_INFO.userPowerTime = 0;
cc.fx.StorageMessage.setStorage("userPowerTime", 0);
}
}
}
// let nowTime = Math.floor(Date.now() / 1000);
// let powerTime = cc.fx.StorageMessage.getStorage("userPowerTime");
// //本地没缓存
// if (powerTime == "undifend" || powerTime == null || powerTime == "" || powerTime == 0) {
// //本地没缓存,但是服务器有缓存,判断服务器的无限体力时间是否过期
// if (data.data.userPowerTime && data.data.userPowerTime != 0) {
// powerTime = data.data.userPowerTime;
// if (powerTime > nowTime) {
// cc.fx.GameConfig.GM_INFO.userPowerTime = powerTime;
// cc.fx.StorageMessage.setStorage("userPowerTime", powerTime);
// }
// else {
// cc.fx.GameConfig.GM_INFO.userPowerTime = 0;
// cc.fx.StorageMessage.setStorage("userPowerTime", 0);
// }
// }
// //本地没缓存,服务器也没缓存
// else {
// powerTime = 0;
// cc.fx.GameConfig.GM_INFO.userPowerTime = 0;
// cc.fx.StorageMessage.setStorage("userPowerTime", 0);
// }
// }
// //本地有缓存
// else {
// //本地有缓存,且服务器有缓存
// if (data.data.userPowerTime && data.data.userPowerTime != 0) {
// //服务器缓存大于本地缓存,以服务器缓存为主
// if (data.data.userPowerTime > powerTime) {
// powerTime = data.data.userPowerTime;
// if (powerTime > nowTime) {
// cc.fx.GameConfig.GM_INFO.userPowerTime = powerTime;
// cc.fx.StorageMessage.setStorage("userPowerTime", powerTime);
// }
// else {
// cc.fx.GameConfig.GM_INFO.userPowerTime = 0;
// cc.fx.StorageMessage.setStorage("userPowerTime", 0);
// }
// }
// //服务器缓存小于本地缓存,以本地缓存为主
// else {
// if (powerTime > nowTime) {
// cc.fx.GameConfig.GM_INFO.userPowerTime = powerTime;
// cc.fx.StorageMessage.setStorage("userPowerTime", powerTime);
// }
// else {
// cc.fx.GameConfig.GM_INFO.userPowerTime = 0;
// cc.fx.StorageMessage.setStorage("userPowerTime", 0);
// }
// }
// }
// //本地有缓存,服务器没缓存
// else {
// if (powerTime > nowTime) {
// cc.fx.GameConfig.GM_INFO.userPowerTime = powerTime;
// cc.fx.StorageMessage.setStorage("userPowerTime", powerTime);
// }
// else {
// cc.fx.GameConfig.GM_INFO.userPowerTime = 0;
// cc.fx.StorageMessage.setStorage("userPowerTime", 0);
// }
// }
// }
}
//#region 设置月卡信息
/** 设置月卡信息 */

View File

@ -316,14 +316,6 @@ export namespace MiniGameSdk {
this._adVideo = wx.createRewardedVideoAd({
adUnitId: this._adUid
});
} else if (isBytedance()) {
// @ts-ignore
this._adVideo = tt.createRewardedVideoAd({
adUnitId: this._adUid,
multiton: true,
multitonRewardMsg: ['多1次奖励', '再多一次奖励', '再多一次奖励'],
multitonRewardTimes: maxVideoCount,
});
} else {
this._adVideo = null;
}
@ -1402,7 +1394,7 @@ export namespace MiniGameSdk {
//@ts-ignore
if (typeof wx !== 'undefined' && wx !== null) {
const configYinli = {
accessToken: "aGws0nluotbm6Jjiv9WMuzOAbXLydxwe", // 项目通行证,在:网站后台-->设置-->应用列表中找到Access Token列 复制(首次使用可能需要先新增应用)
accessToken: "ZxCKV476ksJvu9j2bnqQIelYNeHP0XWz", // 项目通行证,在:网站后台-->设置-->应用列表中找到Access Token列 复制(首次使用可能需要先新增应用)
clientId: cc.fx.GameConfig.GM_INFO.openid, // 用户唯一标识如产品为小游戏则必须填用户openid注意不是小游戏的APPID
name: "ge", // 全局变量名称
debugMode: "none", // 是否开启测试模式,开启测试模式后,可以在 网站后台--设置--元数据--事件流中查看实时数据上报结果。测试时使用上线之后一定要关掉改成none或者删除
@ -1468,10 +1460,23 @@ export namespace MiniGameSdk {
API._ge.payEvent(payAmount, "CNY", orderId, payReason, "微信");
}
console.log("版本:", version);
}
}
/**
* 广
* @param adType 广 rewardbannernativeinterstitialvideo_feedvideo_begin广Banner广告广广广广
* @param adUnitId 广IDadunit开头广
* @param otherProperties
*/
static yinli_AdShow(adType, adUnitId, otherProperties) {
//@ts-ignore
if (typeof wx !== 'undefined' && wx !== null) {
API._ge.adShowEvent(adType, adUnitId, otherProperties);
}
}
static yinli_Login() {
//@ts-ignore
if (typeof wx !== 'undefined' && wx !== null) {

View File

@ -14,7 +14,7 @@ export default class Utils {
//微信登录
wx.login({
success(res) {
console.log("微信login成功");
console.log("微信login成功", res);
//console.log(res.code);
if (res.code) {
// 标志位,用于记录 callBack 是否已经被调用
@ -290,6 +290,9 @@ export default class Utils {
var xhr = cc.loader.getXMLHttpRequest();
xhr.timeout = 5000;//超时时间
let dataStr = '';
if (param) {
param.gameName = "iaa";
}
Object.keys(param).forEach(key => {
dataStr += key + '=' + encodeURIComponent(param[key]) + '&';
})
@ -297,7 +300,8 @@ export default class Utils {
dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
}
url = this.httpip + url;
//console.log("请求地址:" + url);
console.log("请求地址:" + url);
console.log("send: ", dataStr);
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function () {
@ -503,37 +507,6 @@ export default class Utils {
}
}
/** 上传金币信息 */
//上传金币信息
//// static setUserCoin(callBack) {
// if (typeof wx !== 'undefined' && wx !== null) {
// let uid = cc.fx.StorageMessage.getStorage("uid");
// //旧的读取数据设置数据方法,以强联网为主
// if (uid != undefined && uid != "" && uid != null) {
// cc.fx.GameConfig.GM_INFO.uid = uid;
// }
// if (cc.fx.GameConfig.GM_INFO.coin <= 0 || cc.fx.GameConfig.GM_INFO.coin == undefined) {
// cc.fx.GameConfig.GM_INFO.coin = 0;
// }
// let setData = {
// uid: cc.fx.GameConfig.GM_INFO.uid,
// action: 'save',
// coinAmount: Math.floor(cc.fx.GameConfig.GM_INFO.coin)
// }
// //console.log("服务器:上传金币", Math.floor(cc.fx.GameConfig.GM_INFO.coin));
// Utils.POST("userCoin", setData, res => {
// //console.log("获得userCoin数据:", res);
// if (res.code === 1) {
// //console.log("服务器:上传金币成功", res);
// if (callBack) callBack(res);
// } else {
// //console.log("服务器:上传金币失败", res);
// if (callBack) callBack(res);
// }
// });
// }
// }
/** 获得关卡等级信息 */
//获得关卡等级信息