UserFeedbackController.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. void OnEnable()
  14. {
  15. var root = GetComponent<UIDocument>().rootVisualElement;
  16. type = root.Q<DropdownField>("type");
  17. feedback = root.Q<TextField>("feedback");
  18. submit = root.Q<Button>("submit");
  19. cancel = root.Q<Button>("cancel");
  20. InitSetting();
  21. cancel.RegisterCallback<ClickEvent>(ev => CancelClick());
  22. submit.RegisterCallback<ClickEvent>(ev => SubmitRequest());
  23. }
  24. // Update is called once per frame
  25. void Update()
  26. {
  27. if (feedback.text.Trim() == "")
  28. {
  29. submit.SetEnabled(false);
  30. }
  31. else
  32. {
  33. submit.SetEnabled(true);
  34. }
  35. }
  36. void InitSetting()
  37. {
  38. string textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "button", "submit", EnviromentSetting.languageCode });
  39. submit.text = textValue;
  40. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "button", "cancel", EnviromentSetting.languageCode });
  41. cancel.text = textValue;
  42. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "label", "type", EnviromentSetting.languageCode });
  43. type.label = textValue;
  44. textValue = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "label", "feedback", EnviromentSetting.languageCode });
  45. feedback.label = textValue;
  46. string typeOption0 = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "label", "option_0", EnviromentSetting.languageCode });
  47. string typeOption1 = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "label", "option_1", EnviromentSetting.languageCode });
  48. type.choices.Clear();
  49. type.choices.Add(typeOption0);
  50. type.choices.Add(typeOption1);
  51. type.value = typeOption0;
  52. }
  53. void CancelClick()
  54. {
  55. HomeSoundEffectController.Instance.PlaySoundEffect(2);
  56. var uiPlaceholder = GameObject.Find("UI Placeholder");
  57. if (uiPlaceholder != null)
  58. {
  59. var FeedbackUI = uiPlaceholder.transform.Find("Feedback").gameObject;
  60. var VoiceAndMenuUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  61. FeedbackUI.SetActive(false);
  62. VoiceAndMenuUI.SetActive(true);
  63. }
  64. }
  65. void SubmitRequest()
  66. {
  67. Debug.Log("Feedback Submit request");
  68. string url = "/api/user/feedback/";
  69. WWWForm form = new();
  70. form.AddField("user_id", UserProperty.userId);
  71. form.AddField("type", type.value);
  72. form.AddField("feedback", feedback.text);
  73. StartCoroutine(WebController.PostRequest(url, form, callback: SubmitCallback));
  74. }
  75. void SubmitCallback(string json)
  76. {
  77. var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  78. if (data != null && data["status"] == "success")
  79. {
  80. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "FeedbackUI", "message", "success", EnviromentSetting.languageCode });
  81. MessageBoxController.ShowMessage(msg, () => CancelClick());
  82. }
  83. else
  84. {
  85. Debug.Log(json);
  86. }
  87. }
  88. }