CompanyUserController.cs 12 KB

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