XRoom_Unity/xroom/Assets/webservice.cs
2025-05-26 11:51:25 +03:30

239 lines
6.8 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using SimpleJSON;
using UnityEngine;
using UnityEngine.Networking;
[Serializable]
public class ErrorResponse
{
public int status;
public Dictionary<string, object> data;
public string message;
}
public enum ErrorType
{
None,
Authentication,
Network,
Server,
Unknown
}
public class webservice : MonoBehaviour
{
public static webservice instance;
public string lastError; // Store the last error message
public ErrorResponse lastErrorResponse;
public ErrorType lastErrorType;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
instance = this;
}
public IEnumerator LoginRequest(Dictionary<string, string> formData, Action<User> onComplete)
{
string url = "http://194.62.43.230:8000/login";
lastError = null;
lastErrorResponse = null;
lastErrorType = ErrorType.None;
WWWForm form = new WWWForm();
String par = "";
foreach (KeyValuePair<string, string> entry in formData)
{
par += entry.Key + "=" + entry.Value;
form.AddField(entry.Key, entry.Value);
}
print(url+"?"+par);
using (UnityWebRequest www = UnityWebRequest.Post(url, form))
{
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.Success)
{
print("Response: " + www.downloadHandler.text);
PlayerPrefs.SetString("user", www.downloadHandler.text);
User response = new User(www.downloadHandler.text);
onComplete?.Invoke(response);
}
else
{
try
{
lastErrorResponse = JsonUtility.FromJson<ErrorResponse>(www.downloadHandler.text);
lastError = lastErrorResponse.message;
if (lastErrorResponse.status >= 400 && lastErrorResponse.status < 500)
{
lastErrorType = ErrorType.Authentication;
lastError = "خطا در احراز هویت: " + lastErrorResponse.message;
}
else if (lastErrorResponse.status >= 500)
{
lastErrorType = ErrorType.Server;
lastError = "خطای سرور: " + lastErrorResponse.message;
}
else
{
lastErrorType = ErrorType.Unknown;
lastError = "خطای ناشناخته: " + lastErrorResponse.message;
}
Debug.LogError($"خطای API: وضعیت={lastErrorResponse.status}, پیام={lastErrorResponse.message}, نوع={lastErrorType}");
}
catch (Exception e)
{
lastError = www.error;
if (www.error.Contains("NetworkError") ||
www.error.Contains("ConnectionError") ||
www.error.Contains("Cannot connect"))
{
lastErrorType = ErrorType.Network;
lastError = "خطای اتصال به اینترنت";
}
else
{
lastErrorType = ErrorType.Unknown;
lastError = "خطای ناشناخته در ارتباط با سرور";
}
Debug.LogError($"خطای شبکه: {www.error}, نوع={lastErrorType}");
}
onComplete?.Invoke(null);
}
}
}
public IEnumerator GetInfoRequest(Action<GetInfoResponse> onComplete)
{
string url = "http://194.62.43.230:8000/getInfo";
lastError = null;
lastErrorResponse = null;
UnityWebRequest www = UnityWebRequest.Get(url);
www.SetRequestHeader("Authorization", "token "+EN.instance.Me.token);
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.Success)
{
Debug.Log("Response: " + www.downloadHandler.text);
GetInfoResponse response = JsonUtility.FromJson<GetInfoResponse>(www.downloadHandler.text);
onComplete?.Invoke(response);
}
else
{
try
{
lastErrorResponse = JsonUtility.FromJson<ErrorResponse>(www.downloadHandler.text);
lastError = lastErrorResponse.message;
Debug.LogError($"API Error: Status={lastErrorResponse.status}, Message={lastErrorResponse.message}");
}
catch (Exception e)
{
lastError = www.error;
Debug.LogError($"Network Error: {www.error}");
}
onComplete?.Invoke(null);
}
}
}
[System.Serializable]
public class GetInfoResponse
{
public Customer customer;
public User user;
public List<ImageData> images;
public List<PdfData> pdfs;
public List<VideoData> videos;
public List<GlbData> glbs;
}
[System.Serializable]
public class Customer
{
public int id;
public int user;
public int user_id;
public string mobile_number;
public string profile_img;
public string profile_glb;
public bool is_sms_verified;
public bool is_email_verified;
}
[System.Serializable]
public class ImageData
{
public int id;
public string url;
public string image;
public string name;
public string created_at;
}
[System.Serializable]
public class PdfData
{
public int id;
public string url;
public string pdf;
public string name;
public string created_at;
}
[System.Serializable]
public class VideoData
{
public int id;
public string url;
public string video;
public string name;
public string created_at;
}
[System.Serializable]
public class GlbData
{
public int id;
public string url;
public string glb;
public string name;
public string created_at;
}
[Serializable]
public class User
{
public int id;
public string token;
public string first_name;
public string last_name;
public string job;
public GameObject character;
private string user;
public User()
{
}
public User(string j)
{
if(EN.instance)
EN.instance.Print(j);
var json = JSON.Parse(j);
User response = this;
response.token = json["token"];
response.id = json["user"]["id"].AsInt;
response.first_name = json["user"]["first_name"];
response.last_name = json["user"]["last_name"];
EN.instance.Print(response.last_name);
}
}