import User from "../../FrameWork/User/User"; const {ccclass, property} = cc._decorator; @ccclass export default class CommonDefine { public static loginCode:string = ''; public static requestData = true; //获取登录信息 public static replayGetLoginInfo(callFunc?:Function){ let onFail = ()=>{ } let onSuccess = (response)=>{ console.log(response, 'setUserId++===数据+++=====') if(response.msg == 'OK'){ User.setUserId(response.openid) if(callFunc){ callFunc() } } } if(CommonDefine.loginCode){ CommonDefine.getLoginInfo(CommonDefine.loginCode, onFail, onSuccess) } } 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) { 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, '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:"WDZCRJNEW",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('网络请求异常') onFail("网络请求异常"); } //超时 xhr.ontimeout = function (error) { console.log('网络超时') 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); let data = null if(response.data && response.data != ''){ data = JSON.parse(response.data) console.log(data.GameData, 'getServerData+++++') onSuccess(data.GameData); } } 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:"WDZCRJNEW",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:"WDZCRJNEW",GameUserID:userid,"GameData":sendData}; let sendMsg = JSON.stringify(params); xhr.send(sendMsg); } }