LoginDlg.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. using LitJson;
  2. using SC.XR.Unity;
  3. using ShadowStudio.Mgr;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Text.RegularExpressions;
  8. using UnityEngine;
  9. using UnityEngine.SceneManagement;
  10. using UnityEngine.UI;
  11. using XRTool.UI;
  12. using XRTool.Util;
  13. using XRTool.WorldUI;
  14. using static SC.XR.Unity.SCInputField;
  15. using static ScenesManager;
  16. using static ShowLogin;
  17. namespace ShadowStudio.UI
  18. {
  19. [RequireComponent(typeof(WorldDlg))]
  20. /// <summary>
  21. /// 登录的窗口
  22. /// </summary>
  23. public class LoginDlg : MonoBehaviour
  24. {
  25. private SCInputField userInputField;
  26. private SCInputField passwordInputField;
  27. private Button loginBtn;
  28. private Toggle remberToggle;
  29. private GameObject verifyObj;
  30. private SCInputField verifyInputField;
  31. private RawImage verifyTexture;
  32. private Button verifyTextureBtn;
  33. private bool isRemberUserAndPass;
  34. public bool isLock;
  35. private Text userRemind;
  36. private Text passwordRemind;
  37. private Text vcodeRemind;
  38. private Button phoneNumLoginBtn;
  39. public GameObject user;
  40. public GameObject PassBt;
  41. WorldDlg dlg;
  42. public GameObject AccountLoginObj;
  43. public GameObject PhoneLoginObj;
  44. private void Awake()
  45. {
  46. dlg = GetComponent<WorldDlg>();
  47. userInputField = dlg.GetChild<SCInputField>("AccountLoginObj/UIRoot/UserInputField");
  48. passwordInputField = dlg.GetChild<SCInputField>("AccountLoginObj/UIRoot/PasswordInputField");
  49. loginBtn = dlg.GetChild<Button>("AccountLoginObj/FullUIRoot/LoginBtn");
  50. remberToggle = dlg.GetChild<Toggle>("AccountLoginObj/UIRoot/RemberToggle_CheckBox");
  51. remberToggle.onValueChanged.AddListener(ClickOnToggle);
  52. verifyObj = dlg.GetChild<Transform>("AccountLoginObj/UIRoot/VerifyObj").gameObject;
  53. verifyInputField = dlg.GetChild<SCInputField>("AccountLoginObj/UIRoot/VerifyObj/VerifyInputField");
  54. verifyTexture = dlg.GetChild<RawImage>("AccountLoginObj/UIRoot/VerifyObj/VerifyTexture");
  55. verifyTextureBtn = dlg.GetBreadthChild<Button>("VerifyTextureBtn");
  56. verifyTextureBtn.onClick.AddListener(OnClickChangeTex);
  57. loginBtn.onClick.AddListener(OnClickLogin);
  58. userInputField.onEndEdit.AddListener(InputUserEnd);
  59. userInputField.onValueChanged.AddListener(UserValueChanged);
  60. passwordInputField.onEndEdit.AddListener(InputPassEnd);
  61. passwordInputField.onValueChanged.AddListener(PassValueChanged);
  62. verifyInputField.onEndEdit.AddListener(InputVerifyEnd);
  63. verifyInputField.onValueChanged.AddListener(VerifyValueChanged);
  64. userRemind = dlg.GetChild<Text>("AccountLoginObj/UIRoot/ErrorRemind/UserRemind");
  65. passwordRemind = dlg.GetChild<Text>("AccountLoginObj/UIRoot/ErrorRemind/PasswordRemind");
  66. vcodeRemind = dlg.GetChild<Text>("AccountLoginObj/UIRoot/VerifyObj/VcodeRemind");
  67. phoneNumLoginBtn = dlg.GetChild<Button>("AccountLoginObj/UIRoot/PhoneNumLoginBtn");
  68. phoneNumLoginBtn.onClick.AddListener(ClickOnphoneNumLogin);
  69. // OnClickLogin();
  70. }
  71. private void OnEnable()
  72. {
  73. #if UNITY_EDITOR
  74. Invoke("OnClickLogin", 1f);
  75. #endif
  76. }
  77. private void ClickOnphoneNumLogin()
  78. {
  79. LoginInit.LoginMethod = "PhoneLogin";
  80. show();
  81. }
  82. public void hide()
  83. {
  84. this.gameObject.SetActive(false);
  85. }
  86. public void show()
  87. {
  88. switch (LoginInit.LoginMethod)
  89. {
  90. case "AccountLogin":
  91. this.gameObject.SetActive(true);
  92. AccountLoginObj.SetActive(true);
  93. PhoneLoginObj.SetActive(false);
  94. isRemberUserAndPass = PlayerPrefs.GetInt("isRemberUserAndPass") == 1 ? true : false;
  95. Debug.Log(PlayerPrefs.GetInt("isRemberUserAndPass"));
  96. remberToggle.isOn = isRemberUserAndPass;
  97. verifyObj.SetActive(false);
  98. verifyInputField.text = "";
  99. if (LoginMgr.Instance.userInfo != null && isRemberUserAndPass)
  100. {
  101. userInputField.text = LoginMgr.Instance.userInfo.UserID;
  102. passwordInputField.text = LoginMgr.Instance.userInfo.Password;
  103. if (userInputField.text != "")
  104. {
  105. user.SetActive(true);
  106. }
  107. if (passwordInputField.text != "")
  108. {
  109. PassBt.SetActive(true);
  110. }
  111. }
  112. else
  113. {
  114. userInputField.text = "";
  115. passwordInputField.text = "";
  116. //LoginMgr.Instance.DeleteLoginInfo();
  117. user.SetActive(false);
  118. PassBt.SetActive(false);
  119. }
  120. userRemind.gameObject.SetActive(false);
  121. passwordRemind.gameObject.SetActive(false);
  122. break;
  123. case "PhoneLogin":
  124. this.gameObject.SetActive(true);
  125. AccountLoginObj.SetActive(false);
  126. PhoneLoginObj.SetActive(true);
  127. if (PhoneLogin.Instance)
  128. {
  129. PhoneLogin.Instance.Init();
  130. }
  131. break;
  132. }
  133. }
  134. public void cleanUser()
  135. {
  136. userInputField.text = "";
  137. user.SetActive(false);
  138. }
  139. public void cleanPassWord()
  140. {
  141. passwordInputField.text = "";
  142. PassBt.SetActive(false);
  143. }
  144. public void openEye()
  145. {
  146. passwordInputField.contentType = ContentType.Standard;
  147. // passwordInputField.UpdateLabel();
  148. }
  149. public void closeEye()
  150. {
  151. passwordInputField.contentType = ContentType.Password;
  152. // passwordInputField.UpdateLabel();
  153. }
  154. private void OnClickChangeTex()
  155. {
  156. NetWorkHeaders.GetTextureCode(GetSucess);
  157. }
  158. private void UserValueChanged(string user)//实时检测输入值的变化
  159. {
  160. userRemind.gameObject.SetActive(false);
  161. if (user != "")
  162. {
  163. this.user.SetActive(true);
  164. }
  165. }
  166. private void InputUserEnd(string user)
  167. {
  168. if (user == "")
  169. {
  170. userRemind.gameObject.SetActive(true);
  171. userRemind.text = RtcStrConfig.qAccount;
  172. this.user.SetActive(false);
  173. }
  174. else
  175. {
  176. //Regex regNum = new Regex(@"^\d{11}$");
  177. //bool ismatchNum = regNum.IsMatch(user);
  178. //if (!ismatchNum)
  179. //{
  180. // userRemind.gameObject.SetActive(true);
  181. // userRemind.text = RtcStrConfig.gAccount;
  182. //}
  183. this.user.SetActive(true);
  184. }
  185. }
  186. private void PassValueChanged(string password)
  187. {
  188. passwordRemind.gameObject.SetActive(false);
  189. if (password != "")
  190. {
  191. PassBt.SetActive(true);
  192. }
  193. }
  194. private void InputPassEnd(string password)
  195. {
  196. if (password == "")
  197. {
  198. passwordRemind.gameObject.SetActive(true);
  199. passwordRemind.text = RtcStrConfig.qPassWord;
  200. PassBt.SetActive(false);
  201. }
  202. else
  203. {
  204. PassBt.SetActive(true);
  205. }
  206. }
  207. private void VerifyValueChanged(string verifyNum)
  208. {
  209. vcodeRemind.gameObject.SetActive(false);
  210. }
  211. private void InputVerifyEnd(string verifyNum)
  212. {
  213. if (verifyNum == "")
  214. {
  215. vcodeRemind.gameObject.SetActive(true);
  216. vcodeRemind.text = RtcStrConfig.qRemind;
  217. }
  218. }
  219. private void ClickOnToggle(bool ison)
  220. {
  221. Debug.Log("ClickOnToggle");
  222. isRemberUserAndPass = ison;
  223. }
  224. private void OnClickLogin()
  225. {
  226. //userInputField.text = "test12";
  227. //passwordInputField.text = "abc12345";
  228. //userInputField.text = "17606175081";
  229. //passwordInputField.text = "123qweqwe";
  230. //userInputField.text = "test02";
  231. //passwordInputField.text = "abc12345";
  232. #if UNITY_EDITOR
  233. userInputField.text = "test06";
  234. passwordInputField.text = "123456Aa";
  235. #endif
  236. if (userInputField.text == "" || passwordInputField.text == "")
  237. {
  238. if (userInputField.text == "")
  239. {
  240. userRemind.gameObject.SetActive(true);
  241. userRemind.text = RtcStrConfig.qAccount;
  242. }
  243. else if (passwordInputField.text == "")
  244. {
  245. passwordRemind.gameObject.SetActive(true);
  246. passwordRemind.text = RtcStrConfig.qPassWord;
  247. }
  248. }
  249. //else if(userRemind.gameObject.activeSelf && userRemind.text == "账号格式错误")
  250. //{
  251. // return;
  252. //}
  253. else
  254. {
  255. if (!isLock)
  256. {
  257. if (!verifyObj.activeSelf)
  258. {
  259. isLock = true;
  260. NetWorkHeaders.YuLogin(userInputField.text, passwordInputField.text, YuLoginOk, LoginFail);
  261. }
  262. else
  263. {
  264. if (verifyInputField.text != "")
  265. {
  266. isLock = true;
  267. NetWorkHeaders.YuLogin(userInputField.text, passwordInputField.text, verifyInputField.text,capID, YuLoginOk, LoginFail);
  268. }
  269. else
  270. {
  271. vcodeRemind.gameObject.SetActive(true);
  272. vcodeRemind.text = RtcStrConfig.qRemind;
  273. }
  274. }
  275. }
  276. }
  277. }
  278. public void ClickBreakLoginBtn()
  279. {
  280. if (!isLock)
  281. {
  282. NetWorkHeaders.BreakNumLogin(LoginOk, LoginFail);
  283. isLock = true;
  284. }
  285. }
  286. private void YuLoginOk(JsonData js)
  287. {
  288. if (js["code"].ToString() == "200")
  289. {
  290. NetWorkHeaders.Login(userInputField.text, passwordInputField.text, LoginOk, LoginFail);
  291. }
  292. }
  293. private void YuLoginOk()
  294. {
  295. NetWorkHeaders.Login(userInputField.text, passwordInputField.text, LoginOk, LoginFail);
  296. }
  297. public void LoginOk(JsonData js)
  298. {
  299. ///字符串检测/判断
  300. string user = userInputField.text;
  301. string password = passwordInputField.text;
  302. UserInfo.isSN = false;
  303. UserInfo.UnionId = js["data"]["unionId"].ToString();
  304. UserInfo.User_Token = js["data"]["token"].ToString();
  305. UserInfo.Account = user;
  306. UserInfo.PassWord = password;
  307. NetWorkHeaders.GetUserInfo((JsonData sData) => {
  308. UserInfo.phone =sData["data"]["phone"].ToString();
  309. UserInfo.userName = sData["data"]["nickName"].ToString();
  310. if(UserInfo.userName=="")
  311. {
  312. UserInfo.userName = user;
  313. //UserInfo.userName = "YCKJ" + UserInfo.Account.Substring(UserInfo.Account.Length - 4);
  314. }
  315. UserInfo.activateType = int.Parse(sData["data"]["activateType"].ToString());
  316. UserInfo.indate = double.Parse(sData["data"]["indate"].ToString());
  317. CustomInfo.isSendAudio = bool.Parse(sData["data"]["settings"]["mic"].ToString());
  318. CustomInfo.isSendVideo = bool.Parse(sData["data"]["settings"]["camera"].ToString());
  319. CustomInfo.camIndex = int.Parse(sData["data"]["settings"]["resolution"].ToString());
  320. NetWorkHeaders.GetUserAvater((JsonData aData) =>
  321. {
  322. NetWorkHeaders.Instance.getNetTexture(aData["data"]["user"][0].ToString(), null, (Texture tex) => {
  323. UserInfo.defaulttextIcon = tex;
  324. });
  325. UserInfo.defaultavatar = aData["data"]["user"][0].ToString();
  326. });
  327. if (sData["data"]["avatar"].ToString()=="")
  328. {
  329. UserInfo.textIcon = UserInfo.defaulttextIcon;
  330. UserInfo.avatar = UserInfo.defaultavatar;
  331. NetWorkHeaders.SetUserInfo();
  332. }
  333. else
  334. {
  335. UserInfo.avatar = sData["data"]["avatar"].ToString();
  336. NetWorkHeaders.Instance.getNetTexture(sData["data"]["avatar"].ToString(),null, (Texture tex) => {
  337. UserInfo.textIcon = tex;
  338. });
  339. }
  340. isLock = false;
  341. ///发送登录请求
  342. ///回调函数中处理是否保存数据到登录缓存
  343. bool isSuccess = LoginMgr.Instance.Login(user, password);//登录成功
  344. ///
  345. ///保存缓存信息2个条件:1,记录账号,2,登录成功
  346. ///用户选择记住密码时方可保存密码
  347. if (isSuccess)
  348. {
  349. if (!isRemberUserAndPass)
  350. {
  351. user = "";
  352. password = "";
  353. userInputField.text = "";
  354. passwordInputField.text = "";
  355. LoginMgr.Instance.DeleteLoginInfo();
  356. }
  357. else
  358. {
  359. LoginMgr.Instance.SaveLoginInfo(user, password);
  360. }
  361. }
  362. WSHandler.Office.OnInit -= Init;
  363. WSHandler.Office.OnInit += Init;
  364. WSHandler.init();
  365. //
  366. // SceneManager.LoadScene("WebRtc");
  367. // UserInfoMgr.Instance.SaveLoginInfo(userInputField.text, js["data"]["id"].ToString(), js["data"]["sc_uid"].ToString(), js["data"]["nick_name"].ToString(), js["data"]["avatar"].ToString(), js["data"]["is_activate"].ToString(), "1");
  368. if (verifyObj.activeSelf == true)
  369. {
  370. verifyObj.SetActive(false);
  371. }
  372. }, (string failStr) => {
  373. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, RtcStrConfig.serverFail);
  374. });
  375. }
  376. private void Init(JsonData data)
  377. {
  378. if (isRemberUserAndPass)
  379. {
  380. PlayerPrefs.SetInt("isRemberUserAndPass", 1);
  381. }
  382. else
  383. {
  384. PlayerPrefs.SetInt("isRemberUserAndPass", -1);
  385. }
  386. ScenesManager.Instance.showWindow(ScenesManager.SceneType.ShowOffice);
  387. verifyObj.SetActive(false);
  388. WSHandler.Office.OnInit -= Init;
  389. }
  390. void LoginFail(string str)
  391. {
  392. isLock = false;
  393. // passwordRemind.text = "登陆失败";
  394. // passwordRemind.gameObject.SetActive(true);
  395. JsonData data = null;
  396. string code = "";
  397. try
  398. {
  399. data = JsonMapper.ToObject(str);
  400. code = data["code"].ToString();
  401. }
  402. catch (Exception e)
  403. {
  404. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, RtcStrConfig.serverError);
  405. return;
  406. // UnityLog.LogError(e.Data + str);
  407. }
  408. string codeStr = data["message"].ToString();
  409. codeStr = data["message"].ToString();
  410. switch (code)
  411. {
  412. case "5010":
  413. passwordRemind.text = RtcStrConfig.ePassWord;
  414. passwordRemind.gameObject.SetActive(true);
  415. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, RtcStrConfig.ePassWord);
  416. if (this.verifyObj.activeSelf)
  417. {
  418. NetWorkHeaders.GetTextureCode(GetSucess);
  419. }
  420. break;
  421. case "6004":
  422. this.verifyObj.SetActive(true);
  423. NetWorkHeaders.GetTextureCode(GetSucess);
  424. passwordRemind.gameObject.SetActive(true);
  425. passwordRemind.text = RtcStrConfig.eFPassWord;
  426. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, RtcStrConfig.eFPassWord);
  427. break;
  428. case "6005":
  429. this.verifyObj.SetActive(true);
  430. NetWorkHeaders.GetTextureCode(GetSucess);
  431. vcodeRemind.text = RtcStrConfig.eRemind;
  432. vcodeRemind.gameObject.SetActive(true);
  433. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, RtcStrConfig.eRemind);
  434. break;
  435. case "6006":
  436. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Pop, RtcStrConfig.breakotherLogin, "确定登录",()=> { YuLoginOk(); },"返回",
  437. () => {
  438. ShowLogin.Instance.loginType = LoginType.INIT;
  439. ScenesManager.Instance.showWindow(SceneType.ShowLogin);
  440. });
  441. break;
  442. default:
  443. passwordRemind.text = RtcStrConfig.ePassWord;
  444. passwordRemind.gameObject.SetActive(true);
  445. break;
  446. }
  447. }
  448. string capID;
  449. private void GetSucess(JsonData data)
  450. {
  451. capID = data["data"]["captchaId"].ToString();
  452. string[] strs = data["data"]["captcha"].ToString().Split(',');
  453. byte[] bytes = Convert.FromBase64String(strs[strs.Length - 1]);
  454. Texture2D tex2D = new Texture2D(512, 512);
  455. tex2D.LoadImage(bytes);
  456. verifyTexture.texture = tex2D;
  457. }
  458. private void OnClickSet()
  459. {
  460. }
  461. private void OnClickPhoneLogin()
  462. {
  463. }
  464. public void gotoBack()
  465. {
  466. ShowLogin.Instance.loginType = LoginType.INIT;
  467. ScenesManager.Instance.showWindow(SceneType.ShowLogin);
  468. }
  469. }
  470. }