HttpSystem.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { _decorator, Component, CCString, Enum } from 'cc';
  2. import { Log } from '../utils/Log';
  3. import { Random } from '../utils/Random';
  4. const { ccclass, property, executionOrder } = _decorator;
  5. @ccclass("HostData")
  6. export class HostData {
  7. @property({ tooltip: "别名" })
  8. public alias: string = "袁浩";
  9. @property({ tooltip: "地址" })
  10. public host: string = "172.16.15.214";
  11. @property({ tooltip: "端口", min: 0, step: 1 })
  12. public port: number = 80;
  13. @property({ tooltip: "SSL" })
  14. public SSL: boolean = false;
  15. }
  16. /**
  17. * 当请求出错时,抛出错误还是返回对象
  18. */
  19. export enum ErrorDataType {
  20. /**
  21. * 返回空对象
  22. */
  23. Null,
  24. /**
  25. * 抛出异常
  26. */
  27. Throw,
  28. /**
  29. * 返回无key对象:{}
  30. */
  31. Object
  32. }
  33. /**
  34. * Http配置组件
  35. * @description 用于配置HTTP
  36. * @author 袁浩
  37. */
  38. @ccclass('HttpSystem')
  39. export class HttpSystem extends Component {
  40. @property({ tooltip: "默认主机", step: 1, min: 0 })
  41. public hostIndex: number = 0;
  42. @property({ type: [HostData], tooltip: "主机列表" })
  43. public hosts = [];
  44. @property({ tooltip: "请求带的公共的参数,xxx=xxx", type: [CCString] })
  45. public tails: string[] = [];
  46. @property({ tooltip: "当请求出错时,抛出错误还是返回对象", type: Enum(ErrorDataType) })
  47. public errorDataType: ErrorDataType = ErrorDataType.Null;
  48. @property({ tooltip: "消息日志" })
  49. public useDebugLog: boolean = true;
  50. @property({ tooltip: "加密密钥" })
  51. public key = '🧧🎁🎉💰🤩😍💯🉐📦💥';
  52. private encryptionKey: number;
  53. private randomList: string[];
  54. private static thisObject: HttpSystem;
  55. private defaultTails: string[];
  56. start() {
  57. HttpSystem.thisObject = this;
  58. this.randomList = Array.from(this.key);
  59. this.defaultTails = this.tails.concat();
  60. }
  61. private random: Random;
  62. private _publicKey: string;
  63. /**
  64. * 公钥
  65. */
  66. private get publicKey() {
  67. return this._publicKey;
  68. }
  69. private set publicKey(value: string) {
  70. this._publicKey = value;
  71. let values = Array.from(value);
  72. let seed = '';
  73. for (let i = 0; i < values.length; i++) {
  74. seed += this.randomList.indexOf(values[i]);
  75. }
  76. this.random = new Random(parseInt(seed));
  77. Log.trace(`随机密钥:${value} 随机值:${seed}`);
  78. }
  79. private resetPublicKey() {
  80. let randomValue = Math.floor(this.random.range(0, 10000000000)) + '';
  81. let randomStr = "";
  82. for (let i = 0; i < randomValue.length; i++) {
  83. randomStr += this.randomList[randomValue.charAt(i)];
  84. }
  85. this._publicKey = randomStr;
  86. this.random.seed = parseInt(randomValue);
  87. Log.trace(`随机密钥:${randomStr} 随机值:${randomValue}`);
  88. }
  89. private getErrorDataType() {
  90. return this.errorDataType;
  91. }
  92. public getConfig(hostAlias?: string): HostData {
  93. if (!hostAlias) {
  94. return this.hosts[this.hostIndex];
  95. }
  96. for (let i = 0; i < this.hosts.length; i++) {
  97. const host = this.hosts[i];
  98. if (host.alias == hostAlias) {
  99. return host;
  100. }
  101. }
  102. }
  103. public get tailString() {
  104. var result = "";
  105. for (let i = 0; i < this.tails.length; i++) {
  106. const tail = this.tails[i];
  107. result = (result.length ? `${result}&${tail}` : `${tail}`)
  108. }
  109. return result;
  110. }
  111. public addTail(tails: string | string[]) {
  112. if (typeof tails == 'string') {
  113. this.tails.push(tails);
  114. }
  115. else {
  116. this.tails = this.tails.concat(tails);
  117. }
  118. }
  119. public resetTail() {
  120. this.tails = this.defaultTails.concat();
  121. }
  122. public setEncryptionKey(encryptionKey: number) {
  123. this.encryptionKey = encryptionKey;
  124. }
  125. public getPrivateKey(): number {
  126. return this.encryptionKey;
  127. }
  128. private serverTimestamp: number;
  129. private tempTime: number;
  130. public initServerTimestamp(value: number) {
  131. this.serverTimestamp = value;
  132. this.tempTime = Date.now();
  133. }
  134. public get serverTime() {
  135. return this.serverTimestamp + (Date.now() - this.tempTime);
  136. }
  137. /**
  138. * 根据主机别名获取主机的配置
  139. * @param hostAlias 主机别名
  140. * @returns
  141. */
  142. public static getConfig(hostAlias?: string): HostData {
  143. return this.thisObject.getConfig(hostAlias);
  144. }
  145. /**
  146. * 续签公钥
  147. */
  148. public static resetPublicKey() {
  149. return this.thisObject.resetPublicKey();
  150. }
  151. /**
  152. * 请求出错返回的数据类型
  153. * @returns
  154. */
  155. public static getErrorDataType() {
  156. return this.thisObject.getErrorDataType();
  157. }
  158. /**
  159. * 尾巴内容
  160. */
  161. public static get tailString() {
  162. return this.thisObject.tailString;
  163. }
  164. /**
  165. * 是否启用日志
  166. */
  167. public static get useDebugLog() {
  168. return this.thisObject.useDebugLog;
  169. }
  170. public static set useDebugLog(value: boolean) {
  171. this.thisObject.useDebugLog = value;
  172. }
  173. /**
  174. * 服务器当前时间
  175. * @returns
  176. */
  177. public static get serverTime() {
  178. return this.thisObject.serverTime;
  179. }
  180. /**
  181. * 初始化服务器时间戳
  182. */
  183. public static initServerTimestamp(value: number) {
  184. this.thisObject.initServerTimestamp(value);
  185. }
  186. /**
  187. * 获取加密私钥
  188. * @returns 私钥
  189. */
  190. public static getPrivateKey(): number {
  191. return this.thisObject.getPrivateKey();
  192. }
  193. /**
  194. * 加密公钥
  195. * @returns 私钥
  196. */
  197. public static get publicKey(): string {
  198. return this.thisObject.publicKey;
  199. }
  200. public static set publicKey(value: string) {
  201. this.thisObject.publicKey = value;
  202. }
  203. /**
  204. * 新增一个尾巴内容
  205. * @param tails 尾巴内容,xxx=xxx
  206. */
  207. public static addTail(tails: string | string[]) {
  208. this.thisObject.addTail(tails);
  209. }
  210. /**
  211. * 重置尾巴内容到默认值
  212. */
  213. public static resetTail(){
  214. this.thisObject.resetTail();
  215. }
  216. /**
  217. * 设置加密密钥
  218. * @param encryptionKey 密钥
  219. */
  220. public static setEncryptionKey(encryptionKey: number) {
  221. this.thisObject.setEncryptionKey(encryptionKey);
  222. }
  223. }