BusinessUsersController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.Options;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Text.Json;
  12. using System.Threading.Tasks;
  13. using TEAMModelBI.Filter;
  14. using TEAMModelBI.Tool.Extension;
  15. using TEAMModelOS.Models;
  16. using TEAMModelOS.SDK;
  17. using TEAMModelOS.SDK.Context.BI;
  18. using TEAMModelOS.SDK.Context.Constant;
  19. using TEAMModelOS.SDK.DI;
  20. using TEAMModelOS.SDK.Extension;
  21. using TEAMModelOS.SDK.Models.Cosmos.BI.BINormal;
  22. namespace TEAMModelBI.Controllers.BINormal
  23. {
  24. [Route("bizuser")]
  25. [ApiController]
  26. public class BusinessUsersController : ControllerBase
  27. {
  28. public readonly AzureCosmosFactory _azureCosmos;
  29. public readonly AzureStorageFactory _azureStorage;
  30. public readonly DingDing _dingDing;
  31. public readonly Option _option;
  32. //读取配置信息
  33. private readonly IConfiguration _configuration;
  34. private readonly CoreAPIHttpService _coreAPIHttpService;
  35. public BusinessUsersController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, CoreAPIHttpService coreAPIHttpService)
  36. {
  37. _azureCosmos = azureCosmos;
  38. _azureStorage = azureStorage;
  39. _dingDing = dingDing;
  40. _option = option?.Value;
  41. _configuration = configuration;
  42. _coreAPIHttpService = coreAPIHttpService;
  43. }
  44. /// <summary>
  45. /// 获取第三方用户信息
  46. /// </summary>
  47. /// <param name="jsonElement"></param>
  48. /// <returns></returns>
  49. [ProducesDefaultResponseType]
  50. [HttpPost("get-infos")]
  51. public async Task<IActionResult> GetInfos(JsonElement jsonElement)
  52. {
  53. jsonElement.TryGetProperty("id", out JsonElement id);
  54. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  55. var cosmosClient = _azureCosmos.GetCosmosClient();
  56. //if ($"{site}".Equals(BIConst.Global))
  57. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  58. StringBuilder sqlTxt = new("select value(c) from c");
  59. if (!string.IsNullOrEmpty($"{id}"))
  60. {
  61. sqlTxt.Append($" where c.id='{id}'");
  62. }
  63. List<BizUsers> bizUsers = new();
  64. await foreach (var items in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryIterator<BizUsers>(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BizUsers") }))
  65. {
  66. bizUsers.Add(items);
  67. }
  68. return Ok(new { state = RespondCode.Ok, bizUsers });
  69. }
  70. /// <summary>
  71. /// 用户信息企业信息
  72. /// </summary>
  73. /// <param name="bizUsers"></param>
  74. /// <param name="site"></param>
  75. /// <returns></returns>
  76. [ProducesDefaultResponseType]
  77. [AuthToken(Roles = "admin,rdc,assist")]
  78. [HttpPost("set-info")]
  79. public async Task<IActionResult> SetInfos([FromBody] BizUsers bizUsers, [FromHeader] string site)
  80. {
  81. var cosmosClient = _azureCosmos.GetCosmosClient();
  82. var tableClient = _azureStorage.GetCloudTableClient();
  83. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  84. if ($"{site}".Equals(BIConst.Global))
  85. {
  86. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  87. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  88. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  89. }
  90. var (tmdId, tmdName, pic, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  91. StringBuilder strMsg = new($"{tmdName}[{tmdId}]操作:");
  92. string salt = Utils.CreatSaltString(8);
  93. string type = "";
  94. if (string.IsNullOrEmpty(bizUsers.id))
  95. {
  96. bizUsers.id = Guid.NewGuid().ToString();
  97. bizUsers.code = "BizUsers";
  98. bizUsers.name = string.IsNullOrEmpty(bizUsers.name)? bizUsers.mobile.ToString(): bizUsers.name;
  99. bizUsers.salt = salt;
  100. bizUsers.pwd = string.IsNullOrEmpty(bizUsers.pwd) ? Utils.HashedPassword(bizUsers.mobile.ToString(), salt) : Utils.HashedPassword(bizUsers.pwd, salt);
  101. bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<BizUsers>(bizUsers, new PartitionKey("BizUsers"));
  102. strMsg.Append($"{bizUsers.name}【{bizUsers.id}】新增第三方用户信息基础信息。");
  103. type = "bizuser-add";
  104. }
  105. else
  106. {
  107. var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(bizUsers.id, new PartitionKey("BizUsers"));
  108. if (response.Status == RespondCode.Ok)
  109. {
  110. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  111. BizUsers tempbizUsers = json.ToObject<BizUsers>();
  112. bizUsers.pk = "BizUsers";
  113. bizUsers.code = "BizUsers";
  114. bizUsers.ttl = -1;
  115. bizUsers.relation = tempbizUsers.relation;
  116. bizUsers.salt = tempbizUsers.salt;
  117. bizUsers.pwd = tempbizUsers.pwd;
  118. bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizUsers>(bizUsers, bizUsers.id, new PartitionKey("BizUsers"));
  119. strMsg.Append($"{bizUsers.name}【{bizUsers.id}】修改第三方用户信息基础信息。");
  120. type = "bizuser-add";
  121. }
  122. else Ok(new { state = RespondCode.NotFound ,msg="未找到id用户。"});
  123. }
  124. //保存操作记录
  125. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, type, strMsg.ToString(), _dingDing, httpContext: HttpContext);
  126. return Ok(new { state = RespondCode.Ok, bizUsers });
  127. }
  128. /// <summary>
  129. /// 通过手机号查询信息
  130. /// </summary>
  131. /// <param name="jsonElement"></param>
  132. /// <returns></returns>
  133. [ProducesDefaultResponseType]
  134. [AuthToken(Roles = "admin,rdc,assist")]
  135. [HttpPost("get-mobileuser")]
  136. public async Task<IActionResult> GetMobileUser(JsonElement jsonElement)
  137. {
  138. var cosmosClient = _azureCosmos.GetCosmosClient();
  139. if (!jsonElement.TryGetProperty("mobile", out JsonElement mobile)) return BadRequest();
  140. string sql = $"select value(c) from c where c.mobile ='{mobile}'";
  141. List<BizUsers> bizUsers = new();
  142. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS,"Normal").GetItemQueryIterator<BizUsers>(queryText:sql,requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("BizUsers") }))
  143. {
  144. bizUsers.Add(item);
  145. }
  146. CoreUser coreUser = null;
  147. if (bizUsers.Count <= 0)
  148. {
  149. coreUser = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", $"{mobile}" } }, _option.Location, _configuration);
  150. }
  151. return Ok(new { state = RespondCode.Ok, bizUsers, coreUser });
  152. }
  153. /// <summary>
  154. /// 重置密码
  155. /// </summary>
  156. /// <param name="jsonElement"></param>
  157. /// <returns></returns>
  158. [ProducesDefaultResponseType]
  159. [AuthToken(Roles = "admin,rdc,assist")]
  160. [HttpPost("reset-pwd")]
  161. public async Task<IActionResult> ResetPassWord(JsonElement jsonElement)
  162. {
  163. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  164. //jsonElement.TryGetProperty("site", out JsonElement site); //分开部署,就不需要,一站多用时,取消注释
  165. var (tmdId, tmdName, pic, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  166. var cosmosClient = _azureCosmos.GetCosmosClient();
  167. var tableClient = _azureStorage.GetCloudTableClient();
  168. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  169. ////分开部署,就不需要,一站多用时,取消注释
  170. //if ($"{site}".Equals(BIConst.Global))
  171. //{
  172. // cosmosClient = _azureCosmos.GetCosmosClient(BIConst.Global);
  173. // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  174. // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  175. //}
  176. BizUsers bizUsers = new();
  177. StringBuilder strMsg = new($"{tmdName}[{tmdId}]操作:");
  178. string salt = Utils.CreatSaltString(8);
  179. var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync($"{id}", new PartitionKey("BizUsers"));
  180. if (response.Status == 200)
  181. {
  182. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  183. bizUsers = json.ToObject<BizUsers>();
  184. bizUsers.salt = salt;
  185. bizUsers.pwd = Utils.HashedPassword(bizUsers.mobile.ToString(), salt);
  186. strMsg.Append($"重置{bizUsers.name}【{bizUsers.id}】的密码,重置成功!");
  187. bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizUsers>(bizUsers, bizUsers.id, new PartitionKey("BizUsers"));
  188. }else return Ok(new { state = RespondCode.NotFound, msg="未找到该用户信息" });
  189. //保存操作记录
  190. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "bizuser-reset", strMsg.ToString(), _dingDing, httpContext: HttpContext);
  191. return Ok(new { state = RespondCode.Ok, bizUsers });
  192. }
  193. /// <summary>
  194. /// 用户关联/移除企业信息
  195. /// </summary>
  196. /// <param name="jsonElement"></param>
  197. /// <returns></returns>
  198. [ProducesDefaultResponseType]
  199. [AuthToken(Roles = "admin,rdc,assist")]
  200. [HttpPost("rel-biz")]
  201. public async Task<ActionResult> RelationBusiness(JsonElement jsonElement)
  202. {
  203. if(!jsonElement.TryGetProperty("id", out JsonElement _id)) return BadRequest();
  204. if (!jsonElement.TryGetProperty("bizs", out JsonElement _bizs)) return BadRequest();
  205. if (!jsonElement.TryGetProperty("type", out JsonElement type)) return BadRequest();
  206. if (!jsonElement.TryGetProperty("userType", out JsonElement userType)) return BadRequest();
  207. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  208. var (tmdId, tmdName, pic, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  209. List<BizRel> bizRels = _bizs.ToObject<List<BizRel>>();
  210. var cosmosClient = _azureCosmos.GetCosmosClient();
  211. var tableClient = _azureStorage.GetCloudTableClient();
  212. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  213. ////分开部署,就不需要,一站多用时,取消注释
  214. //if ($"{site}".Equals(BIConst.Global))
  215. //{
  216. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  217. // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  218. // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  219. //}
  220. string id = $"{_id}";
  221. StringBuilder strMsg = new($"{tmdName}[{tmdId}]给");
  222. BizUsers bizUsers = new();
  223. List<BizRel> noBizRel = new();
  224. if ($"{userType}".Equals("tmdId"))
  225. {
  226. var coreUser = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", $"{id}" } }, _option.Location, _configuration);
  227. string salt = Utils.CreatSaltString(8);
  228. bizUsers = new()
  229. {
  230. id = Guid.NewGuid().ToString(),
  231. mobile = long.Parse($"{coreUser.mobile}"),
  232. tmdId = coreUser.id,
  233. code = "BizUsers",
  234. name = string.IsNullOrEmpty(bizUsers.name) ? bizUsers.mobile.ToString() : bizUsers.name,
  235. salt = salt,
  236. pwd = string.IsNullOrEmpty(bizUsers.pwd) ? Utils.HashedPassword(bizUsers.mobile.ToString(), salt) : Utils.HashedPassword(bizUsers.pwd, salt),
  237. };
  238. id = bizUsers.id;
  239. }
  240. if ($"{type}".Equals("add"))
  241. {
  242. strMsg.Append($"用户【{id}】关联企业信息:添加企业:");
  243. }
  244. else if ($"{type}".Equals("del"))
  245. {
  246. strMsg.Append($"用户【{id}】移除企业信息,移除企业:");
  247. }
  248. else { return Ok(new { state = RespondCode.ParamsError, msg = "类型错误" }); }
  249. var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync($"{id}", new PartitionKey("BizUsers"));
  250. if (response.Status == RespondCode.Ok)
  251. {
  252. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  253. bizUsers = json.ToObject<BizUsers>();
  254. }
  255. foreach (var item in bizRels)
  256. {
  257. var temp = bizUsers.relation.Find(f => f.bizId == item.bizId);
  258. if ($"{type}".Equals("add"))
  259. {
  260. if (temp != null)
  261. noBizRel.Add(item);
  262. else
  263. {
  264. bizUsers.relation.Add(item);
  265. strMsg.Append($"{item.name}[{item.bizId}]|");
  266. }
  267. }
  268. else if ($"{type}".Equals("del"))
  269. {
  270. if (temp != null)
  271. {
  272. bizUsers.relation.Add(item);
  273. strMsg.Append($"{item.name}[{item.bizId}]|");
  274. }
  275. else
  276. noBizRel.Add(item);
  277. }
  278. }
  279. if (response.Status == RespondCode.Ok)
  280. {
  281. bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizUsers>(bizUsers, bizUsers.id, new PartitionKey("BizUsers"));
  282. }
  283. else
  284. {
  285. bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<BizUsers>(bizUsers, new PartitionKey("BizUsers"));
  286. }
  287. //保存操作记录
  288. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "bizconfig-addSchool", strMsg.ToString(), _dingDing, httpContext: HttpContext);
  289. if (noBizRel.Count > 0)
  290. return Ok(new { state = RespondCode.Created, bizUsers, noBizRel });
  291. return Ok(new { state = RespondCode.Ok, bizUsers });
  292. }
  293. /// <summary>
  294. /// 通过企业Id查询用户信息
  295. /// </summary>
  296. /// <param name="jsonElement"></param>
  297. /// <returns></returns>
  298. [ProducesDefaultResponseType]
  299. [HttpPost("get-bizid")]
  300. public async Task<IActionResult> GetBizIdUsers(JsonElement jsonElement)
  301. {
  302. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  303. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  304. var cosmosClient = _azureCosmos.GetCosmosClient();
  305. ////分开部署,就不需要,一站多用时,取消注释
  306. //if ($"{site}".Equals(BIConst.Global))
  307. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  308. List<BizUsers> businessUsers = new();
  309. string sql = $"select value(c) from c join s in c.relation where c.code='BizUsers' and s.bizId = '{id}'";
  310. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryIterator<BizUsers>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BizUsers") }))
  311. {
  312. businessUsers.Add(item);
  313. }
  314. return Ok(new { state = RespondCode.Ok, businessUsers });
  315. }
  316. }
  317. }