123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- import { _decorator, Component, Node } from 'cc';
- import { config } from './config';
- import { tools } from './tools';
- const { ccclass, property } = _decorator;
- @ccclass('http')
- export class http {
- static post(url,data,call_back){
- var xml = new XMLHttpRequest()
- xml.open('POST',config.domain+url)
- xml.setRequestHeader('Content-Type','application/json');
- xml.send(JSON.stringify(data));
- var array:String[] = ['loadstart','abort', 'error', 'load', 'loadend', 'timeout'];
- array.forEach(function (eventName) {
- xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
- var str = '\nEvent : ' + eventName;
- var lstr = ""
- if (eventName === 'timeout') {
- lstr += '(timeout)';
- }
- else if (eventName === 'loadend') {
- lstr += '...loadend!';
-
- }else if (eventName === 'onerror') {
-
- }
- // console.log("str==",str)
- // console.log("lstr==",lstr)
- };
- });
- // Special event
- xml.onreadystatechange = function () {
- if (xml.readyState === 4 && xml.status >= 200) {
- //label.string = handler(xml.responseText);
- // console.log("xml.responseText==",xml.responseText)
- call_back(null,xml.responseText);
- } else if (xml.status === 404) {
- call_back('404 page not found!',null);
- console.log("status ==",'404 page not found!')
- } else if (xml.readyState === 3) {
- call_back('Request dealing!',null);
- // console.log("status ==",'Request dealing!')
- } else if (xml.readyState === 2) {
- call_back('Request received!',null);
- // console.log("status ==", 'Request received!')
- } else if (xml.readyState === 1) {
- call_back('Server connection established! Request hasn\'t been received',null);
- console.log("status ==", 'Server connection established! Request hasn\'t been received')
- } else if (xml.readyState === 0) {
- call_back( 'Request hasn\'t been initiated!',null);
- console.log("status ==", 'Request hasn\'t been initiated!')
- } else {
- console.log('请求出错=',url)
- tools.showToast('请求出错')
- }
- };
- }
- static get(url,call_back){
- var xml = new XMLHttpRequest()
- xml.open('GET',config.domain+url)
- xml.setRequestHeader('Content-Type','application/json');
- xml.send()
- var array:String[] = ['loadstart','abort', 'error', 'load', 'loadend', 'timeout'];
- array.forEach(function (eventName) {
- xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
- var str = '\nEvent : ' + eventName;
- var lstr = ""
- if (eventName === 'timeout') {
- lstr += '(timeout)';
- }
- else if (eventName === 'loadend') {
- lstr += '...loadend!';
- }else if (eventName === 'onerror') {
-
- }
- // console.log("str==",str)
- // console.log("lstr==",lstr)
- };
- });
- // Special event
- xml.onreadystatechange = function () {
- if(xml.readyState === 4 && xml.status >= 200) {
- call_back(null,xml.responseText);
- } else if (xml.status === 404) {
- call_back('404 page not found!',null);
- } else if (xml.readyState === 3) {
- // console.log("status ==",'Request dealing!')
- } else if (xml.readyState === 2) {
- // console.log("status ==", 'Request received!')
- } else {
- console.log('请求出错=',url)
- tools.showToast('请求出错')
- }
- }
- }
- public static getGameList(page:number,limit:number=6):string{
- return `/smistatic/levels/${limit}_${page}.json`
- }
- public static get_dyopen_id():string{
- return `/note/user/get_dyopen_id`
- }
- public static get_login():string{
- return `/note/user/login`
- }
- public static get_sys_config():string{
- return `/smistatic/sys_config.json`
- }
- public static get_level_info(id:number):string{
- return `/smistatic/level_info/${id}.json`
- }
- //场景 scene 0=共用 1 = 场景1 2 = 场景2,
- public static get_level_resource(id:number,scene:number):string{
- return `/smistatic/resource/${id}_${scene}.json`
- }
- public static sync_data(){
- return `/note/user/sync_data`
- }
- public static run_get(url,call_back){
- var xml = new XMLHttpRequest()
- xml.open('GET',config.domain+url)
- xml.setRequestHeader('Content-Type','application/json');
- xml.send();
-
- var array:String[] = ['loadstart','abort', 'error', 'load', 'loadend', 'timeout'];
- array.forEach(function (eventName) {
- xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
- var str = '\nEvent : ' + eventName;
- var lstr = ""
- if (eventName === 'timeout') {
- lstr += '(timeout)';
- }
- else if (eventName === 'loadend') {
- lstr += '...loadend!';
-
- }else if (eventName === 'onerror') {
-
- }
- // console.log("str==",str)
- // console.log("lstr==",lstr)
- };
- });
- // Special event
- xml.onreadystatechange = function () {
- if(xml.readyState === 4 && xml.status >= 200) {
- call_back(null,xml.responseText);
- } else if (xml.status === 404) {
- call_back('404 page not found!',null);
- } else {
- console.log('请求出错=',url)
- tools.showToast('请求出错')
- }
- };
- }
- public static run_post(url,data,call_back){
- var xml = new XMLHttpRequest()
- xml.open('POST',config.domain+url)
- xml.setRequestHeader('Content-Type','application/json');
- xml.setRequestHeader('token',config.TOKEN);
- xml.send(JSON.stringify(data));
-
- var array:String[] = ['loadstart','abort', 'error', 'load', 'loadend', 'timeout'];
- array.forEach(function (eventName) {
- xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
- var str = '\nEvent : ' + eventName;
- var lstr = ""
- if (eventName === 'timeout') {
- lstr += '(timeout)';
- }
- else if (eventName === 'loadend') {
- lstr += '...loadend!';
-
- }else if (eventName === 'onerror') {
-
- }
- // console.log("str==",str)
- // console.log("lstr==",lstr)
- };
- });
- // Special event
- xml.onreadystatechange = function () {
- if(xml.readyState === 4 && xml.status >= 200) {
- call_back(null,xml.responseText);
- } else if (xml.status === 404) {
- call_back('404 page not found!',null);
- } else {
- console.log('请求出错=',url)
- tools.showToast('请求出错')
- }
- };
- }
- }
|