BiServersController.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using TEAMModelOS.Models;
  8. using TEAMModelOS.SDK.DI;
  9. using Microsoft.Extensions.Options;
  10. using Microsoft.Extensions.Configuration;
  11. using System.Text.Json;
  12. using System.Net.Http;
  13. using TEAMModelOS.SDK.Extension;
  14. using System.Text;
  15. using System.Net;
  16. using HTEXLib.COMM.Helpers;
  17. using System.Net.Http.Json;
  18. namespace TEAMModelBI.Controllers.BIServer
  19. {
  20. [Route("biservers")]
  21. [ApiController]
  22. public class BiServersController : ControllerBase
  23. {
  24. //钉钉信息发送
  25. private readonly DingDing _dingDing;
  26. //数据库连接
  27. public AzureCosmosFactory _azureCosmos;
  28. private readonly Option _option;
  29. //读取配置信息
  30. private readonly IConfiguration _configuration;
  31. private readonly IHttpClientFactory _http;
  32. public BiServersController(AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, IHttpClientFactory http)
  33. {
  34. _azureCosmos = azureCosmos;
  35. _dingDing = dingDing;
  36. _option = option?.Value;
  37. _configuration = configuration;
  38. _http = http;
  39. }
  40. /// <summary>
  41. /// 依据手机号查询醍摩豆基础信息
  42. /// </summary>
  43. /// <param name="jsonElment"></param>
  44. /// <returns></returns>
  45. [ProducesDefaultResponseType]
  46. [HttpPost("get-mobileuserinfo")]
  47. public async Task<IActionResult> SendMobileGetUserIfo(JsonElement jsonElment)
  48. {
  49. try
  50. {
  51. HttpClient httpClient = _http.CreateClient();
  52. string url = _configuration.GetValue<string>("HaBookAuth:CoreId:userinfo");
  53. HttpResponseMessage responseMessage = await httpClient.PostAsJsonAsync(url, jsonElment);
  54. List<userInfo> userInfos = new List<userInfo>();
  55. if (responseMessage.StatusCode == HttpStatusCode.OK)
  56. {
  57. string temp = responseMessage.Content.ReadAsStringAsync().Result;
  58. List<JsonElement> json_id = temp.ToObject<List<JsonElement>>();
  59. foreach (var item in json_id)
  60. {
  61. userInfo userInfo = new userInfo();
  62. userInfo.tmdid = item.GetProperty("id").ToString();
  63. userInfo.tmdname = item.GetProperty("name").ToString();
  64. userInfo.mobile = item.GetProperty("mobile").ToString();
  65. userInfos.Add(userInfo);
  66. }
  67. return Ok(new { state = 200, userInfos });
  68. }
  69. else return Ok(new { responseMessage.StatusCode });
  70. }
  71. catch (Exception ex)
  72. {
  73. await _dingDing.SendBotMsg($"BI, {_option} /biservers/get-mobileuserinfo \n {ex.Message} {ex.StackTrace}", GroupNames.成都开发測試群組);
  74. return BadRequest();
  75. }
  76. }
  77. /// <summary>
  78. /// 发送验证码
  79. /// </summary>
  80. /// <param name="jsonElement"></param>
  81. /// <returns></returns>
  82. [ProducesDefaultResponseType]
  83. [HttpPost("send-sms")]
  84. public async Task<IActionResult> send_sms(JsonElement jsonElement)
  85. {
  86. try
  87. {
  88. if (!jsonElement.TryGetProperty("country", out JsonElement country)) return BadRequest();
  89. if (!jsonElement.TryGetProperty("to", out JsonElement to)) return BadRequest();
  90. if (!jsonElement.TryGetProperty("lang", out JsonElement lang)) return BadRequest();
  91. if (!jsonElement.TryGetProperty("HasUser", out JsonElement HasUser)) return BadRequest();
  92. string smsurl = _configuration.GetValue<string>("HaBookAuth:CoreAPI");
  93. HttpClient httpClient = _http.CreateClient();
  94. HttpResponseMessage responseMessage = await httpClient.PostAsJsonAsync($"{smsurl}/service/sandsms/pin", jsonElement);
  95. if (responseMessage.StatusCode == HttpStatusCode.OK)
  96. {
  97. string str_json = await responseMessage.Content.ReadAsStringAsync();
  98. if (string.IsNullOrEmpty($"{str_json}"))
  99. {
  100. return Ok(new { state = 200, message = "发送成功" });
  101. }
  102. else
  103. {
  104. return Ok(str_json);
  105. }
  106. }
  107. else
  108. {
  109. return Ok(new { state = 0, message = "发送失败!" });
  110. }
  111. }
  112. catch (Exception ex)
  113. {
  114. return Ok(new { state = 0, message = $"发送失败!{ex.Message}" });
  115. }
  116. }
  117. /// <summary>
  118. /// 验证码和手机的验证
  119. /// </summary>
  120. /// <param name="jsonElement"></param>
  121. /// <returns></returns>
  122. [ProducesDefaultResponseType]
  123. [HttpPost("verfiypin")]
  124. public async Task<IActionResult> VerifiyPIN(JsonElement jsonElement)
  125. {
  126. try
  127. {
  128. if (!jsonElement.TryGetProperty("mobile", out JsonElement mobile)) return BadRequest();
  129. if (!jsonElement.TryGetProperty("Authorization_Pin", out JsonElement sms)) return BadRequest();
  130. string smsurl = _configuration.GetValue<string>("HaBookAuth:CoreAPI");
  131. HttpClient httpClient = _http.CreateClient();
  132. var temp_job = new { Authorization_Pin = sms };
  133. HttpResponseMessage responseMessage = await httpClient.PostAsJsonAsync($"{smsurl}/service/verifiy/pin", temp_job);
  134. if (responseMessage.StatusCode == HttpStatusCode.OK)
  135. {
  136. string responseBody = await responseMessage.Content.ReadAsStringAsync();
  137. var json = responseBody.ToObject<JsonElement>();
  138. json.TryGetProperty("resule", out JsonElement jsone);
  139. if (!string.IsNullOrEmpty($"{jsone}"))
  140. {
  141. string[] mobules = $"{jsone}".Split("-");
  142. string temp_mobile = mobules.Length >= 2 ? mobules[1] : mobules[0];
  143. if (mobile.ToString().Equals(temp_mobile))
  144. {
  145. return Ok(new { state = 200, message = "手机号和验证码验证都过了" });
  146. }
  147. else
  148. {
  149. return Ok(new { state = 5, message = "手机号码不正确" });
  150. }
  151. }
  152. else
  153. {
  154. return Ok(json);
  155. }
  156. }
  157. else
  158. {
  159. return Ok(new { state = 0, message = "发送状态错误" });
  160. }
  161. }
  162. catch (Exception ex)
  163. {
  164. return Ok(new { state = 0, message = $"发送状态错误{ex.Message}" });
  165. }
  166. }
  167. public record userInfo
  168. {
  169. public string mobile { get; set; }
  170. public string tmdid { get; set; }
  171. public string tmdname { get; set; }
  172. }
  173. }
  174. }