123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- import { _decorator, Component, Node } from 'cc';
- import { gameManager } from './gameManager';
- import { uiManager } from '../framework/uiManager';
- const { ccclass, property } = _decorator;
- @ccclass('HttpHelp')
- export class HttpHelp {
- /**
- * 请求地址的域名配置
- */
- static HttpsUrlTest: string = "http://testapi.mstardance.com/api/";
- /**
- * 请求地址的域名配置
- */
- static HttpsUrlRelpse: string = "http://api.mstardance.com/api/"; //屏蔽掉,正式的也是测试//"http://star.mstardance.com/api/";
- static httpGet(url: string, callback: Function, token: string = null!, bind: any = null) {
- url = (gameManager.Instance.golabMemory.isReplse?HttpHelp.HttpsUrlRelpse:HttpHelp.HttpsUrlTest) + url;
- console.log('请求地址是:', url);
- let xhr = new XMLHttpRequest();
- xhr.onreadystatechange = function () {
- if (xhr.readyState === 4 && xhr.status == 200) {
- let respone = xhr.responseText;
- try {
- if (bind != null) {
- callback.call(bind, respone);
- } else {
- callback(respone);
- }
- } catch (error3) {
-
- }
-
- } else {
- // console.error(xhr.readyState+"httpGet 失败:"+xhr.status);
- }
- };
- xhr.withCredentials = true;
- xhr.timeout = 5000;// 5 seconds for timeout
- xhr.open('GET', url, true);
- xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
- xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST');
- xhr.setRequestHeader('Access-Control-Allow-Headers', 'x-requested-with,content-type,authorization');
- xhr.setRequestHeader("Content-Type", "application/json");
- if (token && token.length > 3) {
- xhr.setRequestHeader("accessToken", token);
- }
- xhr.send();
- }
- /**
- * post请求
- * @param {string} url
- * @param {object} params
- * @param {function} callback
- */
- static httpPost(url: string, params: any, callback: Function, token: string = null!, bind: any = null) {
- url = (gameManager.Instance.golabMemory.isReplse?HttpHelp.HttpsUrlRelpse:HttpHelp.HttpsUrlTest) + url;
- let xhr = new XMLHttpRequest();
- // try{
- xhr.onreadystatechange = function () {
- // console.log('xhr.readyState=' + xhr.readyState + ' xhr.status=' + xhr.status);
- if (xhr.readyState === 4) {
- let respone = xhr.responseText;
- if (xhr.status == 200) {
- try {
- if (bind != null) {
- callback.call(bind, respone);
- } else {
- callback(respone);
- }
- } catch (error2) {
-
- }
-
- } else {
- try {
- if (bind != null) {
- callback.call(bind, "-1");
- } else {
- callback("-1");
- }
- } catch (error1) {
-
- }
-
- }
- }
- };
- xhr.timeout = 5000;// 5 seconds for timeout
- xhr.open('POST', url, true);
- xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
- xhr.setRequestHeader('Access-Control-Allow-Methods', 'POST');
- // xhr.setRequestHeader('Access-Control-Allow-Headers', 'x-requested-with,content-type');
- xhr.setRequestHeader("Content-type", "application/json");
- if (token && token.length > 3) {
- xhr.setRequestHeader("accessToken", token);
- }
- xhr.send(params);
- // } catch (error1) {
- // uiManager.instance.showTips('网络异常,请稍微再试!data==-2');
-
- // }
- }
- }
|