netUtils.ts 8.4 KB

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