powerfun-unity/Assets/Scripts/UI/Prefab/Panel/EditUserController.cs

497 lines
19 KiB
C#
Raw Normal View History

using Assets.Scripts;
using Assets.Scripts.Apis;
using Assets.Scripts.Apis.Models;
using Assets.Scripts.Mobile.Interface;
using Assets.Scripts.UI.Control;
using Assets.Scripts.UI.UIEffect;
2021-08-27 15:06:56 +08:00
using DG.Tweening;
using Newtonsoft.Json;
2021-04-23 15:36:18 +08:00
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
2021-03-30 14:23:41 +08:00
using System.Collections.Generic;
2021-06-07 17:05:49 +08:00
using System.Globalization;
2021-04-15 15:57:29 +08:00
using System.Runtime.InteropServices;
2021-04-16 17:49:11 +08:00
using System.Threading.Tasks;
2021-03-30 14:23:41 +08:00
using UnityEngine;
using UnityEngine.EventSystems;
2021-04-16 17:49:11 +08:00
using UnityEngine.SceneManagement;
using UnityEngine.UI;
2021-03-30 14:23:41 +08:00
public class EditUserController : PFUIPanel, INativeOnMobileImageSelect
2021-03-30 14:23:41 +08:00
{
private Button mSaveButton;
private PFUIDropdown mSexDropdown;
private PFUIDropdown mDayDropdown;
private PFUIDropdown mMonthDropdown;
private PFUIDropdown mYearDropdown;
2021-04-16 17:49:11 +08:00
private PFUIDropdown mConutryDropdown;
private PFUIDropdown mUnitDropdown;
private Text mID;
private PFUIInputField mNickName;
private PFUIInputField mFTP;
private PFUIInputField mWeight;
private PFUIInputField mHeight;
private PFUIInputField mMHR;
private PFUIInputField mBW;
private PFUIInputField mWD;
private PFUIInputField mName;
private PFUIInputField mPhone;
private PFUIInputField mAddr;
private RawImage mHeadImage;
private UserApi userApi;
2021-04-15 15:57:29 +08:00
private Button mCancelButton;
private Button mBackButton;
2021-04-16 17:49:11 +08:00
private Button mSwitchButton;
2021-04-15 15:57:29 +08:00
PfUIButton mChangeAvatar;
2021-04-21 16:16:14 +08:00
Transform linkContainer;
2021-04-23 15:36:18 +08:00
Dictionary<string, Selectable> dict;
2021-04-21 16:16:14 +08:00
2021-10-09 10:42:27 +08:00
Transform panel;
float? panelTop = null;
UIGradient g1, g2;
2021-10-21 18:04:01 +08:00
TKPanRecognizer pan;
protected override void Awake()
{
2021-04-16 17:49:11 +08:00
Debug.Log(Utils.GetIP(Utils.ADDRESSFAM.IPv4));
userApi = new UserApi();
2021-08-27 15:06:56 +08:00
#if !(UNITY_ANDROID || UNITY_IOS)
2021-10-09 10:42:27 +08:00
panel = this.transform.Find("Panel");
2021-08-27 15:06:56 +08:00
#else
2021-10-09 10:42:27 +08:00
panel = this.transform.Find("Panel/MainPanel");
panelTop = null;
g1 = transform.Find("Panel/Btn1").GetComponent<UIGradient>();
g2 = transform.Find("Panel/Btn2").GetComponent<UIGradient>();
if (!panelTop.HasValue) panelTop = panel.localPosition.y;
2021-08-27 15:06:56 +08:00
UIManager.AddEvent(transform.Find("Panel/Btn1").gameObject, EventTriggerType.PointerClick, b =>
{
2021-10-09 10:42:27 +08:00
setMobilePanelPosition(true);
2021-08-27 15:06:56 +08:00
});
UIManager.AddEvent(transform.Find("Panel/Btn2").gameObject, EventTriggerType.PointerClick, b =>
{
2021-10-09 10:42:27 +08:00
setMobilePanelPosition(false);
2021-08-27 15:06:56 +08:00
});
2021-10-09 10:42:27 +08:00
Debug.Log(panel.localPosition);
2021-10-21 18:04:01 +08:00
pan = new TKPanRecognizer();
2021-10-08 15:39:26 +08:00
var delta = Vector3.zero;
2021-10-09 10:42:27 +08:00
var rect = this.transform.Find("Panel").GetComponent<RectTransform>();
var startLocation = Vector2.zero;
2021-10-08 15:39:26 +08:00
pan.gestureRecognizedEvent += (r) =>
{
2021-10-09 10:42:27 +08:00
//Debug.Log(UIManager.Instance.MainCanvas);
//Debug.Log(this.transform.Find("Panel").GetComponent<Collider>().bounds.size
startLocation = r.startTouchLocation();
if (rect.isPointInTransfrom(startLocation))
{
delta = pan.deltaTranslation;
delta.x = 0;
delta.z = 0;
panel.transform.position += delta;
}
//Debug.Log("pan recognizer fired: " + panel.transform.localPosition);
2021-10-08 15:39:26 +08:00
};
2021-10-09 10:42:27 +08:00
pan.gestureCompleteEvent += (r) =>
{
if (rect.isPointInTransfrom(startLocation))
{
Debug.Log("运行动画");
if (panel.localPosition.y < 185f + 370f / 2f)
{
setMobilePanelPosition(true);
}
else
{
setMobilePanelPosition(false);
}
}
//if (!panelTop.HasValue) panelTop = transform.localPosition.y + 185;
//Debug.Log($"{panel.localPosition.y},{panelTop}");
//Debug.Log(r.deltaTranslation);
};
2021-10-21 18:04:01 +08:00
//TouchKit.addGestureRecognizer(pan);
2021-08-27 15:06:56 +08:00
#endif
2021-04-16 17:49:11 +08:00
mID = panel.Find("IDNumber").Find("input").Find("Text").GetComponent<Text>();
mSexDropdown = panel.Find("SexDropdown").GetComponent<PFUIDropdown>();
mSexDropdown = panel.Find("SexDropdown").GetComponent<PFUIDropdown>();
mYearDropdown = panel.Find("YearDropdown").GetComponent<PFUIDropdown>();
mMonthDropdown = panel.Find("MonthDropdown").GetComponent<PFUIDropdown>();
mDayDropdown = panel.Find("DayDropdown").GetComponent<PFUIDropdown>();
2021-04-16 17:49:11 +08:00
mUnitDropdown = panel.Find("UnitDropdown").GetComponent<PFUIDropdown>();
mSaveButton = this.transform.Find("SaveButton").GetComponent<Button>();
2021-04-15 15:57:29 +08:00
mCancelButton = this.transform.Find("CancelButton").GetComponent<Button>();
mHeadImage = this.transform.Find("HeadImage").GetComponent<RawImage>();
2021-04-15 15:57:29 +08:00
mBackButton = this.transform.Find("BackButton").GetComponent<Button>();
2021-04-16 17:49:11 +08:00
mSwitchButton = this.transform.Find("SwitchAccountButton").GetComponent<Button>();
2021-04-15 15:57:29 +08:00
mChangeAvatar = this.transform.Find("ChangeAvatar").GetComponent<PfUIButton>();
2021-04-16 17:49:11 +08:00
mConutryDropdown = panel.Find("CountryDropdown").GetComponent<PFUIDropdown>();
mNickName = panel.Find("NickName").Find("input").GetComponent<PFUIInputField>();
mFTP = panel.Find("Ftp").Find("input").GetComponent<PFUIInputField>();
mWeight = panel.Find("Weight").Find("input").GetComponent<PFUIInputField>();
mHeight = panel.Find("Height").Find("input").GetComponent<PFUIInputField>();
mMHR = panel.Find("MHR").Find("input").GetComponent<PFUIInputField>();
mBW = panel.Find("BW").Find("input").GetComponent<PFUIInputField>();
mWD = panel.Find("WD").Find("input").GetComponent<PFUIInputField>();
mName = panel.Find("Name").Find("input").GetComponent<PFUIInputField>();
mPhone = panel.Find("PN").Find("input").GetComponent<PFUIInputField>();
mAddr = panel.Find("Addr").Find("input").GetComponent<PFUIInputField>();
UIManager.AddEvent(mBackButton.gameObject, EventTriggerType.PointerClick, Cancel);
2021-04-15 15:57:29 +08:00
UIManager.AddEvent(mChangeAvatar.gameObject, EventTriggerType.PointerClick, (e) =>
{
#if !(UNITY_ANDROID || UNITY_IOS)
2021-04-15 15:57:29 +08:00
OpenFileName ofn = new OpenFileName();
ofn.structSize = Marshal.SizeOf(ofn);
2021-04-16 17:49:11 +08:00
ofn.filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)\0*.jpg;*.png;*.jpeg;*.bmp\0";
2021-04-15 15:57:29 +08:00
ofn.file = new string(new char[256]);
ofn.maxFile = ofn.file.Length;
ofn.fileTitle = new string(new char[64]);
ofn.maxFileTitle = ofn.fileTitle.Length;
ofn.initialDir = UnityEngine.Application.dataPath;//默认路径
ofn.title = "Open Project";
ofn.defExt = "JPG";//显示文件的类型
//注意 一下项目不一定要全选 但是0x00000008项不要缺少
ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR
2021-04-15 15:57:29 +08:00
if (Win32.GetOpenFileName(ofn))
{
2021-04-16 17:49:11 +08:00
ChangeAvatar(ofn.file);
2021-04-15 15:57:29 +08:00
}
#else
App.imageSelectorController.Select("EditUserPanel(Clone)");
#endif
2021-04-15 15:57:29 +08:00
});
2021-04-21 16:16:14 +08:00
linkContainer = transform.Find("LinkContainer");
2021-04-16 17:49:11 +08:00
mSexDropdown.ClearOptions();
mSexDropdown.AddOptions(new List<string>
{
2021-11-11 14:20:08 +08:00
App.GetLocalString("Male"),App.GetLocalString("Female")
});
2021-04-16 17:49:11 +08:00
mUnitDropdown.ClearOptions();
mUnitDropdown.AddOptions(UIManager.Instance.loginRegOptions.units);
mYearDropdown.ClearOptions();
var years = new List<string>();
for (int i = 1960; i < 2099; i++)
{
years.Add(i.ToString());
}
mYearDropdown.AddOptions(years);
mYearDropdown.OnValueChange = (index) =>
{
//Debug.Log(index);
SetDayDropdown();
};
mMonthDropdown.ClearOptions();
var months = new List<string>();
for (int i = 1; i < 13; i++)
{
months.Add(i.ToString());
}
mMonthDropdown.AddOptions(months);
//mMonthDropdown.SelectValue("9");
mMonthDropdown.OnValueChange = (index) =>
{
SetDayDropdown();
};
mDayDropdown.ClearOptions();
SetDayDropdown();
//mDayDropdown.AddOptions()
//mDayDropdown.AddOptions(new List<Dropdown.OptionData>
//{
// new Dropdown.OptionData()
//});
2021-04-23 15:36:18 +08:00
dict = new Dictionary<string, Selectable>
{
{"NickName",mNickName.GetComponent<InputField>() },
{"FTP",mFTP.GetComponent<InputField>() },
{"Weight",mWeight.GetComponent<InputField>() },
{"Height",mHeight.GetComponent<InputField>() },
{"WheelDiameter",mWD.GetComponent<InputField>() },
{"MaxHeartRate",mMHR.GetComponent<InputField>() },
{"BicycleWeight",mBW.GetComponent<InputField>() },
{"Contact",mName.GetComponent<InputField>() },
{"ContactPhone",mPhone.GetComponent<InputField>() },
{"ContactAddress",mAddr.GetComponent<InputField>() },
};
2021-04-21 16:16:14 +08:00
}
2021-04-16 17:49:11 +08:00
async Task Login()
{
2021-04-16 17:49:11 +08:00
var result = await new UserApi().Login("13115011550", "laozhong", "");
App.CurrentUser = result.data;
}
2021-04-28 15:21:06 +08:00
void LoadDataSync()
2021-04-16 17:49:11 +08:00
{
//if (!App.FromLogin)
//{
// await Login();
//}
2021-04-15 15:57:29 +08:00
2021-04-16 17:49:11 +08:00
var currentUser = App.CurrentUser;
if (currentUser == null) return;
//mIdText.text = currentUser.Id.ToString();
//mNameText.text = currentUser.Nickname;
2021-04-16 17:49:11 +08:00
//Utils.DisplayImage(StartCoroutine, mHeadImage, currentUser.WxHeadImg);
2021-05-20 20:00:33 +08:00
Utils.DisplayHead(mHeadImage, currentUser.WxHeadImg);
2021-04-16 17:49:11 +08:00
if (currentUser.Sex == 1)
{
mSexDropdown.SelectIndex(0);
2021-04-16 17:49:11 +08:00
}
else
{
mSexDropdown.SelectIndex(1);
2021-04-16 17:49:11 +08:00
}
mUnitDropdown.SelectIndex(currentUser.Unit);
if (currentUser.Birthday.HasValue)
{
var birthday = currentUser.Birthday.Value;
mYearDropdown.SelectValue(birthday.Year.ToString());
mMonthDropdown.SelectValue(birthday.Month.ToString());
mDayDropdown.SelectValue(birthday.Day.ToString());
}
2021-04-27 18:26:30 +08:00
mID.text = currentUser.Id.ToString("000000");
2021-04-16 17:49:11 +08:00
mNickName.Text = currentUser.Nickname;
mFTP.Text = currentUser.FTP.ToString();
mWeight.Text = currentUser.Weight.ToString();
mHeight.Text = currentUser.Height.ToString();
mMHR.Text = currentUser.MaxHeartRate.ToString();
mBW.Text = currentUser.BicycleWeight.ToString();
mWD.Text = currentUser.WheelDiameter.ToString();
mName.Text = currentUser.Contact;
mPhone.Text = currentUser.ContactPhone;
mAddr.Text = currentUser.ContactAddress;
2021-04-21 16:16:14 +08:00
//string.IsNullOrEmpty(currentUser.Unionid)
linkContainer.Find("Wx").GetComponent<Image>().sprite =
string.IsNullOrEmpty(currentUser.Unionid)
? Resources.Load<Sprite>("Images/Wechat_person_0")
: Resources.Load<Sprite>("Images/Wechat_person_1");
}
2021-03-30 14:23:41 +08:00
// Start is called before the first frame update
protected override void Start()
{
base.Start();
2021-04-16 17:49:11 +08:00
mConutryDropdown.ClearOptions();
mConutryDropdown.AddOptions(UIManager.Instance.loginRegOptions.countrys);
mConutryDropdown.OnValueChange = (int index) =>
{
mConutryDropdown.transform.Find("RawImage").GetComponent<RawImage>().texture =
UIManager.Instance.loginRegOptions.GetCountryImage(index);
};
2021-04-21 11:19:31 +08:00
mConutryDropdown.SelectValue(App.CurrentUser.Country);
2021-09-02 14:18:26 +08:00
mConutryDropdown.transform.Find("RawImage").GetComponent<RawImage>().texture =
UIManager.Instance.loginRegOptions.GetCountryImageByName(App.CurrentUser.Country);
2021-04-16 17:49:11 +08:00
var rect = ((RectTransform)mHeadImage.transform).rect;
SetRounded(mHeadImage.transform, rect.height);
UIManager.AddEvent(mSaveButton.gameObject, EventTriggerType.PointerClick, Save);
UIManager.AddEvent(mCancelButton.gameObject, EventTriggerType.PointerClick, Cancel);
UIManager.AddEvent(mSwitchButton.gameObject, EventTriggerType.PointerClick, Switch);
}
2021-04-23 15:36:18 +08:00
public override void Show()
{
base.Show();
LoadDataSync();
2021-04-28 15:21:06 +08:00
App.isChanged = false;
2021-04-23 15:36:18 +08:00
}
2021-04-16 17:49:11 +08:00
private void Switch(BaseEventData arg0)
{
2021-08-26 09:29:35 +08:00
UIManager.SwitchAccount();
2021-03-30 14:23:41 +08:00
}
// Update is called once per frame
void Update()
{
}
2021-10-09 10:42:27 +08:00
void setMobilePanelPosition(bool flag)
{
if(flag)
{
if (!panelTop.HasValue) panelTop = panel.localPosition.y;
panel.DOLocalMoveY(panelTop.Value, 0.3f);
g1.color1 = Utils.HexToColorHtml("#F93086");
g1.color2 = Utils.HexToColorHtml("#ff7485");
g2.color1 = Utils.HexToColorHtml("#353543");
g2.color2 = Utils.HexToColorHtml("#474759");
//.color1 = Utils.HexToColorHtml()
}
else
{
if (!panelTop.HasValue) panelTop = panel.localPosition.y;
panel.DOLocalMoveY(panelTop.Value + 370, 0.3f);
g2.color1 = Utils.HexToColorHtml("#F93086");
g2.color2 = Utils.HexToColorHtml("#ff7485");
g1.color1 = Utils.HexToColorHtml("#353543");
g1.color2 = Utils.HexToColorHtml("#474759");
}
}
void SetDayDropdown()
{
//Debug.Log(mDayDropdown.SelectedItem);
mDayDropdown.ClearOptions();
var year = mYearDropdown.SelectedItem;
var month = mMonthDropdown.SelectedItem;
//var date = new DateTime(int.Parse(year), int.Parse(month), 1);
var days = DateTime.DaysInMonth(int.Parse(year), int.Parse(month));
var list = new List<string>();
for (int i = 1; i <= days; i++)
{
list.Add(i.ToString());
}
mDayDropdown.ClearOptions();
mDayDropdown.AddOptions(list);
2021-05-20 20:07:45 +08:00
mDayDropdown.SelectIndex(0);
}
2021-04-28 15:21:06 +08:00
void Save(BaseEventData e)
{
//UIManager.ShowConfirm("Confirm", "Are you sure to edit?", SaveFunc);
try
{
SaveFunc();
}
catch (Exception ex)
{
Utils.showToast(gameObject, ex.Message);
}
//App.CurrentUser = user;
2021-04-16 17:49:11 +08:00
//UIManager.ShowAlert("result.errMsg");
}
2021-05-12 10:42:26 +08:00
void SaveFunc()
{
var user = JsonConvert.DeserializeObject<UserResultModel>(JsonConvert.SerializeObject(App.CurrentUser));
user.Birthday = new DateTime(int.Parse(mYearDropdown.SelectedItem), int.Parse(mMonthDropdown.SelectedItem), int.Parse(mDayDropdown.SelectedItem));
user.Sex = mSexDropdown.SelectedIndex == 0 ? 1 : 2;
user.Nickname = mNickName.Text;
user.Country = UIManager.Instance.loginRegOptions.GetCountryName(mConutryDropdown.SelectedIndex);
var errorList = new List<object>();
if (string.IsNullOrEmpty(mNickName.Text)) errorList.Add(new { Field = "Nickname", ErrMsg = "请填写昵称" });
if (string.IsNullOrEmpty(mFTP.Text)) errorList.Add(new { Field = "FTP", ErrMsg = "请填写FTP" });
if (string.IsNullOrEmpty(mHeight.Text)) errorList.Add(new { Field = "Height", ErrMsg = "请填写身高" });
if (string.IsNullOrEmpty(mWeight.Text)) errorList.Add(new { Field = "Weight", ErrMsg = "请填写体重" });
if (string.IsNullOrEmpty(mMHR.Text)) errorList.Add(new { Field = "MaxHeartRate", ErrMsg = "请填写最大心率" });
if (string.IsNullOrEmpty(mBW.Text)) errorList.Add(new { Field = "BicycleWeight", ErrMsg = "请填写车重" });
if (string.IsNullOrEmpty(mWD.Text)) errorList.Add(new { Field = "WheelDiameter", ErrMsg = "请填写轮径" });
if (errorList.Count > 0)
{
Utils.SetValidate(dict, JArray.FromObject(errorList));
Utils.showToast(gameObject, "Please fill in the information");//请填写相关信息
return;
}
user.FTP = Convert.ToInt32(mFTP.Text, CultureInfo.InstalledUICulture); //int.Parse(mFTP.Text);
user.Height = Convert.ToInt32(mHeight.Text, CultureInfo.InstalledUICulture); //int.Parse(mHeight.Text);
user.Weight = Convert.ToDouble(mWeight.Text, CultureInfo.InstalledUICulture); //double.Parse(mWeight.Text);
user.MaxHeartRate = Convert.ToInt32(mMHR.Text, CultureInfo.InstalledUICulture); //int.Parse(mMHR.Text);
user.BicycleWeight = Convert.ToDouble(mBW.Text, CultureInfo.InstalledUICulture); //double.Parse(mBW.Text);
user.WheelDiameter = Convert.ToInt32(mWD.Text, CultureInfo.InstalledUICulture); //int.Parse(mWD.Text);
user.Unit = mUnitDropdown.SelectedIndex;
user.Contact = mName.Text;
user.ContactPhone = mPhone.Text;
user.ContactAddress = mAddr.Text;
var result = userApi.Update(user, out string cookie,"");
App.isChanged = false;
if (result.result == false)
{
Utils.SetValidate(dict, JArray.FromObject(result.data));
Utils.showToast(gameObject, result.errMsg);
//UIManager.ShowAlert(result.errMsg);
}
else
2021-07-27 18:36:49 +08:00
{
2021-04-30 21:09:21 +08:00
App.CurrentUser = JObject.FromObject(result.data).ToObject<UserResultModel>();
App.CurrentUser.cookie = cookie;
2021-07-27 18:36:49 +08:00
if (App.CurrentUser.Birthday.HasValue)
{
App.CurrentUser.Birthday = App.CurrentUser.Birthday.Value.ToLocalTime();
}
var userinfos = UIManager.Instance.userInfos;
var i = userinfos.FindIndex(x => x.Id == App.CurrentUser.Id);
if (i != -1)
{
userinfos[i].NickName = user.Nickname;
UIManager.Instance.userInfos = userinfos;
}
Utils.showToast(gameObject, "Success", type: 1);
//UIManager.ShowAlert("Success");
}
LoadDataSync();
}
2021-05-12 10:42:26 +08:00
void ChangeAvatar(string path)
2021-04-16 17:49:11 +08:00
{
var r = userApi.Update(App.CurrentUser, out string cookie,path);
2021-04-28 15:21:06 +08:00
App.isChanged = false;
2021-04-16 17:49:11 +08:00
if (r.result)
{
2021-04-23 15:36:18 +08:00
var img = JObject.FromObject(r.data).Value<string>("WxHeadImg");
2021-07-26 19:50:10 +08:00
Utils.DisplayImage(mHeadImage, img, true);
2021-04-16 17:49:11 +08:00
var userinfos = UIManager.Instance.userInfos;
var i = userinfos.FindIndex(x => x.Id == App.CurrentUser.Id);
if (i != -1)
{
userinfos[i].Avatar = img;
UIManager.Instance.userInfos = userinfos;
}
2021-07-26 19:50:10 +08:00
Debug.Log(img);
2021-04-16 17:49:11 +08:00
App.CurrentUser.WxHeadImg = img;
App.CurrentUser.cookie = cookie;
2021-04-27 18:26:30 +08:00
Utils.showToast(gameObject, "Success", type: 1);
//UIManager.ShowAlert();
2021-04-16 17:49:11 +08:00
}
else
{
2021-04-27 18:26:30 +08:00
Utils.showToast(gameObject, r.errMsg);
//UIManager.ShowAlert();
2021-04-16 17:49:11 +08:00
}
}
2021-04-15 15:57:29 +08:00
void Cancel(BaseEventData e)
{
UIManager.ShowPrePanel();
//if (App.isChanged)
//{
// UIManager.ShowConfirm("Tips", "It is detected that you have modified, do you want to give up and return?", () =>
// {
// UIManager.ShowPrePanel();
// UIManager.CloseConfirm();
// });
2021-04-28 15:21:06 +08:00
//}
//else
//{
// UIManager.ShowPrePanel();
//}
2021-04-15 15:57:29 +08:00
}
public void OnMobileImageSelect(string res)
{
Debug.Log(res);
if (res.IndexOf("true,") == 0)
{
var path = res.Replace("true,", "");
ChangeAvatar(path);
}
}
2021-03-30 14:23:41 +08:00
}