BiServersController.cs 10 KB

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