UserFeedbackController.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Newtonsoft.Json;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. /* 用于管理用户反馈界面和提交
  6. */
  7. public class UserFeedbackController : MonoBehaviour
  8. {
  9. // Start is called once before the first execution of Update after the MonoBehaviour is created
  10. private DropdownField type;
  11. private TextField feedback;
  12. private Button submit, cancel;
  13. private string typeOption0, typeOption1;
  14. void Awake()
  15. {
  16. var root = GetComponent<UIDocument>().rootVisualElement;
  17. type = root.Q<DropdownField>("type");
  18. feedback = root.Q<TextField>("feedback");
  19. submit = root.Q<Button>("submit");
  20. cancel = root.Q<Button>("cancel");
  21. InitSetting();
  22. cancel.RegisterCallback<ClickEvent>(ev => CancelClick());
  23. submit.RegisterCallback<ClickEvent>(ev => SubmitRequest());
  24. }
  25. // Update is called once per frame
  26. void Update()
  27. {
  28. if (feedback.text.Trim() == "")
  29. {
  30. submit.SetEnabled(false);
  31. }
  32. else
  33. {
  34. submit.SetEnabled(true);
  35. }
  36. }
  37. void InitSetting()
  38. {
  39. string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "button", "submit", EnviromentSetting.languageCode });
  40. submit.text = textValue;
  41. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "button", "cancel", EnviromentSetting.languageCode });
  42. cancel.text = textValue;
  43. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "label", "type", EnviromentSetting.languageCode });
  44. type.label = textValue;
  45. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "label", "feedback", EnviromentSetting.languageCode });
  46. feedback.label = textValue;
  47. string typeOption0 = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "label", "option_0", EnviromentSetting.languageCode });
  48. string typeOption1 = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "label", "option_1", EnviromentSetting.languageCode });
  49. type.choices.Clear();
  50. type.choices.Add(typeOption0);
  51. type.choices.Add(typeOption1);
  52. type.value = typeOption0;
  53. }
  54. void CancelClick()
  55. {
  56. HomeSoundEffectController.Instance.PlaySoundEffect(2);
  57. var uiPlaceholder = GameObject.Find("UI Placeholder");
  58. if (uiPlaceholder != null)
  59. {
  60. var FeedbackUI = uiPlaceholder.transform.Find("Feedback").gameObject;
  61. var UserInfoUI = uiPlaceholder.transform.Find("User Info").gameObject;
  62. FeedbackUI.SetActive(false);
  63. UserInfoUI.SetActive(false);
  64. }
  65. }
  66. void SubmitRequest()
  67. {
  68. Debug.Log("Feedback Submit request");
  69. string url = "/api/user/feedback/";
  70. WWWForm form = new();
  71. form.AddField("user_id", UserProperty.userId);
  72. form.AddField("type", type.value);
  73. form.AddField("feedback", feedback.text);
  74. StartCoroutine(WebController.PostRequest(url, form, callback: SubmitCallback));
  75. }
  76. void SubmitCallback(string json)
  77. {
  78. var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  79. if (data != null && data["status"] == "success")
  80. {
  81. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "message", "success", EnviromentSetting.languageCode });
  82. MessageBoxController.ShowMessage(msg, () => CancelClick());
  83. }
  84. else
  85. {
  86. Debug.Log("Login Token fail!");
  87. Debug.Log(json);
  88. }
  89. }
  90. }