CompanyUserController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Options;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. using TEAMModelBI.Models;
  9. using TEAMModelOS.Models;
  10. using TEAMModelOS.SDK.Context.BI;
  11. using TEAMModelOS.SDK.Context.Constant;
  12. using TEAMModelOS.SDK.DI;
  13. using TEAMModelOS.SDK.Extension;
  14. using TEAMModelOS.SDK.Models.Table;
  15. namespace TEAMModelBI.Controllers.BITable
  16. {
  17. [Route("bizuser")]
  18. [ApiController]
  19. public class CompanyUserController : ControllerBase
  20. {
  21. public readonly AzureCosmosFactory _azureCosmos;
  22. public readonly AzureStorageFactory _azureStorage;
  23. public readonly DingDing _dingDing;
  24. public readonly Option _option;
  25. public CompanyUserController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  26. {
  27. _azureCosmos = azureCosmos;
  28. _azureStorage = azureStorage;
  29. _dingDing = dingDing;
  30. _option = option?.Value;
  31. }
  32. /// <summary>
  33. /// 新增和修改第三方用户信息
  34. /// </summary>
  35. /// <param name="bizUser"></param>
  36. /// <param name="site"></param>
  37. /// <returns></returns>
  38. [ProducesDefaultResponseType]
  39. [HttpPost("set-bizuser")]
  40. public async Task<IActionResult> SetBizUser([FromBody] BusinessUser bizUser, [FromHeader] string site)
  41. {
  42. var tableClient = _azureStorage.GetCloudTableClient();
  43. if ($"{site}".Equals(BIConst.Global))
  44. {
  45. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  46. }
  47. var table = tableClient.GetTableReference("IESOpenApi");
  48. if (bizUser.RowKey != null)
  49. {
  50. BusinessUser tempUser = table.Get<BusinessUser>(partitionKey: "BusinessUser", rowKey: $"{bizUser.RowKey}");
  51. if (tempUser != null)
  52. {
  53. bizUser.PartitionKey = "BusinessUser";
  54. bizUser = await table.SaveOrUpdate<BusinessUser>(bizUser);
  55. }
  56. else
  57. {
  58. bizUser.PartitionKey = "BusinessUser";
  59. bizUser.RowKey = Guid.NewGuid().ToString();
  60. string salt = Utils.CreatSaltString(8);
  61. bizUser.salt = string.IsNullOrEmpty(bizUser.pwd) ? Utils.HashedPassword(bizUser.mobile, salt) : Utils.HashedPassword(bizUser.pwd, salt);
  62. bizUser = await table.SaveOrUpdate<BusinessUser>(bizUser);
  63. }
  64. }
  65. else
  66. {
  67. bizUser.PartitionKey = "BusinessUser";
  68. bizUser.RowKey = Guid.NewGuid().ToString();
  69. string salt = Utils.CreatSaltString(8);
  70. bizUser.salt = string.IsNullOrEmpty(bizUser.pwd) ? Utils.HashedPassword(bizUser.mobile, salt) : Utils.HashedPassword(bizUser.pwd, salt);
  71. bizUser = await table.SaveOrUpdate<BusinessUser>(bizUser);
  72. }
  73. return Ok(new { state = 200, bizUser });
  74. }
  75. /// <summary>
  76. /// 重置密码
  77. /// </summary>
  78. /// <param name="jsonElement"></param>
  79. /// <returns></returns>
  80. [ProducesDefaultResponseType]
  81. [HttpPost("res-pwd")]
  82. public async Task<IActionResult> ResetPwd(JsonElement jsonElement)
  83. {
  84. try
  85. {
  86. if (jsonElement.TryGetProperty("mobile", out JsonElement mobile)) return BadRequest();
  87. jsonElement.TryGetProperty("pwd", out JsonElement pwd);
  88. jsonElement.TryGetProperty("site", out JsonElement site);
  89. var tableClient = _azureStorage.GetCloudTableClient();
  90. if ($"{site}".Equals(BIConst.Global))
  91. {
  92. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  93. }
  94. var table = tableClient.GetTableReference("IESOpenApi");
  95. List<BusinessUser> bizUsers = await table.QueryWhereString<BusinessUser>($" PartitionKey eq 'BusinessUser' and mobile eq '{mobile}'");
  96. string salt = Utils.CreatSaltString(8);
  97. string resPwd = string.IsNullOrEmpty($"{pwd}") ? Utils.HashedPassword($"{mobile}", salt) : Utils.HashedPassword($"{pwd}", salt);
  98. foreach (var item in bizUsers)
  99. {
  100. item.salt = salt;
  101. item.pwd = resPwd;
  102. }
  103. bizUsers = await table.SaveOrUpdateAll<BusinessUser>(bizUsers);
  104. //BusinessUser bizUser = table.Get<BusinessUser>(partitionKey: "BusinessUser", rowKey: $"{mobile}");
  105. //bizUser.RowKey = Guid.NewGuid().ToString();
  106. //bizUser.pwd = string.IsNullOrEmpty($"{pwd}") ? Utils.HashedPassword(bizUser.mobile, salt) : Utils.HashedPassword(bizUser.pwd, salt);
  107. //bizUser = await table.SaveOrUpdate<BusinessUser>(bizUser);
  108. return Ok(new { state = 200, bizUsers });
  109. }
  110. catch (Exception e)
  111. {
  112. await _dingDing.SendBotMsg($"open ,{_option.Location} , /business/set-companyschool \n {e.Message}\n{e.StackTrace} \n ", GroupNames.成都开发測試群組);
  113. return BadRequest();
  114. }
  115. }
  116. /// <summary>
  117. /// 查询用户信息
  118. /// </summary>
  119. /// <param name="jsonElement"></param>
  120. /// <returns></returns>
  121. [ProducesDefaultResponseType]
  122. [HttpPost("get-infos")]
  123. public async Task<IActionResult> GetInfos(JsonElement jsonElement)
  124. {
  125. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  126. jsonElement.TryGetProperty("site", out JsonElement site);
  127. var tableClient = _azureStorage.GetCloudTableClient();
  128. if ($"{site}".Equals(BIConst.Global))
  129. {
  130. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  131. }
  132. var table = tableClient.GetTableReference("IESOpenApi");
  133. List<BusinessUser> bizUsers = await table.QueryWhereString<BusinessUser>($" PartitionKey eq 'BusinessUser' and tmdId eq '{tmdId}'");
  134. return Ok(new { state = 200 , bizUsers });
  135. }
  136. /// <summary>
  137. /// 关联用户和企业
  138. /// </summary>
  139. /// <param name="jsonElement"></param>
  140. /// <returns></returns>
  141. [ProducesDefaultResponseType]
  142. [HttpPost("set-userrebiz")]
  143. public async Task<IActionResult> SetUserReBiz(JsonElement jsonElement)
  144. {
  145. if(!jsonElement.TryGetProperty("userRowKey", out JsonElement userRowKey)) return BadRequest();
  146. if (!jsonElement.TryGetProperty("bizRowKey", out JsonElement bizRowKey)) return BadRequest();
  147. if (!jsonElement.TryGetProperty("bizName", out JsonElement bizName)) return BadRequest();
  148. jsonElement.TryGetProperty("roles", out JsonElement roles);
  149. jsonElement.TryGetProperty("site", out JsonElement site);
  150. var tableClient = _azureStorage.GetCloudTableClient();
  151. if ($"{site}".Equals(BIConst.Global))
  152. {
  153. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  154. }
  155. var table = tableClient.GetTableReference("IESOpenApi");
  156. BizRelUser findBizRelUser = table.Get<BizRelUser>("BizRelUser", $"{userRowKey}|{bizRowKey}");
  157. if (findBizRelUser == null)
  158. {
  159. BizRelUser bizRelUser = new() { PartitionKey = "BizRelUser", RowKey = $"{userRowKey}|{bizRowKey}", userId = $"{userRowKey}", bizId = $"{bizRowKey}", bizName = $"{bizName}", roles = string.IsNullOrEmpty($"{roles}") ? "develo" : $"{roles}" };
  160. await table.SaveOrUpdate<BizRelUser>(bizRelUser);
  161. return Ok(new { state = RespondCode.Ok, bizRelUser });
  162. }
  163. else return Ok(new { state = RespondCode.NotFound,msg="该账户已经是该企业的用户" });
  164. }
  165. /// <summary>
  166. /// 获取账户关联的企业信息
  167. /// </summary>
  168. /// <param name="jsonElement"></param>
  169. /// <returns></returns>
  170. [ProducesDefaultResponseType]
  171. [HttpPost("get-userrebizs")]
  172. public async Task<IActionResult> GetUserReBizs(JsonElement jsonElement)
  173. {
  174. jsonElement.TryGetProperty("userId", out JsonElement userId);
  175. jsonElement.TryGetProperty("site", out JsonElement site);
  176. var tableClient = _azureStorage.GetCloudTableClient();
  177. if ($"{site}".Equals(BIConst.Global))
  178. {
  179. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  180. }
  181. var table = tableClient.GetTableReference("IESOpenApi");
  182. List<BizRelUser> bizRelUsers = new();
  183. List<RelBizInfo> relBizInfos = new();
  184. if (!string.IsNullOrEmpty($"{userId}"))
  185. bizRelUsers = await table.QueryWhereString<BizRelUser>($"PartitionKey eq 'BizRelUser' and userId eq '{userId}'");
  186. else
  187. //bizRelUsers = await table.FindListByDict<BizRelUser>(new Dictionary<string, object>() { { "PartitionKey ", "BizRelUser" } });
  188. bizRelUsers = await table.QueryWhereString<BizRelUser>($"PartitionKey eq 'BizRelUser'");
  189. if (bizRelUsers.Count > 0)
  190. {
  191. if (bizRelUsers.Count > 0)
  192. {
  193. foreach (var item in bizRelUsers)
  194. {
  195. BusinessConfig businessConfig = table.Get<BusinessConfig>("BusinessConfig", item.bizId);
  196. RelBizInfo relBizInfo = new()
  197. {
  198. userRowKey = item.userId,
  199. relId = item.RowKey,
  200. bizRowKey = item.bizId,
  201. roles = !string.IsNullOrEmpty($"{item.roles}") ? new List<string>(item.roles.Split(',')) : new List<string>()
  202. };
  203. if (businessConfig != null)
  204. {
  205. relBizInfo.bizName = businessConfig.name;
  206. relBizInfo.bizCredit = businessConfig.credit;
  207. relBizInfo.bizPicture = businessConfig.picture;
  208. relBizInfos.Add(relBizInfo);
  209. }
  210. }
  211. }
  212. }
  213. return Ok(new { state = RespondCode.Ok, relBizInfos });
  214. }
  215. }
  216. }