MeView.cs 17 KB

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