powerfun-unity/Assets/Scripts/UI/Prefab/Panel/EditUserController.cs
2021-04-15 15:57:29 +08:00

215 lines
6.6 KiB
C#

using Assets.Scripts;
using Assets.Scripts.Apis;
using Assets.Scripts.Apis.Models;
using Assets.Scripts.UI.Control;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class EditUserController : PFUIPanel
{
private Button mSaveButton;
private PFUIDropdown mSexDropdown;
private PFUIDropdown mDayDropdown;
private PFUIDropdown mMonthDropdown;
private PFUIDropdown mYearDropdown;
private Text mIdText;
private Text mNameText;
private RawImage mHeadImage;
private UserApi userApi;
private Button mCancelButton;
private Button mBackButton;
PfUIButton mChangeAvatar;
protected override void Awake()
{
userApi = new UserApi();
mIdText = this.transform.Find("IdText").GetComponent<Text>();
mNameText = this.transform.Find("NameText").GetComponent<Text>();
var panel = this.transform.Find("Panel");
mSexDropdown = panel.Find("SexDropdown").GetComponent<PFUIDropdown>();
mYearDropdown = panel.Find("YearDropdown").GetComponent<PFUIDropdown>();
mMonthDropdown = panel.Find("MonthDropdown").GetComponent<PFUIDropdown>();
mDayDropdown = panel.Find("DayDropdown").GetComponent<PFUIDropdown>();
mSaveButton = this.transform.Find("SaveButton").GetComponent<Button>();
mCancelButton = this.transform.Find("CancelButton").GetComponent<Button>();
mHeadImage = this.transform.Find("HeadImage").GetComponent<RawImage>();
mBackButton = this.transform.Find("BackButton").GetComponent<Button>();
UIManager.AddEvent(mBackButton.gameObject, EventTriggerType.PointerClick, Cancel);
mChangeAvatar = this.transform.Find("ChangeAvatar").GetComponent<PfUIButton>();
UIManager.AddEvent(mChangeAvatar.gameObject, EventTriggerType.PointerClick, (e) =>
{
OpenFileName ofn = new OpenFileName();
ofn.structSize = Marshal.SizeOf(ofn);
ofn.filter = "All Files\0*.*\0\0";
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 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR
if (Win32.GetOpenFileName(ofn))
{
Debug.Log(ofn.file);
}
});
mSexDropdown.ClearOptions();
mSexDropdown.AddOptions(new List<string>
{
"男","女"
});
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()
//});
LoadDataSync();
}
void LoadDataSync()
{
var rect = ((RectTransform)mHeadImage.transform).rect;
SetRounded(mHeadImage.transform, rect.height);
var currentUser = App.CurrentUser;
mIdText.text = currentUser.Id.ToString();
mNameText.text = currentUser.Nickname;
Utils.DisplayImage(StartCoroutine, mHeadImage, currentUser.WxHeadImg);
//if (!string.IsNullOrWhiteSpace(currentUser.WxHeadImg))
//{
// Utils.DisplayImage(StartCoroutine, mHeadImage, currentUser.WxHeadImg);
//}
//if (currentUser.Sex == 1)
//{
// mSexDropdown.SelectValue("男");
//}
//else
//{
// mSexDropdown.SelectValue("女");
//}
//if (currentUser.Birthday.HasValue)
//{
// var birthday = currentUser.Birthday.Value;
// mYearDropdown.SelectValue(birthday.Year.ToString());
// mMonthDropdown.SelectValue(birthday.Month.ToString());
// mDayDropdown.SelectValue(birthday.Day.ToString());
//}
UIManager.AddEvent(mSaveButton.gameObject, EventTriggerType.PointerClick, Save);
UIManager.AddEvent(mCancelButton.gameObject, EventTriggerType.PointerClick, Cancel);
}
// Start is called before the first frame update
protected override void Start()
{
base.Start();
}
// Update is called once per frame
void Update()
{
}
void SetDayDropdown()
{
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);
}
async void Save(BaseEventData e)
{
//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.SelectedItem == "男" ? 1 : 2;
//var result = await userApi.Update(user);
//if (result.result == false)
//{
// UIManager.ShowAlert(result.errMsg);
// return;
//}
//App.CurrentUser = user;
UIManager.ShowAlert("result.errMsg");
}
void Cancel(BaseEventData e)
{
UIManager.ShowHomePanel();
}
}