228 lines
5.8 KiB
TypeScript
228 lines
5.8 KiB
TypeScript
// import Text_to_Speech from "./Text_to_Speech";
|
|
|
|
const { ccclass } = cc._decorator;
|
|
|
|
@ccclass
|
|
export default class NativeClass extends cc.Component {
|
|
//#region static 屬性
|
|
|
|
private static _instance: NativeClass = null;
|
|
public static get Instance(): NativeClass { return NativeClass._instance; }
|
|
|
|
//#endregion
|
|
|
|
//#region public 屬性
|
|
|
|
public URLscheme: Object = {};
|
|
|
|
public URLschemeFlags: number = 0;
|
|
|
|
//#endregion
|
|
|
|
//#region private
|
|
|
|
// private _text_to_Speech: Text_to_Speech;
|
|
|
|
//#endregion
|
|
|
|
//#region Lifecycle
|
|
|
|
constructor() {
|
|
super();
|
|
NativeClass._instance = this;
|
|
this.init();
|
|
}
|
|
|
|
private init(): void {
|
|
let self: this = this;
|
|
// this._text_to_Speech = new Text_to_Speech();
|
|
let HttpData: any[] = [];
|
|
// let HttpParmData: any[] = [];
|
|
// 註冊要被Java、OC呼叫的事件
|
|
if (!window["JSBridge"]) {
|
|
window["JSBridge"] = function (method: string = "", value: string = ""): void {
|
|
if (method || self[method]) {
|
|
if (value) {
|
|
self[method](value);
|
|
} else if (self[method]) {
|
|
self[method]();
|
|
} else {
|
|
console.log(`not function: ${method}, value: ${value}`);
|
|
}
|
|
} else {
|
|
console.log(`not function: ${method}, value: ${value}`);
|
|
}
|
|
};
|
|
}
|
|
if (cc.sys.isBrowser) {
|
|
let url: string = window.location.search;
|
|
// let request: object = [];
|
|
if (url.indexOf("?") !== -1) {
|
|
let str: string = url.substring(1);
|
|
let strs: string[] = str.split("&");
|
|
for (let i: number = 0; i < strs.length; i++) {
|
|
this.URLscheme[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
|
|
}
|
|
}
|
|
} else {
|
|
switch (cc.sys.os) {
|
|
case cc.sys.OS_WINDOWS: {
|
|
// cc.sys.platform = 101(DESKTOP_BROWSER)
|
|
break;
|
|
}
|
|
case cc.sys.OS_ANDROID: {
|
|
// cc.sys.platform = 3(Android)
|
|
|
|
// 呼叫Java取得由URL打開時的網址(呼叫getIntent 無帶入參數(null) 回傳字串((Ljava/lang/String;)Ljava/lang/String;))
|
|
/** 取得由URL打開時的網址(內容是自己取得後用特殊符號拼成字串) */
|
|
let scheme: any = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "getURLData", "(Ljava/lang/String;)Ljava/lang/String;", null);
|
|
if (scheme && scheme !== "null") {
|
|
let _Get_Arr: string[] = scheme.split("?");
|
|
let Get_Arr: string[] = _Get_Arr[1].split("#");
|
|
for (let i: number = 0; i < Get_Arr.length; i++) {
|
|
let Get: string[] = Get_Arr[i].split("$");
|
|
HttpData[Get[0]] = Get[1];
|
|
}
|
|
let Query: string = HttpData["Query"];
|
|
if (Query) {
|
|
Get_Arr = Query.split("&");
|
|
for (let i: number = 0; i < Get_Arr.length; i++) {
|
|
let Get: string[] = Get_Arr[i].split("=");
|
|
this.URLscheme[Get[0]] = Get[1];
|
|
}
|
|
}
|
|
}
|
|
// cc.log("getIntent OK, " + getIntent);
|
|
break;
|
|
}
|
|
|
|
case cc.sys.OS_IOS: {
|
|
// cc.sys.platform = 4(iPhone)
|
|
|
|
// 呼叫OC取得由URL打開時的網址(呼叫getIntent 無帶入參數 回傳字串)(開發中功能,預計要取得由URL打開時的網址)
|
|
/** 取得由URL打開時的網址(內容是自己取得後用特殊符號拼成字串) */
|
|
let scheme: any = jsb.reflection.callStaticMethod("AppController", "getIntent");
|
|
if (scheme && scheme !== "null") {
|
|
let Get_Arr: string[] = scheme.split("#");
|
|
for (let i: number = 0; i < Get_Arr.length; i++) {
|
|
let Get: string[] = Get_Arr[i].split("$");
|
|
HttpData[Get[0]] = Get[1];
|
|
}
|
|
let Query: string = HttpData["Query"];
|
|
if (Query) {
|
|
Get_Arr = Query.split("&");
|
|
for (let i: number = 0; i < Get_Arr.length; i++) {
|
|
let Get: string[] = Get_Arr[i].split("=");
|
|
this.URLscheme[Get[0]] = Get[1];
|
|
}
|
|
}
|
|
}
|
|
// cc.log("getIntent OK, " + getIntent);
|
|
break;
|
|
}
|
|
|
|
default: {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//#endregion
|
|
|
|
//#region Custom Function
|
|
|
|
/** GetFCMToken */
|
|
public GetFCMToken(): string {
|
|
let resp: any = "";
|
|
switch (cc.sys.os) {
|
|
case cc.sys.OS_WINDOWS: {
|
|
// 網頁端
|
|
break;
|
|
}
|
|
case cc.sys.OS_ANDROID: {
|
|
break;
|
|
}
|
|
|
|
case cc.sys.OS_IOS: {
|
|
resp = jsb.reflection.callStaticMethod("AppController", "GetFCMToken");
|
|
break;
|
|
}
|
|
|
|
default: {
|
|
break;
|
|
}
|
|
}
|
|
return resp;
|
|
}
|
|
|
|
/**
|
|
* TTS_Play
|
|
* @param {string} msg msg
|
|
*/
|
|
public TTS_Play(msg: string): void {
|
|
switch (cc.sys.os) {
|
|
case cc.sys.OS_WINDOWS: {
|
|
// 網頁端
|
|
break;
|
|
}
|
|
case cc.sys.OS_ANDROID: {
|
|
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "TTS_Play", "(Ljava/lang/String;)V", msg);
|
|
// this._text_to_Speech.speak(msg);
|
|
break;
|
|
}
|
|
|
|
case cc.sys.OS_IOS: {
|
|
jsb.reflection.callStaticMethod("AppController", "TTS_Play:", msg);
|
|
break;
|
|
}
|
|
|
|
default: {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public HideNativeSplash(): void {
|
|
if (CC_JSB) {
|
|
if (cc.sys.os === cc.sys.OS_ANDROID) {
|
|
// 反射调用原生的隐藏方法
|
|
jsb.reflection.callStaticMethod(
|
|
"org/cocos2dx/javascript/AppActivity",
|
|
"hideSplash",
|
|
"()V"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
public CCLOG(msg: String): void {
|
|
console.log(msg);
|
|
}
|
|
|
|
public DispatchCallback(msg: string): void {
|
|
let data: string[] = msg.split("=");
|
|
let data1: string[] = data[1] ? data[1].split("#") : [];
|
|
|
|
let scenename: string = cc.director.getScene().name;
|
|
// cc.director.on(cc.Director.EVENT_AFTER_SCENE_LAUNCH, callback, this);
|
|
// this.NativeCallback.DispatchCallback(data[0], data1);
|
|
}
|
|
|
|
public ClearAllCache(): void {
|
|
cc.sys.localStorage.clear();
|
|
cc.assetManager.bundles.clear();
|
|
cc.assetManager.cacheManager.clearCache();
|
|
cc.game.restart();
|
|
}
|
|
|
|
//#endregion
|
|
}
|
|
|
|
//#region Enum
|
|
|
|
export enum DeepLink {
|
|
ClearAllCache = "ClearAllCache",
|
|
}
|
|
|
|
//#endregion
|