123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- // Learn TypeScript:
- // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
- // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
- // Learn Attribute:
- // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
- // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
- // Learn life-cycle callbacks:
- // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
- // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
- import { EncryptUtil } from "./EncryptUtil";
- import SDK from "../SDK";
- const { ccclass, property } = cc._decorator;
- export interface LoaderListener {
- onOk(config: any): void;
- }
- @ccclass
- export default class Utils extends cc.Component {
- private _isLoaded = false;
- // private _configUrl = "http://c.zhickeji.com:14000/gts-service/api/terminal/";
- // private _reportUrl = "http://cl.zhickeji.com:14000/gts-service/api/terminal/";
- // private _url = ["https://z.zhickeji.com/api/t/", "https://c.xianyouxx.com/api/xy/","https://ch.chumikj.com/ter/"];
- private _url = ["", "", "https://ch.chumikj.com/ter/"];
- private _isComplete = false;
- private _listener: LoaderListener[] = [];
- private _config: any;
- private static _instance: Utils = null;
- public static get instance(): Utils {
- if (Utils._instance == null) {
- Utils._instance = new Utils();
- }
- return Utils._instance;
- }
- public log(log) {
- if (this._config) {
- if (this._config.logEnable) {
- console.log(log);
- }
- }
- }
- /**随机两数之前的值 */
- public getRandomInt(min, max) {
- return Math.floor(Math.random() * Math.floor(max - min + 1)) + min;
- }
- public loadPrefabs(path, callbacks) {
- this.log("**********loadPrefabs " + path);
- let self = this;
- cc.loader.loadRes(path, (err, loadedResource) => {
- if (err) {
- cc.error(err.message || err);
- self.loadPrefabs(path, callbacks);
- return;
- }
- callbacks(loadedResource);
- });
- }
- /**post请求 */
- public sendPost(url, body) {
- let hasCallBack = false;
- let curUrl = this._url[SDK.Instance.edType] + url;
- this._isComplete = false;
- let u = ["c", "gg", "cnf"];
- if (url == u[SDK.Instance.edType]) {
- hasCallBack = true;
- curUrl = this._url[SDK.Instance.edType] + url;
- }
- this.log("**********net url is: " + curUrl);
- this.log("**********body: " + body);
- if (curUrl == null) {
- this.log("**********net url is null: " + curUrl);
- return;
- }
- body = EncryptUtil.en(body);
- this.log("**********bodyEncrypt : " + body);
- let self = this;
- let req = new XMLHttpRequest();
- let onReadyStateChange = (ev: Event) => {
- if (req.readyState == 4) {
- if (req.status == 200) {
- self.log("ok: " + req.response);
- let reqR = EncryptUtil.de(req.response);
- self.log("reqR ok: " + reqR);
- let obj = JSON.parse(reqR);
- if (obj) {
- if (self._isComplete || !hasCallBack) {
- return;
- }
- self._isComplete = true;
- self.log("reqR ok: " + obj);
- self.ok(obj);
- }
- } else {
- self.ok(null);
- }
- }
- }
- let onTimeOut = (ev: ProgressEvent) => { }
- let onError = (ev: ProgressEvent) => { }
- req.onreadystatechange = onReadyStateChange;
- req.ontimeout = onTimeOut;
- req.onerror = onError;
- req.open("POST", curUrl, true);
- req.setRequestHeader("Content-Type", "application/json");
- req.send(body);
- }
- /**
- * 加载完成
- *
- * @param config 配置
- */
- public ok(config: any) {
- this._config = config;
- if (config != null) {
- this._isLoaded = true;
- }
- if (this._listener.length > 0) {
- this._listener.forEach(function (lst, index) {
- if (lst != null) {
- lst.onOk(config);
- }
- });
- }
- }
- /**
- * 添加一个监听器
- *
- * @param listener 监听器
- */
- public addListener(listener: LoaderListener) {
- this._listener.push(listener);
- }
- /**
- * 是否已经加载
- */
- public isLoaded() {
- return this._isLoaded;
- }
- // update (dt) {}
- }
|