MeView.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using JCUnityLib;
  6. /* 我的信息界面(点击主界面头像进入) */
  7. public class MeView : ViewBase, MenuBackInterface
  8. {
  9. [SerializeField] Image avatarImage;
  10. [SerializeField] Text nameText;
  11. [SerializeField] GameObject inputs;
  12. void Start()
  13. {
  14. PersistenHandler.ins?.menuBackCtr.views.Add(this);
  15. meUserInfo = new MeUserInfo(this);
  16. this.transform.Find("AvatarFrame").GetComponent<Button>().onClick.AddListener(ShowAvatarSelectView);
  17. if (CommonConfig.serverIndex != 0) {
  18. inputs.transform.GetChild(2).gameObject.SetActive(false);
  19. transform.Find("BtnSave").Translate(Vector3.up * 25, Space.Self);
  20. }
  21. if (CommonConfig.banBindRelateAccount)
  22. {
  23. inputs.transform.GetChild(2).gameObject.SetActive(false);
  24. inputs.transform.GetChild(3).gameObject.SetActive(false);
  25. (transform.Find("BtnSave") as RectTransform).anchoredPosition = new Vector2(154, -208);
  26. }
  27. if (CommonConfig.StandaloneMode)
  28. {
  29. inputs.transform.GetChild(2).gameObject.SetActive(false);
  30. inputs.transform.GetChild(3).gameObject.SetActive(false);
  31. inputs.transform.GetChild(6).gameObject.SetActive(false);
  32. (transform.Find("BtnSave") as RectTransform).anchoredPosition = new Vector2(154, -155);
  33. transform.Find("BtnDeleteAccount").gameObject.SetActive(false);
  34. }
  35. RenderAfterSave();
  36. }
  37. void OnDestroy()
  38. {
  39. PersistenHandler.ins?.menuBackCtr.views.Remove(this);
  40. }
  41. public bool OnMenuBack() {
  42. if (!meUserInfo.ShowModalForSave()) {
  43. ExcuteLogic_Destroy();
  44. return true;
  45. } else {
  46. return false;
  47. }
  48. }
  49. public void Back() {
  50. AudioMgr.ins.PlayBtn();
  51. if (!meUserInfo.ShowModalForSave()) {
  52. ExcuteLogic_Destroy();
  53. }
  54. }
  55. private void ExcuteLogic_Destroy() {
  56. ViewMgr.Instance.DestroyView<MeView>();
  57. }
  58. public void Save()
  59. {
  60. AudioMgr.ins.PlayBtn();
  61. ExcuteLogic_SaveUserInfo(true);
  62. }
  63. [SerializeField] GameObject prefabValidateJigsaw;
  64. public void OnClick_DeleteAccount()
  65. {
  66. AudioMgr.ins.PlayBtn();
  67. ModalView mv = ModalView.Show();
  68. mv.textKey = "me_delete-account-c1";
  69. mv.onRejectTextKey = "me_delete-account-c2";
  70. mv.onAgreeTextKey = "me_delete-account-c3";
  71. mv.onAgree = () =>
  72. {
  73. var validateJigsawView = Instantiate(prefabValidateJigsaw).GetComponent<JCUnityLib.UI.ValidateJigsawView>();
  74. validateJigsawView.SetTextLabel(TextAutoLanguage2.GetTextByKey("ValidateJigsawView_label"));
  75. validateJigsawView.SetTextTip(TextAutoLanguage2.GetTextByKey("ValidateJigsawView_tip"));
  76. validateJigsawView.SetTextOK(TextAutoLanguage2.GetTextByKey("ValidateJigsawView_ok"));
  77. validateJigsawView.onComplete += () =>
  78. {
  79. StartCoroutine(RunDeleteAccount());
  80. };
  81. };
  82. }
  83. IEnumerator RunDeleteAccount()
  84. {
  85. Transform tf = transform.Find("AccountDeleting");
  86. Text textUI = tf.GetComponentInChildren<Text>();
  87. TextAutoLanguage2 tal2 = tf.GetComponentInChildren<TextAutoLanguage2>();
  88. Color textColor = Color.white;
  89. tal2.SetTextKey("me_delete-account-c4");
  90. textUI.color = textColor;
  91. tf.gameObject.SetActive(true);
  92. string textKey = null;
  93. bool deleteSuccess = false;
  94. UserComp.Instance.deleteAccount((success) =>
  95. {
  96. deleteSuccess = success;
  97. if (success)
  98. {
  99. textKey = "me_delete-account-c5";
  100. textColor = Color.green;
  101. }
  102. else
  103. {
  104. textKey = "me_delete-account-c6";
  105. textColor = Color.red;
  106. }
  107. });
  108. while (textKey == null) yield return new WaitForSecondsRealtime(2);
  109. tal2.SetTextKey(textKey);
  110. textUI.color = textColor;
  111. yield return new WaitForSecondsRealtime(2);
  112. if (deleteSuccess)
  113. {
  114. PlayerPrefs.DeleteKey(LoginMgr.LoginTokenKey);
  115. UserPlayer.ins?.Close();
  116. UnityEngine.SceneManagement.SceneManager.LoadScene("Login", UnityEngine.SceneManagement.LoadSceneMode.Single);
  117. }
  118. else
  119. {
  120. tf.gameObject.SetActive(false);
  121. }
  122. }
  123. JCUnityLib.Throttler throttlerSave = new JCUnityLib.Throttler(3000);
  124. private bool ExcuteLogic_SaveUserInfo(bool useThrottler) {
  125. System.Func<bool> interceptor = delegate() {
  126. if (string.IsNullOrEmpty(meUserInfo.nickname)) {
  127. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("昵称不能为空"));
  128. return true;
  129. }
  130. if (useThrottler && throttlerSave.CanPass() == false) {
  131. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  132. return true;
  133. }
  134. return false;
  135. };
  136. if (!meUserInfo.Save(interceptor)) return false;
  137. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("保存成功"));
  138. RenderAfterSave();
  139. return true;
  140. }
  141. InputField GetInputField(Transform transform)
  142. {
  143. return transform.Find("InputField").GetComponent<InputField>();
  144. }
  145. public void RenderAfterSave() {
  146. for (int i = 0; i < inputs.transform.childCount; i++)
  147. {
  148. if (i == 4) {
  149. inputs.transform.GetChild(i).Find("ToggleGroup")
  150. .GetChild(LoginMgr.myUserInfo.gender == 2 ? 1 : 0)
  151. .GetComponent<Toggle>().isOn = true;
  152. continue;
  153. }
  154. InputField inputField = GetInputField(inputs.transform.GetChild(i));
  155. if (i == 0) {
  156. inputField.text = LoginMgr.myUserInfo.id.ToString();
  157. }
  158. else if (i == 1) {
  159. inputField.text = LoginMgr.myUserInfo.nickname;
  160. }
  161. else if (i == 2) {
  162. inputField.text = LoginMgr.myUserInfo.phone;
  163. }
  164. else if (i == 3) {
  165. inputField.text = LoginMgr.myUserInfo.email;
  166. }
  167. else if (i == 5) {
  168. inputField.text = LoginMgr.myUserInfo.birthday;
  169. }
  170. else if (i == 6) {
  171. // countryCode = LoginMgr.myUserInfo.country;
  172. // stateCode = LoginMgr.myUserInfo.state;
  173. // cityCode = LoginMgr.myUserInfo.city;
  174. // System.Tuple<string, string, string> xxx = JC.Unity.Picker.LocationParseComponent.ins.ParseNameByCode(countryCode, stateCode, cityCode);
  175. // inputField.text = (xxx.Item1 + " " + xxx.Item2 + " " + xxx.Item3).Trim();
  176. //渲染后端返回的gps地理位置
  177. countryCode = LoginMgr.myUserInfo.country;
  178. stateCode = LoginMgr.myUserInfo.state;
  179. cityCode = LoginMgr.myUserInfo.city;
  180. inputField.text = (countryCode + " " + stateCode + " " + cityCode).Trim();
  181. }
  182. }
  183. nameText.text = LoginMgr.myUserInfo.nickname;
  184. RoleMgr.SetAvatarToImage(avatarImage, LoginMgr.myUserInfo.avatarID, LoginMgr.myUserInfo.avatarUrl);
  185. HomeView.ins.RenderNameOrGender();
  186. }
  187. public void OnClick_BindPhone()
  188. {
  189. AudioMgr.ins.PlayBtn();
  190. RelateValidateView relateValidateView =
  191. Instantiate(SceneResourceManager.Instance.GetPrefab("RelateValidateView"))
  192. .GetComponent<RelateValidateView>();
  193. CanvasUtils.PlusSortOrder(gameObject.GetComponentInParent<Canvas>().gameObject, relateValidateView.gameObject, 1);
  194. relateValidateView.InitForPhone2();
  195. relateValidateView.onValidateSuccess = (a, b, c) => {
  196. relateValidateView?.BanButtons();
  197. StartCoroutine(UserController.Instance.SavePhone(a, b, c, (res) => {
  198. if (res.code == 0) {
  199. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass1"));
  200. GetInputField(inputs.transform.GetChild(2)).text = LoginMgr.myUserInfo.phone = a;
  201. relateValidateView?.CloseView();
  202. }
  203. }));
  204. };
  205. }
  206. public void OnClick_BindEmail()
  207. {
  208. AudioMgr.ins.PlayBtn();
  209. RelateValidateView relateValidateView =
  210. Instantiate(SceneResourceManager.Instance.GetPrefab("RelateValidateView"))
  211. .GetComponent<RelateValidateView>();
  212. CanvasUtils.PlusSortOrder(gameObject.GetComponentInParent<Canvas>().gameObject, relateValidateView.gameObject, 1);
  213. relateValidateView.InitForEmail2();
  214. relateValidateView.onValidateSuccess = (a, b, c) => {
  215. relateValidateView?.BanButtons();
  216. StartCoroutine(UserController.Instance.SaveEmail(a, b, c, (res) => {
  217. if (res.code == 0) {
  218. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass1"));
  219. GetInputField(inputs.transform.GetChild(3)).text = LoginMgr.myUserInfo.email = a;
  220. relateValidateView?.CloseView();
  221. }
  222. }));
  223. };
  224. }
  225. #region Picker
  226. [SerializeField] GameObject datePickerPrefab;
  227. public void OpenDatePicker() {
  228. GameObject o = GameObject.Instantiate(datePickerPrefab);
  229. o.GetComponentInChildren<JC.Unity.Picker.DatePickerGroup>().onEnter += (JC.Unity.Picker.DatePickerGroup picker) => {
  230. GetInputField(inputs.transform.GetChild(5)).text = picker.GetSelectDateStr();
  231. };
  232. }
  233. [SerializeField] GameObject locationPickerPrefab;
  234. private string countryCode = "", stateCode = "", cityCode = "";
  235. public void OpenLocationPicker() {
  236. // GameObject o = GameObject.Instantiate(locationPickerPrefab);
  237. // o.GetComponentInChildren<JC.Unity.Picker.LocationPickerGroup>().onEnter += (JC.Unity.Picker.LocationInfo info) => {
  238. // countryCode = info.GetCountryRegion().Item2;
  239. // stateCode = info.GetState().Item2;
  240. // cityCode = info.GetCity().Item2;
  241. // GetInputField(inputs.transform.GetChild(6)).text =
  242. // info.GetCountryRegion().Item1 + " " +
  243. // info.GetState().Item1 + " " +
  244. // info.GetCity().Item1;
  245. // };
  246. //2022-12-6 gps获取地理位置
  247. System.Action eOnAgree = () => {
  248. GPSTool.GetAddress((address) => {
  249. if (address != null) {
  250. countryCode = address[0];
  251. stateCode = address[1];
  252. cityCode = address[2];
  253. GetInputField(inputs.transform.GetChild(6)).text =
  254. countryCode + " " +
  255. stateCode + " " +
  256. cityCode;
  257. }
  258. });
  259. };
  260. if (!HomeView.ShowProminentBeforeConnectBLE(eOnAgree)) eOnAgree.Invoke();
  261. }
  262. #endregion
  263. #region 头像选择
  264. bool avatarSelectViewInited = false;
  265. int curAvatarSelectID = 0;
  266. public void ShowAvatarSelectView() {
  267. AudioMgr.ins.PlayBtn();
  268. Transform avatarSelectView = this.transform.Find("AvatarSelectView");
  269. avatarSelectView.gameObject.SetActive(true);
  270. if (avatarSelectViewInited) {
  271. } else {
  272. avatarSelectViewInited = true;
  273. curAvatarSelectID = LoginMgr.myUserInfo.avatarID;
  274. avatarSelectView.Find("FrameBox/BtnClose").GetComponent<Button>().onClick.AddListener(CloseAvatarSelectView);
  275. GridLayoutGroup gridLayoutGroup = this.transform.GetComponentInChildren<GridLayoutGroup>();
  276. GameObject avatarPrefab = gridLayoutGroup.transform.Find("Avatar").gameObject;
  277. for (int id = -1; id < RoleMgr.GetAvatarListLen(); id++) {
  278. if (RoleMgr.IsRoleAvatar(id)) continue;
  279. if (id == -1 && string.IsNullOrWhiteSpace(LoginMgr.myUserInfo.avatarUrl)) continue;
  280. GameObject avatar = GameObject.Instantiate(avatarPrefab, gridLayoutGroup.transform);
  281. Image avatarImage = avatar.transform.Find("Mask/Sprite").GetComponent<Image>();
  282. RoleMgr.SetAvatarToImage(avatarImage, id, LoginMgr.myUserInfo.avatarUrl);
  283. avatar.transform.Find("Check").gameObject.SetActive(id == curAvatarSelectID);
  284. avatar.gameObject.name = id.ToString();
  285. int aid = id; //记录该值
  286. avatar.GetComponent<Button>().onClick.AddListener(() => {
  287. AudioMgr.ins.PlayBtn();
  288. curAvatarSelectID = aid;
  289. for (int i = 0; i < gridLayoutGroup.transform.childCount; i++) {
  290. Transform item = gridLayoutGroup.transform.GetChild(i);
  291. int theAvatarID = int.Parse(item.gameObject.name);
  292. item.Find("Check").gameObject.SetActive(theAvatarID == curAvatarSelectID);
  293. }
  294. });
  295. }
  296. Destroy(avatarPrefab);
  297. LayoutRebuilder.ForceRebuildLayoutImmediate(gridLayoutGroup.transform.parent.GetComponent<RectTransform>());
  298. }
  299. }
  300. public void CloseAvatarSelectView() {
  301. AudioMgr.ins.PlayBtn();
  302. this.transform.Find("AvatarSelectView").gameObject.SetActive(false);
  303. if (curAvatarSelectID != LoginMgr.myUserInfo.avatarID) {
  304. LoginMgr.myUserInfo.avatarID = curAvatarSelectID;
  305. //render
  306. RoleMgr.SetAvatarToImage(avatarImage, curAvatarSelectID, LoginMgr.myUserInfo.avatarUrl);
  307. HomeView.ins.RenderMyAvatarSprite();
  308. //save data
  309. LoginMgr.myUserInfo.Save();
  310. }
  311. }
  312. #endregion
  313. MeUserInfo meUserInfo;
  314. private class MeUserInfo {
  315. private MeView m_context;
  316. public MeUserInfo(MeView context) {
  317. m_context = context;
  318. }
  319. #region UserInfo关联属性
  320. public string nickname;
  321. public int gender;
  322. public string birthday;
  323. public string country;
  324. public string state;
  325. public string city;
  326. #endregion
  327. private void RefreshValues() {
  328. //从组件中读取值
  329. string[] texts = new string[7];
  330. for (int i = 0; i < m_context.inputs.transform.childCount; i++)
  331. {
  332. if (i == 4) {
  333. Transform toggleGroup = m_context.inputs.transform.GetChild(i).Find("ToggleGroup");
  334. texts[i] = toggleGroup.GetChild(0).GetComponent<Toggle>().isOn ? "1" : "2";
  335. continue;
  336. }
  337. InputField inputField = m_context.GetInputField(m_context.inputs.transform.GetChild(i));
  338. texts[i] = inputField.text.Trim();
  339. }
  340. //缓存读取到的值
  341. this.nickname = texts[1];
  342. this.gender = int.Parse(texts[4]);
  343. this.birthday = texts[5];
  344. this.country = m_context.countryCode;
  345. this.state = m_context.stateCode;
  346. this.city = m_context.cityCode;
  347. }
  348. public bool Save(System.Func<bool> interceptor = null) {
  349. RefreshValues();
  350. if (interceptor != null) {
  351. if (interceptor.Invoke()) return false;
  352. }
  353. LoginMgr.myUserInfo.nickname = this.nickname;
  354. LoginMgr.myUserInfo.gender = this.gender;
  355. LoginMgr.myUserInfo.birthday = this.birthday;
  356. LoginMgr.myUserInfo.country = this.country;
  357. LoginMgr.myUserInfo.state = this.state;
  358. LoginMgr.myUserInfo.city = this.city;
  359. LoginMgr.myUserInfo.Save();
  360. return true;
  361. }
  362. private bool IsEqualOrigin() {
  363. return
  364. LoginMgr.myUserInfo.nickname == this.nickname &&
  365. LoginMgr.myUserInfo.gender == this.gender &&
  366. LoginMgr.myUserInfo.birthday == this.birthday &&
  367. LoginMgr.myUserInfo.country == this.country &&
  368. LoginMgr.myUserInfo.state == this.state &&
  369. LoginMgr.myUserInfo.city == this.city;
  370. }
  371. ModalView modalViewForSave;
  372. public bool ShowModalForSave() {
  373. RefreshValues();
  374. if (IsEqualOrigin()) return false;
  375. if (!modalViewForSave) {
  376. modalViewForSave = ModalView.Show();
  377. modalViewForSave.textKey= "me_askSave";
  378. modalViewForSave.onAgreeTextKey = "common_yes";
  379. modalViewForSave.onRejectTextKey = "common_no";
  380. modalViewForSave.onAgree = () => {
  381. bool res = m_context.ExcuteLogic_SaveUserInfo(false);
  382. if (res) {
  383. m_context.ExcuteLogic_Destroy();
  384. }
  385. };
  386. modalViewForSave.onReject = () => m_context.ExcuteLogic_Destroy();
  387. }
  388. return true;
  389. }
  390. }
  391. }