| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using JCUnityLib;
- /* 我的信息界面(点击主界面头像进入) */
- public class MeView : ViewBase, MenuBackInterface
- {
- [SerializeField] Image avatarImage;
- [SerializeField] Text nameText;
- [SerializeField] GameObject inputs;
- void Start()
- {
- PersistenHandler.ins?.menuBackCtr.views.Add(this);
- meUserInfo = new MeUserInfo(this);
- this.transform.Find("AvatarFrame").GetComponent<Button>().onClick.AddListener(ShowAvatarSelectView);
- if (CommonConfig.serverIndex != 0) {
- inputs.transform.GetChild(2).gameObject.SetActive(false);
- transform.Find("BtnSave").Translate(Vector3.up * 25, Space.Self);
- }
- if (CommonConfig.banBindRelateAccount)
- {
- inputs.transform.GetChild(2).gameObject.SetActive(false);
- inputs.transform.GetChild(3).gameObject.SetActive(false);
- (transform.Find("BtnSave") as RectTransform).anchoredPosition = new Vector2(154, -208);
- }
- if (CommonConfig.StandaloneMode)
- {
- inputs.transform.GetChild(2).gameObject.SetActive(false);
- inputs.transform.GetChild(3).gameObject.SetActive(false);
- inputs.transform.GetChild(6).gameObject.SetActive(false);
- (transform.Find("BtnSave") as RectTransform).anchoredPosition = new Vector2(154, -155);
- transform.Find("BtnDeleteAccount").gameObject.SetActive(false);
- }
- RenderAfterSave();
- }
- void OnDestroy()
- {
- PersistenHandler.ins?.menuBackCtr.views.Remove(this);
- }
- public bool OnMenuBack() {
- if (!meUserInfo.ShowModalForSave()) {
- ExcuteLogic_Destroy();
- return true;
- } else {
- return false;
- }
- }
- public void Back() {
- AudioMgr.ins.PlayBtn();
- if (!meUserInfo.ShowModalForSave()) {
- ExcuteLogic_Destroy();
- }
- }
- private void ExcuteLogic_Destroy() {
- ViewMgr.Instance.DestroyView<MeView>();
- }
- public void Save()
- {
- AudioMgr.ins.PlayBtn();
- ExcuteLogic_SaveUserInfo(true);
- }
- [SerializeField] GameObject prefabValidateJigsaw;
- public void OnClick_DeleteAccount()
- {
- AudioMgr.ins.PlayBtn();
- ModalView mv = ModalView.Show();
- mv.textKey = "me_delete-account-c1";
- mv.onRejectTextKey = "me_delete-account-c2";
- mv.onAgreeTextKey = "me_delete-account-c3";
- mv.onAgree = () =>
- {
- var validateJigsawView = Instantiate(prefabValidateJigsaw).GetComponent<JCUnityLib.UI.ValidateJigsawView>();
- validateJigsawView.SetTextLabel(TextAutoLanguage2.GetTextByKey("ValidateJigsawView_label"));
- validateJigsawView.SetTextTip(TextAutoLanguage2.GetTextByKey("ValidateJigsawView_tip"));
- validateJigsawView.SetTextOK(TextAutoLanguage2.GetTextByKey("ValidateJigsawView_ok"));
- validateJigsawView.onComplete += () =>
- {
- StartCoroutine(RunDeleteAccount());
- };
- };
- }
- IEnumerator RunDeleteAccount()
- {
- Transform tf = transform.Find("AccountDeleting");
- Text textUI = tf.GetComponentInChildren<Text>();
- TextAutoLanguage2 tal2 = tf.GetComponentInChildren<TextAutoLanguage2>();
- Color textColor = Color.white;
- tal2.SetTextKey("me_delete-account-c4");
- textUI.color = textColor;
- tf.gameObject.SetActive(true);
- string textKey = null;
- bool deleteSuccess = false;
- UserComp.Instance.deleteAccount((success) =>
- {
- deleteSuccess = success;
- if (success)
- {
- textKey = "me_delete-account-c5";
- textColor = Color.green;
- }
- else
- {
- textKey = "me_delete-account-c6";
- textColor = Color.red;
- }
- });
- while (textKey == null) yield return new WaitForSecondsRealtime(2);
- tal2.SetTextKey(textKey);
- textUI.color = textColor;
- yield return new WaitForSecondsRealtime(2);
- if (deleteSuccess)
- {
- PlayerPrefs.DeleteKey(LoginMgr.LoginTokenKey);
- UserPlayer.ins?.Close();
- UnityEngine.SceneManagement.SceneManager.LoadScene("Login", UnityEngine.SceneManagement.LoadSceneMode.Single);
- }
- else
- {
- tf.gameObject.SetActive(false);
- }
- }
- JCUnityLib.Throttler throttlerSave = new JCUnityLib.Throttler(3000);
- private bool ExcuteLogic_SaveUserInfo(bool useThrottler) {
- System.Func<bool> interceptor = delegate() {
- if (string.IsNullOrEmpty(meUserInfo.nickname)) {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("昵称不能为空"));
- return true;
- }
- if (useThrottler && throttlerSave.CanPass() == false) {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
- return true;
- }
- return false;
- };
- if (!meUserInfo.Save(interceptor)) return false;
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("保存成功"));
- RenderAfterSave();
- return true;
- }
- InputField GetInputField(Transform transform)
- {
- return transform.Find("InputField").GetComponent<InputField>();
- }
- public void RenderAfterSave() {
- for (int i = 0; i < inputs.transform.childCount; i++)
- {
- if (i == 4) {
- inputs.transform.GetChild(i).Find("ToggleGroup")
- .GetChild(LoginMgr.myUserInfo.gender == 2 ? 1 : 0)
- .GetComponent<Toggle>().isOn = true;
- continue;
- }
- InputField inputField = GetInputField(inputs.transform.GetChild(i));
- if (i == 0) {
- inputField.text = LoginMgr.myUserInfo.id.ToString();
- }
- else if (i == 1) {
- inputField.text = LoginMgr.myUserInfo.nickname;
- }
- else if (i == 2) {
- inputField.text = LoginMgr.myUserInfo.phone;
- }
- else if (i == 3) {
- inputField.text = LoginMgr.myUserInfo.email;
- }
- else if (i == 5) {
- inputField.text = LoginMgr.myUserInfo.birthday;
- }
- else if (i == 6) {
- // countryCode = LoginMgr.myUserInfo.country;
- // stateCode = LoginMgr.myUserInfo.state;
- // cityCode = LoginMgr.myUserInfo.city;
- // System.Tuple<string, string, string> xxx = JC.Unity.Picker.LocationParseComponent.ins.ParseNameByCode(countryCode, stateCode, cityCode);
- // inputField.text = (xxx.Item1 + " " + xxx.Item2 + " " + xxx.Item3).Trim();
- //渲染后端返回的gps地理位置
- countryCode = LoginMgr.myUserInfo.country;
- stateCode = LoginMgr.myUserInfo.state;
- cityCode = LoginMgr.myUserInfo.city;
- inputField.text = (countryCode + " " + stateCode + " " + cityCode).Trim();
- }
- }
- nameText.text = LoginMgr.myUserInfo.nickname;
- RoleMgr.SetAvatarToImage(avatarImage, LoginMgr.myUserInfo.avatarID, LoginMgr.myUserInfo.avatarUrl);
- HomeView.ins.RenderNameOrGender();
- }
- public void OnClick_BindPhone()
- {
- AudioMgr.ins.PlayBtn();
- RelateValidateView relateValidateView =
- Instantiate(SceneResourceManager.Instance.GetPrefab("RelateValidateView"))
- .GetComponent<RelateValidateView>();
- CanvasUtils.PlusSortOrder(gameObject.GetComponentInParent<Canvas>().gameObject, relateValidateView.gameObject, 1);
- relateValidateView.InitForPhone2();
- relateValidateView.onValidateSuccess = (a, b, c) => {
- relateValidateView?.BanButtons();
- StartCoroutine(UserController.Instance.SavePhone(a, b, c, (res) => {
- if (res.code == 0) {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass1"));
- GetInputField(inputs.transform.GetChild(2)).text = LoginMgr.myUserInfo.phone = a;
- relateValidateView?.CloseView();
- }
- }));
- };
- }
- public void OnClick_BindEmail()
- {
- AudioMgr.ins.PlayBtn();
- RelateValidateView relateValidateView =
- Instantiate(SceneResourceManager.Instance.GetPrefab("RelateValidateView"))
- .GetComponent<RelateValidateView>();
- CanvasUtils.PlusSortOrder(gameObject.GetComponentInParent<Canvas>().gameObject, relateValidateView.gameObject, 1);
- relateValidateView.InitForEmail2();
- relateValidateView.onValidateSuccess = (a, b, c) => {
- relateValidateView?.BanButtons();
- StartCoroutine(UserController.Instance.SaveEmail(a, b, c, (res) => {
- if (res.code == 0) {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass1"));
- GetInputField(inputs.transform.GetChild(3)).text = LoginMgr.myUserInfo.email = a;
- relateValidateView?.CloseView();
- }
- }));
- };
- }
-
- #region Picker
- [SerializeField] GameObject datePickerPrefab;
- public void OpenDatePicker() {
- GameObject o = GameObject.Instantiate(datePickerPrefab);
- o.GetComponentInChildren<JC.Unity.Picker.DatePickerGroup>().onEnter += (JC.Unity.Picker.DatePickerGroup picker) => {
- GetInputField(inputs.transform.GetChild(5)).text = picker.GetSelectDateStr();
- };
- }
- [SerializeField] GameObject locationPickerPrefab;
- private string countryCode = "", stateCode = "", cityCode = "";
- public void OpenLocationPicker() {
- // GameObject o = GameObject.Instantiate(locationPickerPrefab);
- // o.GetComponentInChildren<JC.Unity.Picker.LocationPickerGroup>().onEnter += (JC.Unity.Picker.LocationInfo info) => {
- // countryCode = info.GetCountryRegion().Item2;
- // stateCode = info.GetState().Item2;
- // cityCode = info.GetCity().Item2;
- // GetInputField(inputs.transform.GetChild(6)).text =
- // info.GetCountryRegion().Item1 + " " +
- // info.GetState().Item1 + " " +
- // info.GetCity().Item1;
- // };
- //2022-12-6 gps获取地理位置
- System.Action eOnAgree = () => {
- GPSTool.GetAddress((address) => {
- if (address != null) {
- countryCode = address[0];
- stateCode = address[1];
- cityCode = address[2];
- GetInputField(inputs.transform.GetChild(6)).text =
- countryCode + " " +
- stateCode + " " +
- cityCode;
- }
- });
- };
- if (!HomeView.ShowProminentBeforeConnectBLE(eOnAgree)) eOnAgree.Invoke();
- }
- #endregion
- #region 头像选择
- bool avatarSelectViewInited = false;
- int curAvatarSelectID = 0;
- public void ShowAvatarSelectView() {
- AudioMgr.ins.PlayBtn();
- Transform avatarSelectView = this.transform.Find("AvatarSelectView");
- avatarSelectView.gameObject.SetActive(true);
- if (avatarSelectViewInited) {
- } else {
- avatarSelectViewInited = true;
- curAvatarSelectID = LoginMgr.myUserInfo.avatarID;
- avatarSelectView.Find("FrameBox/BtnClose").GetComponent<Button>().onClick.AddListener(CloseAvatarSelectView);
- GridLayoutGroup gridLayoutGroup = this.transform.GetComponentInChildren<GridLayoutGroup>();
- GameObject avatarPrefab = gridLayoutGroup.transform.Find("Avatar").gameObject;
- for (int id = -1; id < RoleMgr.GetAvatarListLen(); id++) {
- if (RoleMgr.IsRoleAvatar(id)) continue;
- if (id == -1 && string.IsNullOrWhiteSpace(LoginMgr.myUserInfo.avatarUrl)) continue;
- GameObject avatar = GameObject.Instantiate(avatarPrefab, gridLayoutGroup.transform);
- Image avatarImage = avatar.transform.Find("Mask/Sprite").GetComponent<Image>();
- RoleMgr.SetAvatarToImage(avatarImage, id, LoginMgr.myUserInfo.avatarUrl);
- avatar.transform.Find("Check").gameObject.SetActive(id == curAvatarSelectID);
- avatar.gameObject.name = id.ToString();
- int aid = id; //记录该值
- avatar.GetComponent<Button>().onClick.AddListener(() => {
- AudioMgr.ins.PlayBtn();
- curAvatarSelectID = aid;
- for (int i = 0; i < gridLayoutGroup.transform.childCount; i++) {
- Transform item = gridLayoutGroup.transform.GetChild(i);
- int theAvatarID = int.Parse(item.gameObject.name);
- item.Find("Check").gameObject.SetActive(theAvatarID == curAvatarSelectID);
- }
- });
- }
- Destroy(avatarPrefab);
- LayoutRebuilder.ForceRebuildLayoutImmediate(gridLayoutGroup.transform.parent.GetComponent<RectTransform>());
- }
- }
- public void CloseAvatarSelectView() {
- AudioMgr.ins.PlayBtn();
- this.transform.Find("AvatarSelectView").gameObject.SetActive(false);
- if (curAvatarSelectID != LoginMgr.myUserInfo.avatarID) {
- LoginMgr.myUserInfo.avatarID = curAvatarSelectID;
- //render
- RoleMgr.SetAvatarToImage(avatarImage, curAvatarSelectID, LoginMgr.myUserInfo.avatarUrl);
- HomeView.ins.RenderMyAvatarSprite();
- //save data
- LoginMgr.myUserInfo.Save();
- }
- }
- #endregion
- MeUserInfo meUserInfo;
- private class MeUserInfo {
- private MeView m_context;
- public MeUserInfo(MeView context) {
- m_context = context;
- }
- #region UserInfo关联属性
- public string nickname;
- public int gender;
- public string birthday;
- public string country;
- public string state;
- public string city;
- #endregion
- private void RefreshValues() {
- //从组件中读取值
- string[] texts = new string[7];
- for (int i = 0; i < m_context.inputs.transform.childCount; i++)
- {
- if (i == 4) {
- Transform toggleGroup = m_context.inputs.transform.GetChild(i).Find("ToggleGroup");
- texts[i] = toggleGroup.GetChild(0).GetComponent<Toggle>().isOn ? "1" : "2";
- continue;
- }
- InputField inputField = m_context.GetInputField(m_context.inputs.transform.GetChild(i));
- texts[i] = inputField.text.Trim();
- }
- //缓存读取到的值
- this.nickname = texts[1];
- this.gender = int.Parse(texts[4]);
- this.birthday = texts[5];
- this.country = m_context.countryCode;
- this.state = m_context.stateCode;
- this.city = m_context.cityCode;
- }
- public bool Save(System.Func<bool> interceptor = null) {
- RefreshValues();
- if (interceptor != null) {
- if (interceptor.Invoke()) return false;
- }
- LoginMgr.myUserInfo.nickname = this.nickname;
- LoginMgr.myUserInfo.gender = this.gender;
- LoginMgr.myUserInfo.birthday = this.birthday;
- LoginMgr.myUserInfo.country = this.country;
- LoginMgr.myUserInfo.state = this.state;
- LoginMgr.myUserInfo.city = this.city;
- LoginMgr.myUserInfo.Save();
- return true;
- }
- private bool IsEqualOrigin() {
- return
- LoginMgr.myUserInfo.nickname == this.nickname &&
- LoginMgr.myUserInfo.gender == this.gender &&
- LoginMgr.myUserInfo.birthday == this.birthday &&
- LoginMgr.myUserInfo.country == this.country &&
- LoginMgr.myUserInfo.state == this.state &&
- LoginMgr.myUserInfo.city == this.city;
- }
- ModalView modalViewForSave;
- public bool ShowModalForSave() {
- RefreshValues();
- if (IsEqualOrigin()) return false;
- if (!modalViewForSave) {
- modalViewForSave = ModalView.Show();
- modalViewForSave.textKey= "me_askSave";
- modalViewForSave.onAgreeTextKey = "common_yes";
- modalViewForSave.onRejectTextKey = "common_no";
- modalViewForSave.onAgree = () => {
- bool res = m_context.ExcuteLogic_SaveUserInfo(false);
- if (res) {
- m_context.ExcuteLogic_Destroy();
- }
- };
- modalViewForSave.onReject = () => m_context.ExcuteLogic_Destroy();
- }
- return true;
- }
- }
- }
|