netUtils.ts 8.5 KB

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