123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- import { _decorator, Component, Node, sys } from 'cc';
- import { config } from '../config';
- import { gameManager } from '../gameManager';
- const { ccclass, property } = _decorator;
- @ccclass('http')
- export class http extends Component {
- static get(url,call_back){
- try{
- if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME){
- const options = {
- url: config.static_domain+url, //请求地址
- method: "GET", //指定对应的网络请求方法,详见文档中method枚举值
- // dataType: "json", // 指定返回数据的类型为 json。不填默认为json
- enableCache: false, //是否开启 cache ,不填默认为false
- // responseType: "text", //期望响应的数据类型
- timeout: 60000, //超时时间,单位为毫秒(最大值 60000ms)
- }
-
- let task = tt.request({
- ...options,
- success(res) { //请求成功回调
- // console.log("请求成功", res.data);
- call_back(null,JSON.stringify(res.data));
- },
- fail(res) {//请求失败回调
- call_back( -1,null);
- console.log("请求失败", res.errMsg);
- },
- complete(res){ //请求结束回调
- console.log("调用接口结束",res.data)
- }
- });
-
- }else{
- var xml = new XMLHttpRequest()
- xml.open('GET',config.domain+url)
- xml.setRequestHeader('Content-Type','text/plain');
- xml.send();
- // Special event
- xml.onreadystatechange = function () {
-
- // console.log("e === ",xml)
- if (xml.readyState === 4 && xml.status >= 200) {
- //label.string = handler(xml.responseText);
- call_back(null,xml.responseText);
- } else if (xml.status === 404) {
- call_back('404 page not found!',null);
- }
- else if (xml.readyState === 3) {
- call_back('Request dealing!',null);
- } else if (xml.readyState === 2) {
- call_back('Request received!',null);
- } else if (xml.readyState === 1) {
- call_back('Server connection established! Request hasn\'t been received',null);
- } else if (xml.readyState === 0) {
- call_back( 'Request hasn\'t been initiated!',null);
- }else if (xml.readyState === 4 &&xml.status === 0) {
- call_back( -1,null);
- }
- }
-
- };
- }catch(e){
- console.log("e === ",e)
- call_back(-1,null)
- }
-
- }
- static post(url,_data:any,call_back){
- // console.log("post",sys.Platform.BYTEDANCE_MINI_GAME)
- // console.log("platform",sys.platform)
- if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME){
- const options = {
- url: config.domain+url, //请求地址
- data: _data,
- header: {
- "token": gameManager.userInfo.token,
- },
- method: "POST", //指定对应的网络请求方法,详见文档中method枚举值
- // dataType: "json", // 指定返回数据的类型为 json。不填默认为json
- enableCache: false, //是否开启 cache ,不填默认为false
- // responseType: "text", //期望响应的数据类型
- timeout: 60000, //超时时间,单位为毫秒(最大值 60000ms)
- }
- let task = tt.request({
- ...options,
- success(res) { //请求成功回调
- // console.log("请求成功", res.data);
- call_back(null,JSON.stringify(res.data));
- },
- fail(res) {//请求失败回调
- call_back( -1,null);
- console.log("请求失败", res.errMsg);
-
- },
- complete(res){ //请求结束回调
- console.log("调用接口结束",res.data)
- }
- });
- }else{
- var xml = new XMLHttpRequest()
- xml.open('POST',config.domain+url)
- // xml.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
- xml.setRequestHeader('token',gameManager.userInfo.token);
- xml.send(_data);
-
- // Special event
- xml.onreadystatechange = function () {
- if (xml.readyState === 4 && xml.status >= 200) {
- //label.string = handler(xml.responseText);
- call_back(null,xml.responseText);
- } else if (xml.status === 404) {
- call_back('404 page not found!',null);
-
- } else if (xml.readyState === 3) {
- call_back('Request dealing!',null);
-
- } else if (xml.readyState === 2) {
- call_back('Request received!',null);
-
- } else if (xml.readyState === 1) {
- call_back('Server connection established! Request hasn\'t been received',null);
-
- } else if (xml.readyState === 0) {
- call_back( 'Request hasn\'t been initiated!',null);
- }else if (xml.readyState === 4 &&xml.status === 0) {
- call_back( -1,null);
- }
- };
- }
-
- }
- }
|