netUtils.ts 8.4 KB

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