| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- import notifyCenter from "./Comming/NotificationCenter";
- // import CryptoJS from "./encrypt/cryptojs";
- // import JSEncrypt from "./encrypt/jsencrypt";
- //通用工具函数类
- var utils = {
- version: "0.15",
- // baseUrl: "https://www.yuyekeji.cn/game/",
- // baseUrl: "http://127.0.0.1:26001/api_dapp/game/",
- // baseUrl: "https://www.yuyekeji.cn/api_dapp/game/",
- baseUrl: window['dappHost']+'/api_dapp/game/',
-
- api: {
- //获取public 和 系统设置
- publicKeyAndSys :'comUsers/publicKeyAndSys',
- loginToken: 'comUsers/loginToken',
- //平台账户相关信息
- userInfo: 'comUsers/getUserInfo',
- //角色的信息和属性
- playerInfo: 'comPlayers/getPlayerAndBattleAttribute',
- playerPushInfo:'comPlayers/playerPushInfo',
- playerPullInfo:'comPlayers/playerPullInfo',
-
-
- //获取宝箱
- getChest: 'comPlayerGoods/getChest',
- //开启宝箱,获取一个装备
- openChest: 'comPlayerGoods/openChest',
- //穿戴装备
- wearableEquip: 'comPlayerGoods/wearableEquip',
- },
- /** 登录获取的token */
- token: null,
- /** 后端RSA公钥 */
- javaPublicKey: null,
- /** api加密开关 */
- sysApiEncrypt: null,
- /** 设置token格式 */
- setToken(value) {
- this.token = "Bearer " + value;
- },
- setJavaPublicKeyAndSysApiEncrypt(javaPublicKey,sysApiEncrypt){
- this.javaPublicKey = javaPublicKey;
- this.sysApiEncrypt = sysApiEncrypt;
- },
- get(url, params, callback) {
- let dataStr = '';
- Object.keys(params).forEach(key => {
- dataStr += key + '=' + encodeURIComponent(params[key]) + '&';
- })
- if (dataStr !== '') {
- dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
- url = url + '?' + dataStr;
- }
- url = this.baseUrl + url;
- let xhr = cc.loader.getXMLHttpRequest();
- xhr.open("GET", url, true);
- xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
- if (this.token) {
- xhr.setRequestHeader("Authorization", this.token);
- }
- xhr.onreadystatechange = function () {
- if (xhr.readyState === 4) {
- let response = xhr.responseText;
- if (xhr.status >= 200 && xhr.status < 300) {
- // let aesKeyS = aesUtil.genKey();
- // let encryptS = aesUtil.encrypt(response, aesKeyS);
- // let aesKeyE = rsaUtil.encrypt(aesKeyS, sessionStorage.getItem('javaPublicKey')),//后端RSA公钥加密后的AES的key
- // console.log('aesKeyS:',aesKeyS);
- // console.log('encryptS:',encryptS);
- // data = aesUtil.decrypt(data.data.data, rsaUtil.decrypt(data.data.aesKey, window.jsPrivateKey));
- let httpStatus = xhr.statusText;
- notifyCenter.emit("netSuccess", JSON.parse(response));
- callback(true, JSON.parse(response));
- } else {
- callback(false, response);
- notifyCenter.emit("netError", JSON.parse(response));
- }
- }
- };
- xhr.send();
- },
- //Post请求
- post(url, param, callback) {
- url = this.baseUrl + url;
- var xhr = cc.loader.getXMLHttpRequest();
- let dataStr = '';
- Object.keys(param).forEach(key => {
- dataStr += key + '=' + encodeURIComponent(param[key]) + '&';
- })
- if (dataStr !== '') {
- dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
- }
- xhr.open("POST", url, true);
- xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- if (this.token) {
- xhr.setRequestHeader("Authorization", this.token);
- }
- xhr.onreadystatechange = function () {
- if (xhr.readyState === 4) {
- let response = xhr.responseText;
- if (xhr.status >= 200 && xhr.status < 300) {
- let httpStatus = xhr.statusText;
- notifyCenter.emit("netSuccess", JSON.parse(response));
- callback(true, JSON.parse(response));
- } else {
- callback(false, response);
- notifyCenter.emit("netError", JSON.parse(response));
- }
- }
- };
- xhr.send(dataStr);
- },
- // getKey(){
- // let aesKey = aesUtil.genKey();
- // console.log('aesKey:',aesKey);
- // }
- init(id, callback: Function, target: any){
- if(this.ws != null)return;
- // https://www.yuyekeji.cn/api_dapp/index 192.168.0.106:26001
- // wss://www.yuyekeji.cn/api_dapp/websocket/dappBack/
- console.log("utils.token:",utils.token);
- this.ws = new WebSocket(window['dappWss']+id,["11"]);
- this.ws.onopen = (event: Event)=>{
- callback.call(target, "登录成功");
- }
-
- this.ws.onmessage = (event: MessageEvent)=>{
- callback.call(target, event.data);
- }
- this.ws.onerror = function (e) {
- console.error("WebSocket连接发生错误");
- };
- },
- sendMsg(data: string){
- if(this.ws != null && this.ws.readyState != WebSocket.OPEN)return;
- this.ws.send(data);
- }
- };
- /**
- * 加解密操作简单封装一下
- */
- // let aesUtil = {
- // //获取key,
- // genKey : function (length = 16) {
- // let random = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
- // let str = "";
- // for (let i = 0; i < length; i++) {
- // str = str + random.charAt(Math.random() * random.length)
- // }
- // return str;
- // },
- // //加密
- // encrypt : function (plaintext,key) {
- // if (plaintext instanceof Object) {
- // //JSON.stringify
- // plaintext = JSON.stringify(plaintext)
- // }
- // let encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(plaintext), CryptoJS.enc.Utf8.parse(key), {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
- // return encrypted.toString();
- // },
- // //解密
- // decrypt : function (ciphertext,key) {
- // let decrypt = CryptoJS.AES.decrypt(ciphertext, CryptoJS.enc.Utf8.parse(key), {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
- // let decString = CryptoJS.enc.Utf8.stringify(decrypt).toString();
- // if(decString.charAt(0) === "{" || decString.charAt(0) === "[" ){
- // //JSON.parse
- // decString = JSON.parse(decString);
- // }
- // return decString;
- // }
- // };
- // let rsaUtil = {
- // //RSA 位数,这里要跟后端对应
- // bits: 1024,
- // //当前JSEncrypted对象
- // thisKeyPair: {},
- // //生成密钥对(公钥和私钥)
- // genKeyPair: function (bits = rsaUtil.bits) {
- // let genKeyPair = {};
- // rsaUtil.thisKeyPair = new JSEncrypt({default_key_size: bits});
- // //获取私钥
- // genKeyPair.privateKey = rsaUtil.thisKeyPair.getPrivateKey();
- // //获取公钥
- // genKeyPair.publicKey = rsaUtil.thisKeyPair.getPublicKey();
- // return genKeyPair;
- // },
- // //公钥加密
- // encrypt: function (plaintext, publicKey) {
- // if (plaintext instanceof Object) {
- // //1、JSON.stringify
- // plaintext = JSON.stringify(plaintext)
- // }
- // publicKey && rsaUtil.thisKeyPair.setPublicKey(publicKey);
- // return rsaUtil.thisKeyPair.encrypt(plaintext);
- // },
- // //私钥解密
- // decrypt: function (ciphertext, privateKey) {
- // privateKey && rsaUtil.thisKeyPair.setPrivateKey(privateKey);
- // let decString = rsaUtil.thisKeyPair.decrypt(ciphertext);
- // if(decString.charAt(0) === "{" || decString.charAt(0) === "[" ){
- // //JSON.parse
- // decString = JSON.parse(decString);
- // }
- // return decString;
- // }
- // };
- export { utils as default };
|