Commit 18ee85bb authored by liujiangnan's avatar liujiangnan

feat: 更新base文件

parent 079e6ef8
const testMockData = {"file":"/storage/emulated/0/Android/data/com.example.oxford/cache/shengtong/gradeVoice.mp3","audioUrl":"https://staging-teach.cdn.ireadabc.com/9bee787cf2a1934ccaf7dd596ede9c91.mp3","version":"2.3.9","eof":1,"tokenId":"629f0fe93327936c0b000001","result":{"duration":"3.620","fluency":91,"integrity":100,"kernel_version":"3.7.1","overall":92,"pronunciation":95,"resource_version":"2.3.6","rhythm":74,"sentences":[{"details":[{"overall":94,"word":"what"},{"overall":98,"word":"can"},{"overall":99,"word":"i"},{"overall":77,"word":"do"},{"overall":100,"word":"for"},{"overall":100,"word":"you"}],"overall":88,"sentence":"what can i do for you"}],"speed":162}}; const testMockData = {"eof":1,"file":"/storage/emulated/0/Android/data/com.example.oxford/cache/shengtong/gradeVoice.mp3","audioUrl":"https://staging-teach.cdn.ireadabc.com/db48d49e122ba65aa7c5ee56963418a6.mp3","result":{"duration":"2.470","fluency":100,"integrity":100,"kernel_version":"3.6.7","overall":99,"pronunciation":99,"resource_version":"2.3.6","rhythm":0,"speed":78},"tokenId":"621c32543327932cfc000002"};
const recordMockData = {"text":"what can i do for you","audioUrl":"https://staging-teach.cdn.ireadabc.com/368ad56174f85e2ff1f39f12bac5c693.mp3"}; const recordMockData = {"text":"what can i do for you","audioUrl":"https://staging-teach.cdn.ireadabc.com/368ad56174f85e2ff1f39f12bac5c693.mp3"};
export function initAir(_this) { export function initAir(_this) {
...@@ -196,6 +196,10 @@ export function initAir(_this) { ...@@ -196,6 +196,10 @@ export function initAir(_this) {
}; };
window.courseware.callOcMethod("getEngineInfo", {}); window.courseware.callOcMethod("getEngineInfo", {});
}, },
exitApp() {
_this.log(`===成功调用exitApp===`);
window.courseware.callOcMethod("exitApp", {});
},
changeHost() { changeHost() {
_this.log(`===成功调用changeHost===`); _this.log(`===成功调用changeHost===`);
// 调用原生获取机器信息 // 调用原生获取机器信息
......
function asyncDelay(time) { import { asyncDelay, randomSortByArr } from "./util";
return new Promise((resolve, reject) => {
try {
setTimeout(() => {
resolve();
}, time * 1000);
} catch (e) {
reject(e);
}
})
}
export abstract class middleLayerBase extends cc.Component { export abstract class middleLayerBase extends cc.Component {
...@@ -18,6 +8,8 @@ export abstract class middleLayerBase extends cc.Component { ...@@ -18,6 +8,8 @@ export abstract class middleLayerBase extends cc.Component {
abstract hideWaitingLetters(): void; abstract hideWaitingLetters(): void;
abstract showWaitingLetters(): void; abstract showWaitingLetters(): void;
oldSceneName = null;
IS_BUNDLE_LOADING = false;
onMiddleLayerLoadingFinish() { onMiddleLayerLoadingFinish() {
if (cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS) { if (cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS) {
...@@ -75,12 +67,27 @@ export abstract class middleLayerBase extends cc.Component { ...@@ -75,12 +67,27 @@ export abstract class middleLayerBase extends cc.Component {
}); });
} }
getConfigInfo(url, callback) {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
callback(JSON.parse(xhr.responseText));
}
};
console.log('url = ' + `${url}/config.json`);
xhr.open('GET', `${url}/config.json`, true);
xhr.send();
}
DOMAIN;
async getBaseUrl(callback) { async getBaseUrl(callback) {
const engineInfo = await this.getEngineInfo(); const engineInfo = await this.getEngineInfo();
const { isDev } = JSON.parse(engineInfo); const { isDev } = JSON.parse(engineInfo);
if (isDev == 1) { if (isDev == 1) {
this.DOMAIN = "http://staging-teach.cdn.ireadabc.com/";
callback('http://staging-openapi.iteachabc.com'); callback('http://staging-openapi.iteachabc.com');
} else { } else {
this.DOMAIN = "http://teach.cdn.ireadabc.com/";
callback('http://openapi.iteachabc.com'); callback('http://openapi.iteachabc.com');
} }
} }
...@@ -96,14 +103,14 @@ export abstract class middleLayerBase extends cc.Component { ...@@ -96,14 +103,14 @@ export abstract class middleLayerBase extends cc.Component {
}); });
} }
} }
asyncCallNetworkApiGet(apiName, data) { asyncCallNetworkApiGet(apiName, data): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.callNetworkApiGet(apiName, data, (res => { this.callNetworkApiGet(apiName, data, (res => {
resolve(res); resolve(res);
})); }));
}); });
} }
asyncCallNetworkApiPost(uri, data) { asyncCallNetworkApiPost(uri, data): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.callNetworkApiPost(uri, data, (res) => { this.callNetworkApiPost(uri, data, (res) => {
resolve(res); resolve(res);
...@@ -111,49 +118,75 @@ export abstract class middleLayerBase extends cc.Component { ...@@ -111,49 +118,75 @@ export abstract class middleLayerBase extends cc.Component {
}); });
} }
loadOnlineBundle(bundleName) { loadOnlineBundle(bundleName, callback = null) {
this.callNetworkApiGet(`/api/template/v1/${bundleName}`, {}, (datastr) => { if (this.IS_BUNDLE_LOADING) {
const data = JSON.parse(datastr); return;
let configData = null; }
this.IS_BUNDLE_LOADING = true;
console.log(' in loadOnlineBundle')
this.callNetworkApiGet(`/api/template/v2/${bundleName}`, {}, (datastr) => {
const data = JSON.parse(datastr).data;
let sceneName, version, bondleUrl = "";
const templateBaseUrl = `${this.DOMAIN}h5template/${data.name}/v${data.last_version}`;
this.getConfigInfo(templateBaseUrl, (conf) => {
if (cc.sys.os == cc.sys.OS_IOS) { if (cc.sys.os == cc.sys.OS_IOS) {
configData = data.data.conf.ios; sceneName = conf.ios.sceneName;
version = conf.ios.version;
bondleUrl = `${templateBaseUrl}/ios/${conf.ios.sceneName}`;
} else if (cc.sys.os == cc.sys.OS_ANDROID) { } else if (cc.sys.os == cc.sys.OS_ANDROID) {
configData = data.data.conf.android; sceneName = conf.android.sceneName;
version = conf.android.version;
bondleUrl = `${templateBaseUrl}/android/${conf.ios.sceneName}`;
} else { } else {
configData = data.data.conf.web_desktop; sceneName = conf.android.sceneName;
version = "";
bondleUrl = `${templateBaseUrl}/web_desktop`;
} }
const sceneName = configData.sceneName; this.loadBundle(sceneName, version, bondleUrl, callback);
const version = configData.version; });
const bondleUrl = configData.bondleUrl;
this.loadBundle(sceneName, version, bondleUrl);
}); });
} }
currentBundleInfo = null; currentBundleInfo = null;
reloadBundle() { reloadBundle() {
if (this.IS_BUNDLE_LOADING) {
return;
}
this.IS_BUNDLE_LOADING = true;
const { sceneName, version, bondleUrl } = this.currentBundleInfo; const { sceneName, version, bondleUrl } = this.currentBundleInfo;
this.loadBundle(sceneName, version, bondleUrl); this.loadBundle(sceneName, version, bondleUrl);
} }
async loadBundle(sceneName, version, bondleUrl) { async loadBundle(sceneName, version, bondleUrl, callback = null) {
if (this.oldSceneName) {
await this.checkRotateScreen(this.oldSceneName, sceneName);
}
this.assetList = [];
this.textureList = [];
this.assetObj = {};
this.atlasList = [];
this.nodeList = [];
this.getDragonDisplayAssetList(cc.find('Canvas'));
this.currentBundleInfo = { sceneName, version, bondleUrl }; this.currentBundleInfo = { sceneName, version, bondleUrl };
this.showMask();
this.showWaitingLetters(); this.showWaitingLetters();
cc.audioEngine.stopAll();
const oldSceneName = this.currentBundleInfo?.sceneName; this.setNodeListVisible();
const engineInfo = await this.getEngineInfo();
const { isChanged } = JSON.parse(engineInfo); // 手机端强制垃圾回收
if (isChanged) { if (cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_ANDROID) {
await this.checkRotateScreen(oldSceneName, sceneName); cc.sys.garbageCollect();
} }
cc.assetManager.loadBundle(bondleUrl, { version: version }, async (err, bundle) => { cc.assetManager.loadBundle(bondleUrl, { version: version }, async (err, bundle) => {
console.log('bundle: ', bundle);
bundle.loadScene(sceneName, null, null, (err, scene) => { bundle.loadScene(sceneName, null, null, (err, scene) => {
const btnRestart = cc.find('middleLayer/BtnRestart'); this.courseOpenTimeStamp = Date.now();
btnRestart.active = false;
cc.audioEngine.stopAll();
cc.director.runScene(scene, null, () => { cc.director.runScene(scene, null, () => {
console.log('sceneName = ' + sceneName); console.log('sceneName = ' + sceneName);
const canvas = cc.find('Canvas'); const canvas = cc.find('Canvas');
...@@ -163,14 +196,49 @@ export abstract class middleLayerBase extends cc.Component { ...@@ -163,14 +196,49 @@ export abstract class middleLayerBase extends cc.Component {
this.hideMask(); this.hideMask();
canvas.getComponent(cc.Widget).updateAlignment(); canvas.getComponent(cc.Widget).updateAlignment();
middleLayer.scale = canvas.width / middleLayer.width; middleLayer.scale = canvas.width / middleLayer.width;
// this.showLog('middleLayer.scale = ' + middleLayer.scale);
// 挂载清洁骨骼机制节点
const destroyer = cc.instantiate(cc.find('middleLayer/Destroyer'));
destroyer.parent = cc.find('Canvas');
destroyer.active = true;
// 手机端强制垃圾回收
if (cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_ANDROID) {
cc.sys.garbageCollect();
}
callback && callback();
this.IS_BUNDLE_LOADING = false;
this.oldSceneName = sceneName;
}); });
}); });
}); });
} }
setNodeListVisible() {
const setParentVisible = (n) => {
n.active = true;
if (n.parent) {
setParentVisible(n.parent);
}
}
this.nodeList.forEach(node => {
setParentVisible(node);
});
}
getUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0,
v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
checkRotateScreen(oldSceneName: string, newSceneName: string) { checkRotateScreen(oldSceneName: string, newSceneName: string) {
const sceneNameList = []; const sceneNameList = ["cc_mz_link_write_03"];
if (sceneNameList.includes(oldSceneName) || sceneNameList.includes(newSceneName)) { if (sceneNameList.includes(oldSceneName) || sceneNameList.includes(newSceneName)) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (sceneNameList.includes(newSceneName)) { if (sceneNameList.includes(newSceneName)) {
...@@ -207,49 +275,73 @@ export abstract class middleLayerBase extends cc.Component { ...@@ -207,49 +275,73 @@ export abstract class middleLayerBase extends cc.Component {
courses = null; courses = null;
courseIndex = 0; courseIndex = 0;
courseItem = null; courseItem = null;
loadOnlineCourseWare(courseId, linkFlag = false) { courseOpenTimeStamp = null;
loadOnlineCourseWare(courseId, linkFlag = false, coursewareId = null) {
if (this.IS_BUNDLE_LOADING) {
return;
}
this.IS_BUNDLE_LOADING = true;
this.showWaitingLetters(); this.showWaitingLetters();
let api = `/api/courseware/v1/${courseId}/list`; let api = `/api/courseware/v2/${courseId}/list`;
if (linkFlag === true) { if (linkFlag === true) {
api = `/api/courseware/v1/${courseId}/eq/level/list`; api = `/api/courseware/v2/${courseId}/eq/level/list`;
} }
this.callNetworkApiGet(api, {}, (datastr) => { this.callNetworkApiGet(api, {}, (datastr) => {
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.courses = data.rows; this.courses = data.rows;
if (coursewareId) {
this.courseIndex = data.rows.findIndex(item => item.id == coursewareId);
} else {
this.courseIndex = data.index || 0; this.courseIndex = data.index || 0;
}
this.courseItem = data.rows[this.courseIndex]; this.courseItem = data.rows[this.courseIndex];
} else {
this.hideWaitingLetters();
this.showTips(`内容还未制作`);
this.IS_BUNDLE_LOADING = false;
return;
} }
if (this.courses && this.courses.length > 1 && this.courseIndex <= this.courses.length - 1) { this.loadPageBundle(() => {
if (this.courses && this.courses.length > 1 && this.courseIndex < this.courses.length - 1) {
cc.find('middleLayer/BtnRight').active = true; cc.find('middleLayer/BtnRight').active = true;
} else {
cc.find('middleLayer/BtnRight').active = false;
} }
if (this.courseIndex > 0) { if (this.courseIndex > 0) {
cc.find('middleLayer/BtnLeft').active = false; cc.find('middleLayer/BtnLeft').active = false;
} }
this.reWriteAir(); });
this.loadPageBundle();
}); });
} }
loadPageBundle() { loadPageBundle(callback = null) {
console.log(" in loadPageBundle");
let sceneName, version, bondleUrl = ""; let sceneName, version, bondleUrl = "";
const templateBaseUrl = `${this.DOMAIN}h5template/${this.courseItem.template_name}/v${this.courseItem.last_version}`;
this.getConfigInfo(templateBaseUrl, (conf) => {
if (cc.sys.os == cc.sys.OS_IOS) { if (cc.sys.os == cc.sys.OS_IOS) {
sceneName = this.courseItem.conf.ios.sceneName; sceneName = conf.ios.sceneName;
version = this.courseItem.conf.ios.version; version = conf.ios.version;
bondleUrl = this.courseItem.conf.ios.bondleUrl; bondleUrl = `${templateBaseUrl}/ios/${conf.ios.sceneName}`;
} else if (cc.sys.os == cc.sys.OS_ANDROID) { } else if (cc.sys.os == cc.sys.OS_ANDROID) {
sceneName = this.courseItem.conf.android.sceneName; sceneName = conf.android.sceneName;
version = this.courseItem.conf.android.version; version = conf.android.version;
bondleUrl = this.courseItem.conf.android.bondleUrl; bondleUrl = `${templateBaseUrl}/android/${conf.ios.sceneName}`;
} else { } else {
sceneName = this.courseItem.conf.web_desktop.sceneName; sceneName = conf.android.sceneName;
version = this.courseItem.conf.web_desktop.version; version = "";
bondleUrl = this.courseItem.conf.web_desktop.bondleUrl; bondleUrl = `${templateBaseUrl}/web_desktop`;
} }
this.loadBundle(sceneName, version, bondleUrl, callback);
this.loadBundle(sceneName, version, bondleUrl); });
} }
goPrePage() { goPrePage() {
this.goTarIndexPage(this.courseIndex - 1); this.goTarIndexPage(this.courseIndex - 1);
} }
...@@ -257,18 +349,31 @@ export abstract class middleLayerBase extends cc.Component { ...@@ -257,18 +349,31 @@ export abstract class middleLayerBase extends cc.Component {
this.goTarIndexPage(this.courseIndex + 1); this.goTarIndexPage(this.courseIndex + 1);
} }
goTarIndexPage(index) { goTarIndexPage(index) {
if (this.IS_BUNDLE_LOADING) {
return;
}
this.IS_BUNDLE_LOADING = true;
if (!this.courses || index == this.courseIndex || index < 0 || index > this.courses.length - 1) { if (!this.courses || index == this.courseIndex || index < 0 || index > this.courses.length - 1) {
this.IS_BUNDLE_LOADING = false;
return; return;
} }
this.showWaitingLetters();
(<any>window).courseware.freeAllOcMethod(); (<any>window).courseware.freeAllOcMethod();
this.courseIndex = index; this.courseIndex = index;
this.courseItem = this.courses[this.courseIndex]; this.courseItem = this.courses[this.courseIndex];
if (this.courseIndex < this.courses.length - 1) {
cc.find('middleLayer/BtnRight').active = true;
} else {
cc.find('middleLayer/BtnRight').active = false;
}
this.loadPageBundle(); this.loadPageBundle();
} }
showTips(str) { showTips(str: string) {
const tipsNode = new cc.Node(); const tipsNode = new cc.Node();
tipsNode.parent = cc.find('Canvas'); tipsNode.parent = cc.find('Canvas');
tipsNode.zIndex = 100; tipsNode.zIndex = 100;
...@@ -298,4 +403,73 @@ export abstract class middleLayerBase extends cc.Component { ...@@ -298,4 +403,73 @@ export abstract class middleLayerBase extends cc.Component {
cc.log(str); cc.log(str);
} }
async clearDragonDisplayAssetList() {
if (this.atlasList.length == 0) {
return;
}
this.setNodeListVisible();
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();
asset.decRef();
cc.assetManager.releaseAsset(asset);
asset.destroy();
texture.decRef();
}
}
assetObj = null;
nodeList = null;
atlasList = null;
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 => {
this.getDragonDisplayAssetList(child);
});
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);
});
}
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