You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

79 lines
1.9 KiB

  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using Cysharp.Threading.Tasks;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class HomeView : MonoBehaviour
  8. {
  9. [SerializeField]
  10. private Button PayButton;
  11. [HideInInspector]
  12. public string phonenum = "";
  13. // Start is called before the first frame update
  14. void Start()
  15. {
  16. PayButton.onClick.AddListener(ClickPayButton);
  17. }
  18. private void ClickPayButton()
  19. {
  20. ClickPayButton_Task().Forget();
  21. }
  22. private async UniTask ClickPayButton_Task()
  23. {
  24. string localIP = GetLocalIP();
  25. Debug.Log("Local IP Address: " + localIP);
  26. PayResponse payResponse = await HttpManager.Instance.RequestPost<PayRequest, PayResponse>("payment/pay", new PayRequest(){phone = phonenum, money = 600, clientIp = localIP});
  27. if (payResponse.code == 0)
  28. {
  29. // 解码 URL
  30. string decodedUrl = Uri.UnescapeDataString(payResponse.data);
  31. Application.OpenURL(decodedUrl);
  32. }
  33. // else
  34. // {
  35. // ErrorText.text = loginResponse.message;
  36. // }
  37. }
  38. string GetLocalIP()
  39. {
  40. string localIP = string.Empty;
  41. try
  42. {
  43. var host = Dns.GetHostEntry(Dns.GetHostName());
  44. foreach (var ip in host.AddressList)
  45. {
  46. // 检查是否是 IPv4 地址,并且排除回环地址
  47. if (ip.AddressFamily == AddressFamily.InterNetwork)
  48. {
  49. localIP = ip.ToString();
  50. break;
  51. }
  52. }
  53. }
  54. catch (System.Exception ex)
  55. {
  56. Debug.LogError("Error getting local IP: " + ex.Message);
  57. }
  58. return string.IsNullOrEmpty(localIP) ? "Unable to determine local IP" : localIP;
  59. }
  60. public void Show(){
  61. gameObject.SetActive(true);
  62. }
  63. // Update is called once per frame
  64. void Update()
  65. {
  66. }
  67. }