Http工具类
1.get 方式访问数据接口
2.post 方式范围数据接口
function Request(e) {
return new Promise(function (t, i) {
var n = new XMLHttpRequest;
n.open(e.method || "GET", e.url), e.headers && Object.keys(e.headers).forEach(function (t) {
n.setRequestHeader(t, e.headers[t]);
}), n.onload = function () {
n.status >= 200 && n.status < 300 ? t(n.response) : i(n.statusText);
}, n.onerror = function () {
return i(n.statusText);
}
n.send(JSON.stringify(e.body));
})
}
var baseurl = "http://192.168.1.122:8090/";
// 获取商品信息
function getShopItemList(gid, callback){
var time = (new Date).getTime();
var sign = md5("time"+time+"&gid"+gid);
Request({
method: "GET",
url: baseurl+"public/?s=GameApi.getcommoditylist&gid="+gid+"&time="+time+"&sign="+sign,
}).then(function (e) {
e = JSON.parse(e);
callback && callback(e);
}).catch(function (e) {
console.log(e);
callback && callback(e);
})
}
//获取用户信息
function getUserInfoList(gid,uid,callback){
var time = (new Date).getTime();
var sign = md5("time"+time+"&gid"+gid+"&uid"+uid);
Request({
method: "GET",
url: baseurl+"public/?s=GameApi.getuserinfo&gid="+gid+"&time="+time+"&uid"+uid+"&sign="+sign,
}).then(function (e) {
e = JSON.parse(e);
console.log(e);
callback && callback(e);
}).catch(function (e) {
console.log(e);
callback && callback(e);
})
}
/**
* 回调函数定义
*
* @interface ResponseTextCallback
*/
interface ResponseTextCallback {
(data: string): void;
}
/**
* Http的基础操作类
*
* @export
* @class Http
*/
export class Http {
/**
* 基础的url
*
* @static
* @type {string}
* @memberof Http
*/
static BASE_URL: string = 'http://127.0.0.1:1234';
/**
* http get获取方法
*
* @static
* @memberof Http
*/
static get = (route: string, param: object = {}, callback?: ResponseTextCallback) => {
const xhr = cc.loader.getXMLHttpRequest();
xhr.timeout = 5000;
let url: string = '';
let paramStr = '';
for(let key in param) {
if (paramStr === ''){
paramStr = '?';
}
if (paramStr !== '?') {
paramStr += '&';
}
paramStr += key + '=' + param[key];
}
url = `${Http.BASE_URL}${route}${encodeURI(paramStr)}`
console.log('http get url:', url);
xhr.open("GET", url, true);
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
if (cc.sys.isNative){
xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
}
xhr.onreadystatechange = () => {
if(xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)){
callback(xhr.responseText);
}
};
xhr.send();
};
/**
* http post 方法
*
* @static
* @memberof Http
*/
static post = (route: string, param: object = {}, callback?: ResponseTextCallback) => {
const xhr = cc.loader.getXMLHttpRequest();
xhr.timeout = 5000;
let url: string = '';
let paramStr = '';
for(let key in param) {
if (paramStr !== '') {
paramStr += '&';
}
paramStr += key + '=' + param[key];
}
url = `${Http.BASE_URL}${route}`
console.log('http post url:', url);
xhr.open("POST", url, true);
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
if (cc.sys.isNative){
// xhr.setRequestHeader("Accept-Encoding", "gzip,deflate", "text/html;charset=UTF-8");
xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
}
xhr.onreadystatechange = () => {
if(xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)){
callback(xhr.responseText);
}
};
xhr.send(encodeURI(paramStr));
}
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。