BiServersController.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 TEAMModeBI.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. if (!jsonElment.TryGetProperty("mobile", out JsonElement _mobile)) return BadRequest();
  52. HttpClient httpClient = new HttpClient();
  53. List<JsonElement> mbs = new List<JsonElement>() { _mobile };
  54. string url = _configuration.GetValue<string>("HaBookAuth:CoreId:userinfo");
  55. var content = new StringContent(mbs.ToJsonString(), Encoding.UTF8, "application/json");
  56. HttpResponseMessage responseMessage = await httpClient.PostAsync(url, content);
  57. userInfo userInfo = new userInfo();
  58. userInfo.mobile = $"{_mobile}";
  59. if (responseMessage.StatusCode == HttpStatusCode.OK)
  60. {
  61. string responseBody = await responseMessage.Content.ReadAsStringAsync();
  62. List<JsonElement> json_id = responseBody.ToObject<List<JsonElement>>();
  63. if (json_id.IsNotEmpty())
  64. {
  65. userInfo.tmdid = json_id[0].GetProperty("id").ToString();
  66. userInfo.tmdname = json_id[0].GetProperty("name").ToString();
  67. }
  68. return Ok(new { state = 200, userInfo });
  69. }
  70. else return Ok(new { responseMessage.StatusCode });
  71. }
  72. catch (Exception ex)
  73. {
  74. await _dingDing.SendBotMsg($"BI, {_option} /biservers/get-mobileuserinfo \n {ex.Message} {ex.StackTrace}", GroupNames.AI智慧學校申請通知群);
  75. return BadRequest();
  76. }
  77. }
  78. /// <summary>
  79. /// 发送验证码
  80. /// </summary>
  81. /// <param name="jsonElement"></param>
  82. /// <returns></returns>
  83. [ProducesDefaultResponseType]
  84. [HttpPost("send-sms")]
  85. public async Task<IActionResult> send_sms(JsonElement jsonElement)
  86. {
  87. try
  88. {
  89. if (!jsonElement.TryGetProperty("country", out JsonElement country)) return BadRequest();
  90. if (!jsonElement.TryGetProperty("to", out JsonElement to)) return BadRequest();
  91. if (!jsonElement.TryGetProperty("lang", out JsonElement lang)) return BadRequest();
  92. if (!jsonElement.TryGetProperty("HasUser", out JsonElement HasUser)) return BadRequest();
  93. string smsurl = _configuration.GetValue<string>("HaBookAuth:CoreAPI");
  94. HttpClient httpClient = _http.CreateClient();
  95. HttpResponseMessage responseMessage = await httpClient.PostAsJsonAsync($"{smsurl}/service/sandsms/pin", jsonElement);
  96. if (responseMessage.StatusCode == HttpStatusCode.OK)
  97. {
  98. string str_json = await responseMessage.Content.ReadAsStringAsync();
  99. if (string.IsNullOrEmpty($"{str_json}"))
  100. {
  101. return Ok(new { status = 200, message = "发送成功" });
  102. }
  103. else
  104. {
  105. return Ok(str_json);
  106. }
  107. }
  108. else
  109. {
  110. return Ok(new { status = 0, message = "发送失败!" });
  111. }
  112. }
  113. catch (Exception ex)
  114. {
  115. return Ok(new { status = 0, message = $"发送失败!{ex.Message}" });
  116. }
  117. }
  118. /// <summary>
  119. /// 验证码和手机的验证
  120. /// </summary>
  121. /// <param name="jsonElement"></param>
  122. /// <returns></returns>
  123. [ProducesDefaultResponseType]
  124. [HttpPost("verfiypin")]
  125. public async Task<IActionResult> VerifiyPIN(JsonElement jsonElement)
  126. {
  127. try
  128. {
  129. if (!jsonElement.TryGetProperty("mobile", out JsonElement mobile)) return BadRequest();
  130. if (!jsonElement.TryGetProperty("Authorization_Pin", out JsonElement sms)) return BadRequest();
  131. string smsurl = _configuration.GetValue<string>("HaBookAuth:CoreAPI");
  132. HttpClient httpClient = new HttpClient();
  133. var temp_job = new { Authorization_Pin = sms };
  134. //var content = new StringContent(temp_job.ToJsonString(), Encoding.UTF8, "application/json");
  135. HttpResponseMessage responseMessage = await httpClient.PostAsJsonAsync($"{smsurl}/service/verifiy/pin", temp_job);
  136. if (responseMessage.StatusCode == HttpStatusCode.OK)
  137. {
  138. string responseBody = await responseMessage.Content.ReadAsStringAsync();
  139. var json = responseBody.ToObject<JsonElement>();
  140. json.TryGetProperty("resule", out JsonElement jsone);
  141. if (!string.IsNullOrEmpty($"{jsone}"))
  142. {
  143. string[] mobules = $"{jsone}".Split("-");
  144. string temp_mobile = mobules.Length >= 2 ? mobules[1] : mobules[0];
  145. if (mobile.ToString().Equals(temp_mobile))
  146. {
  147. return Ok(new { status = 200, message = "手机号和验证码验证都过了" });
  148. }
  149. else
  150. {
  151. return Ok(new { status = 5, message = "手机号码不正确" });
  152. }
  153. }
  154. else
  155. {
  156. return Ok(json);
  157. }
  158. }
  159. else
  160. {
  161. return Ok(new { status = 0, message = "发送状态错误" });
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. return Ok(new { status = 0, message = $"发送状态错误{ex.Message}" });
  167. }
  168. }
  169. public record userInfo
  170. {
  171. public string mobile { get; set; }
  172. public string tmdid { get; set; }
  173. public string tmdname { get; set; }
  174. }
  175. }
  176. }