From b991f1259594eecfa4d72aedb4b7072937f21ad2 Mon Sep 17 00:00:00 2001 From: zlw Date: Tue, 11 Jun 2024 17:14:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/Script/crypto/serverAPI.ts | 120 ++++++++++++------------------ 1 file changed, 49 insertions(+), 71 deletions(-) diff --git a/assets/Script/crypto/serverAPI.ts b/assets/Script/crypto/serverAPI.ts index 74869e7..fcc1b7a 100644 --- a/assets/Script/crypto/serverAPI.ts +++ b/assets/Script/crypto/serverAPI.ts @@ -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 { - 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 { - 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 { - 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 { 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 { + 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 { + 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; + } } }