BiServersController.cs 10 KB

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