|
@@ -1,15 +1,16 @@
|
|
|
-import { _decorator, Component, Node, sys } from 'cc';
|
|
|
+import { _decorator, Component, Node, sys, url } from 'cc';
|
|
|
import { config } from './config';
|
|
|
import { tools } from './tools';
|
|
|
import { userDataManager } from './manager/userDataManager';
|
|
|
const { ccclass, property } = _decorator;
|
|
|
@ccclass('http')
|
|
|
export class http {
|
|
|
+ public static domain = "https://zcapi.xwrun.com"
|
|
|
public static static_domain = "https://zcapi.xwrun.com"
|
|
|
public static statistics_domain = "https://zcapi.xwrun.com"
|
|
|
public static post(url,opt, call_back,method:string='POST') {
|
|
|
var xml = new XMLHttpRequest()
|
|
|
- let request_url = http.static_domain+url
|
|
|
+ let request_url = http.domain+url
|
|
|
xml.open(method, request_url)
|
|
|
xml.setRequestHeader('Content-Type', 'application/json');
|
|
|
if(userDataManager.user_data!=null){
|
|
@@ -83,9 +84,83 @@ export class http {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ public static uploadFile(url, filePath, progress_call_back, call_back) {
|
|
|
+ if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
|
|
|
+ let task = tt.uploadFile({
|
|
|
+ url: http.domain + url,
|
|
|
+ filePath: filePath,
|
|
|
+ name: "file",
|
|
|
+ success(res) {
|
|
|
+ if (res.statusCode === 200) {
|
|
|
+ // console.log(`uploadFile调用成功 ${res.data}`);
|
|
|
+ call_back(null,res.data)
|
|
|
+ } else {
|
|
|
+ call_back(res.statusCode,null)
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail(err) {
|
|
|
+ console.log(`uploadFile调用失败`,err);
|
|
|
+ call_back(err,null)
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ task.onProgressUpdate((res) => {
|
|
|
+ // console.log('res.progress=',res.progress)
|
|
|
+ if(progress_call_back) {
|
|
|
+ progress_call_back(res.progress)
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var xml = new XMLHttpRequest()
|
|
|
+ let request_url = http.domain+url
|
|
|
+ console.log('request_url=',request_url)
|
|
|
+ xml.open('POST', request_url, true)
|
|
|
+ xml.setRequestHeader('Content-Type', 'multipart/form-data');
|
|
|
+ if(userDataManager.user_data!=null){
|
|
|
+ xml.setRequestHeader('token',userDataManager.user_data.token);
|
|
|
+ }
|
|
|
+
|
|
|
+ //创建一个FormData对象来处理文件上传
|
|
|
+ let formData = new FormData()
|
|
|
+ formData.append("file", filePath)
|
|
|
+ //发送请求
|
|
|
+ xml.send(formData)
|
|
|
|
|
|
-
|
|
|
+ // 监听上传进度事件
|
|
|
+ xml.upload.onprogress = function(event) {
|
|
|
+ if (event.lengthComputable) {
|
|
|
+ var percentComplete = (event.loaded / event.total) * 100;
|
|
|
+ console.log(Math.round(percentComplete) + "% uploaded");
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ // 监听请求完成事件
|
|
|
+ xml.onload = function() {
|
|
|
+ if (xml.status === 200) {
|
|
|
+ console.log("File uploaded successfully");
|
|
|
+ } else {
|
|
|
+ console.log("Upload failed with status: " + xml.status);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 监听错误事件
|
|
|
+ xml.onerror = function() {
|
|
|
+ console.log("Network error occurred");
|
|
|
+ };
|
|
|
+
|
|
|
+ // Special event
|
|
|
+ xml.onreadystatechange = function () {
|
|
|
+ console.log('xml.readyState=',xml.readyState,' xml.status=',xml.status)
|
|
|
+ if (xml.readyState === 4 && xml.status >= 200) {
|
|
|
+ call_back(null, xml.responseText);
|
|
|
+ } else if (xml.status === 404) {
|
|
|
+ call_back('404 page not found!', null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|