netUtils.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import notifyCenter from "./Comming/NotificationCenter";
  2. // import CryptoJS from "./encrypt/cryptojs";
  3. // import JSEncrypt from "./encrypt/jsencrypt";
  4. //通用工具函数类
  5. var utils = {
  6. version: "0.15",
  7. // baseUrl: "https://www.yuyekeji.cn/game/",
  8. // baseUrl: "http://127.0.0.1:26001/api_dapp/game/",
  9. // baseUrl: "https://www.yuyekeji.cn/api_dapp/game/",
  10. baseUrl: window['dappHost']+'/api_dapp/game/',
  11. api: {
  12. //获取public 和 系统设置
  13. publicKeyAndSys :'comUsers/publicKeyAndSys',
  14. loginToken: 'comUsers/loginToken',
  15. //平台账户相关信息
  16. userInfo: 'comUsers/getUserInfo',
  17. //角色的信息和属性
  18. playerInfo: 'comPlayers/getPlayerAndBattleAttribute',
  19. playerPushInfo:'comPlayers/playerPushInfo',
  20. playerPullInfo:'comPlayers/playerPullInfo',
  21. //获取宝箱
  22. getChest: 'comPlayerGoods/getChest',
  23. //开启宝箱,获取一个装备
  24. openChest: 'comPlayerGoods/openChest',
  25. //穿戴装备
  26. wearableEquip: 'comPlayerGoods/wearableEquip',
  27. },
  28. /** 登录获取的token */
  29. token: null,
  30. /** 后端RSA公钥 */
  31. javaPublicKey: null,
  32. /** api加密开关 */
  33. sysApiEncrypt: null,
  34. /** 设置token格式 */
  35. setToken(value) {
  36. this.token = "Bearer " + value;
  37. },
  38. setJavaPublicKeyAndSysApiEncrypt(javaPublicKey,sysApiEncrypt){
  39. this.javaPublicKey = javaPublicKey;
  40. this.sysApiEncrypt = sysApiEncrypt;
  41. },
  42. get(url, params, callback) {
  43. let dataStr = '';
  44. Object.keys(params).forEach(key => {
  45. dataStr += key + '=' + encodeURIComponent(params[key]) + '&';
  46. })
  47. if (dataStr !== '') {
  48. dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
  49. url = url + '?' + dataStr;
  50. }
  51. url = this.baseUrl + url;
  52. let xhr = cc.loader.getXMLHttpRequest();
  53. xhr.open("GET", url, true);
  54. xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
  55. if (this.token) {
  56. xhr.setRequestHeader("Authorization", this.token);
  57. }
  58. xhr.onreadystatechange = function () {
  59. if (xhr.readyState === 4) {
  60. let response = xhr.responseText;
  61. if (xhr.status >= 200 && xhr.status < 300) {
  62. // let aesKeyS = aesUtil.genKey();
  63. // let encryptS = aesUtil.encrypt(response, aesKeyS);
  64. // let aesKeyE = rsaUtil.encrypt(aesKeyS, sessionStorage.getItem('javaPublicKey')),//后端RSA公钥加密后的AES的key
  65. // console.log('aesKeyS:',aesKeyS);
  66. // console.log('encryptS:',encryptS);
  67. // data = aesUtil.decrypt(data.data.data, rsaUtil.decrypt(data.data.aesKey, window.jsPrivateKey));
  68. let httpStatus = xhr.statusText;
  69. notifyCenter.emit("netSuccess", JSON.parse(response));
  70. callback(true, JSON.parse(response));
  71. } else {
  72. callback(false, response);
  73. notifyCenter.emit("netError", JSON.parse(response));
  74. }
  75. }
  76. };
  77. xhr.send();
  78. },
  79. //Post请求
  80. post(url, param, callback) {
  81. url = this.baseUrl + url;
  82. var xhr = cc.loader.getXMLHttpRequest();
  83. let dataStr = '';
  84. Object.keys(param).forEach(key => {
  85. dataStr += key + '=' + encodeURIComponent(param[key]) + '&';
  86. })
  87. if (dataStr !== '') {
  88. dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
  89. }
  90. xhr.open("POST", url, true);
  91. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  92. if (this.token) {
  93. xhr.setRequestHeader("Authorization", this.token);
  94. }
  95. xhr.onreadystatechange = function () {
  96. if (xhr.readyState === 4) {
  97. let response = xhr.responseText;
  98. if (xhr.status >= 200 && xhr.status < 300) {
  99. let httpStatus = xhr.statusText;
  100. notifyCenter.emit("netSuccess", JSON.parse(response));
  101. callback(true, JSON.parse(response));
  102. } else {
  103. callback(false, response);
  104. notifyCenter.emit("netError", JSON.parse(response));
  105. }
  106. }
  107. };
  108. xhr.send(dataStr);
  109. },
  110. // getKey(){
  111. // let aesKey = aesUtil.genKey();
  112. // console.log('aesKey:',aesKey);
  113. // }
  114. init(id, callback: Function, target: any){
  115. if(this.ws != null)return;
  116. // https://www.yuyekeji.cn/api_dapp/index 192.168.0.106:26001
  117. // wss://www.yuyekeji.cn/api_dapp/websocket/dappBack/
  118. console.log("utils.token:",utils.token);
  119. this.ws = new WebSocket(window['dappWss']+id,["11"]);
  120. this.ws.onopen = (event: Event)=>{
  121. callback.call(target, "登录成功");
  122. }
  123. this.ws.onmessage = (event: MessageEvent)=>{
  124. callback.call(target, event.data);
  125. }
  126. this.ws.onerror = function (e) {
  127. console.error("WebSocket连接发生错误");
  128. };
  129. },
  130. sendMsg(data: string){
  131. if(this.ws != null && this.ws.readyState != WebSocket.OPEN)return;
  132. this.ws.send(data);
  133. }
  134. };
  135. /**
  136. * 加解密操作简单封装一下
  137. */
  138. // let aesUtil = {
  139. // //获取key,
  140. // genKey : function (length = 16) {
  141. // let random = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  142. // let str = "";
  143. // for (let i = 0; i < length; i++) {
  144. // str = str + random.charAt(Math.random() * random.length)
  145. // }
  146. // return str;
  147. // },
  148. // //加密
  149. // encrypt : function (plaintext,key) {
  150. // if (plaintext instanceof Object) {
  151. // //JSON.stringify
  152. // plaintext = JSON.stringify(plaintext)
  153. // }
  154. // let encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(plaintext), CryptoJS.enc.Utf8.parse(key), {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
  155. // return encrypted.toString();
  156. // },
  157. // //解密
  158. // decrypt : function (ciphertext,key) {
  159. // let decrypt = CryptoJS.AES.decrypt(ciphertext, CryptoJS.enc.Utf8.parse(key), {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
  160. // let decString = CryptoJS.enc.Utf8.stringify(decrypt).toString();
  161. // if(decString.charAt(0) === "{" || decString.charAt(0) === "[" ){
  162. // //JSON.parse
  163. // decString = JSON.parse(decString);
  164. // }
  165. // return decString;
  166. // }
  167. // };
  168. // let rsaUtil = {
  169. // //RSA 位数,这里要跟后端对应
  170. // bits: 1024,
  171. // //当前JSEncrypted对象
  172. // thisKeyPair: {},
  173. // //生成密钥对(公钥和私钥)
  174. // genKeyPair: function (bits = rsaUtil.bits) {
  175. // let genKeyPair = {};
  176. // rsaUtil.thisKeyPair = new JSEncrypt({default_key_size: bits});
  177. // //获取私钥
  178. // genKeyPair.privateKey = rsaUtil.thisKeyPair.getPrivateKey();
  179. // //获取公钥
  180. // genKeyPair.publicKey = rsaUtil.thisKeyPair.getPublicKey();
  181. // return genKeyPair;
  182. // },
  183. // //公钥加密
  184. // encrypt: function (plaintext, publicKey) {
  185. // if (plaintext instanceof Object) {
  186. // //1、JSON.stringify
  187. // plaintext = JSON.stringify(plaintext)
  188. // }
  189. // publicKey && rsaUtil.thisKeyPair.setPublicKey(publicKey);
  190. // return rsaUtil.thisKeyPair.encrypt(plaintext);
  191. // },
  192. // //私钥解密
  193. // decrypt: function (ciphertext, privateKey) {
  194. // privateKey && rsaUtil.thisKeyPair.setPrivateKey(privateKey);
  195. // let decString = rsaUtil.thisKeyPair.decrypt(ciphertext);
  196. // if(decString.charAt(0) === "{" || decString.charAt(0) === "[" ){
  197. // //JSON.parse
  198. // decString = JSON.parse(decString);
  199. // }
  200. // return decString;
  201. // }
  202. // };
  203. export { utils as default };