123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelBI.Models;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK.Context.BI;
- using TEAMModelOS.SDK.Context.Constant;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models.Table;
- namespace TEAMModelBI.Controllers.BITable
- {
- [Route("bizuser")]
- [ApiController]
- public class CompanyUserController : ControllerBase
- {
- public readonly AzureCosmosFactory _azureCosmos;
- public readonly AzureStorageFactory _azureStorage;
- public readonly DingDing _dingDing;
- public readonly Option _option;
- public CompanyUserController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
- {
- _azureCosmos = azureCosmos;
- _azureStorage = azureStorage;
- _dingDing = dingDing;
- _option = option?.Value;
- }
- /// <summary>
- /// 新增和修改第三方用户信息
- /// </summary>
- /// <param name="bizUser"></param>
- /// <param name="site"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("set-bizuser")]
- public async Task<IActionResult> SetBizUser([FromBody] BusinessUser bizUser, [FromHeader] string site)
- {
- var tableClient = _azureStorage.GetCloudTableClient();
- if ($"{site}".Equals(BIConst.Global))
- {
- tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
- }
- var table = tableClient.GetTableReference("IESOpenApi");
- if (bizUser.RowKey != null)
- {
- BusinessUser tempUser = table.Get<BusinessUser>(partitionKey: "BusinessUser", rowKey: $"{bizUser.RowKey}");
- if (tempUser != null)
- {
- bizUser.PartitionKey = "BusinessUser";
- bizUser = await table.SaveOrUpdate<BusinessUser>(bizUser);
- }
- else
- {
- bizUser.PartitionKey = "BusinessUser";
- bizUser.RowKey = Guid.NewGuid().ToString();
- string salt = Utils.CreatSaltString(8);
- bizUser.salt = string.IsNullOrEmpty(bizUser.pwd) ? Utils.HashedPassword(bizUser.mobile, salt) : Utils.HashedPassword(bizUser.pwd, salt);
- bizUser = await table.SaveOrUpdate<BusinessUser>(bizUser);
- }
- }
- else
- {
- bizUser.PartitionKey = "BusinessUser";
- bizUser.RowKey = Guid.NewGuid().ToString();
- string salt = Utils.CreatSaltString(8);
- bizUser.salt = string.IsNullOrEmpty(bizUser.pwd) ? Utils.HashedPassword(bizUser.mobile, salt) : Utils.HashedPassword(bizUser.pwd, salt);
- bizUser = await table.SaveOrUpdate<BusinessUser>(bizUser);
- }
- return Ok(new { state = 200, bizUser });
- }
- /// <summary>
- /// 重置密码
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("res-pwd")]
- public async Task<IActionResult> ResetPwd(JsonElement jsonElement)
- {
- try
- {
- if (jsonElement.TryGetProperty("mobile", out JsonElement mobile)) return BadRequest();
- jsonElement.TryGetProperty("pwd", out JsonElement pwd);
- jsonElement.TryGetProperty("site", out JsonElement site);
- var tableClient = _azureStorage.GetCloudTableClient();
- if ($"{site}".Equals(BIConst.Global))
- {
- tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
- }
- var table = tableClient.GetTableReference("IESOpenApi");
- List<BusinessUser> bizUsers = await table.QueryWhereString<BusinessUser>($" PartitionKey eq 'BusinessUser' and mobile eq '{mobile}'");
- string salt = Utils.CreatSaltString(8);
- string resPwd = string.IsNullOrEmpty($"{pwd}") ? Utils.HashedPassword($"{mobile}", salt) : Utils.HashedPassword($"{pwd}", salt);
- foreach (var item in bizUsers)
- {
- item.salt = salt;
- item.pwd = resPwd;
- }
- bizUsers = await table.SaveOrUpdateAll<BusinessUser>(bizUsers);
- //BusinessUser bizUser = table.Get<BusinessUser>(partitionKey: "BusinessUser", rowKey: $"{mobile}");
- //bizUser.RowKey = Guid.NewGuid().ToString();
- //bizUser.pwd = string.IsNullOrEmpty($"{pwd}") ? Utils.HashedPassword(bizUser.mobile, salt) : Utils.HashedPassword(bizUser.pwd, salt);
- //bizUser = await table.SaveOrUpdate<BusinessUser>(bizUser);
- return Ok(new { state = 200, bizUsers });
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"open ,{_option.Location} , /business/set-companyschool \n {e.Message}\n{e.StackTrace} \n ", GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 查询用户信息
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-infos")]
- public async Task<IActionResult> GetInfos(JsonElement jsonElement)
- {
- if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
- jsonElement.TryGetProperty("site", out JsonElement site);
- var tableClient = _azureStorage.GetCloudTableClient();
- if ($"{site}".Equals(BIConst.Global))
- {
- tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
- }
- var table = tableClient.GetTableReference("IESOpenApi");
- List<BusinessUser> bizUsers = await table.QueryWhereString<BusinessUser>($" PartitionKey eq 'BusinessUser' and tmdId eq '{tmdId}'");
- return Ok(new { state = 200 , bizUsers });
- }
- /// <summary>
- /// 关联用户和企业
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("set-userrebiz")]
- public async Task<IActionResult> SetUserReBiz(JsonElement jsonElement)
- {
- if(!jsonElement.TryGetProperty("userRowKey", out JsonElement userRowKey)) return BadRequest();
- if (!jsonElement.TryGetProperty("bizRowKey", out JsonElement bizRowKey)) return BadRequest();
- if (!jsonElement.TryGetProperty("bizName", out JsonElement bizName)) return BadRequest();
- jsonElement.TryGetProperty("roles", out JsonElement roles);
- jsonElement.TryGetProperty("site", out JsonElement site);
- var tableClient = _azureStorage.GetCloudTableClient();
- if ($"{site}".Equals(BIConst.Global))
- {
- tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
- }
- var table = tableClient.GetTableReference("IESOpenApi");
- BizRelUser findBizRelUser = table.Get<BizRelUser>("BizRelUser", $"{userRowKey}|{bizRowKey}");
- if (findBizRelUser == null)
- {
- BizRelUser bizRelUser = new() { PartitionKey = "BizRelUser", RowKey = $"{userRowKey}|{bizRowKey}", userId = $"{userRowKey}", bizId = $"{bizRowKey}", bizName = $"{bizName}", roles = string.IsNullOrEmpty($"{roles}") ? "develo" : $"{roles}" };
- await table.SaveOrUpdate<BizRelUser>(bizRelUser);
- return Ok(new { state = RespondCode.Ok, bizRelUser });
- }
- else return Ok(new { state = RespondCode.NotFound,msg="该账户已经是该企业的用户" });
- }
- /// <summary>
- /// 获取账户关联的企业信息
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-userrebizs")]
- public async Task<IActionResult> GetUserReBizs(JsonElement jsonElement)
- {
- jsonElement.TryGetProperty("userId", out JsonElement userId);
- jsonElement.TryGetProperty("site", out JsonElement site);
- var tableClient = _azureStorage.GetCloudTableClient();
- if ($"{site}".Equals(BIConst.Global))
- {
- tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
- }
- var table = tableClient.GetTableReference("IESOpenApi");
- List<BizRelUser> bizRelUsers = new();
- List<RelBizInfo> relBizInfos = new();
- if (!string.IsNullOrEmpty($"{userId}"))
- bizRelUsers = await table.QueryWhereString<BizRelUser>($"PartitionKey eq 'BizRelUser' and userId eq '{userId}'");
- else
- //bizRelUsers = await table.FindListByDict<BizRelUser>(new Dictionary<string, object>() { { "PartitionKey ", "BizRelUser" } });
- bizRelUsers = await table.QueryWhereString<BizRelUser>($"PartitionKey eq 'BizRelUser'");
- if (bizRelUsers.Count > 0)
- {
- if (bizRelUsers.Count > 0)
- {
- foreach (var item in bizRelUsers)
- {
- BusinessConfig businessConfig = table.Get<BusinessConfig>("BusinessConfig", item.bizId);
- RelBizInfo relBizInfo = new()
- {
- userRowKey = item.userId,
- relId = item.RowKey,
- bizRowKey = item.bizId,
- roles = !string.IsNullOrEmpty($"{item.roles}") ? new List<string>(item.roles.Split(',')) : new List<string>()
- };
- if (businessConfig != null)
- {
- relBizInfo.bizName = businessConfig.name;
- relBizInfo.bizCredit = businessConfig.credit;
- relBizInfo.bizPicture = businessConfig.picture;
- relBizInfos.Add(relBizInfo);
- }
- }
- }
- }
- return Ok(new { state = RespondCode.Ok, relBizInfos });
- }
- }
- }
|