netUtils.ts 8.1 KB

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