BusinessUsersController.cs 14 KB

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