123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- using AR.Core;
- using Cysharp.Threading.Tasks;
- using LitJson;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.NetworkInformation;
- using TMPro;
- using UnityEngine;
- using UnityEngine.Networking;
- using UnityEngine.UI;
- namespace Assets
- {
- public class DlgLogin : BasePanel
- {
- public TMP_InputField inputField;
- public Button confirmBtn;
- public GameObject inputCode;
- public GameObject comeBackImg;
- WhiteList whiteList;
-
-
-
- string urlWhiteList = "http://txcdn.starmelody.art/WhiteList.json";
-
-
-
- bool isVerify = true;
- public override void OnCloseWindow()
- {
- UIManager.Instance.HideUI(WindowID.DlgLoading);
- }
- public override void OnDestroyWindow()
- {
- }
- public override void OnInitWindow()
- {
-
- confirmBtn.onClick.AddListener(async () =>
- {
- await UIManager.Instance.ShowUI(WindowID.DlgLoading);
- VerifyAccount();
- });
- OnShowWindow();
- }
- public override void OnShowWindow()
- {
- string account = PlayerPrefs.GetString("account");
- if (string.IsNullOrEmpty(account))
- {
- Debug.Log("没有本地保存的账号");
- inputCode.SetActive(true);
- comeBackImg.SetActive(false);
- }
- else
- {
- Debug.Log("已有本地保存的账号");
- Debug.Log("本地账号" + account);
- inputCode.SetActive(false);
- comeBackImg.SetActive(true);
- inputField.text = account;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- async void VerifyAccount()
- {
- if (isVerify)
- {
- StartCoroutine(GetSecretKey());
- }
- else
- {
- await UIManager.Instance.ShowUI(WindowID.DlgLoginFaild);
- }
- }
-
-
-
-
- IEnumerator GetSecretKey()
- {
- string url = "http://api.starmelody.art/api/Account/SecretKey";
- AccountMsg accountMsg = new AccountMsg();
- accountMsg.Account = inputField.text;
- string json = JsonMapper.ToJson(accountMsg);
- byte[] postBytes = System.Text.Encoding.Default.GetBytes(json);
- UnityWebRequest request = UnityWebRequest.Post(url, UnityWebRequest.kHttpVerbPOST);
- request.uploadHandler =new UploadHandlerRaw(postBytes);
- request.SetRequestHeader("Content-Type", "application/json");
- request.timeout = 5;
- yield return request.SendWebRequest();
- if (request.result == UnityWebRequest.Result.Success)
- {
- Debug.Log(request.downloadHandler.text);
- GetSecretKeyResult result = JsonMapper.ToObject<GetSecretKeyResult>(request.downloadHandler.text);
- if (result.code==0)
- {
- StartCoroutine(Login(result.message));
- }
- else
- {
- Debug.Log($"获取秘钥失败:{result.message}");
- StepManager.requestCode = result.code;
- yield return UIManager.Instance.ShowUI(WindowID.DlgLoginFaild);
- }
- }
- else
- {
- Debug.Log("密钥请求失败"+request.error);
- StepManager.requestCode = 1;
- yield return UIManager.Instance.ShowUI(WindowID.DlgLoginFaild);
- }
- }
-
-
-
-
-
- IEnumerator Login(string secretKey)
- {
- string url = "http://api.starmelody.art/api/Account/Login";
- Userdata userInfo = new Userdata();
- userInfo.account = inputField.text;
- userInfo.mac = DesEncryptHelper.Encrypt(GetMacAddress(), secretKey);
- string json = JsonMapper.ToJson(userInfo);
- byte[] postBytes = System.Text.Encoding.Default.GetBytes(json);
- UnityWebRequest request = UnityWebRequest.Post(url, UnityWebRequest.kHttpVerbPOST);
- request.uploadHandler = new UploadHandlerRaw(postBytes);
- request.SetRequestHeader("Content-Type", "application/json");
- yield return request.SendWebRequest();
- if (request.result == UnityWebRequest.Result.Success)
- {
- Debug.Log(request.downloadHandler.text);
- LoginResult loginResult = JsonMapper.ToObject<LoginResult>(request.downloadHandler.text);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (loginResult.code == 0)
- {
- PlayerPrefs.SetString("account", inputField.text);
- PlayerPrefs.Save();
- StepManager.timeStamp = loginResult.data;
- UIManager.Instance.HideUI(WindowID.DlgLogin);
- yield return UIManager.Instance.ShowUI(WindowID.FaceBuildPanel);
- }
- else
- {
- StepManager.requestCode = loginResult.code;
- yield return UIManager.Instance.ShowUI(WindowID.DlgLoginFaild);
- }
- }
- else
- {
- Debug.Log("登录请求"+request.error);
-
- StepManager.requestCode = 1;
- yield return UIManager.Instance.ShowUI(WindowID.DlgLoginFaild);
- }
- }
-
-
-
-
- private string GetMacAddress()
- {
- string physicalAddress = "";
- NetworkInterface[] nice = NetworkInterface.GetAllNetworkInterfaces();
- foreach (NetworkInterface adaper in nice)
- {
- if (adaper.Description == "en0")
- {
- physicalAddress = adaper.GetPhysicalAddress().ToString();
- break;
- }
- else
- {
- physicalAddress = adaper.GetPhysicalAddress().ToString();
- if (physicalAddress != "")
- {
- break;
- };
- }
- }
- Debug.Log("Mac地址:"+physicalAddress);
- #if UNITY_ANDROID
- return SystemInfo.deviceUniqueIdentifier;
- #else
- return physicalAddress;
- #endif
- }
- public async void LoadJson()
- {
-
- var txtAsset = await YooAssetManager.Instance.LoadAsset<TextAsset>("Assets/Bundles/Config/WhiteList");
- whiteList = JsonMapper.ToObject<WhiteList>(txtAsset.text);
- }
- }
-
-
-
- struct Userdata
- {
- public string account;
- public string mac;
- }
-
-
-
- struct AccountMsg
- {
- public string Account;
- }
-
-
-
- struct GetSecretKeyResult
- {
- public int code;
- public string message;
- public string data;
- }
-
-
-
- struct LoginResult
- {
- public int code;
- public string message;
- public long data;
- }
-
-
-
- struct WhiteList
- {
- public string[] table;
- public string TimeStamp;
- }
- }
|