Commit 93271cb4 authored by liujiangnan's avatar liujiangnan

feat: 预加载功能

parent e417d2cc
...@@ -45,69 +45,60 @@ export default class NewClass extends middleLayerBase { ...@@ -45,69 +45,60 @@ export default class NewClass extends middleLayerBase {
role; role;
// 统一释放异步资源的方法 // 统一释放异步资源的方法
onLoad() { async onLoad() {
this.preloadCount = 0; this.preloadCount = 0;
this.loadedCount = 0; this.loadedCount = 0;
initAir(this); initAir(this);
this.reWriteAir(); this.reWriteAir();
// this.preloadAll();
this.role = 'student'; this.role = 'student';
} }
start() { async start() {
this.node.zIndex = 9999; this.node.zIndex = 9999;
cc.game.addPersistRootNode(this.node); cc.game.addPersistRootNode(this.node);
global.middleLayer = cc.find('middleLayer').getComponent('middleLayer'); global.middleLayer = cc.find('middleLayer').getComponent('middleLayer');
this.loadOnlineCourseWare(23075); // 预加载
// await this.preloadAll();
cc.find(`middleLayer/preload`).active = false;
this.loadOnlineCourseWare(23751);
} }
preloadAll() { async preloadAll() {
if ((<any>window).preloadBundleAndSourcesFlag) { if ((<any>window).preloadBundleAndSourcesFlag) {
// 只加载一次就行了 // 只加载一次就行了
return; return;
} }
(<any>window).preloadBundleAndSourcesFlag = true; (<any>window).preloadBundleAndSourcesFlag = true;
cc.find("middleLayer/ui/ProgressLabel").active = true;
cc.find('middleLayer/ui/loadingProgress').active = true;
let platform = "web_desktop"; let platform = "web_desktop";
if (cc.sys.os == cc.sys.OS_IOS) { if (cc.sys.os == cc.sys.OS_IOS) {
platform = "ios"; platform = "ios";
} else if (cc.sys.os == cc.sys.OS_ANDROID) { } else if (cc.sys.os == cc.sys.OS_ANDROID) {
platform = "android"; platform = "android";
} }
this.callNetworkApiGet(`/api/syllabus/v1/allbundles`, { orgid: 483, platform }, (datastr) => { const datastr:any = await this.asyncCallNetworkApiGet(`/api/syllabus/v1/allbundles`, { orgid: 512, platform });
const data = JSON.parse(datastr); const data = JSON.parse(datastr);
if (data.rows && data.rows.length > 0) { if (data.rows && data.rows.length > 0) {
this.preloadCount += data.rows.length; this.preloadCount += data.rows.length;
} }
this.callNetworkApiGet(`/api/syllabus/v1/allresources`, { orgid: 483 }, (datastr1) => { const datastr1:any = await this.asyncCallNetworkApiGet(`/api/syllabus/v1/allresources`, { orgid: 512 });
const data1 = JSON.parse(datastr1); const data1 = JSON.parse(datastr1);
if (data1.rows && data1.rows.length > 0) { if (data1.rows && data1.rows.length > 0) {
this.preloadCount += data1.rows.length; this.preloadCount += data1.rows.length;
} }
console.log('data.rows = ' + JSON.stringify(data.rows)); console.log('data.rows = ' + JSON.stringify(data.rows));
this.batchPreloadScene(data.rows); await this.preloadSceneAndSource(data, data1);
this.preloadSource(data1.rows);
});
});
} }
// 测试用 preloadSceneAndSource(scenes, sources) {
preloadSourceImman() { const p1 = this.batchPreloadScene(scenes.rows);
cc.find("middleLayer/ui/ProgressLabel").active = true; const p2 = this.preloadSource(sources.rows);
cc.find('middleLayer/ui/loadingProgress').active = true; return Promise.all([p1, p2]);
this.callNetworkApiGet(`/api/courseware/v1/getresources`, { courseid: 20894 }, (datastr1) => {
const data1 = JSON.parse(datastr1);
if (data1.rows && data1.rows.length > 0) {
this.preloadCount += data1.rows.length;
}
this.log('data1.rows = ' + JSON.stringify(data1.rows));
this.preloadSource(data1.rows);
});
} }
batchPreloadScene(scenes = []) { batchPreloadScene(scenes = []) {
return new Promise((resolve) => {
const expects = [ const expects = [
// 'op_03_2', // 'op_03_2',
// 'OP10_online', // 'OP10_online',
...@@ -124,6 +115,8 @@ export default class NewClass extends middleLayerBase { ...@@ -124,6 +115,8 @@ export default class NewClass extends middleLayerBase {
setTimeout(() => { setTimeout(() => {
load(); load();
}, 1); }, 1);
} else {
resolve();
} }
return; return;
} }
...@@ -137,6 +130,8 @@ export default class NewClass extends middleLayerBase { ...@@ -137,6 +130,8 @@ export default class NewClass extends middleLayerBase {
setTimeout(() => { setTimeout(() => {
load(); load();
}, 1); }, 1);
} else {
resolve();
} }
return; return;
} }
...@@ -149,6 +144,8 @@ export default class NewClass extends middleLayerBase { ...@@ -149,6 +144,8 @@ export default class NewClass extends middleLayerBase {
setTimeout(() => { setTimeout(() => {
load(); load();
}, 1); }, 1);
} else {
resolve();
} }
}); });
}); });
...@@ -156,11 +153,16 @@ export default class NewClass extends middleLayerBase { ...@@ -156,11 +153,16 @@ export default class NewClass extends middleLayerBase {
if (scenes && scenes.length > 0) { if (scenes && scenes.length > 0) {
load(); load();
} else {
resolve();
} }
});
} }
preloadSource(array) { preloadSource(array) {
return new Promise((resolve) => {
const rows = array || []; const rows = array || [];
let loadFlag = 0;
const batchLoad = () => { const batchLoad = () => {
let tempArr = rows.splice(0, 10); let tempArr = rows.splice(0, 10);
let len = tempArr.length; let len = tempArr.length;
...@@ -174,22 +176,26 @@ export default class NewClass extends middleLayerBase { ...@@ -174,22 +176,26 @@ export default class NewClass extends middleLayerBase {
if (i === len - 1) { if (i === len - 1) {
batchLoad(); batchLoad();
} }
loadFlag ++;
if (loadFlag == rows.length) {
resolve();
}
}); });
} }
} }
batchLoad(); batchLoad();
});
} }
updateProcessBar() { updateProcessBar() {
const label = cc.find("middleLayer/ui/ProgressLabel").getComponent(cc.Label); const label = cc.find("middleLayer/preload/progress/page").getComponent(cc.Label);
const loadingProgress = cc.find('middleLayer/ui/loadingProgress'); label.string = `${Math.floor(this.loadedCount/this.preloadCount*100)}%`;
label.string = `正在加载:${this.loadedCount} / ${this.preloadCount}`;
if (this.loadedCount > this.preloadCount * 0.99) { // 课件加载完成的钩子
cc.find("middleLayer/ui/ProgressLabel").active = false; const process = cc.find(`middleLayer/preload/progress`);
loadingProgress.active = false; const bar = cc.find(`bar`, process);
this.log("资源全部加载完成"); const barWidth = process.width / this.preloadCount * this.loadedCount;
} bar.width = barWidth;
loadingProgress.getComponent(cc.ProgressBar).progress = this.loadedCount / this.preloadCount;
} }
onHomeworkFinish(callBack, data = null) { onHomeworkFinish(callBack, data = null) {
......
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