You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
239 lines
7.8 KiB
239 lines
7.8 KiB
using Newtonsoft.Json; |
|
using System.IO; |
|
using System.Text; |
|
using TMPro; |
|
using UnityEngine; |
|
using UnityEngine.UI; |
|
|
|
public class RememberPwdData |
|
{ |
|
public string Account; |
|
public string Password; |
|
public bool IsRemember; |
|
public int RoleType; |
|
} |
|
public class LoginManager : MonoBehaviour |
|
{ |
|
public TMP_InputField UserNameInput; |
|
public TMP_InputField PwdInput; |
|
public Toggle JiaoYuanToggle; |
|
public Toggle XueYuanToggle; |
|
public Toggle GuanLiYuanToggle; |
|
public Button RegistorBtn; |
|
public Button LoginBtn; |
|
public Button LostPwdBtn; |
|
public Toggle RePwdToggle; |
|
public Toggle PwdShowToggle; |
|
private string SignInApi = "/api/Account/SignIn"; |
|
private RememberPwdData rememberData; |
|
private string rememberfilepath; |
|
void Start() |
|
{ |
|
rememberfilepath = Application.streamingAssetsPath + @"/RememberPwd.json"; |
|
LoadFileHelper.Instance.LoadFileStringSync(rememberfilepath, (a) => |
|
{ |
|
rememberData = JsonConvert.DeserializeObject<RememberPwdData>(a); |
|
if (rememberData.IsRemember) |
|
{ |
|
UserNameInput.text = rememberData.Account; |
|
PwdInput.text = RSAHelper.RSADecrypt(rememberData.Password); |
|
RePwdToggle.isOn = true; |
|
PwdShowToggle.interactable = false; |
|
if (rememberData.RoleType == (int)RoleType.学员) |
|
{ |
|
XueYuanToggle.isOn = true; |
|
} |
|
else if (rememberData.RoleType == (int)RoleType.教员) |
|
{ |
|
JiaoYuanToggle.isOn = true; |
|
} |
|
else |
|
{ |
|
GuanLiYuanToggle.isOn = true; |
|
} |
|
} |
|
else |
|
{ |
|
//此处记住上一次登录的账号,如果不需要记住直接赋值空 |
|
//UserNameInput.text = rememberData.Account; |
|
UserNameInput.text = ""; |
|
PwdInput.text = ""; |
|
RePwdToggle.isOn = false; |
|
} |
|
}); |
|
LoginBtn.onClick.AddListener(LoginBtn_Click); |
|
LostPwdBtn.onClick.AddListener(LostPwdBtn_Click); |
|
RegistorBtn.onClick.AddListener(RegistorBtn_Click); |
|
RePwdToggle.onValueChanged.AddListener(RePwdBtn_ValueChanged); |
|
JiaoYuanToggle.onValueChanged.AddListener((a) => { JiaoYuanToggle_ValueChanged(); }); |
|
XueYuanToggle.onValueChanged.AddListener((a) => { JiaoYuanToggle_ValueChanged(); }); |
|
GuanLiYuanToggle.onValueChanged.AddListener((a) => { JiaoYuanToggle_ValueChanged(); }); |
|
} |
|
|
|
private void JiaoYuanToggle_ValueChanged() |
|
{ |
|
if (JiaoYuanToggle.isOn) |
|
{ |
|
PanelsManager.Instance.NowRoleType = RoleType.教员; |
|
RegistorBtn.gameObject.SetActive(true); |
|
} |
|
else if (XueYuanToggle.isOn) |
|
{ |
|
PanelsManager.Instance.NowRoleType = RoleType.学员; |
|
RegistorBtn.gameObject.SetActive(true); |
|
} |
|
else |
|
{ |
|
PanelsManager.Instance.NowRoleType = RoleType.管理员; |
|
RegistorBtn.gameObject.SetActive(false); |
|
} |
|
} |
|
|
|
private void RePwdBtn_ValueChanged(bool IsOn) |
|
{ |
|
if (IsOn)//记住密码 |
|
{ |
|
PwdShowToggle.isOn = false; |
|
PwdShowToggle.interactable = false; |
|
} |
|
else |
|
{ |
|
PwdShowToggle.interactable = true; |
|
} |
|
} |
|
|
|
private void RegistorBtn_Click() |
|
{ |
|
if (PanelsManager.Instance.NowRoleType == RoleType.教员 || |
|
PanelsManager.Instance.NowRoleType == RoleType.学员 |
|
) |
|
{ |
|
PanelsManager.Instance.GotoRegistorWindow(); |
|
} |
|
else |
|
{ |
|
TipPanelManager.GetInstance.Show("管理员无法注册!"); |
|
} |
|
} |
|
|
|
private void LostPwdBtn_Click() |
|
{ |
|
//if (string.IsNullOrEmpty(UserNameInput.text)) |
|
//{ |
|
// TipPanelManager.GetInstance.Show("请输入用户名!"); |
|
// return; |
|
//} |
|
PanelsManager.Instance.GotoLostPasswordWindow(UserNameInput.text); |
|
} |
|
|
|
private void LoginBtn_Click() |
|
{ |
|
if (string.IsNullOrEmpty(UserNameInput.text)) |
|
{ |
|
TipPanelManager.GetInstance.Show("请输入用户名!"); |
|
return; |
|
} |
|
|
|
if (string.IsNullOrEmpty(PwdInput.text)) |
|
{ |
|
TipPanelManager.GetInstance.Show("请输入密码!"); |
|
return; |
|
} |
|
User user = new User() { username = UserNameInput.text.Trim(), password = PwdInput.text }; |
|
HTTPRequestManager.Instance.PostJson<User, IdentityInfo>( |
|
SignInApi, user, SignInSuccessed, SignInError); |
|
} |
|
private void SignInSuccessed(IdentityInfo identityInfo) |
|
{ |
|
if (identityInfo == null) |
|
{ |
|
identityInfo = new IdentityInfo() { token = "", expires = 20, realName = "张三", roleType = RoleType.教员 }; |
|
} |
|
HTTPRequestManager.Instance.identityInfo = identityInfo; |
|
HTTPRequestManager.Instance.userName = UserNameInput.text.Trim(); |
|
HTTPRequestManager.Instance.StartTimer(); |
|
|
|
//首次登录,跳转到重置密码页面 |
|
if (identityInfo.changePasswordRequired && HTTPRequestManager.Instance.identityInfo.roleType != RoleType.管理员) |
|
{ |
|
//LoginSceneManager.Instance.GotoChangePasswordWindow(); |
|
PanelsManager.Instance.GotoChangePasswordWindow(HTTPRequestManager.Instance.userName); |
|
return; |
|
} |
|
|
|
//记住密码 |
|
if (RePwdToggle.isOn) |
|
{ |
|
rememberData.Account = UserNameInput.text.Trim(); |
|
rememberData.IsRemember = true; |
|
rememberData.RoleType = (int)identityInfo.roleType; |
|
rememberData.Password = RSAHelper.RSAEncryption(PwdInput.text); |
|
} |
|
else |
|
{ |
|
rememberData.Account = UserNameInput.text.Trim(); |
|
rememberData.IsRemember = false; |
|
rememberData.RoleType = (int)identityInfo.roleType; |
|
rememberData.Password = ""; |
|
} |
|
var json = JsonConvert.SerializeObject(rememberData); |
|
using (var filestream = new FileStream(rememberfilepath, FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite)) |
|
{ |
|
var fileBytes = Encoding.UTF8.GetBytes(json); |
|
filestream.Write(fileBytes, 0, fileBytes.Length); |
|
} |
|
PanelsManager.Instance.GotoSystemSelectWindow(PwdInput.text); |
|
} |
|
private void LateUpdate() |
|
{ |
|
if (Input.GetKeyDown(KeyCode.Tab)) |
|
{ |
|
if (UserNameInput.isFocused) |
|
{ |
|
PwdInput.Select(); |
|
} |
|
else if (PwdInput.isFocused) |
|
{ |
|
UserNameInput.Select(); |
|
} |
|
else |
|
{ |
|
UserNameInput.Select(); |
|
} |
|
} |
|
if (Input.GetKeyDown(KeyCode.Return)) |
|
{ |
|
if (!string.IsNullOrEmpty(UserNameInput.text.Trim()) && !string.IsNullOrEmpty(PwdInput.text.Trim())) |
|
{ |
|
LoginBtn_Click(); |
|
} |
|
} |
|
} |
|
private void SignInError(int errorCode, string errorMsg) |
|
{ |
|
TipPanelManager.GetInstance.Show($"登录错误!\n{errorMsg}"); |
|
} |
|
internal void OnShow(string userName) |
|
{ |
|
UserNameInput.text = userName; |
|
PwdInput.text = ""; |
|
RePwdToggle.isOn = false; |
|
} |
|
/// <summary> |
|
/// 英文字符转为中文字符 |
|
/// </summary> |
|
/// <param name="text">转换的中文字符串</param> |
|
/// <returns></returns> |
|
public static string ConvertToEn(string text) |
|
{ |
|
const string ch = "。;,?!、“”‘’()—";//中文字符 |
|
const string en = @".;,?!\""""''()-";//英文字符 |
|
char[] c = text.ToCharArray(); |
|
for (int i = 0; i < c.Length; i++) |
|
{ |
|
int n = ch.IndexOf(c[i]); |
|
if (n != -1) c[i] = en[n]; |
|
} |
|
return new string(c); |
|
} |
|
}
|
|
|