netUtils.ts 8.7 KB

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