Commit 02e023e2 authored by 李维's avatar 李维

在心跳请求中,增加登录状态判断,实现单点登录

parent cb398c89
......@@ -81,6 +81,9 @@ export default class NewClass extends cc.Component {
isAnonymous: false,
isIOSReview: false,
})
// 清空信息
cc.sys.localStorage.setItem('token', "");
cc.sys.localStorage.setItem('phone', "");
}
return this.getStatus("userInfo")
}
......@@ -238,39 +241,33 @@ export default class NewClass extends cc.Component {
.start();
})
} else {
inPage.opacity = 0;
outPage.opacity = 255;
if(outPage) {
outPage.active = false;
} else {
// 未传移除页面 则全部移除
this.justShowView("")
}
inPage.active = true;
cc.tween(inPage)
.to(0.1, {
opacity: 255
})
.start();
inPage.x = 0;
inPage.y = 0;
return new Promise((resovle, reject)=>{
cc.tween(outPage)
.to(0.1, {
opacity: 0
})
.call(() => {
outPage.active = false;
resovle(true)
})
.start();
setTimeout(()=>{
resovle(true)
}, 1)
})
}
}
justShowView(viewName){
this.loginPage.active=viewName=="LoginPage"
this.homePage.active=viewName=="homePage"
this.libraryPage.active=viewName=="libraryPage"
this.libraryReadPage.active=viewName=="libraryReadPage"
this.libraryAudioPage.active=viewName=="libraryAudioPage"
this.libraryVideoPage.active=viewName=="libraryVideoPage"
this.libraryReadDetailPage.active=viewName=="libraryReadDetailPage"
this.unitPage.active=viewName=="unitPage"
this.missionPage.active=viewName=="missionPage"
justShowView(viewName) {
this.loginPage.active = viewName == "LoginPage"
this.homePage.active = viewName == "homePage"
this.libraryPage.active = viewName == "libraryPage"
this.libraryReadPage.active = viewName == "libraryReadPage"
this.libraryAudioPage.active = viewName == "libraryAudioPage"
this.libraryVideoPage.active = viewName == "libraryVideoPage"
this.libraryReadDetailPage.active = viewName == "libraryReadDetailPage"
this.unitPage.active = viewName == "unitPage"
this.missionPage.active = viewName == "missionPage"
}
// update (dt) {}
}
import { asyncDelay, randomSortByArr } from "./util";
export abstract class middleLayerBase extends cc.Component {
abstract reWriteAir(): void;
isShowWaiting = false;
hidePreloadWaiting(){
hidePreloadWaiting() {
if (!this.isShowWaiting) {
return;
}
this.isShowWaiting = false;
// 隐藏遮罩层
cc.find(`middleLayer/loadingLayer`).active = false;
}
showPreloadWaiting(){
showPreloadWaiting() {
if (this.isShowWaiting) {
return;
return;
}
this.isShowWaiting = true;
this.isShowWaiting = true;
cc.audioEngine.stopAll();
// 显示遮罩层
cc.find(`middleLayer/loadingLayer`).active = true;
......@@ -47,7 +46,7 @@ export abstract class middleLayerBase extends cc.Component {
callNetworkApiPost(uri, data, callBack, sendHook = null) {
const token = cc.sys.localStorage.getItem('token');
data = {...data, token};
data = { ...data, token };
this.getEngineInfo().then((engineInfo) => {
const xhr = new XMLHttpRequest();
const url = `${engineInfo["apiBase"]}${uri}`;
......@@ -63,9 +62,11 @@ export abstract class middleLayerBase extends cc.Component {
});
}
// 超时时间 单位秒
timeoutTime = 10;
callNetworkApiGet(uri, data, callback, sendHook = null) {
const token = cc.sys.localStorage.getItem('token');
data = {...data, token};
data = { ...data, token };
this.getEngineInfo().then((engineInfo) => {
let queryStr = '?';
const params = [];
......@@ -74,33 +75,68 @@ export abstract class middleLayerBase extends cc.Component {
params.push(`${key}=${data[key]}`);
}
}
queryStr += params.join("&");
const xhr = new XMLHttpRequest();
let flag = false;
xhr.onreadystatechange = () => {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
flag = true;
callback(JSON.parse(xhr.responseText));
const resData = JSON.parse(xhr.responseText)
// if(uri.includes("heart")) {
// console.log("--Heart info-------------------")
// console.log("token: ", token);
// console.log("响应码: ", resData.code);
// }
// 如果是心跳请求,并且有token - 判断是否已经登出
if(uri.includes("heart")) {
if(resData.code == 1005) {
// 如果是有Token的情况下 视为已经登录,登出
if(token) {
console.log("您的账号在其他设备上登录,请重新登陆")
this.showTips(`您的账号在其他设备上登录,请重新登陆`);
// 强制隐藏遮罩层
this.isShowWaiting = false;
cc.find(`middleLayer/loadingLayer`).active = false;
cc.find(`middleLayer/loading`).active = false;
// 登出
const vc = cc.find("middleLayer/Views").getComponent("PageViewer");
global.middleLayer.isLogin = false;
vc.setUserInfo({
phone: "",
isAnonymous: false,
isIOSReview: false,
});
cc.sys.localStorage.setItem('token', "");
cc.sys.localStorage.setItem('phone', "");
// 跳转登录页
vc.toView("LoginPage")
}
}
}
callback(resData);
}
};
const url = `${engineInfo["apiBase"]}${uri}${queryStr}`;
// console.log('url = ' + url);
sendHook && sendHook(xhr);
xhr.open('GET', url, true);
xhr.send();
// 五秒超时重试一次
// 超时重试一次
if (sendHook) {
// 有hook的无法重试请求
return;
}
this.scheduleOnce(() => {
if (flag) {
return;
}
xhr.abort();
this.callNetworkApiGet(uri, data, callback);
}, 10);
}, this.timeoutTime);
});
}
......@@ -160,6 +196,7 @@ export abstract class middleLayerBase extends cc.Component {
loopEngineInfo();
});
}
asyncCallNetworkApiGet(uri, data): Promise<any> {
return new Promise((resolve, reject) => {
this.callNetworkApiGet(uri, data, (res => {
......@@ -167,6 +204,7 @@ export abstract class middleLayerBase extends cc.Component {
}));
});
}
asyncCallNetworkApiPost(uri, data): Promise<any> {
return new Promise((resolve, reject) => {
this.callNetworkApiPost(uri, data, (res) => {
......@@ -206,35 +244,28 @@ export abstract class middleLayerBase extends cc.Component {
cc.assetManager.loadBundle(bondleUrl, { version: version }, async (err, bundle) => {
bundle.loadScene(sceneName, null, (finish, total, item) => {
this.setLoadingProgressBar(Math.floor(finish / total * 100));
}, (err, scene) => {
}, (err, scene) => {
this.setLoadingProgressBar(0);
this.courseOpenTimeStamp = Date.now();
cc.director.runScene(scene, null, () => {
console.log('sceneName = ' + sceneName);
const canvas = cc.find('Canvas');
const middleLayer = cc.find('middleLayer');
const wgt = canvas.getComponent(cc.Widget);
if(wgt) {
if (wgt) {
wgt.updateAlignment();
}
middleLayer.scale = canvas.width / middleLayer.width;
// 挂载清洁骨骼机制节点
const destroyer = cc.instantiate(cc.find('middleLayer/Destroyer'));
destroyer.parent = cc.find('Canvas');
destroyer.active = true;
// 手机端强制垃圾回收
if (cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_ANDROID) {
cc.sys.garbageCollect();
}
}
//显示退出按钮
this.showExitBtn(sceneName);
callback && callback();
this.IS_BUNDLE_LOADING = false;
this.oldSceneName = sceneName;
......@@ -246,10 +277,10 @@ export abstract class middleLayerBase extends cc.Component {
progressTextCom = null;
progressBarScript = null;
setLoadingProgressBar(pInt) {
if(!this.progressTextCom) {
if (!this.progressTextCom) {
this.progressTextCom = cc.find("middleLayer/loadingLayer/progressText").getComponent(cc.Label)
}
if(!this.progressBarScript) {
if (!this.progressBarScript) {
this.progressBarScript = cc.find('middleLayer/loadingLayer/prograss').getComponent('progress');
}
this.progressTextCom.string = `${pInt}%`;
......@@ -267,7 +298,6 @@ export abstract class middleLayerBase extends cc.Component {
setParentVisible(n.parent);
}
}
this.nodeList.forEach(node => {
setParentVisible(node);
});
......@@ -334,7 +364,7 @@ export abstract class middleLayerBase extends cc.Component {
this.callNetworkApiGet(api, {}, (data) => {
if (data.rows && data.rows.length > 0) {
this.courses = data.rows;
if(typeof courseIndex != null && !isNaN(courseIndex) && courseIndex > 0) {
if (typeof courseIndex != null && !isNaN(courseIndex) && courseIndex > 0) {
this.courseIndex = courseIndex - 1;
} else {
this.courseIndex = data.index || 0;
......@@ -399,7 +429,7 @@ export abstract class middleLayerBase extends cc.Component {
version = "";
bundleUrl = `${templateBaseUrl}/web_desktop`;
}
this.loadBundle(sceneName, version, bundleUrl, ()=>{
this.loadBundle(sceneName, version, bundleUrl, () => {
callback && callback(true)
});
});
......@@ -417,9 +447,11 @@ export abstract class middleLayerBase extends cc.Component {
goPrePage() {
this.goTarIndexPage(this.courseIndex - 1);
}
goNextPage() {
this.goTarIndexPage(this.courseIndex + 1);
}
goTarIndexPage(index) {
if (this.IS_BUNDLE_LOADING) {
return;
......@@ -431,7 +463,7 @@ export abstract class middleLayerBase extends cc.Component {
}
this.showPreloadWaiting();
(<any>window).courseware.freeAllOcMethod();
this.courseIndex = index;
......@@ -446,34 +478,34 @@ export abstract class middleLayerBase extends cc.Component {
}
tipsTween = null;
async showTips(str: string) {
const tipsLayer = cc.find('middleLayer/tipsLayer');
if (this.tipsTween) {
this.tipsTween.stop();
tipsLayer.active = false;
}
const label = cc.find('middleLayer/tipsLayer/tips/label');
label.getComponent(cc.Label).string = ` ${str} `;
const tips = cc.find('middleLayer/tipsLayer/tips');
tipsLayer.active = true;
tips.width = Math.max(300, label.width + 50);
this.tipsTween = cc.tween(tips)
.set({ opacity: 255 })
.to(5, { opacity: 0 })
.call(() => {
tipsLayer.active = false;
this.tipsTween = null;
})
.start();
async showTips(str: string) {
const tipsLayer = cc.find('middleLayer/tipsLayer');
if (this.tipsTween) {
this.tipsTween.stop();
tipsLayer.active = false;
}
const label = cc.find('middleLayer/tipsLayer/tips/label');
label.getComponent(cc.Label).string = ` ${str} `;
const tips = cc.find('middleLayer/tipsLayer/tips');
tipsLayer.active = true;
tips.width = Math.max(300, label.width + 50);
this.tipsTween = cc.tween(tips)
.set({ opacity: 255 })
.to(5, { opacity: 0 })
.call(() => {
tipsLayer.active = false;
this.tipsTween = null;
})
.start();
}
async hideTips () {
async hideTips() {
const tipsLayer = cc.find('middleLayer/tipsLayer');
if (this.tipsTween) {
this.tipsTween.stop();
}
this.tipsTween.stop();
}
tipsLayer.active = false;
}
......@@ -488,13 +520,12 @@ export abstract class middleLayerBase extends cc.Component {
this.setNodeListVisible();
for (let i=0; i<this.atlasList.length; i++) {
for (let i = 0; i < this.atlasList.length; i++) {
const atlas = this.atlasList[i];
const asset = this.assetList[i];
const texture = this.textureList[i];
const node = this.nodeList[i];
atlas.decRef();
cc.assetManager.releaseAsset(atlas);
atlas.destroy();
......@@ -504,10 +535,7 @@ export abstract class middleLayerBase extends cc.Component {
asset.destroy();
texture.decRef();
}
}
assetObj = null;
......@@ -516,12 +544,10 @@ export abstract class middleLayerBase extends cc.Component {
assetList = null;
textureList = null;
getDragonDisplayAssetList(node) {
const dragonDisplay = node.getComponent(dragonBones.ArmatureDisplay);
if (dragonDisplay) {
const atlas = dragonDisplay.dragonAtlasAsset;
const asset = dragonDisplay.dragonAsset;
if (atlas && atlas.texture) {
if (atlas.texture.nativeUrl.includes('h5template')) {
node.children.forEach(child => {
......@@ -529,14 +555,12 @@ export abstract class middleLayerBase extends cc.Component {
});
return;
}
this.nodeList.push(node);
this.atlasList.push(atlas);
this.assetList.push(asset);
this.textureList.push(atlas.texture);
}
}
node.children.forEach(child => {
this.getDragonDisplayAssetList(child);
});
......@@ -544,10 +568,8 @@ export abstract class middleLayerBase extends cc.Component {
toCleanParent() {
const canvas = cc.find("Canvas");
this.nodeList.forEach(node => {
node.parent = canvas;
});
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment