using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using System; using System.Linq; /// /// 组织机构需要的数据类 /// [Serializable] public class OrgItemData { public string id; public string parentId; public string name; public int level; } /// /// 组织机构数据存储类(只获取一遍组织机构) /// public class OrganizationsData { private static OrganizationsData _Instance; private OrganizationsData() { } public static OrganizationsData Instance { get { if (_Instance == null) { _Instance = new OrganizationsData(); } return _Instance; } } private List AllData = new List(); public List GetOrgDatas() { lock (this) { return this.AllData; } } public void SetData(List datas) { lock (this) { foreach (var item in datas) { if (!AllData.Contains(item)) AllData.Add(item); } } } } public class OrganizationsLoadPanel : MonoBehaviour { public Toggle Top; public Color SelectColor; public Color DisSelectColor; public Image ShowImage; public Image HideImage; public TMP_Text ShowOrgName; public GameObject Main; public Transform OrgsParent; public GameObject OrgsItem; public ToggleGroup Group; public OrgItemData SelectData; public Color NormalColor; private OrgItem rootItem; public PostsLoadPanel postsPanel; public List IndexColor = new List() { Color.white,Color.red,Color.green,Color.yellow }; private string Api = "/api/Organizations"; private List AllResultData = new List(); void Start() { AllResultData = OrganizationsData.Instance.GetOrgDatas(); if (AllResultData.Count == 0) { var Url = ""; if (HTTPRequestManager.Instance != null) { Url = $"{Api}?ContainsChildren=true&PageNumber={1}&PageSize={10000}"; } //向服务端请求数据 HTTPRequestManager.Instance.GetJson>(Url, SkipPageSuccessed, SkipPageError); } else { DataBind(AllResultData); } Top.onValueChanged.AddListener(Top_ValueChanged); } private void Top_ValueChanged(bool IsOn) { Main.SetActive(IsOn); ShowImage.gameObject.SetActive(IsOn); HideImage.gameObject.SetActive(!IsOn); if (IsOn) { if (postsPanel != null) { postsPanel.Top.isOn = false; } } } private void SkipPageError(int code, string message) { //MessageNotifyBox.Instance.ShowDialogOK("获取组织机构失败!"); Debug.LogError("获取组织机构失败!"); } private void SkipPageSuccessed(Page resultSet) { if (resultSet.Items.Count > 0) { foreach (Transform item in OrgsParent) { Destroy(item.gameObject); } //数据处理 DataBind(resultSet.Items); } } private void DataBind(List data) { OrganizationsData.Instance.SetData(data); var root = AllResultData.Where(d => string.IsNullOrEmpty(d.parentId)).SingleOrDefault(); rootItem = Create(root); rootItem.DataBind(root, this, 0); var node1 = data.Where(d => d.parentId == root.id).ToList(); if (node1.Count > 0) { for (int i = 0; i < node1.Count; i++) { var nood1Item = Create(node1[i]); nood1Item.DataBind(node1[i], this, 1); rootItem.M_Children.Add(nood1Item); rootItem.HaChildObj.SetActive(true); var node2 = data.Where(d => d.parentId == node1[i].id).ToList(); if (node2.Count > 0) { for (int j = 0; j < node2.Count; j++) { var nood2Item = Create(node2[j]); nood2Item.DataBind(node2[j], this, 2); nood1Item.M_Children.Add(nood2Item); nood1Item.HaChildObj.SetActive(true); var node3 = data.Where(d => d.parentId == node2[j].id).ToList(); if (node3.Count > 0) { for (int k = 0; k < node3.Count; k++) { var nood3Item = Create(node3[k]); nood3Item.DataBind(node3[k], this, 3); nood2Item.M_Children.Add(nood3Item); nood2Item.HaChildObj.SetActive(true); } nood2Item.HideChild(); } nood1Item.HideChild(); } } } } rootItem.ShowChildUI(); } public OrgItem Create(OrgItemData data) { GameObject go = Instantiate(OrgsItem, OrgsParent); return go.GetComponent(); } public void AutoHeight() { StopAllCoroutines(); StartCoroutine(autoHeight()); } IEnumerator autoHeight() { yield return new WaitForSeconds(0.05f); float height = OrgsParent.GetComponent().sizeDelta.y < 330 ? OrgsParent.GetComponent().sizeDelta.y : 330; Main.GetComponent().sizeDelta = new Vector2( Main.GetComponent().sizeDelta.x, height); } public void OrgSelect(OrgItemData data, bool IsSelect) { if (IsSelect) { this.SelectData = data; Top.isOn = false; ShowOrgName.text = data.name; ShowOrgName.color = Color.white; } else { if (SelectData == data) { SelectData = null; ShowOrgName.text = "请选择"; ShowOrgName.color = NormalColor; Top.isOn = false; } } if (postsPanel != null) { postsPanel.OrgDataBind(SelectData); } } public void Reset() { foreach (Transform item in OrgsParent) { if (item.GetComponent().isOn) { item.GetComponent().isOn = false; } } if (rootItem) { rootItem.ShowChild.isOn = false; rootItem.ShowChildUI(); } Top.isOn = false; ShowOrgName.text = "请选择"; ShowOrgName.color = NormalColor; } }