import NetConfig from "./NetConfig"; import User from "../User/User"; import AesTools from "./AesTools"; import AppConfig from "../Config/AppConfig"; import { LogUtils } from "../Util/LogUtils"; import AppPlatform from "../Util/AppPlatform"; export class requestData { public meth_custom: string = "post"; public readonly data_custom: any; public url_custom: string = ""; public onSuccess_custom: Function = null; public onFail_custom: Function = null; constructor() { this.data_custom = {}; } } export default class HttpUnit { /** * 发送 Http 请求 * @param req * @param sendMsg * @param completeFunc * @param errorFunc * @param headers */ public static sendHttpUrl_custom(req: requestData, sendMsg: string, completeFunc: Function, errorFunc: Function, headers?: any) { let headersTab = headers || {}; //头数据 let xhr: XMLHttpRequest = new XMLHttpRequest(); //请求过程发生意外的回调 xhr.onerror = function (error) { LogUtils.networkError_custom("网络请求异常" + error) errorFunc("网络请求异常"); } //超时 xhr.ontimeout = function (error) { LogUtils.networkError_custom("网络超时" + error) errorFunc("网络超时"); } xhr.onreadystatechange = function () { /* 0 UNSENT 代理被创建,但尚未调用 open() 方法。 1 OPENED open() 方法已经被调用。 2 HEADERS_RECEIVED send() 方法已经被调用,并且头部和状态已经可获得。 3 LOADING 下载中; responseText 属性已经包含部分数据。 4 DONE 下载操作已完成。 */ let readyState = xhr.readyState; let status = xhr.status; if (readyState === 4) { if (status >= 200 && status < 300) { let responseText = xhr.responseText; // LogUtils.networkLog_custom("接收数据:----------------------") // LogUtils.networkLog_custom("接收内容:" + responseText) const response = JSON.parse(responseText); completeFunc(response); } else { let statusText = xhr.statusText; // LogUtils.networkError_custom("接收数据异常:----------------------status " + status) // LogUtils.networkLog_custom("接收内容:" + statusText) errorFunc(statusText); } } } xhr.open(req.meth_custom, req.url_custom, true); if (headersTab) { for (let key in headersTab) { xhr.setRequestHeader(key, headersTab[key]); } } // LogUtils.networkLog_custom("发送数据:----------------------") // LogUtils.networkLog_custom(req.url_custom + " " + req.meth_custom) // LogUtils.networkLog_custom(sendMsg); // LogUtils.networkLog_custom(JSON.stringify(headers)); // LogUtils.networkLog_custom("----------------------") xhr.send(sendMsg); } public static request_custom(req: requestData) { if (req.url_custom.indexOf("https://") > -1 || req.url_custom.indexOf("http://") > -1) { req.url_custom = req.url_custom; } else { // if (AppPlatform.is_BD_GAME_custom()) {//百度的用test域名 // req.url_custom = NetConfig.serverUrl_test_custom + req.url_custom; // } // // else if (AppPlatform.is_VIVO_GAME_custom() || AppPlatform.is_Android_custom()) {//vivo andorid 用php // // req.url_custom = NetConfig.php_serverUrl_custom + req.url_custom; // // } // else { req.url_custom = NetConfig.serverUrl_custom + req.url_custom; // } } let completeFunc = (res) => { LogUtils.networkLog_custom(res, "http Success") if (req.onSuccess_custom) { req.onSuccess_custom(res); } req.onSuccess_custom = null; req = null; }; let fail = req.onFail_custom; let errorFunc = (res) => { LogUtils.networkLog_custom(res, "http fail") if (fail) { fail(res); } req && (req.onFail_custom = null); fail = null; req = null; }; //let dataStr: string = JSON.stringify(req.data); // if (Laya.Browser.onMiniGame || AppConfig.onTTMiniGame) { // req.data.code = User.code; // } // else if (Laya.Browser.onQGMiniGame) //OPPO小游戏 // { // req.data.oppotoken = User.code; // } // else if (Laya.Browser.onQQMiniGame) //qq小游戏 // { // req.data.code = User.code; // } // else { if (AppPlatform.is_OPPO_GAME_custom()) { req.data_custom.oppotoken = User.code_custom; } else if (AppPlatform.is_Android_custom()) { req.data_custom.sbh = User.code_custom; } req.data_custom.code = User.code_custom; //} //头文件 let time = "time=" + String(Date.now()); let headers = {}; //headers["Access-Control-Allow-Origin"] = "*"; headers["Content-Type"] = "application/json"; headers["state"] = AppConfig.state_custom; headers["gameid"] = AppConfig.gameid_custom; headers["sign"] = AesTools.encrypt_custom(time); if (User.token_custom) { headers["token"] = User.token_custom; } //发送数据 let sendMsg = null; if (req.data_custom) { sendMsg = JSON.stringify(req.data_custom) } this.sendHttpUrl_custom(req, sendMsg, completeFunc, errorFunc, headers); } //todo:这里添加你们和服务器相互的接口 public static getServerTime_custom(onSuccess: Function, onFail: Function) { var req = new requestData(); req.url_custom = NetConfig.Get_ServerTime_custom; req.onSuccess_custom = onSuccess; req.onFail_custom = onFail; HttpUnit.request_custom(req); } //todo:这里添加你们和服务器相互的接口 public static login_custom(onSuccess: Function, onFail: Function) { var req = new requestData(); req.url_custom = NetConfig.Login_custom; req.onSuccess_custom = onSuccess; req.onFail_custom = onFail; // if (AppPlatform.is_VIVO_GAME_custom()) {//vivo 用php // req.url_custom = NetConfig.php_Login_custom; // } HttpUnit.request_custom(req); } public static saveGameData_custom(gameData: any, onSuccess: Function, onFail: Function) { var req = new requestData(); req.url_custom = NetConfig.SaveGameData_custom; // if (AppPlatform.is_VIVO_GAME_custom() || AppPlatform.is_Android_custom()) {//vivo andorid 用php // req.url_custom = NetConfig.php_SaveGameData_custom; // } req.data_custom.gameData = gameData; req.onSuccess_custom = onSuccess; req.onFail_custom = onFail; HttpUnit.request_custom(req); } public static getGameData_custom(onSuccess: Function, onFail: Function) { var req = new requestData(); req.url_custom = NetConfig.GetUser_custom; req.onSuccess_custom = onSuccess; // if (AppPlatform.is_VIVO_GAME_custom() || AppPlatform.is_Android_custom()) {//vivo andorid 用php // req.url_custom = NetConfig.php_GetUser_custom; // let fucSuccess = (res) => { // if (res.data && res.data.gamedata) {//对php的数据做加工 // let _cdata = res.data.gamedata; // res.data = _cdata; // } // if (onSuccess) { // onSuccess(res); // } // } // req.onSuccess_custom = fucSuccess; // } req.onFail_custom = onFail; HttpUnit.request_custom(req); } /** * IP屏蔽方法,需要在NetConfig类中设置IpBlock的接口地址 * onSuccess方法返回参数的范例为 Object {code: 0, msg: "准一线", time: "1571034447", data: null} * @static * @memberof HttpUnit */ public static GetIpBlock_custom(onSuccess: Function, onFail: Function) { if (-1 != AppConfig.gameid_custom) { console.error("启动新版本的屏蔽系统") let url = "https://wxxyx.renyouwangluo.cn/renyou_other_template/ipLogin" let headersTab = {}; //头数据 let xhr: XMLHttpRequest = new XMLHttpRequest(); xhr.timeout = 15000; //请求过程发生意外的回调 xhr.onerror = function (error) { LogUtils.networkError_custom("网络请求异常" + error) onFail("网络请求异常"); } //超时 xhr.ontimeout = function (error) { LogUtils.networkError_custom("网络超时" + error) onFail("网络超时"); } xhr.onreadystatechange = function () { /* 0 UNSENT 代理被创建,但尚未调用 open() 方法。 1 OPENED open() 方法已经被调用。 2 HEADERS_RECEIVED send() 方法已经被调用,并且头部和状态已经可获得。 3 LOADING 下载中; responseText 属性已经包含部分数据。 4 DONE 下载操作已完成。 */ let readyState = xhr.readyState; let status = xhr.status; if (readyState === 4) { let tab = { code: 1 } if (status >= 200 && status < 300) { let responseText = xhr.responseText; LogUtils.networkLog_custom("接收数据:----------------------") LogUtils.networkLog_custom("接收内容:" + responseText) const response = JSON.parse(responseText); if (response && response.isBlackIp == 0) {//没有屏蔽 tab.code = 0; console.error("新版本的屏蔽系统 不屏蔽") } else { console.error("新版本的屏蔽系统 ", responseText) } onSuccess(tab); } else { let statusText = xhr.statusText; LogUtils.networkError_custom("接收数据异常:----------------------status " + status) LogUtils.networkLog_custom("接收内容:" + statusText) onSuccess(tab); } } } xhr.open("POST", url, true); if (!headersTab["Content-Type"]) { headersTab["Content-Type"] = "application/json"; } if (headersTab) { for (let key in headersTab) { xhr.setRequestHeader(key, headersTab[key]); } } //1351 let params = { gameId: AppConfig.gameid_custom }; // let params = { gameId: 1351 }; let sendMsg = JSON.stringify(params); xhr.send(sendMsg); } } public static reportExport_custom(appid: string, game_name: string, onSuccess: Function, onFail: Function) { var req = new requestData(); req.url_custom = NetConfig.reportExport_custom; req.data_custom.wbappid = appid; req.data_custom.game_name = game_name; req.onSuccess_custom = onSuccess; req.onFail_custom = onFail; HttpUnit.request_custom(req); } public static reportImport_custom(appid: string, channel: string, onSuccess: Function, onFail: Function) { var req = new requestData(); req.url_custom = NetConfig.reportImport_custom; req.data_custom.wbappid = appid; req.data_custom.channel = channel; req.onSuccess_custom = onSuccess; req.onFail_custom = onFail; HttpUnit.request_custom(req); } public static Getuserip_custom(onSuccess: Function, onFail: Function) { var req = new requestData(); req.url_custom = NetConfig.getuserip_custom; // if (AppPlatform.is_VIVO_GAME_custom() || AppPlatform.is_Android_custom()) {//vivo andorid 用php // req.url_custom = NetConfig.php_getuserip_custom; // } req.onSuccess_custom = onSuccess; req.onFail_custom = onFail; HttpUnit.request_custom(req); } //签到 public static SignIn_custom(onSuccess: Function, onFail: Function) { var req = new requestData(); req.url_custom = NetConfig.signin_custom; // if (AppPlatform.is_VIVO_GAME_custom() || AppPlatform.is_Android_custom()) {//vivo andorid 用php // req.url_custom = NetConfig.php_signin_custom; // } req.onSuccess_custom = onSuccess; req.onFail_custom = onFail; req.data_custom.type = 1; HttpUnit.request_custom(req); } //获取签到状态 public static GetSignIn_custom(onSuccess: Function, onFail: Function) { var req = new requestData(); req.url_custom = NetConfig.signin_custom; // if (AppPlatform.is_VIVO_GAME_custom() || AppPlatform.is_Android_custom()) {//vivo andorid 用php // req.url_custom = NetConfig.php_signin_custom; // } req.onSuccess_custom = onSuccess; req.onFail_custom = onFail; req.data_custom.type = 0; HttpUnit.request_custom(req); } //抖音上报渠道参数 public static reportTTLaunchChannel_custom(ak: string, cd: string, bid: string, onSuccess: Function, onFail: Function) { var req = new requestData(); req.url_custom = NetConfig.ttReportLaunchChannel_custom; req.onSuccess_custom = onSuccess; req.onFail_custom = onFail; req.data_custom.ak = ak; req.data_custom.bid = bid; req.data_custom.cd = cd; HttpUnit.request_custom(req); } /** * 扫码屏蔽 * @param data * @param onSuccess * @param onFail */ public static userScanCode_custom(data: any, onSuccess?: Function, onFail?: Function) { var req = new requestData(); req.url_custom = NetConfig.userScanCode_custom; req.onSuccess_custom = onSuccess; req.onFail_custom = onFail; req.data_custom.code = data.code; req.data_custom.state = data.state; // req.data_custom.gameId = data.gameId; req.data_custom.type = data.type; req.data_custom.scan = data.scan; HttpUnit.request_custom(req); } }