diff --git a/xiaofang/Assets/Scenes/loginscene.unity b/xiaofang/Assets/Scenes/loginscene.unity index ce30caad..b78a9ed7 100644 --- a/xiaofang/Assets/Scenes/loginscene.unity +++ b/xiaofang/Assets/Scenes/loginscene.unity @@ -741,6 +741,7 @@ GameObject: - component: {fileID: 252707971} - component: {fileID: 252707973} - component: {fileID: 252707972} + - component: {fileID: 252707974} m_Layer: 5 m_Name: LoginPanel m_TagString: Untagged @@ -808,6 +809,18 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 252707970} m_CullTransparentMesh: 1 +--- !u!114 &252707974 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 252707970} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: f381a54ea0a90e3448e8ae6e3cf61375, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &287371580 GameObject: m_ObjectHideFlags: 0 diff --git a/xiaofang/Assets/Script/login.meta b/xiaofang/Assets/Script/login.meta new file mode 100644 index 00000000..54da14f8 --- /dev/null +++ b/xiaofang/Assets/Script/login.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: fcb636bea24d970419745267fa282f54 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/xiaofang/Assets/Script/login/Global.cs b/xiaofang/Assets/Script/login/Global.cs new file mode 100644 index 00000000..e4f73f65 --- /dev/null +++ b/xiaofang/Assets/Script/login/Global.cs @@ -0,0 +1,35 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Global : Singleton +{ + public static Global global; + + + + + +} + + +public class server : Response +{ + + public Data data; + //public string access_token; +} +public class Response +{ + public int code; + public string msg; +} +public class Data +{ + public string access_token; + public string companyId; + public string companyName; + public string client_id; + public int expire_in;//toling剩余时间 + +} diff --git a/xiaofang/Assets/Script/login/Global.cs.meta b/xiaofang/Assets/Script/login/Global.cs.meta new file mode 100644 index 00000000..581e7087 --- /dev/null +++ b/xiaofang/Assets/Script/login/Global.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 015522dae649528488644d01a5248b5e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/xiaofang/Assets/Script/login/Singleton.cs b/xiaofang/Assets/Script/login/Singleton.cs new file mode 100644 index 00000000..68ab6bff --- /dev/null +++ b/xiaofang/Assets/Script/login/Singleton.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +public class Singleton where T : new() +{ + private static T ms_instance; + + public static T Instance + { + get + { + if (ms_instance == null) + { + ms_instance = new T(); + } + + return ms_instance; + } + } +} \ No newline at end of file diff --git a/xiaofang/Assets/Script/login/Singleton.cs.meta b/xiaofang/Assets/Script/login/Singleton.cs.meta new file mode 100644 index 00000000..0b98e6c5 --- /dev/null +++ b/xiaofang/Assets/Script/login/Singleton.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2f0680be77c89a6408f0f224e3f199c9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/xiaofang/Assets/Script/login/login.cs b/xiaofang/Assets/Script/login/login.cs new file mode 100644 index 00000000..14de276e --- /dev/null +++ b/xiaofang/Assets/Script/login/login.cs @@ -0,0 +1,41 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using Newtonsoft.Json; +public class auth_login +{ + public string clientId = "e5cd7e4891bf95d1d19206ce24a7b32e"; + public string grantType = "password"; + public string userType = "sys_user"; + public string username = "adminyyl"; + public string password = "admin123"; +} +public class login : MonoBehaviour + +{ + public string token;//登录返回的token + public void Start() + { + loging(); + //createTemplate(); + } + public async void loging() + { + auth_login auth_Login = new auth_login(); + + string response = await web.SendRequest(web.URL + "/auth/login", "POST", JsonUtility.ToJson(auth_Login)); + Debug.Log(response); + // 解析服务器返回的数据 + server serverData = JsonConvert.DeserializeObject(response); + //token = serverData.token; + //Debug.Log(setverData.msg); + + } + public async void createTemplate() + { + + string response = await web.SendRequest(web.URL + "/game/gameTemplate/list", "GET"); + Debug.Log(response); + + } +} diff --git a/xiaofang/Assets/Script/login/login.cs.meta b/xiaofang/Assets/Script/login/login.cs.meta new file mode 100644 index 00000000..eb1ab616 --- /dev/null +++ b/xiaofang/Assets/Script/login/login.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f381a54ea0a90e3448e8ae6e3cf61375 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/xiaofang/Assets/comm.meta b/xiaofang/Assets/comm.meta new file mode 100644 index 00000000..f453f24e --- /dev/null +++ b/xiaofang/Assets/comm.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d11e46e12fbe0404d9cbe4df1759b23a +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/xiaofang/Assets/comm/web.cs b/xiaofang/Assets/comm/web.cs new file mode 100644 index 00000000..6d4a6ecc --- /dev/null +++ b/xiaofang/Assets/comm/web.cs @@ -0,0 +1,108 @@ + +using System.Collections; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Unity.VisualScripting.Antlr3.Runtime; +using UnityEngine; +using UnityEngine.Networking; + + + + +public class web : MonoBehaviour +{ + public const string URL = "http://192.168.3.103:8089"; // 被注释掉的服务器地址(常量) + + // 存储服务器返回的Cookie + private static Dictionary cookieJar = new Dictionary(); + + // 发送HTTP请求的方法,支持GET和POST等不同的请求方式 + public static async Task SendRequest(string url, string method = "GET", string jsonData = "{}", Dictionary head = null) + { + Debug.Log(url + "启动web"); // 调试输出,启动网络请求 + // 使用UnityWebRequest类进行HTTP请求,支持自定义请求方法 + using (UnityWebRequest request = new UnityWebRequest(url, method)) + { + // 设置下载处理器,用于处理服务器响应的数据 + request.downloadHandler = new DownloadHandlerBuffer(); + + // 如果是POST请求,设置请求体 + if (method == "POST" && jsonData != null) + { + // 将JSON字符串转换为字节数组 + byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData); + // 设置上传处理器,用于发送请求体数据 + request.uploadHandler = new UploadHandlerRaw(bodyRaw); + // 设置请求头,指定请求体内容类型为JSON + request.SetRequestHeader("Content-Type", "application/json"); + } + + // 如果请求头部存在,添加请求头部信息 + if (head != null) + foreach (string key in head.Keys) + { + // 遍历传入的头部字典,逐项设置请求头部 + request.SetRequestHeader(key, head[key]); + } + + // 设置已存储的 Cookie + if (cookieJar.Count > 0) + { + // 用于存储多个cookie值的字符串 + string cookieHeader = ""; + foreach (var cookie in cookieJar) + { + // 格式化每个cookie为“键=值”的形式,并附加到字符串中,以分号分隔 + cookieHeader += $"{cookie.Key}={cookie.Value}; "; + } + // 设置请求头中的Cookie字段 + request.SetRequestHeader("Cookie", cookieHeader); + } + + // 发送请求并等待响应 + var operation = request.SendWebRequest(); + while (!operation.isDone) + await Task.Yield(); // 等待请求完成,使用await以非阻塞的方式处理 + + // 检查是否存在网络错误或协议错误 + if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError) + { + // 如果有错误,输出错误信息并返回null + Debug.LogError($"Error: {request.error}" + url); + // 可以自定义弹窗提示用户错误信息,代码被注释掉了 + // Promptmgr.Instance.PromptBubble(request.error, Color.black,Color.red); + return null; + } + else + { + // 处理返回的Cookie + string setCookieHeader; + string str = request.downloadHandler.text; // 获取服务器返回的文本内容 + //Debug.Log(str); // 输出返回内容到控制台 + + // 尝试获取响应头中的“Set-Cookie”字段 + if (request.GetResponseHeaders().TryGetValue("Set-Cookie", out setCookieHeader)) + { + // 将“Set-Cookie”字段的内容按分号分隔为多个部分 + string[] cookies = setCookieHeader.Split(';'); + foreach (var cookie in cookies) + { + // 将每个cookie分为“键=值”的形式 + var cookieParts = cookie.Split('='); + if (cookieParts.Length == 2) + { + string key = cookieParts[0].Trim(); // 去掉键的空格 + string value = cookieParts[1].Trim(); // 去掉值的空格 + // 存储到cookieJar中,以备后续请求使用 + cookieJar[key] = value; + } + } + } + + // 返回响应内容(文本形式) + return request.downloadHandler.text; + } + } + } +} diff --git a/xiaofang/Assets/comm/web.cs.meta b/xiaofang/Assets/comm/web.cs.meta new file mode 100644 index 00000000..d4935bc2 --- /dev/null +++ b/xiaofang/Assets/comm/web.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2c4b51a8b53ef05469c7aa617c131ad1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: