更新
This commit is contained in:
parent
15554d15c0
commit
b991f12595
|
@ -6,84 +6,62 @@ import CryptoJS = require('./crypto-js.min.js'); //引用AES源码js
|
|||
|
||||
// import axios from 'axios'
|
||||
const {ccclass, property} = cc._decorator;
|
||||
const BASE_URL = "http://api.sparkus.cn";
|
||||
|
||||
@ccclass
|
||||
export default class HttpUtil extends cc.Component {
|
||||
//排行榜type2为获取,type1为上传
|
||||
static async rankData(type,callback,data): Promise<any> {
|
||||
data.gameId = GameData._instance.GM_INFO.gameId;
|
||||
data.userId = GameData._instance.GM_INFO.userId;
|
||||
const time = Math.floor((new Date().getTime()) / 1000)
|
||||
const url = apiSign(`/api/get/rank/data?gameId=${config.gameId}&dataType=${type}&time=${time}`, data)
|
||||
this.httpPost(url,data,callback);
|
||||
}
|
||||
|
||||
static async uploadUserLogData(data,callback): Promise<any> {
|
||||
data.gameId = GameData._instance.GM_INFO.gameId;
|
||||
data.userId = GameData._instance.GM_INFO.userId;
|
||||
const url = '/log/collect/data';
|
||||
this.httpPost(url,data,callback);
|
||||
}
|
||||
//暂时用不到
|
||||
static async getUserRecord(data,callback): Promise<any> {
|
||||
data.gameId = GameData._instance.GM_INFO.gameId;
|
||||
data.userId = GameData._instance.GM_INFO.userId;
|
||||
const time = Math.floor((new Date().getTime()) / 1000)
|
||||
const url = apiSign(`/api/get/user/data?gameId=${config.gameId}&time=${time}`, data)
|
||||
this.httpPost(url,data,callback);
|
||||
}
|
||||
|
||||
static httpPost(url,data,callBack){
|
||||
//排行榜type2为获取,type1为上传
|
||||
static async rankData(type,callback,data): Promise<any> {
|
||||
data.gameId = GameData._instance.GM_INFO.gameId;
|
||||
data.userId = GameData._instance.GM_INFO.userId;
|
||||
var urlData = "http://api.sparkus.cn" + url;
|
||||
// console.log("params:",JSON.stringify(data));
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', urlData);
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState == 4 && xhr.status == 200) {
|
||||
var data = xhr.responseText;
|
||||
if(!data){
|
||||
// console.log("初始化失败");
|
||||
return;
|
||||
}
|
||||
var json = JSON.parse(data);
|
||||
// console.log('http success:' + json);
|
||||
callBack(json);
|
||||
}
|
||||
else{
|
||||
// var json = JSON.parse(data);
|
||||
// console.log('http fail:' + url);
|
||||
callBack(json);
|
||||
}
|
||||
};
|
||||
xhr.send(JSON.stringify(data));
|
||||
const time = Math.floor((new Date().getTime()) / 1000)
|
||||
const url = apiSign(`/api/get/rank/data?gameId=${config.gameId}&dataType=${type}&time=${time}`, data)
|
||||
this.post(url,data,callback);
|
||||
}
|
||||
|
||||
static async uploadUserLogData(data,callback): Promise<any> {
|
||||
data.gameId = GameData._instance.GM_INFO.gameId;
|
||||
data.userId = GameData._instance.GM_INFO.userId;
|
||||
const url = '/log/collect/data';
|
||||
this.get(url,callback);
|
||||
}
|
||||
//暂时用不到
|
||||
static async getUserRecord(data,callback): Promise<any> {
|
||||
data.gameId = GameData._instance.GM_INFO.gameId;
|
||||
data.userId = GameData._instance.GM_INFO.userId;
|
||||
const time = Math.floor((new Date().getTime()) / 1000)
|
||||
const url = apiSign(`/api/get/user/data?gameId=${config.gameId}&time=${time}`, data)
|
||||
this.post(url,data,callback);
|
||||
}
|
||||
static async post(url, data, callback) {
|
||||
const response = await this.fetchData(url, data, 'POST');
|
||||
callback && callback(response);
|
||||
}
|
||||
|
||||
static httpGet(url,callBack){
|
||||
var urlData = "http://api.sparkus.cn" + url;
|
||||
console.log(urlData);
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', urlData);
|
||||
xhr.setRequestHeader('Content-Type', 'text/plain');
|
||||
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState == 4 && xhr.status == 200) {
|
||||
var data = xhr.responseText;
|
||||
if(data){
|
||||
var json = JSON.parse(data);
|
||||
console.info('http success:' + json);
|
||||
callBack(json);
|
||||
}
|
||||
else callBack(data);
|
||||
}
|
||||
else{
|
||||
console.info('http fail:' + url);
|
||||
callBack(null);
|
||||
}
|
||||
};
|
||||
xhr.send();
|
||||
static async get(url, callback) {
|
||||
const response = await this.fetchData(url, null, 'GET');
|
||||
callback && callback(response);
|
||||
}
|
||||
|
||||
static async fetchData(url, data, method) {
|
||||
const fullUrl = `${BASE_URL}${url}`;
|
||||
const headers = { 'Content-Type': 'application/json' };
|
||||
const options = {
|
||||
method,
|
||||
headers,
|
||||
body: data ? JSON.stringify(data) : null,
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch(fullUrl, options);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error('Fetch error:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user