BusinessUsersController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using Microsoft.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").GetItemQueryIteratorSql<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.StatusCode == System.Net.HttpStatusCode.OK)
  109. {
  110. using var json = await JsonDocument.ParseAsync(response.Content);
  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").GetItemQueryIteratorSql<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.StatusCode == System.Net.HttpStatusCode.OK)
  181. {
  182. using var json = await JsonDocument.ParseAsync(response.Content);
  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. jsonElement.TryGetProperty("userType", out JsonElement _userType);
  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. string userType = "default";
  222. if (!string.IsNullOrWhiteSpace($"{_userType}"))
  223. {
  224. userType = $"{_userType}";
  225. }
  226. StringBuilder strMsg = new($"{tmdName}[{tmdId}]给");
  227. BizUsers bizUsers = new();
  228. List<BizRel> noBizRel = new();
  229. if ($"{userType}".Equals("tmdId"))
  230. {
  231. var coreUser = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", $"{id}" } }, _option.Location, _configuration);
  232. string salt = Utils.CreatSaltString(8);
  233. bizUsers = new()
  234. {
  235. id = Guid.NewGuid().ToString(),
  236. mobile = long.Parse($"{coreUser.mobile}"),
  237. name = coreUser.name,
  238. tmdId = coreUser.id,
  239. picture = coreUser.picture,
  240. mail = coreUser.mail,
  241. code = "BizUsers",
  242. salt = salt,
  243. pwd = string.IsNullOrEmpty(bizUsers.pwd) ? Utils.HashedPassword(bizUsers.mobile.ToString(), salt) : Utils.HashedPassword(bizUsers.pwd, salt),
  244. };
  245. id = bizUsers.id;
  246. }
  247. if ($"{type}".Equals("add"))
  248. {
  249. strMsg.Append($"用户【{id}】关联企业信息:添加企业:");
  250. }
  251. else if ($"{type}".Equals("del"))
  252. {
  253. strMsg.Append($"用户【{id}】移除企业信息,移除企业:");
  254. }
  255. else { return Ok(new { state = RespondCode.ParamsError, msg = "类型错误" }); }
  256. var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync($"{id}", new PartitionKey("BizUsers"));
  257. if (response.StatusCode == System.Net.HttpStatusCode.OK)
  258. {
  259. using var json = await JsonDocument.ParseAsync(response.Content);
  260. bizUsers = json.ToObject<BizUsers>();
  261. }
  262. foreach (var item in bizRels)
  263. {
  264. var temp = bizUsers.relation.Find(f => f.bizId == item.bizId);
  265. if ($"{type}".Equals("add"))
  266. {
  267. if (temp != null)
  268. noBizRel.Add(item);
  269. else
  270. {
  271. bizUsers.relation.Add(item);
  272. strMsg.Append($"{item.name}[{item.bizId}]|");
  273. }
  274. }
  275. else if ($"{type}".Equals("del"))
  276. {
  277. if (temp != null)
  278. {
  279. bizUsers.relation.Remove(temp);
  280. strMsg.Append($"{item.name}[{item.bizId}]|");
  281. }
  282. else
  283. noBizRel.Add(item);
  284. }
  285. }
  286. if (response.StatusCode == System.Net.HttpStatusCode.OK)
  287. {
  288. bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizUsers>(bizUsers, bizUsers.id, new PartitionKey("BizUsers"));
  289. }
  290. else
  291. {
  292. bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<BizUsers>(bizUsers, new PartitionKey("BizUsers"));
  293. }
  294. //保存操作记录
  295. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "bizconfig-addSchool", strMsg.ToString(), _dingDing, httpContext: HttpContext);
  296. if (noBizRel.Count > 0)
  297. return Ok(new { state = RespondCode.Created, bizUsers, noBizRel });
  298. return Ok(new { state = RespondCode.Ok, bizUsers });
  299. }
  300. /// <summary>
  301. /// 通过企业Id查询用户信息
  302. /// </summary>
  303. /// <param name="jsonElement"></param>
  304. /// <returns></returns>
  305. [ProducesDefaultResponseType]
  306. [HttpPost("get-bizid")]
  307. public async Task<IActionResult> GetBizIdUsers(JsonElement jsonElement)
  308. {
  309. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  310. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  311. var cosmosClient = _azureCosmos.GetCosmosClient();
  312. ////分开部署,就不需要,一站多用时,取消注释
  313. //if ($"{site}".Equals(BIConst.Global))
  314. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  315. List<BizUsers> businessUsers = new();
  316. string sql = $"select value(c) from c join s in c.relation where c.code='BizUsers' and s.bizId = '{id}'";
  317. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryIteratorSql<BizUsers>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BizUsers") }))
  318. {
  319. businessUsers.Add(item);
  320. }
  321. return Ok(new { state = RespondCode.Ok, businessUsers });
  322. }
  323. }
  324. }