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