import HttpUnit, { requestData } from "../../FrameWork/NetWork/HttpUnit"; import User from "../../FrameWork/User/User"; import AppPlatform from "../../FrameWork/Util/AppPlatform"; const {ccclass, property} = cc._decorator; @ccclass export default class CommonDefine { public static loginCode:string = ''; //获取登录信息 public static tryIndex = 0; public static replayGetLoginInfo(onSuccesscall?:Function){ console.log('replayGetLoginInfo++=') this.tryIndex++ if(this.tryIndex>=5){ return } let successcall = (code)=>{ CommonDefine.loginCode = code; let onFail = ()=>{ } let onSuccess = (response)=>{ console.log(response, '数据+++=====') if(response.msg == 'OK'){ User.setUserId(response.openid) if(onSuccesscall){ onSuccesscall() } } } CommonDefine.getLoginInfo(CommonDefine.loginCode, onFail, onSuccess) } let failcall = ()=>{ } AppPlatform.loginPlatform_custom( successcall, failcall) } public static getLoginInfo(code, onFail, onSuccess){ let url = "https://xmb.ioe-times.com/api/getopenid" let headersTab = {}; //头数据 let xhr: XMLHttpRequest = new XMLHttpRequest(); xhr.timeout = 15000; //请求过程发生意外的回调 xhr.onerror = function (error) { console.log("网络请求异常getLoginInfo"); } //超时 xhr.ontimeout = function (error) { console.log("网络超时getLoginInfo"); } xhr.onreadystatechange = function () { let readyState = xhr.readyState; let status = xhr.status; if (readyState === 4) { let tab = { code: 1 } if (status >= 200 && status < 300) { let responseText = xhr.responseText; const response = JSON.parse(responseText); console.log(response, 'CommonDefine ++= getLoginInfo data+++++') onSuccess(response); } else { let statusText = xhr.statusText; console.log("接收内容:" + statusText) onSuccess(null); } } } 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]); } } console.log(code, '用户code') let params = {GameName:"JueXingShiKe",Anonymous_code:"", "Code":code}; let sendMsg = JSON.stringify(params); xhr.send(sendMsg); } public static getServerData(onSuccess: Function, onFail: Function){ let userid = User.getUserId() if(userid == ''){ cc.log('userid为空号getServerData') CommonDefine.replayGetLoginInfo() return } let url = "https://xmb.ioe-times.com/api/getdata" let headersTab = {}; //头数据 let xhr: XMLHttpRequest = new XMLHttpRequest(); xhr.timeout = 15000; //请求过程发生意外的回调 xhr.onerror = function (error) { console.log('网络请求异常getServerData') onFail("网络请求异常"); } //超时 xhr.ontimeout = function (error) { console.log('网络超时getServerData') onFail("网络超时"); } xhr.onreadystatechange = function () { let readyState = xhr.readyState; let status = xhr.status; if (readyState === 4) { let tab = { code: 1 } if (status >= 200 && status < 300) { // console.log(xhr, 'xhr+++++') let responseText = xhr.responseText; // console.log(responseText, 'responseText+++++') const response = JSON.parse(responseText); if (response.flag == 0 && response.data != ""){ let data = JSON.parse(response.data) console.log(data.GameData, 'getServerData+++++') onSuccess(data.GameData); } else{ onSuccess(null); } } else { let statusText = xhr.statusText; console.log("接收内容:" + statusText) onSuccess(null); } } } 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]); } } let params = {GameID:"JueXingShiKe",GameUserID:userid}; let sendMsg = JSON.stringify(params); xhr.send(sendMsg); } public static setServerData(sendData, onSuccess: Function, onFail: Function){ let userid = User.getUserId() if(userid == ''){ CommonDefine.replayGetLoginInfo() cc.log('userid为空号setServerData') return } let url = "https://xmb.ioe-times.com/api/setdata" let headersTab = {}; //头数据 let xhr: XMLHttpRequest = new XMLHttpRequest(); xhr.timeout = 15000; //请求过程发生意外的回调 xhr.onerror = function (error) { onFail("网络请求异常"); } //超时 xhr.ontimeout = function (error) { onFail("网络超时"); } xhr.onreadystatechange = function () { let readyState = xhr.readyState; let status = xhr.status; if (readyState === 4) { let tab = { code: 1 } if (status >= 200 && status < 300) { let responseText = xhr.responseText; const response = JSON.parse(responseText); console.log(response, 'getServerData+++++') onSuccess(response); } else { let statusText = xhr.statusText; console.log("接收内容:" + statusText) onSuccess(null); } } } 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]); } } let params = {GameID:"JueXingShiKe",GameUserID:userid,"GameData":sendData}; let sendMsg = JSON.stringify(params); xhr.send(sendMsg); } public static checkInitFromServer(){ return new Promise((resolve,reject) => { let url = "https://xmb.ioe-times.com/api/getcheck" let headersTab = {}; //头数据 let xhr: XMLHttpRequest = new XMLHttpRequest(); xhr.timeout = 15000; //请求过程发生意外的回调 xhr.onerror = function (error) { resolve(false); } //超时 xhr.ontimeout = function (error) { resolve(false); } xhr.onreadystatechange = function () { let readyState = xhr.readyState; let status = xhr.status; if (readyState === 4) { if (status >= 200 && status < 300) { let responseText = xhr.responseText; const response = JSON.parse(responseText); if (response.flag == 0){ resolve(true); } else{ resolve(false) } } else { let statusText = xhr.statusText; resolve(false) } } else{ // resolve(false) } } 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]); } } let params = {gameNameV:"zuiyou-clksdjh",}; let sendMsg = JSON.stringify(params); xhr.send(sendMsg); }).catch( error => { console.log('error', error); //这里会打印捕获的异常是什么,我这里是false return false; }); } }