LoginDlg.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 = "test01";
  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. verifyInputField.text = "";
  269. }
  270. else
  271. {
  272. vcodeRemind.gameObject.SetActive(true);
  273. vcodeRemind.text = RtcStrConfig.qRemind;
  274. }
  275. }
  276. }
  277. }
  278. }
  279. public void ClickBreakLoginBtn()
  280. {
  281. if (!isLock)
  282. {
  283. NetWorkHeaders.BreakNumLogin(LoginOk, LoginFail);
  284. isLock = true;
  285. }
  286. }
  287. private void YuLoginOk(JsonData js)
  288. {
  289. if (js["code"].ToString() == "200")
  290. {
  291. NetWorkHeaders.Login(userInputField.text, passwordInputField.text, LoginOk, LoginFail);
  292. }
  293. }
  294. private void YuLoginOk()
  295. {
  296. NetWorkHeaders.Login(userInputField.text, passwordInputField.text, LoginOk, LoginFail);
  297. }
  298. public void LoginOk(JsonData js)
  299. {
  300. LoginInitRTC.Instance.InitRTC();
  301. ///字符串检测/判断
  302. string user = userInputField.text;
  303. string password = passwordInputField.text;
  304. UserInfo.isSN = false;
  305. UserInfo.UnionId = js["data"]["unionId"].ToString();
  306. UserInfo.User_Token = js["data"]["token"].ToString();
  307. UserInfo.Account = user;
  308. UserInfo.PassWord = password;
  309. NetWorkHeaders.GetUserInfo((JsonData sData) => {
  310. UserInfo.phone =sData["data"]["phone"].ToString();
  311. UserInfo.userName = sData["data"]["nickName"].ToString();
  312. if(UserInfo.userName=="")
  313. {
  314. UserInfo.userName = user;
  315. //UserInfo.userName = "YCKJ" + UserInfo.Account.Substring(UserInfo.Account.Length - 4);
  316. }
  317. UserInfo.activateType = int.Parse(sData["data"]["activateType"].ToString());
  318. UserInfo.indate = double.Parse(sData["data"]["indate"].ToString());
  319. CustomInfo.isSendAudio = bool.Parse(sData["data"]["settings"]["mic"].ToString());
  320. CustomInfo.isSendVideo = bool.Parse(sData["data"]["settings"]["camera"].ToString());
  321. CustomInfo.camIndex = int.Parse(sData["data"]["settings"]["resolution"].ToString());
  322. NetWorkHeaders.GetUserAvater((JsonData aData) =>
  323. {
  324. NetWorkHeaders.Instance.getNetTexture(aData["data"]["user"][0].ToString(), null, (Texture tex) => {
  325. UserInfo.defaulttextIcon = tex;
  326. });
  327. UserInfo.defaultavatar = aData["data"]["user"][0].ToString();
  328. });
  329. if (sData["data"]["avatar"].ToString()=="")
  330. {
  331. UserInfo.textIcon = UserInfo.defaulttextIcon;
  332. UserInfo.avatar = UserInfo.defaultavatar;
  333. NetWorkHeaders.SetUserInfo();
  334. }
  335. else
  336. {
  337. UserInfo.avatar = sData["data"]["avatar"].ToString();
  338. NetWorkHeaders.Instance.getNetTexture(sData["data"]["avatar"].ToString(),null, (Texture tex) => {
  339. UserInfo.textIcon = tex;
  340. });
  341. }
  342. isLock = false;
  343. ///发送登录请求
  344. ///回调函数中处理是否保存数据到登录缓存
  345. bool isSuccess = LoginMgr.Instance.Login(user, password);//登录成功
  346. ///
  347. ///保存缓存信息2个条件:1,记录账号,2,登录成功
  348. ///用户选择记住密码时方可保存密码
  349. if (isSuccess)
  350. {
  351. if (!isRemberUserAndPass)
  352. {
  353. user = "";
  354. password = "";
  355. userInputField.text = "";
  356. passwordInputField.text = "";
  357. LoginMgr.Instance.DeleteLoginInfo();
  358. }
  359. else
  360. {
  361. LoginMgr.Instance.SaveLoginInfo(user, password);
  362. }
  363. }
  364. WSHandler.Office.OnInit -= Init;
  365. WSHandler.Office.OnInit += Init;
  366. WSHandler.init();
  367. //
  368. // SceneManager.LoadScene("WebRtc");
  369. // 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");
  370. if (verifyObj.activeSelf == true)
  371. {
  372. verifyObj.SetActive(false);
  373. }
  374. }, (string failStr) => {
  375. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, RtcStrConfig.serverFail);
  376. });
  377. }
  378. private void Init(JsonData data)
  379. {
  380. if (isRemberUserAndPass)
  381. {
  382. PlayerPrefs.SetInt("isRemberUserAndPass", 1);
  383. }
  384. else
  385. {
  386. PlayerPrefs.SetInt("isRemberUserAndPass", -1);
  387. }
  388. ScenesManager.Instance.showWindow(ScenesManager.SceneType.ShowOffice);
  389. verifyObj.SetActive(false);
  390. WSHandler.Office.OnInit -= Init;
  391. }
  392. void LoginFail(string str)
  393. {
  394. isLock = false;
  395. // passwordRemind.text = "登陆失败";
  396. // passwordRemind.gameObject.SetActive(true);
  397. JsonData data = null;
  398. string code = "";
  399. try
  400. {
  401. data = JsonMapper.ToObject(str);
  402. code = data["code"].ToString();
  403. }
  404. catch (Exception e)
  405. {
  406. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, RtcStrConfig.serverError);
  407. return;
  408. // UnityLog.LogError(e.Data + str);
  409. }
  410. string codeStr = data["message"].ToString();
  411. codeStr = data["message"].ToString();
  412. switch (code)
  413. {
  414. case "5010":
  415. passwordRemind.text = RtcStrConfig.ePassWord;
  416. passwordRemind.gameObject.SetActive(true);
  417. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, RtcStrConfig.ePassWord);
  418. if (this.verifyObj.activeSelf)
  419. {
  420. NetWorkHeaders.GetTextureCode(GetSucess);
  421. }
  422. break;
  423. case "6004":
  424. this.verifyObj.SetActive(true);
  425. NetWorkHeaders.GetTextureCode(GetSucess);
  426. passwordRemind.gameObject.SetActive(true);
  427. passwordRemind.text = RtcStrConfig.eFPassWord;
  428. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, RtcStrConfig.eFPassWord);
  429. break;
  430. case "6005":
  431. this.verifyObj.SetActive(true);
  432. NetWorkHeaders.GetTextureCode(GetSucess);
  433. vcodeRemind.text = RtcStrConfig.eRemind;
  434. vcodeRemind.gameObject.SetActive(true);
  435. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Tip, RtcStrConfig.eRemind);
  436. break;
  437. case "6006":
  438. PopUpInfo.Instance.showPublic(PopUpInfo.PopType.Pop, RtcStrConfig.breakotherLogin, "确定登录",()=> { YuLoginOk(); },"返回",
  439. () => {
  440. ShowLogin.Instance.loginType = LoginType.INIT;
  441. ScenesManager.Instance.showWindow(SceneType.ShowLogin);
  442. });
  443. break;
  444. default:
  445. passwordRemind.text = RtcStrConfig.ePassWord;
  446. passwordRemind.gameObject.SetActive(true);
  447. break;
  448. }
  449. }
  450. string capID;
  451. private void GetSucess(JsonData data)
  452. {
  453. Debug.Log(" 收到验证码图片====> ");
  454. capID = data["data"]["captchaId"].ToString();
  455. string[] strs = data["data"]["captcha"].ToString().Split(',');
  456. byte[] bytes = Convert.FromBase64String(strs[strs.Length - 1]);
  457. Debug.Log(bytes.Length);
  458. Texture2D tex2D = new Texture2D(512, 512);
  459. tex2D.LoadImage(bytes);
  460. verifyTexture.texture = tex2D;
  461. }
  462. private void OnClickSet()
  463. {
  464. }
  465. private void OnClickPhoneLogin()
  466. {
  467. }
  468. public void gotoBack()
  469. {
  470. ShowLogin.Instance.loginType = LoginType.INIT;
  471. ScenesManager.Instance.showWindow(SceneType.ShowLogin);
  472. }
  473. }
  474. }