update
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
import { _decorator } from 'cc'
|
||||
|
||||
import { Logger } from './Logger'
|
||||
|
||||
const { ccclass } = _decorator
|
||||
|
||||
@ccclass('HttpClient')
|
||||
export default class HttpClient {
|
||||
public static instance: HttpClient //= new HttpClient();
|
||||
public static instance: HttpClient // = new HttpClient();
|
||||
|
||||
//example
|
||||
// example
|
||||
// HttpClient.instance.request("http://localhost:8080/haohttp/test", ()=>{
|
||||
// console.log("http 请求 end=============");
|
||||
// }, {"nickName":"jhao", "hh":1, "id":9527});
|
||||
@@ -27,12 +28,10 @@ export default class HttpClient {
|
||||
let resParams = ''
|
||||
let nowIndex = 1
|
||||
for (const key in paramsObj) {
|
||||
if (paramsObj.hasOwnProperty(key)) {
|
||||
if (nowIndex == 1) {
|
||||
resParams += key + '=' + paramsObj[key]
|
||||
} else {
|
||||
resParams += '&' + key + '=' + paramsObj[key]
|
||||
}
|
||||
if (Object.prototype.hasOwnProperty.call(paramsObj, key)) {
|
||||
if (nowIndex === 1) resParams += `${key}=${paramsObj[key]}`
|
||||
else resParams += `&${key}=${paramsObj[key]}`
|
||||
|
||||
nowIndex += 1
|
||||
}
|
||||
}
|
||||
@@ -44,41 +43,29 @@ export default class HttpClient {
|
||||
this.responseType = responseType
|
||||
}
|
||||
|
||||
public setContentType() {
|
||||
}
|
||||
public setContentType() {}
|
||||
|
||||
public request(
|
||||
url: string,
|
||||
callback: Function,
|
||||
params: any = null,
|
||||
timeOut: number = 5 * 1000
|
||||
) {
|
||||
if (params && this.methodType == 'GET') {
|
||||
public request(url: string, callback: Function, params: any = null, timeOut: number = 5 * 1000) {
|
||||
if (params && this.methodType === 'GET') {
|
||||
let getParams: string = this.setParams(params)
|
||||
// getParams = StringUtil:encodeURI(params)
|
||||
getParams = encodeURI(getParams)
|
||||
url += '?' + getParams
|
||||
url += `?${getParams}`
|
||||
}
|
||||
this.xhr = new XMLHttpRequest() // http请求 fget
|
||||
//this.xhr = cc.loader.getXMLHttpRequest();
|
||||
let xhr: XMLHttpRequest = this.xhr
|
||||
// this.xhr = cc.loader.getXMLHttpRequest();
|
||||
const xhr: XMLHttpRequest = this.xhr
|
||||
xhr.responseType = this.responseType
|
||||
xhr.timeout = timeOut
|
||||
// xhr.setRequestHeader("Content-Type", "text/plain");
|
||||
xhr.onreadystatechange = () => {
|
||||
Logger.log(
|
||||
this,
|
||||
'status======',
|
||||
xhr.status,
|
||||
xhr.readyState,
|
||||
xhr.statusText
|
||||
)
|
||||
Logger.log(this, 'status======', xhr.status, xhr.readyState, xhr.statusText)
|
||||
// if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
|
||||
if (xhr.readyState == 4 && xhr.status == 200) {
|
||||
let response = xhr.response
|
||||
if (xhr.readyState === 4 && xhr.status === 200) {
|
||||
const response = xhr.response
|
||||
Logger.log(this, 'http response1============', xhr)
|
||||
try {
|
||||
let testJson = JSON.stringify(response)
|
||||
const testJson = JSON.stringify(response)
|
||||
Logger.log(this, 'http response json============', testJson)
|
||||
if (callback) {
|
||||
callback(true, response)
|
||||
@@ -91,31 +78,21 @@ export default class HttpClient {
|
||||
callback = null
|
||||
}
|
||||
}
|
||||
} else if (xhr.readyState == 4 && xhr.status == 301) {
|
||||
//域名转移
|
||||
Logger.log(
|
||||
this,
|
||||
'http response222============',
|
||||
xhr.getResponseHeader('Location')
|
||||
)
|
||||
} else if (xhr.readyState === 4 && xhr.status === 301) {
|
||||
// 域名转移
|
||||
Logger.log(this, 'http response222============', xhr.getResponseHeader('Location'))
|
||||
// console.log("http response333============", xhr.getAllResponseHeaders());
|
||||
if (HttpClient.instance == null) HttpClient.instance = new HttpClient()
|
||||
HttpClient.instance.request(xhr.getResponseHeader('Location'), callback)
|
||||
} else if (xhr.readyState == 4 && xhr.status == 404) {
|
||||
} else if (xhr.readyState === 4 && xhr.status === 404) {
|
||||
Logger.log(this, 'http onError============')
|
||||
if (callback) {
|
||||
callback(false)
|
||||
callback = null
|
||||
}
|
||||
} else {
|
||||
Logger.log(
|
||||
this,
|
||||
'onreadystatechange else====',
|
||||
xhr.status,
|
||||
xhr.readyState,
|
||||
xhr.response
|
||||
)
|
||||
if (xhr.readyState == 4) {
|
||||
Logger.log(this, 'onreadystatechange else====', xhr.status, xhr.readyState, xhr.response)
|
||||
if (xhr.readyState === 4) {
|
||||
Logger.log(this, 'http onError else============')
|
||||
if (callback) {
|
||||
callback(false)
|
||||
@@ -125,13 +102,7 @@ export default class HttpClient {
|
||||
}
|
||||
}
|
||||
xhr.onprogress = () => {
|
||||
Logger.log(
|
||||
this,
|
||||
'http onprogress===',
|
||||
xhr.status,
|
||||
xhr.readyState,
|
||||
xhr.response
|
||||
)
|
||||
Logger.log(this, 'http onprogress===', xhr.status, xhr.readyState, xhr.response)
|
||||
}
|
||||
xhr.onerror = () => {
|
||||
Logger.log(this, 'http onError============')
|
||||
@@ -155,6 +126,5 @@ export default class HttpClient {
|
||||
xhr.send(params)
|
||||
}
|
||||
|
||||
public getInfo(callback: Function = null) {
|
||||
}
|
||||
public getInfo(callback: Function = null) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user