| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- import { _decorator, Component, CCString, Enum } from 'cc';
- import { Log } from '../utils/Log';
- import { Random } from '../utils/Random';
- const { ccclass, property, executionOrder } = _decorator;
- @ccclass("HostData")
- export class HostData {
- @property({ tooltip: "别名" })
- public alias: string = "袁浩";
- @property({ tooltip: "地址" })
- public host: string = "172.16.15.214";
- @property({ tooltip: "端口", min: 0, step: 1 })
- public port: number = 80;
- @property({ tooltip: "SSL" })
- public SSL: boolean = false;
- }
- /**
- * 当请求出错时,抛出错误还是返回对象
- */
- export enum ErrorDataType {
- /**
- * 返回空对象
- */
- Null,
- /**
- * 抛出异常
- */
- Throw,
- /**
- * 返回无key对象:{}
- */
- Object
- }
- /**
- * Http配置组件
- * @description 用于配置HTTP
- * @author 袁浩
- */
- @ccclass('HttpSystem')
- export class HttpSystem extends Component {
- @property({ tooltip: "默认主机", step: 1, min: 0 })
- public hostIndex: number = 0;
- @property({ type: [HostData], tooltip: "主机列表" })
- public hosts = [];
- @property({ tooltip: "请求带的公共的参数,xxx=xxx", type: [CCString] })
- public tails: string[] = [];
- @property({ tooltip: "当请求出错时,抛出错误还是返回对象", type: Enum(ErrorDataType) })
- public errorDataType: ErrorDataType = ErrorDataType.Null;
- @property({ tooltip: "消息日志" })
- public useDebugLog: boolean = true;
- @property({ tooltip: "加密密钥" })
- public key = '🧧🎁🎉💰🤩😍💯🉐📦💥';
- private encryptionKey: number;
- private randomList: string[];
- private static thisObject: HttpSystem;
- private defaultTails: string[];
- start() {
- HttpSystem.thisObject = this;
- this.randomList = Array.from(this.key);
- this.defaultTails = this.tails.concat();
- }
- private random: Random;
- private _publicKey: string;
- /**
- * 公钥
- */
- private get publicKey() {
- return this._publicKey;
- }
- private set publicKey(value: string) {
- this._publicKey = value;
- let values = Array.from(value);
- let seed = '';
- for (let i = 0; i < values.length; i++) {
- seed += this.randomList.indexOf(values[i]);
- }
- this.random = new Random(parseInt(seed));
- Log.trace(`随机密钥:${value} 随机值:${seed}`);
- }
- private resetPublicKey() {
- let randomValue = Math.floor(this.random.range(0, 10000000000)) + '';
- let randomStr = "";
- for (let i = 0; i < randomValue.length; i++) {
- randomStr += this.randomList[randomValue.charAt(i)];
- }
- this._publicKey = randomStr;
- this.random.seed = parseInt(randomValue);
- Log.trace(`随机密钥:${randomStr} 随机值:${randomValue}`);
- }
- private getErrorDataType() {
- return this.errorDataType;
- }
- public getConfig(hostAlias?: string): HostData {
- if (!hostAlias) {
- return this.hosts[this.hostIndex];
- }
- for (let i = 0; i < this.hosts.length; i++) {
- const host = this.hosts[i];
- if (host.alias == hostAlias) {
- return host;
- }
- }
- }
- public get tailString() {
- var result = "";
- for (let i = 0; i < this.tails.length; i++) {
- const tail = this.tails[i];
- result = (result.length ? `${result}&${tail}` : `${tail}`)
- }
- return result;
- }
- public addTail(tails: string | string[]) {
- if (typeof tails == 'string') {
- this.tails.push(tails);
- }
- else {
- this.tails = this.tails.concat(tails);
- }
- }
- public resetTail() {
- this.tails = this.defaultTails.concat();
- }
- public setEncryptionKey(encryptionKey: number) {
- this.encryptionKey = encryptionKey;
- }
- public getPrivateKey(): number {
- return this.encryptionKey;
- }
- private serverTimestamp: number;
- private tempTime: number;
- public initServerTimestamp(value: number) {
- this.serverTimestamp = value;
- this.tempTime = Date.now();
- }
- public get serverTime() {
- return this.serverTimestamp + (Date.now() - this.tempTime);
- }
- /**
- * 根据主机别名获取主机的配置
- * @param hostAlias 主机别名
- * @returns
- */
- public static getConfig(hostAlias?: string): HostData {
- return this.thisObject.getConfig(hostAlias);
- }
- /**
- * 续签公钥
- */
- public static resetPublicKey() {
- return this.thisObject.resetPublicKey();
- }
- /**
- * 请求出错返回的数据类型
- * @returns
- */
- public static getErrorDataType() {
- return this.thisObject.getErrorDataType();
- }
- /**
- * 尾巴内容
- */
- public static get tailString() {
- return this.thisObject.tailString;
- }
- /**
- * 是否启用日志
- */
- public static get useDebugLog() {
- return this.thisObject.useDebugLog;
- }
- public static set useDebugLog(value: boolean) {
- this.thisObject.useDebugLog = value;
- }
- /**
- * 服务器当前时间
- * @returns
- */
- public static get serverTime() {
- return this.thisObject.serverTime;
- }
- /**
- * 初始化服务器时间戳
- */
- public static initServerTimestamp(value: number) {
- this.thisObject.initServerTimestamp(value);
- }
- /**
- * 获取加密私钥
- * @returns 私钥
- */
- public static getPrivateKey(): number {
- return this.thisObject.getPrivateKey();
- }
- /**
- * 加密公钥
- * @returns 私钥
- */
- public static get publicKey(): string {
- return this.thisObject.publicKey;
- }
- public static set publicKey(value: string) {
- this.thisObject.publicKey = value;
- }
- /**
- * 新增一个尾巴内容
- * @param tails 尾巴内容,xxx=xxx
- */
- public static addTail(tails: string | string[]) {
- this.thisObject.addTail(tails);
- }
- /**
- * 重置尾巴内容到默认值
- */
- public static resetTail(){
- this.thisObject.resetTail();
- }
- /**
- * 设置加密密钥
- * @param encryptionKey 密钥
- */
- public static setEncryptionKey(encryptionKey: number) {
- this.thisObject.setEncryptionKey(encryptionKey);
- }
- }
|