-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
koreanlancer
committed
Nov 15, 2020
1 parent
208e8e0
commit 468b47e
Showing
8 changed files
with
195 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
var X9NetworkData = cc.Class({ | ||
statics:{ | ||
TOKEN_KEY: '__token__', | ||
}, | ||
|
||
/** | ||
* Lấy character id từ URL | ||
*/ | ||
// getCharacterIDFromURL() { | ||
// let id, token; | ||
// if (cc.sys.isBrowser) { | ||
// let searchParams = new URLSearchParams(location.search); | ||
// token = searchParams.get("i"); | ||
// if(token){ | ||
// let base64Url = token.split('.')[1]; | ||
// let base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); | ||
// let jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) { | ||
// return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); | ||
// }).join('')); | ||
// // Verify | ||
// // if(KJUR){ | ||
// // var isValid = KJUR.jws.JWS.verify(jsonPayload, "x9.guru.1M$$$", ["HS256"]); | ||
// // cc.log('>>>> Charid :: ' + id + ' valid: ' + isValid); | ||
// // } | ||
// // Save token | ||
// let buf = new ArrayBuffer(jsonPayload.length*2); // 2 bytes for each char | ||
// let bufView = new Uint16Array(buf); | ||
// for (var i=0, strLen=jsonPayload.length; i < strLen; i++) { | ||
// bufView[i] = str.charCodeAt(i); | ||
// } | ||
// this._jwtToken = buf; | ||
// // | ||
// let payloadObj = JSON.parse(jsonPayload); | ||
// id = payloadObj.charId; | ||
// // | ||
// cc.log('parse token:: ' + String.fromCharCode.apply(null, new Uint16Array(this._jwtToken))) | ||
// // | ||
|
||
// }else if(CC_DEBUG){ | ||
// id = searchParams.get("id"); | ||
// } | ||
// // | ||
// id = parseInt(id); | ||
// if (id == NaN) return null; | ||
// } | ||
// return id; | ||
// }, | ||
|
||
getURLParams(key){ | ||
return new URLSearchParams(location.search).get(key); | ||
}, | ||
|
||
/** | ||
* | ||
* @param {*} tokenKey | ||
*/ | ||
setToken(tokenKey){ | ||
let classPrototype = this.constructor.prototype; | ||
classPrototype.__$request = classPrototype.__$request ? classPrototype.__$request : classPrototype.request; | ||
classPrototype.request = function(url, data, token){ | ||
let requestToken = token ? token : tokenKey; | ||
return this.__$request(url, data, requestToken); | ||
} | ||
}, | ||
|
||
|
||
/** | ||
* | ||
* @param {*} url | ||
* @param {*} data | ||
*/ | ||
request(url, data, token) { | ||
return new Promise((resolve, reject) => { | ||
let xhr = new XMLHttpRequest(); | ||
xhr.onreadystatechange = ()=>{ | ||
// this._httpStatus = xhr.status; | ||
if (xhr.readyState == 4) { | ||
if(xhr.status >= 200 && xhr.status < 400){ | ||
let response = xhr.responseText; | ||
resolve(response); | ||
}else{ | ||
reject(url); | ||
} | ||
} | ||
}; | ||
xhr.onerror = ()=>{ | ||
reject(url); | ||
} ; | ||
|
||
xhr.open( (data ? 'POST' : 'GET'), url, true ); | ||
xhr.setRequestHeader("Content-type", "application/json"); | ||
if(token){ | ||
CC_DEBUG && cc.log(this.uuid + ' Request with Token:: ' + token); | ||
xhr.setRequestHeader('Authorization', 'Bearer ' + token); | ||
} | ||
data ? xhr.send(JSON.stringify(data)) : xhr.send(); | ||
|
||
}); | ||
}, | ||
|
||
|
||
|
||
}) |
Oops, something went wrong.