352 lines
12 KiB
TypeScript
352 lines
12 KiB
TypeScript
import {
|
|
ProfileResult, Config, Dictionary, RawResponse,
|
|
UserInfo, ProfileData, LogoutResult, AccountBindGameResult
|
|
} from './Result';
|
|
import { APIUtil } from './APIUtil';
|
|
import { ConfigLoader } from './ConfigLoader';
|
|
import { DeepLink } from './DeepLink';
|
|
import { get_deeplink_url, url_find_key, open_url } from './Util';
|
|
import { Platform, Language } from './Enum';
|
|
|
|
export class Coresdk {
|
|
public static prefKeyToken: string = "coresdk.token";
|
|
|
|
private static instance: Coresdk;
|
|
|
|
public config: Config;
|
|
public gameId: string;
|
|
public applicationIdentifier: string;
|
|
public apiUtil: APIUtil;
|
|
public platform: Platform;
|
|
public language: Language;
|
|
|
|
private token: string;
|
|
private coins: string;
|
|
private configLoader: ConfigLoader;
|
|
private isCoresdkInitialized: boolean;
|
|
|
|
private constructor() { }
|
|
|
|
public static getInstance(): Coresdk {
|
|
if (!Coresdk.instance) {
|
|
Coresdk.instance = new Coresdk();
|
|
Coresdk.instance.apiUtil = new APIUtil();
|
|
Coresdk.instance.configLoader = new ConfigLoader();
|
|
|
|
// 試著取出token
|
|
var token = cc.sys.localStorage.getItem(Coresdk.prefKeyToken);
|
|
if (!token)
|
|
token = "";
|
|
|
|
Coresdk.instance.token = token;
|
|
}
|
|
|
|
return Coresdk.instance;
|
|
}
|
|
|
|
public setLanguage( language:string ){
|
|
Coresdk.getInstance().setLanguage( language );
|
|
}
|
|
|
|
public initialize(gameId: string, platform: Platform, language: Language, callback: (isSuccess: boolean) => void): void {
|
|
Coresdk.getInstance().gameId = gameId;
|
|
Coresdk.getInstance().platform = platform;
|
|
Coresdk.getInstance().language = language;
|
|
|
|
// 先取得 applicationIdentifier
|
|
DeepLink.getPackageName(applicationIdentifier => {
|
|
Coresdk.getInstance().applicationIdentifier = applicationIdentifier;
|
|
|
|
|
|
// 更新設定檔及確認domain 網址
|
|
Coresdk.getInstance().configLoader.initialize((isSuccess, result) => {
|
|
if (isSuccess) {
|
|
this.isCoresdkInitialized = true;
|
|
Coresdk.getInstance().config = result;
|
|
|
|
// 輸出組態檔相關資訊
|
|
// console.log("ApiDomain:", JSON.stringify(result) );
|
|
}
|
|
|
|
if (callback)
|
|
callback(isSuccess);
|
|
});
|
|
});
|
|
}
|
|
|
|
public openLogin(callback: (result: ProfileResult) => void): void {
|
|
var token = Coresdk.getInstance().getToken();
|
|
|
|
// console.log( "autoLogin3333" );
|
|
if (token.length > 0) {
|
|
|
|
Coresdk.requestProfile(
|
|
Coresdk.getInstance().config ? Coresdk.getInstance().config.ApiDomain : "https://sadpki-portal.ebuajk.com/api",
|
|
token,
|
|
_ => callback(_));
|
|
return;
|
|
}
|
|
|
|
// 組合成指定網址
|
|
var endpoint = "/login/";
|
|
var applicationIdentifier = Coresdk.getInstance().applicationIdentifier;
|
|
var callbackUrl = get_deeplink_url(applicationIdentifier, "");
|
|
var game_id = Coresdk.getInstance().gameId;
|
|
var platform = Coresdk.getInstance().platform;
|
|
var language = Coresdk.getInstance().language;
|
|
|
|
var head = Coresdk.getInstance().config ? Coresdk.getInstance().config.Domain : "https://sadpki-portal.ebuajk.com";
|
|
|
|
console.log( "headddd" + head );
|
|
|
|
var url = head + endpoint + "?" + "login=true" + "&callback=" + callbackUrl + "&game_id=" + game_id + "&platform=" + platform + "&lang=" + language;
|
|
|
|
console.log( "autoLoginAdress" + url );
|
|
DeepLink.openURL(url, (deeplinkurl) => {
|
|
// 取得回傳網址中的token
|
|
|
|
console.log( "deeplinkurl" + deeplinkurl );
|
|
var token = url_find_key(deeplinkurl, "token");
|
|
this.setToken(token);
|
|
|
|
// 向 web api 要求 profile 資訊
|
|
Coresdk.requestProfile(
|
|
Coresdk.getInstance().config ? Coresdk.getInstance().config.ApiDomain : "https://sadpki-portal.ebuajk.com/api",
|
|
token,
|
|
_ => callback(_));
|
|
});
|
|
}
|
|
|
|
public openLogout(callback: (result: LogoutResult) => void) {
|
|
// 組合成指定網址
|
|
var endpoint = "/login/";
|
|
var applicationIdentifier = Coresdk.getInstance().applicationIdentifier;
|
|
var language = Coresdk.getInstance().language;
|
|
var callbackUrl = get_deeplink_url(applicationIdentifier, "");
|
|
|
|
var url = Coresdk.getInstance().config.Domain + endpoint + "?" + "logout=true" + "&callback=" + callbackUrl + "&lang=" + language;
|
|
DeepLink.openURL(url, _ => {
|
|
this.setToken("");
|
|
var result = new LogoutResult();
|
|
result.exception = "";
|
|
|
|
callback(result);
|
|
});
|
|
}
|
|
|
|
public openPayment(): void {
|
|
// 組合成指定網址
|
|
var endpoint = "/payment/";
|
|
var token = Coresdk.getInstance().getToken();
|
|
var domain = Coresdk.getInstance().config.PaymentDomain;
|
|
var language = Coresdk.getInstance().language;
|
|
|
|
var url = domain + endpoint + "?jwt=" + token + "&lang=" + language;
|
|
|
|
// open with browser
|
|
open_url(url);
|
|
}
|
|
|
|
public getUserCoins(): void {
|
|
// 組合成指定網址
|
|
var endpoint = "/getUserCoins";
|
|
// var url = ;
|
|
var query = new Dictionary();
|
|
query["merchantId"] = "";
|
|
query["serviceId"] = "";
|
|
var token = Coresdk.getInstance().getToken();
|
|
var domain = Coresdk.getInstance().config ? Coresdk.getInstance().config.ApiDomain : "https://sadpki-portal.ebuajk.com/api";
|
|
var url = domain + endpoint ;
|
|
Coresdk.getInstance().apiUtil.requestAPI( url, query, token, "", res => {
|
|
|
|
|
|
// this.coins = url_find_key( res.Data, "coins");
|
|
let data = JSON.parse( res.Data );
|
|
Coresdk.getInstance().coins = data.coins;
|
|
// console.log( this.coins + "获取ecoin返回数据-----" + JSON.stringify( data ) );
|
|
})
|
|
// open with browser
|
|
// Coresdk.getInstance().apiUtil.requestAPI(url, deeplinkurl => {
|
|
// this.coins = url_find_key( deeplinkurl, "coins");
|
|
// console.log( JSON.stringify( deeplinkurl ) + "地址顶顶顶顶" + this.coins );
|
|
// });
|
|
|
|
|
|
|
|
}
|
|
|
|
public openAccountBindGame(game_account: string, callback: (result: ProfileResult) => void): void {
|
|
// 組合成指定網址
|
|
var endpoint = "/bind/";
|
|
var applicationIdentifier = Coresdk.getInstance().applicationIdentifier;
|
|
var callbackUrl = get_deeplink_url(applicationIdentifier, "");
|
|
var game_id = Coresdk.getInstance().gameId;
|
|
var platform = Coresdk.getInstance().platform;
|
|
var language = Coresdk.getInstance().language;
|
|
|
|
|
|
|
|
|
|
var url = Coresdk.getInstance().config.Domain + endpoint + "?" + "game_id=" + game_id + "&game_account=" + game_account + "&callback=" + callbackUrl + "&platform=" + platform + "&lang=" + language;
|
|
|
|
|
|
console.log( "绑定游客账号返回相关bindgame" + url );
|
|
|
|
DeepLink.openURL(url, deeplinkurl => {
|
|
var token = url_find_key(deeplinkurl, "token");
|
|
|
|
console.log( deeplinkurl + "绑定游客账号返回相关bindgamesssss" + url );
|
|
var bind = url_find_key(deeplinkurl, "bind");
|
|
if (bind == "success") {
|
|
this.setToken(token);
|
|
|
|
// 向 web api 要求 profile 資訊
|
|
Coresdk.requestProfile(
|
|
Coresdk.getInstance().config.ApiDomain,
|
|
token,
|
|
_ => callback(_));
|
|
return;
|
|
}
|
|
|
|
var errorRet = new ProfileResult();
|
|
errorRet.isSuccess = false;
|
|
errorRet.exception = "bind_failed";
|
|
callback(errorRet);
|
|
});
|
|
}
|
|
|
|
public postAccountBindGame(game_account: string, callback: (result: ProfileResult) => void): void {
|
|
// 組合成指定網址
|
|
var endpoint = "/accountBindGame";
|
|
var game_id = Coresdk.getInstance().gameId;
|
|
|
|
Coresdk.postAccountBindGame(
|
|
Coresdk.getInstance().config.ApiDomain,
|
|
Coresdk.getInstance().getToken(),
|
|
game_id,
|
|
game_account,
|
|
result => {
|
|
if (result.isSuccess) {
|
|
Coresdk.requestProfile(
|
|
Coresdk.getInstance().config.ApiDomain,
|
|
Coresdk.getInstance().getToken(),
|
|
callback);
|
|
|
|
return;
|
|
}
|
|
|
|
var profileResult = new ProfileResult();
|
|
profileResult.isSuccess = false;
|
|
profileResult.exception = result.reason;
|
|
|
|
if (callback)
|
|
callback(profileResult);
|
|
});
|
|
}
|
|
|
|
public getToken(): string {
|
|
return this.token;
|
|
}
|
|
|
|
public getCoins(): string {
|
|
|
|
return Coresdk.getInstance().coins;
|
|
}
|
|
|
|
public getLoginURL(): string {
|
|
if (!this.isCoresdkInitialized) {
|
|
console.log("Please initialize() first.");
|
|
return "";
|
|
}
|
|
|
|
return Coresdk.getInstance().config.Domain + "/login/";
|
|
}
|
|
|
|
setToken(value: string): void {
|
|
Coresdk.getInstance().token = value;
|
|
cc.sys.localStorage.setItem(Coresdk.prefKeyToken, value);
|
|
}
|
|
|
|
static requestProfile(domain: string, token: string, callback: (result: ProfileResult) => void): void {
|
|
var endpoint = "/getUserInfo";
|
|
var url = domain + endpoint;
|
|
|
|
var query = new Dictionary();
|
|
query["merchantId"] = "";
|
|
query["serviceId"] = "";
|
|
|
|
Coresdk.getInstance().apiUtil.requestAPI(url, query, token, "", (res) => {
|
|
var result = Coresdk.parseProfileResult(res);
|
|
|
|
if (callback) {
|
|
callback(result);
|
|
}
|
|
});
|
|
}
|
|
|
|
static postAccountBindGame(domain: string, token: string, game_id: string, game_account: string, callback: (result: AccountBindGameResult) => void): void {
|
|
var endpoint = "/accountBindGame";
|
|
var url = domain + endpoint;
|
|
var platform = Coresdk.getInstance().platform;
|
|
|
|
var query = new Dictionary();
|
|
var body =
|
|
"game_id=" + game_id +
|
|
"&game_account=" + game_account +
|
|
"&platform=" + platform;
|
|
|
|
Coresdk.getInstance().apiUtil.requestAPI(url, query, token, body, res => {
|
|
var result = new AccountBindGameResult();
|
|
if (res.Exception != null && res.Exception.length > 0)
|
|
{
|
|
result.isSuccess = false;
|
|
result.result = "-1";
|
|
result.reason = res.Exception;
|
|
}
|
|
else
|
|
{
|
|
var d = JSON.parse(res.Data);
|
|
|
|
result.result = d["result"];
|
|
result.reason = d["reason"];
|
|
result.isSuccess = result.result == "0000";
|
|
}
|
|
|
|
if (callback)
|
|
callback(result);
|
|
|
|
});
|
|
}
|
|
|
|
static parseProfileResult(res: RawResponse): ProfileResult {
|
|
var result = new ProfileResult();
|
|
result.exception = res.Exception;
|
|
result.isSuccess = res.Exception ? false : true;
|
|
|
|
if (result.isSuccess) {
|
|
var d = JSON.parse(res.Data);
|
|
|
|
result.data = new ProfileData();
|
|
result.data.result = d.result;
|
|
result.data.server_time = d.server_time;
|
|
|
|
result.data.user_info = new UserInfo();
|
|
|
|
if (d.user_info) {
|
|
result.data.user_info.account = d.user_info.account;
|
|
result.data.user_info.account_status = d.user_info.account_status;
|
|
result.data.user_info.birthday = d.user_info.birthday;
|
|
result.data.user_info.coins = d.user_info.coins;
|
|
result.data.user_info.country = d.user_info.country;
|
|
result.data.user_info.free_coins = d.user_info.free_coins;
|
|
result.data.user_info.gender = d.user_info.gender;
|
|
result.data.user_info.hobbies = d.user_info.hobbies;
|
|
result.data.user_info.nickname = d.user_info.nickname;
|
|
result.data.user_info.user_id = d.user_info.user_id;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|