HomeController.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using GuestName.Models;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Logging;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Net.Http;
  9. using System.Threading.Tasks;
  10. namespace GuestName.Controllers
  11. {
  12. public class HomeController : Controller
  13. {
  14. private readonly ILogger<HomeController> _logger;
  15. public HomeController(ILogger<HomeController> logger)
  16. {
  17. _logger = logger;
  18. }
  19. public IActionResult Index()
  20. {
  21. return View();
  22. }
  23. [HttpPost]
  24. public JsonResult RefreshGuestName(string nextStatus)
  25. {
  26. try
  27. {
  28. using (var client = new HttpClient())
  29. {
  30. string url = "http://10.226.234.12:8039/merchant/getGuestForWeb?nextStatus=" + nextStatus;
  31. var response = client.GetAsync(url).Result;
  32. if (response.IsSuccessStatusCode)
  33. {
  34. var responseContent = response.Content;
  35. // by calling .Result you are synchronously reading the result
  36. string responseString = responseContent.ReadAsStringAsync().Result;
  37. GuestResponse res = GuestResponse.Parse(responseString);
  38. if (res.errorCode == "00000")
  39. {
  40. if (res.guest != null)
  41. {
  42. return Json(new
  43. {
  44. error = "0",
  45. content = res.message,
  46. name = res.guest.name,
  47. gender = res.guest.gender == 1 ? "Mr." : "Ms.",
  48. organize = res.guest.organize,
  49. position = res.guest.position
  50. });
  51. }
  52. else
  53. {
  54. return Json(new
  55. {
  56. error = "1",
  57. content = "No more guest"
  58. });
  59. }
  60. }
  61. }
  62. var errorCode = response.StatusCode.ToString();
  63. return Json(new
  64. {
  65. error = errorCode,
  66. content = "System failed"
  67. });
  68. }
  69. }
  70. catch (Exception ex)
  71. {
  72. _logger.LogError("Error get guest name", ex);
  73. return Json(new
  74. {
  75. error = "-1",
  76. content = "System failed"
  77. });
  78. }
  79. }
  80. [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
  81. public IActionResult Error()
  82. {
  83. return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
  84. }
  85. }
  86. }