netUtils.ts 8.2 KB

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