MeView.cs 17 KB

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