netUtils.ts 8.8 KB

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