BiServersController.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. using TEAMModelOS.SDK;
  19. using TEAMModelBI.Filter;
  20. using Microsoft.AspNetCore.Authorization;
  21. namespace TEAMModelBI.Controllers.BIServer
  22. {
  23. [Route("biservers")]
  24. [ApiController]
  25. public class BiServersController : ControllerBase
  26. {
  27. //钉钉信息发送
  28. private readonly DingDing _dingDing;
  29. //数据库连接
  30. public AzureCosmosFactory _azureCosmos;
  31. private readonly Option _option;
  32. //读取配置信息
  33. private readonly IConfiguration _configuration;
  34. private readonly IHttpClientFactory _http;
  35. private readonly CoreAPIHttpService _coreAPIHttpService;
  36. public BiServersController(AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, IHttpClientFactory http, CoreAPIHttpService coreAPIHttpService)
  37. {
  38. _azureCosmos = azureCosmos;
  39. _dingDing = dingDing;
  40. _option = option?.Value;
  41. _configuration = configuration;
  42. _http = http;
  43. _coreAPIHttpService = coreAPIHttpService;
  44. }
  45. /// <summary>
  46. /// 通过手机号、醍摩豆账户、邮箱查询信息
  47. /// </summary>
  48. /// <param name="request"></param>
  49. /// <returns></returns>
  50. [AuthToken(Roles = "admin,rdc,assist,sales")]
  51. [HttpPost("get-coreinfo")]
  52. public async Task<IActionResult> GetCoreInfo(JsonElement jsonElement)
  53. {
  54. try
  55. {
  56. var content = new StringContent(jsonElement.ToString(), Encoding.UTF8, "application/json");
  57. string json = await _coreAPIHttpService.GetUserInfos(content);
  58. return Ok(new { state = 200, infos = json.ToObject<JsonElement>() });
  59. }
  60. catch (Exception ex)
  61. {
  62. await _dingDing.SendBotMsg($"BI, {_option} /biservers/get-coreinfo \n {ex.Message}\n{ex.StackTrace} ", GroupNames.成都开发測試群組);
  63. return BadRequest();
  64. }
  65. }
  66. /// <summary>
  67. /// 依据手机号查询醍摩豆基础信息
  68. /// </summary>
  69. /// <param name="jsonElment"></param>
  70. /// <returns></returns>
  71. [ProducesDefaultResponseType]
  72. [HttpPost("get-userinfo")]
  73. public async Task<IActionResult> SendMobileGetUserIfo(JsonElement jsonElment)
  74. {
  75. try
  76. {
  77. if (!jsonElment.TryGetProperty("mobile", out JsonElement mobile)) return BadRequest();
  78. var coreUser = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", $"{mobile}" } }, _option.Location, _configuration);
  79. if (coreUser.id != null)
  80. return Ok(new { state = 200, coreUser });
  81. else return Ok(new { state = 404, msg = "未找到改账户" });
  82. //HttpClient httpClient = _http.CreateClient();
  83. //string url = _configuration.GetValue<string>("HaBookAuth:CoreId:userinfo");
  84. //HttpResponseMessage responseMessage = await httpClient.PostAsJsonAsync(url, jsonElment);
  85. //List<userInfo> userInfos = new List<userInfo>();
  86. //if (responseMessage.StatusCode == HttpStatusCode.OK)
  87. //{
  88. // string temp = responseMessage.Content.ReadAsStringAsync().Result;
  89. // List<JsonElement> json_id = temp.ToObject<List<JsonElement>>();
  90. // foreach (var item in json_id)
  91. // {
  92. // userInfo userInfo = new userInfo();
  93. // userInfo.tmdid = item.GetProperty("id").ToString();
  94. // userInfo.tmdname = item.GetProperty("name").ToString();
  95. // userInfo.mobile = item.GetProperty("mobile").ToString();
  96. // userInfos.Add(userInfo);
  97. // }
  98. // return Ok(new { state = 200, userInfos });
  99. //}
  100. //else return Ok(new { responseMessage.StatusCode });
  101. }
  102. catch (Exception ex)
  103. {
  104. await _dingDing.SendBotMsg($"BI, {_option} /biservers/get-mobileuserinfo \n {ex.Message}\n{ex.StackTrace} ", GroupNames.成都开发測試群組);
  105. return BadRequest();
  106. }
  107. }
  108. /// <summary>
  109. /// 发送验证码
  110. /// </summary>
  111. /// <param name="jsonElement"></param>
  112. /// <returns></returns>
  113. [ProducesDefaultResponseType]
  114. [HttpPost("send-sms")]
  115. public async Task<IActionResult> send_sms(JsonElement jsonElement)
  116. {
  117. try
  118. {
  119. if (!jsonElement.TryGetProperty("country", out JsonElement country)) return BadRequest();
  120. if (!jsonElement.TryGetProperty("to", out JsonElement to)) return BadRequest();
  121. if (!jsonElement.TryGetProperty("lang", out JsonElement lang)) return BadRequest();
  122. if (!jsonElement.TryGetProperty("HasUser", out JsonElement HasUser)) return BadRequest();
  123. string smsurl = _configuration.GetValue<string>("HaBookAuth:CoreAPI");
  124. HttpClient httpClient = _http.CreateClient();
  125. HttpResponseMessage responseMessage = await httpClient.PostAsJsonAsync($"{smsurl}/service/sandsms/pin", jsonElement);
  126. if (responseMessage.StatusCode == HttpStatusCode.OK)
  127. {
  128. string str_json = await responseMessage.Content.ReadAsStringAsync();
  129. if (string.IsNullOrEmpty($"{str_json}"))
  130. {
  131. return Ok(new { state = 200, message = "发送成功" });
  132. }
  133. else
  134. {
  135. return Ok(str_json);
  136. }
  137. }
  138. else
  139. {
  140. return Ok(new { state = 0, message = "发送失败!" });
  141. }
  142. }
  143. catch (Exception ex)
  144. {
  145. return Ok(new { state = 0, message = $"发送失败!{ex.Message}\n{ex.StackTrace}" });
  146. }
  147. }
  148. /// <summary>
  149. /// 验证码和手机的验证
  150. /// </summary>
  151. /// <param name="jsonElement"></param>
  152. /// <returns></returns>
  153. [ProducesDefaultResponseType]
  154. [HttpPost("verfiypin")]
  155. public async Task<IActionResult> VerifiyPIN(JsonElement jsonElement)
  156. {
  157. try
  158. {
  159. if (!jsonElement.TryGetProperty("mobile", out JsonElement mobile)) return BadRequest();
  160. if (!jsonElement.TryGetProperty("Authorization_Pin", out JsonElement sms)) return BadRequest();
  161. string smsurl = _configuration.GetValue<string>("HaBookAuth:CoreAPI");
  162. HttpClient httpClient = _http.CreateClient();
  163. var temp_job = new { Authorization_Pin = sms };
  164. HttpResponseMessage responseMessage = await httpClient.PostAsJsonAsync($"{smsurl}/service/verifiy/pin", temp_job);
  165. if (responseMessage.StatusCode == HttpStatusCode.OK)
  166. {
  167. string responseBody = await responseMessage.Content.ReadAsStringAsync();
  168. var json = responseBody.ToObject<JsonElement>();
  169. json.TryGetProperty("resule", out JsonElement jsone);
  170. if (!string.IsNullOrEmpty($"{jsone}"))
  171. {
  172. string[] mobules = $"{jsone}".Split("-");
  173. string temp_mobile = mobules.Length >= 2 ? mobules[1] : mobules[0];
  174. if (mobile.ToString().Equals(temp_mobile))
  175. {
  176. return Ok(new { state = 200, message = "手机号和验证码验证都过了" });
  177. }
  178. else
  179. {
  180. return Ok(new { state = 5, message = "手机号码不正确" });
  181. }
  182. }
  183. else
  184. {
  185. return Ok(json);
  186. }
  187. }
  188. else
  189. {
  190. return Ok(new { state = 0, message = "发送状态错误" });
  191. }
  192. }
  193. catch (Exception ex)
  194. {
  195. return Ok(new { state = 0, message = $"发送状态错误{ex.Message}\n{ex.StackTrace}" });
  196. }
  197. }
  198. public record userInfo
  199. {
  200. public string mobile { get; set; }
  201. public string tmdid { get; set; }
  202. public string tmdname { get; set; }
  203. }
  204. }
  205. }