LoginPanel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. using LitJson;
  2. using Newtonsoft.Json.Linq;
  3. using SC.XR.Unity;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Text.RegularExpressions;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. public class LoginPanel : SingletonMono<LoginPanel>
  12. {
  13. public SCInputField m_Account;
  14. public Button m_EmptyAccountBtn;
  15. public Text m_AccountErrorStr;
  16. public Transform m_AccountError;
  17. public string m_AccountStr;
  18. public SCInputField m_Password;
  19. public Button m_EmptyPwdBtn;
  20. public Button m_LockPwdBtn;
  21. public Text m_PwdErrorStr;
  22. public Transform m_PwdError;
  23. public string m_PasswordStr;
  24. public Button m_Logn;
  25. public Toggle m_RemenberPwd;
  26. public bool m_IsRemberPwd;
  27. public Text error;
  28. public GameObject loaginError;
  29. public string message;
  30. public Text sn;
  31. public Text version;
  32. private string token;
  33. private void Awake()
  34. {
  35. m_Account = transform.Find("Account/SCInputField").GetComponent<SCInputField>();
  36. m_EmptyAccountBtn = transform.Find("Account/EmptyAccount").GetComponent<Button>();
  37. m_AccountErrorStr = transform.Find("Error/AccountError/Text").GetComponent<Text>();
  38. m_AccountError = transform.Find("Error/AccountError");
  39. error = transform.Find("Error/CaptchaError/Text").GetComponent<Text>();
  40. loaginError = transform.Find("Error/CaptchaError").gameObject;
  41. m_Password = transform.Find("Password/SCInputField").GetComponent<SCInputField>();
  42. m_EmptyPwdBtn = transform.Find("Password/EmptyPassword").GetComponent<Button>();
  43. m_LockPwdBtn = transform.Find("Password/Icon").GetComponent<Button>();
  44. m_PwdErrorStr = transform.Find("Error/PasswordError/Text").GetComponent<Text>();
  45. m_PwdError = transform.Find("Error/PasswordError");
  46. sn = transform.Find("SN").GetComponent<Text>();
  47. version = transform.Find("Version").GetComponent<Text>();
  48. m_Logn = transform.Find("LoginBtn").GetComponent<Button>();
  49. m_RemenberPwd = transform.Find("RememberPwd").GetComponent<Toggle>();
  50. m_RemenberPwd.isOn = false;
  51. m_IsRemberPwd = false;
  52. AddUIListenEvent();
  53. LitJson.JsonMapper.RegisterImporter<int, string>((int input) => { return input.ToString(); });
  54. LitJson.JsonMapper.RegisterImporter<string, int>((string input) => { return Int32.Parse(input); });
  55. LitJson.JsonMapper.RegisterImporter<float, Double>((float input) => { return (double)(input); });
  56. OnShow();
  57. }
  58. private void Start()
  59. {
  60. // WriteLog.ConsoleLog.LogStart();
  61. sn.text = DeviceSN.Instance.SendSerialBy16();
  62. version.text = "V"+Application.version;
  63. // PlayerPrefs.DeleteAll();
  64. #if UNITY_EDITOR
  65. OnClickLogin();
  66. # endif
  67. //SendInit send = new SendInit();
  68. //send.sn = "ABD123456";
  69. //send.projectId = 10;
  70. //string jsonString = JsonMapper.ToJson(send);
  71. //HttpTool.Instance.PostTest("/sn/init", jsonString, InitSceneValueCallBack);
  72. //InitSceneValueCallBack(message);
  73. // //登录
  74. //SendLogin sendLogin = new SendLogin();
  75. // sendLogin.email = "test_basic@ghz-tech.com";
  76. // sendLogin.password = "123456789";
  77. // string jsonLogin = JsonMapper.ToJson(sendLogin);
  78. // HttpTool.Instance.PostLogin("/user/login", jsonLogin, LoginCallBack);
  79. // GameManager.Instance.text2.text = " 选择场景";
  80. //// 模拟下载
  81. //SendMaterials sendMaterials = new SendMaterials();
  82. //sendMaterials.url = "/file/user3/material/5_scaled.jpg";
  83. //string jsonMaterials = JsonMapper.ToJson(sendMaterials);
  84. //MaterialObjValue mObj = new MaterialObjValue();
  85. //mObj.name = "Test";
  86. //mObj.DownloadPath = "/file/user3/material/5_scaled.jpg";
  87. //mObj.localSavePath = Application.persistentDataPath + "/Image/" + Path.GetFileName(mObj.DownloadPath);
  88. //DownLoadMaterial value = new DownLoadMaterial(mObj, 1646126688);
  89. //HttpTool.Instance.PostImage(value, "/file/download", jsonMaterials, DownLoadCallBack);
  90. }
  91. public float timernext = 1;
  92. float timer = 0;
  93. private void Update()
  94. {
  95. timer += Time.deltaTime;
  96. if (timer > timernext)
  97. {
  98. timer = 0;
  99. timernext = UnityEngine.Random.Range(13, 33) * 60;
  100. }
  101. }
  102. /// <summary>
  103. /// 添加UI的监听事件
  104. /// </summary>
  105. private void AddUIListenEvent()
  106. {
  107. m_Account.onEndEdit.AddListener(OnAccountEndEdit);
  108. m_EmptyAccountBtn.onClick.AddListener(OnclickEmptyAccount);
  109. m_Password.onEndEdit.AddListener(OnPasswordEndEdit);
  110. m_EmptyPwdBtn.onClick.AddListener(OnClickEmptyPwd);
  111. m_LockPwdBtn.onClick.AddListener(OnclickLockPwd);
  112. m_Password.contentType = SCInputField.ContentType.Password;
  113. m_Logn.onClick.AddListener(OnClickLogin);
  114. m_RemenberPwd.onValueChanged.AddListener(OnRemPwdValueChange);
  115. }
  116. #region UI监听事件
  117. /// <summary>
  118. /// 账号输入完成
  119. /// </summary>
  120. /// <param name="str"></param>
  121. private void OnAccountEndEdit(string str)
  122. {
  123. m_AccountStr = "";
  124. if (string.IsNullOrWhiteSpace(str))
  125. {
  126. m_AccountError.gameObject.SetActive(true);
  127. m_AccountErrorStr.text = "请输入邮箱账号";
  128. }
  129. else
  130. {
  131. //邮箱名以数字或字母开头;邮箱名可由字母、数字、点号、减号、下划线组成;邮箱名(@前的字符)长度为3~18个字符;邮箱名不能以点号、减号或下划线结尾;不能出现连续两个或两个以上的点号、减号。
  132. //string pattern = @"^[a-zA-Z0-9]((?<!(\.\.|--))[a-zA-Z0-9\._-]){1,16}[a-zA-Z0-9]@([0-9a-zA-Z][0-9a-zA-Z-]{0,62}\.)+([0-9a-zA-Z][0-9a-zA-Z-]{0,62})\.?|((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$";
  133. string pattern = @"^([\w-\.]+)@([\w-\.]+)(\.[a-zA-Z0-9]+)$";
  134. if (Regex.IsMatch(str, pattern))
  135. {
  136. m_AccountStr = str;
  137. m_AccountErrorStr.text = "";
  138. m_AccountError.gameObject.SetActive(false);
  139. }
  140. else
  141. {
  142. m_AccountError.gameObject.SetActive(true);
  143. m_AccountErrorStr.text = "账号不合法";
  144. }
  145. }
  146. if (!string.IsNullOrWhiteSpace(m_AccountStr) && !string.IsNullOrWhiteSpace(m_PasswordStr))
  147. {
  148. m_Logn.interactable = true;
  149. }
  150. }
  151. /// <summary>
  152. /// 清理账号输入
  153. /// </summary>
  154. private void OnclickEmptyAccount()
  155. {
  156. m_AccountErrorStr.text = "";
  157. m_AccountError.gameObject.SetActive(false);
  158. m_AccountStr = "";
  159. m_Account.text = "";
  160. }
  161. /// <summary>
  162. /// 密码输入完成
  163. /// </summary>
  164. /// <param name="str"></param>
  165. private void OnPasswordEndEdit(string str)
  166. {
  167. m_PasswordStr = "";
  168. if (string.IsNullOrWhiteSpace(str))
  169. {
  170. m_PwdError.gameObject.SetActive(true);
  171. m_PwdErrorStr.text = "请输入密码";
  172. }
  173. else
  174. {
  175. //Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
  176. //if (RegCHZN.IsMatch(str))
  177. //{
  178. // m_PwdError.gameObject.SetActive(true);
  179. // m_PwdErrorStr.text = "密码不合法";
  180. // return;
  181. //}
  182. //int n = 0;
  183. ////必须包含小写字母
  184. //string pattern1 = @"(?=.*[a-z])";
  185. ////必须包含大写字母
  186. //string pattern2 = @"(?=.*[A-Z])";
  187. ////必须包含数字
  188. //string pattern3 = @"(?=.*\d)";
  189. ////string pattern3 = @"(?=.*[0 - 9])";
  190. ////必须包含特殊符号
  191. //string pattern4 = @"(?=.*[ !""#$%&'()*+,-./:;<=>?@\[\]\^_`{|}~])";
  192. //string pattern4 = @"(?=([\x21-\x7e]+)[^a-zA-Z0-9])";
  193. //至少8个字符,最多32个字符
  194. string pattern5 = @"^.{8,32}$";
  195. if (!Regex.IsMatch(str, pattern5))
  196. {
  197. m_PwdError.gameObject.SetActive(true);
  198. m_PwdErrorStr.text = "密码小于8位数或大于32位";
  199. return;
  200. }
  201. m_PasswordStr = str;
  202. m_PwdErrorStr.text = "";
  203. m_PwdError.gameObject.SetActive(false);
  204. //if (Regex.IsMatch(str, pattern1))
  205. //{
  206. // n++;
  207. //}
  208. //if (Regex.IsMatch(str, pattern2))
  209. //{
  210. // n++;
  211. //}
  212. //if (Regex.IsMatch(str, pattern3))
  213. //{
  214. // n++;
  215. //}
  216. //if (Regex.IsMatch(str, pattern4))
  217. //{
  218. // n++;
  219. //}
  220. //if (n >= 3)
  221. //{
  222. // m_PasswordStr = str;
  223. // m_PwdErrorStr.text = "";
  224. // m_PwdError.gameObject.SetActive(false);
  225. //}
  226. //else
  227. //{
  228. // m_PwdError.gameObject.SetActive(true);
  229. // m_PasswordStr = "";
  230. // m_PwdErrorStr.text = "密码不正确,请重新输入";
  231. //}
  232. }
  233. if (!string.IsNullOrWhiteSpace(m_AccountStr) && !string.IsNullOrWhiteSpace(m_PasswordStr))
  234. {
  235. m_Logn.interactable = true;
  236. }
  237. }
  238. /// <summary>
  239. /// 清理密码输入
  240. /// </summary>
  241. private void OnClickEmptyPwd()
  242. {
  243. m_PwdErrorStr.text = "";
  244. m_PwdError.gameObject.SetActive(false);
  245. m_PasswordStr = "";
  246. m_Password.text = "";
  247. }
  248. /// <summary>
  249. /// 是否可以显示密码
  250. /// </summary>
  251. private void OnclickLockPwd()
  252. {
  253. if (m_LockPwdBtn.image.sprite.name == "Lock_Close")
  254. {
  255. m_LockPwdBtn.image.sprite = Resources.Load<Sprite>("Texture/Lock_Open");
  256. m_Password.contentType = SCInputField.ContentType.Standard;
  257. }
  258. else if (m_LockPwdBtn.image.name == "Lock_Open")
  259. {
  260. m_LockPwdBtn.image.sprite = Resources.Load<Sprite>("Texture/Lock_Close");
  261. m_Password.contentType = SCInputField.ContentType.Password;
  262. }
  263. }
  264. /// <summary>
  265. /// 登录
  266. /// </summary>
  267. private void OnClickLogin()
  268. {
  269. #if UNITY_EDITOR
  270. m_AccountStr = "mandan.chao@ghz-tech.com";
  271. m_PasswordStr = "123456Aa";
  272. #endif
  273. loaginError.SetActive(false);
  274. //m_AccountStr = "yifei.yang@ivglass.com";
  275. //m_AccountStr = "1400672881@qq.com";
  276. ////m_AccountStr = "test_basic@ghz-tech.com";
  277. ////m_PasswordStr = "123456789";
  278. if (string.IsNullOrWhiteSpace(m_AccountStr))
  279. {
  280. m_AccountErrorStr.text = "账号不能为空";
  281. return;
  282. }
  283. Debug.Log(m_PasswordStr);
  284. if (string.IsNullOrWhiteSpace(m_PasswordStr))
  285. {
  286. m_PwdErrorStr.text = "密码不能为空";
  287. return;
  288. }
  289. if (m_IsRemberPwd)
  290. {
  291. PlayerPrefs.SetString("Account", m_AccountStr);
  292. PlayerPrefs.SetString("Password", m_PasswordStr);
  293. PlayerPrefs.SetString("IsremPwd", "1");
  294. }
  295. else
  296. {
  297. PlayerPrefs.SetString("Account", "");
  298. PlayerPrefs.SetString("Password", "");
  299. PlayerPrefs.SetString("IsremPwd", "0");
  300. }
  301. //向服务器发送账号和密码进行验证
  302. Debug.Log("向服务器发送账号和密码进行登录");
  303. m_Logn.interactable = false;
  304. GameManager.Instance.text.text = " 登录";
  305. // 登录
  306. SendLogin sendLogin = new SendLogin();
  307. sendLogin.email = m_AccountStr;
  308. sendLogin.password = m_PasswordStr;
  309. string jsonLogin = JsonMapper.ToJson(sendLogin);
  310. GameManager.Instance.text.text = jsonLogin;
  311. HttpTool.Instance.PostLogin("/user/login", jsonLogin, LoginCallBack);
  312. GameManager.Instance.text2.text = " 选择场景";
  313. }
  314. /// <summary>
  315. /// 是否记住密码
  316. /// </summary>
  317. /// <param name="b"></param>
  318. private void OnRemPwdValueChange(bool b)
  319. {
  320. m_IsRemberPwd = b;
  321. }
  322. #endregion
  323. public void OnShow()
  324. {
  325. // gameObject.transform.localScale = new Vector3(1, 1, 1);
  326. m_Logn.interactable = false;
  327. m_AccountStr = PlayerPrefs.GetString("Account");
  328. m_PasswordStr = PlayerPrefs.GetString("Password");
  329. int n = int.Parse(PlayerPrefs.GetString("IsremPwd", "0"));
  330. m_RemenberPwd.isOn = n > 0 ? true : false;
  331. m_IsRemberPwd = n > 0 ? true : false;
  332. if (!string.IsNullOrWhiteSpace(m_AccountStr) && !string.IsNullOrWhiteSpace(m_PasswordStr))
  333. {
  334. m_Account.text = m_AccountStr;
  335. m_Password.text = m_PasswordStr;
  336. m_Logn.interactable = true;
  337. }
  338. else
  339. {
  340. m_Account.text = "";
  341. m_Password.text = "";
  342. m_AccountStr = "";
  343. m_PasswordStr = "";
  344. m_Logn.interactable = false;
  345. }
  346. }
  347. public void LoginCallBack(string message)
  348. {
  349. Debug.Log( message);
  350. GameManager.Instance.text.text = message;
  351. if (message.Contains("密码不正确,请重新输入") || message.Contains("用户未注册"))
  352. {
  353. JObject obj = JObject.Parse(message);
  354. message = obj["message"].ToString();
  355. loaginError.SetActive(true);
  356. error.text = message;
  357. m_Logn.interactable = true;
  358. return;
  359. }
  360. // 获取素材库列表
  361. //JObject obj = JObject.Parse(message);
  362. //Debug.Log(obj["data"]["token"].ToString());
  363. HttpTool.Instance.GetAllMaterials("/material/init", message, AllMaterialsCallBack);
  364. HttpTool.Instance.Get("/project/index", GetAllSceneDataCallBack);
  365. }
  366. public void DownLoadCallBack(DownLoadMaterial mObj, object obj)
  367. {
  368. Debug.Log(mObj.mObj.name + " " + mObj.mObj.DownloadPath);
  369. Sprite sprite = (Sprite)obj;
  370. m_LockPwdBtn.GetComponent<Image>().sprite = sprite;
  371. }
  372. private void AllMaterialsCallBack(string message)
  373. {
  374. GameManager.Instance.text.text = " 获取素材列表";
  375. GameManager.Instance.text4.text = message;
  376. Debug.Log(message);
  377. JObject jObject = JObject.Parse(message);
  378. string data = jObject["data"].ToString();
  379. JObject dataObject = JObject.Parse(data);
  380. string single = dataObject["single"].ToString();
  381. string library = dataObject["library"].ToString();
  382. string combination = dataObject["combination"].ToString();
  383. //string test = dataObject["combination"][0].ToString();
  384. //Debug.Log(test);
  385. //CompositeMaterialValueTest test111 = JsonMapper.ToObject<CompositeMaterialValueTest>(test);
  386. List<CompositeMaterialValue> singleMaterials = JsonMapper.ToObject<List<CompositeMaterialValue>>(single);
  387. List<CompositeMaterialValue> libraryMaterials = JsonMapper.ToObject<List<CompositeMaterialValue>>(library);
  388. Debug.Log(combination);
  389. List<CompositeMaterialValue> combinationMaterials = JsonMapper.ToObject<List<CompositeMaterialValue>>(combination);
  390. List<CompositeMaterialValue> upLoadMaterials = new List<CompositeMaterialValue>();
  391. //for (int i = 0; i < libraryMaterials.Count; i++)
  392. //{
  393. // upLoadMaterials.Add(libraryMaterials[i]);
  394. //}
  395. for (int i = 0; i < combinationMaterials.Count; i++)
  396. {
  397. upLoadMaterials.Add(combinationMaterials[i]);
  398. }
  399. for (int i = 0; i < singleMaterials.Count; i++)
  400. {
  401. upLoadMaterials.Add(singleMaterials[i]);
  402. }
  403. Debug.Log(upLoadMaterials.Count);
  404. //for (int i = 0; i < upLoadMaterials.Count; i++)
  405. //{
  406. // Debug.Log(upLoadMaterials[i].name);
  407. // for (int j = 0; j < upLoadMaterials[i].materialList.Count; j++)
  408. // {
  409. // Debug.Log(upLoadMaterials[i].materialList[j].name);
  410. // }
  411. //}
  412. GameManager.Instance.listCMValue = GameManager.Instance.ProcessCMVJsonData(upLoadMaterials, true);
  413. GameManager.Instance.text.text = " 素材列表获取结束";
  414. try
  415. {
  416. }
  417. catch (Exception e)
  418. {
  419. ErrorLogPanel.Instance.Show(" 处理素材列表数据出现错误 " + e.Message);
  420. }
  421. }
  422. private void GetAllSceneDataCallBack(string message)
  423. {
  424. Debug.Log("@@@@ "+message);
  425. JObject jobject = JObject.Parse(message);
  426. message = jobject["data"].ToString();
  427. List<ScenesPage> listScenesPage = JsonMapper.ToObject<List<ScenesPage>>(message);
  428. if (listScenesPage == null || listScenesPage.Count == 0)
  429. {
  430. error.text = "当前账号下未创建场景";
  431. return;
  432. }
  433. SelectScene.Instance.Show(listScenesPage);
  434. }
  435. private void InitSceneValueCallBack(string message)
  436. {
  437. try
  438. {
  439. GameManager.Instance.text2.text = " 获取场景信息";
  440. JObject jObject = JObject.Parse(message);
  441. message = jObject["data"].ToString();
  442. Debug.Log("@@@ " + message);
  443. SceneValue scene = JsonMapper.ToObject<SceneValue>(message);
  444. GameManager.Instance.scene = GameManager.Instance.ProcesSceneValueJsonData(scene);
  445. GameManager.Instance.text2.text = " 场景信息获取结束";
  446. }
  447. catch (Exception e)
  448. {
  449. throw;
  450. }
  451. }
  452. }