123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- /*
- ***********************************************************************************
- * 作者:xxj
- * 功能:HTTP网络请求
- * 日期:2021.6.3
- * 备注:
- ***********************************************************************************
- */
- //import { LocalStorageManager } from "../../utils/LocalStorageManager";
- export class HttpMgr {
- // 单例对象
- private static _instance: HttpMgr = null;
- //private httpObj:Laya.HttpRequest;
- /**
- * 获取单例
- */
- public static getInstance(): HttpMgr {
- if (!this._instance) {
- this._instance = new HttpMgr();
- }
- return this._instance;
- }
- constructor() {
- HttpMgr._instance = this;
- }
- /**
- * post请求,参数待扩展
- */
- public sendPost(url, data, call) {
- let httpObj = new Laya.HttpRequest();
- httpObj.http.timeout = 5000;//设置超时时间;
- httpObj.once(Laya.Event.COMPLETE, this, completeHandler);
- httpObj.once(Laya.Event.ERROR, this, errorHandler);
- httpObj.send(url, data, 'post', 'text', ["Content-Type", "application/json; charset=UTF-8"]);
- function errorHandler(error): void {
- console.log("errorHandler:" + error);
- alert("Please check your network connection.");
- }
- function completeHandler(data): void {
- if (data && data.length > 0) {
- let jsonData = JSON.parse(data);
- if (call)
- call(jsonData);
- }
- }
- }
- public getReqData() {
- let reqData = JSON.stringify({
- gameName: "TQ",
- gameUserName: "zhangSan"
- });
- return reqData;
- }
- /**
- * post请求,参数待扩展
- */
- public sendPost2025(reqUrl, data, call) {
- let isDev = false;//是否是开发环境
- // let urlStr ="https://api.kessongame.site/prod-api/api/game/";
- //////////////https://api.kessongame.site/prod-api/api/game/getClassicCfg
- let urlTop = "https://api.kessongame.site/";
- let urlMid = urlTop + "prod-api/api/";
- if (isDev) {
- urlTop = "http://192.168.1.13:8090/";
- urlMid = urlTop + "api/";
- }
- let url = urlMid + reqUrl;
- if (reqUrl == "registerForApp" || reqUrl == "loginForApp") {
- if (isDev) {
- url = urlTop + reqUrl;
- } else {
- url = urlTop + "prod-api/" + reqUrl;
- }
- }
- let headers = ["Content-Type", "application/json; charset=UTF-8"];
- //---------------
- if(reqUrl.indexOf("/api/game/") >= 0){//这里不需要鉴权
- }
- const keyForLoginInfo = "userInfo";
- const userInfoData = Laya.LocalStorage.getItem(keyForLoginInfo);
- if (userInfoData != null) {
- try {
- let bb = JSON.parse(userInfoData);
- if (bb) {
- let token = bb.token;
- headers.push("Authorization");
- headers.push(`Bearer ${token}`);
- }
- // const token = LocalStorageManager.getItem<{ username: string, token: string }>(keyForLoginInfo)?.token;
- } catch (e) {
- }
- }
- //---------------
- let httpObj = new Laya.HttpRequest();
- httpObj.http.timeout = 5000;
- httpObj.once(Laya.Event.COMPLETE, this, completeHandler);
- httpObj.once(Laya.Event.ERROR, this, errorHandler);
- // httpObj.send(url, data, 'post', 'text', ["Content-Type", "application/json; charset=UTF-8"]);
- httpObj.send(url, data, 'post', 'text', headers);
- function errorHandler(error): void {
- if (call) {
- call("{'msg':'Network exception','code':500}");//固定
- } else {
- alert("Tip:Please check your network connection.");
- }
- }
- function completeHandler(data): void {
- if (data && data.length > 0) {
- let jsonData = JSON.parse(data);
- if (call)
- call(jsonData);
- }
- }
- }
- // getHeadInfo() {
- // let headers = ["Content-Type", "application/json; charset=UTF-8"];
- // const keyForLoginInfo = "loginInfo";
- // const token = LocalStorageManager.getItem<{ username: string, token: string }>(keyForLoginInfo)?.token;
- // if (token) {
- // headers.push("Authorization");
- // headers.push(`Bearer ${token}`);
- // }
- // return headers;
- // }
- public sendGet(url, call) {
- let httpObj = new Laya.HttpRequest();
- httpObj.http.timeout = 5000;//设置超时时间;
- httpObj.once(Laya.Event.COMPLETE, this, completeHandler);
- httpObj.once(Laya.Event.ERROR, this, errorHandler);
- httpObj.send(url, '', 'get');
- function errorHandler(error): void {
- console.log("errorHandler:" + error);
- //UIMgr.getInstance().showTip("请求失败,请稍后重试");
- //call(null);
- }
- function completeHandler(data): void {
- if (data && data.length > 0) {
- //let jsonData = JSON.parse(data);
- if (call)
- call(data);
- }
- }
- }
- }
|