netUtils.ts 8.6 KB

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