123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- using Microsoft.Azure.Cosmos;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelBI.Filter;
- using TEAMModelBI.Tool.Extension;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.Context.BI;
- using TEAMModelOS.SDK.Context.Constant;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models.Cosmos.BI.BINormal;
- namespace TEAMModelBI.Controllers.BINormal
- {
- [Route("bizuser")]
- [ApiController]
- public class BusinessUsersController : ControllerBase
- {
- public readonly AzureCosmosFactory _azureCosmos;
- public readonly AzureStorageFactory _azureStorage;
- public readonly DingDing _dingDing;
- public readonly Option _option;
- //读取配置信息
- private readonly IConfiguration _configuration;
- private readonly CoreAPIHttpService _coreAPIHttpService;
- public BusinessUsersController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, CoreAPIHttpService coreAPIHttpService)
- {
- _azureCosmos = azureCosmos;
- _azureStorage = azureStorage;
- _dingDing = dingDing;
- _option = option?.Value;
- _configuration = configuration;
- _coreAPIHttpService = coreAPIHttpService;
- }
- /// <summary>
- /// 获取第三方用户信息
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-infos")]
- public async Task<IActionResult> GetInfos(JsonElement jsonElement)
- {
- jsonElement.TryGetProperty("id", out JsonElement id);
- //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
- var cosmosClient = _azureCosmos.GetCosmosClient();
- //if ($"{site}".Equals(BIConst.Global))
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- StringBuilder sqlTxt = new("select value(c) from c");
- if (!string.IsNullOrEmpty($"{id}"))
- {
- sqlTxt.Append($" where c.id='{id}'");
- }
- List<BizUsers> bizUsers = new();
- await foreach (var items in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryIteratorSql<BizUsers>(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BizUsers") }))
- {
- bizUsers.Add(items);
- }
- return Ok(new { state = RespondCode.Ok, bizUsers });
- }
- /// <summary>
- /// 用户信息企业信息
- /// </summary>
- /// <param name="bizUsers"></param>
- /// <param name="site"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin,rdc,assist")]
- [HttpPost("set-info")]
- public async Task<IActionResult> SetInfos([FromBody] BizUsers bizUsers, [FromHeader] string site)
- {
- var cosmosClient = _azureCosmos.GetCosmosClient();
- var tableClient = _azureStorage.GetCloudTableClient();
- var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
- if ($"{site}".Equals(BIConst.Global))
- {
- cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
- blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
- }
- var (tmdId, tmdName, pic, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
- StringBuilder strMsg = new($"{tmdName}[{tmdId}]操作:");
- string salt = Utils.CreatSaltString(8);
- string type = "";
- if (string.IsNullOrEmpty(bizUsers.id))
- {
- bizUsers.id = Guid.NewGuid().ToString();
- bizUsers.code = "BizUsers";
- bizUsers.name = string.IsNullOrEmpty(bizUsers.name)? bizUsers.mobile.ToString(): bizUsers.name;
- bizUsers.salt = salt;
- bizUsers.pwd = string.IsNullOrEmpty(bizUsers.pwd) ? Utils.HashedPassword(bizUsers.mobile.ToString(), salt) : Utils.HashedPassword(bizUsers.pwd, salt);
-
- bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<BizUsers>(bizUsers, new PartitionKey("BizUsers"));
- strMsg.Append($"{bizUsers.name}【{bizUsers.id}】新增第三方用户信息基础信息。");
- type = "bizuser-add";
- }
- else
- {
- var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(bizUsers.id, new PartitionKey("BizUsers"));
- if (response.StatusCode == System.Net.HttpStatusCode.OK)
- {
- using var json = await JsonDocument.ParseAsync(response.Content);
- BizUsers tempbizUsers = json.ToObject<BizUsers>();
- bizUsers.pk = "BizUsers";
- bizUsers.code = "BizUsers";
- bizUsers.ttl = -1;
- bizUsers.relation = tempbizUsers.relation;
- bizUsers.salt = tempbizUsers.salt;
- bizUsers.pwd = tempbizUsers.pwd;
- bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizUsers>(bizUsers, bizUsers.id, new PartitionKey("BizUsers"));
- strMsg.Append($"{bizUsers.name}【{bizUsers.id}】修改第三方用户信息基础信息。");
- type = "bizuser-add";
- }
- else Ok(new { state = RespondCode.NotFound ,msg="未找到id用户。"});
- }
- //保存操作记录
- await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, type, strMsg.ToString(), _dingDing, httpContext: HttpContext);
- return Ok(new { state = RespondCode.Ok, bizUsers });
- }
- /// <summary>
- /// 通过手机号查询信息
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin,rdc,assist")]
- [HttpPost("get-mobileuser")]
- public async Task<IActionResult> GetMobileUser(JsonElement jsonElement)
- {
- var cosmosClient = _azureCosmos.GetCosmosClient();
- if (!jsonElement.TryGetProperty("mobile", out JsonElement mobile)) return BadRequest();
- string sql = $"select value(c) from c where c.mobile ={mobile}";
- List<BizUsers> bizUsers = new();
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS,"Normal").GetItemQueryIteratorSql<BizUsers>(queryText:sql,requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("BizUsers") }))
- {
- bizUsers.Add(item);
- }
- CoreUser coreUser = null;
- if (bizUsers.Count <= 0)
- {
- coreUser = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", $"{mobile}" } }, _option.Location, _configuration);
- }
- return Ok(new { state = RespondCode.Ok, bizUsers, coreUser });
- }
- /// <summary>
- /// 重置密码
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin,rdc,assist")]
- [HttpPost("reset-pwd")]
- public async Task<IActionResult> ResetPassWord(JsonElement jsonElement)
- {
- if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
- //jsonElement.TryGetProperty("site", out JsonElement site); //分开部署,就不需要,一站多用时,取消注释
- var (tmdId, tmdName, pic, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
- var cosmosClient = _azureCosmos.GetCosmosClient();
- var tableClient = _azureStorage.GetCloudTableClient();
- var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
- ////分开部署,就不需要,一站多用时,取消注释
- //if ($"{site}".Equals(BIConst.Global))
- //{
- // cosmosClient = _azureCosmos.GetCosmosClient(BIConst.Global);
- // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
- // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
- //}
- BizUsers bizUsers = new();
- StringBuilder strMsg = new($"{tmdName}[{tmdId}]操作:");
- string salt = Utils.CreatSaltString(8);
- var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync($"{id}", new PartitionKey("BizUsers"));
- if (response.StatusCode == System.Net.HttpStatusCode.OK)
- {
- using var json = await JsonDocument.ParseAsync(response.Content);
- bizUsers = json.ToObject<BizUsers>();
- bizUsers.salt = salt;
- bizUsers.pwd = Utils.HashedPassword(bizUsers.mobile.ToString(), salt);
- strMsg.Append($"重置{bizUsers.name}【{bizUsers.id}】的密码,重置成功!");
- bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizUsers>(bizUsers, bizUsers.id, new PartitionKey("BizUsers"));
- }else return Ok(new { state = RespondCode.NotFound, msg="未找到该用户信息" });
- //保存操作记录
- await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "bizuser-reset", strMsg.ToString(), _dingDing, httpContext: HttpContext);
- return Ok(new { state = RespondCode.Ok, bizUsers });
- }
- /// <summary>
- /// 用户关联/移除企业信息
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin,rdc,assist")]
- [HttpPost("rel-biz")]
- public async Task<ActionResult> RelationBusiness(JsonElement jsonElement)
- {
- if(!jsonElement.TryGetProperty("id", out JsonElement _id)) return BadRequest();
- if (!jsonElement.TryGetProperty("bizs", out JsonElement _bizs)) return BadRequest();
- if (!jsonElement.TryGetProperty("type", out JsonElement type)) return BadRequest();
- jsonElement.TryGetProperty("userType", out JsonElement _userType);
- //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
- var (tmdId, tmdName, pic, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
- List<BizRel> bizRels = _bizs.ToObject<List<BizRel>>();
- var cosmosClient = _azureCosmos.GetCosmosClient();
- var tableClient = _azureStorage.GetCloudTableClient();
- var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
- ////分开部署,就不需要,一站多用时,取消注释
- //if ($"{site}".Equals(BIConst.Global))
- //{
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
- // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
- //}
- string id = $"{_id}";
- string userType = "default";
- if (!string.IsNullOrWhiteSpace($"{_userType}"))
- {
- userType = $"{_userType}";
- }
-
- StringBuilder strMsg = new($"{tmdName}[{tmdId}]给");
- BizUsers bizUsers = new();
- List<BizRel> noBizRel = new();
- if ($"{userType}".Equals("tmdId"))
- {
- var coreUser = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", $"{id}" } }, _option.Location, _configuration);
- string salt = Utils.CreatSaltString(8);
- bizUsers = new()
- {
- id = Guid.NewGuid().ToString(),
- mobile = long.Parse($"{coreUser.mobile}"),
- name = coreUser.name,
- tmdId = coreUser.id,
- picture = coreUser.picture,
- mail = coreUser.mail,
- code = "BizUsers",
- salt = salt,
- pwd = string.IsNullOrEmpty(bizUsers.pwd) ? Utils.HashedPassword(bizUsers.mobile.ToString(), salt) : Utils.HashedPassword(bizUsers.pwd, salt),
- };
- id = bizUsers.id;
- }
- if ($"{type}".Equals("add"))
- {
- strMsg.Append($"用户【{id}】关联企业信息:添加企业:");
- }
- else if ($"{type}".Equals("del"))
- {
- strMsg.Append($"用户【{id}】移除企业信息,移除企业:");
- }
- else { return Ok(new { state = RespondCode.ParamsError, msg = "类型错误" }); }
- var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync($"{id}", new PartitionKey("BizUsers"));
- if (response.StatusCode == System.Net.HttpStatusCode.OK)
- {
- using var json = await JsonDocument.ParseAsync(response.Content);
- bizUsers = json.ToObject<BizUsers>();
- }
- foreach (var item in bizRels)
- {
- var temp = bizUsers.relation.Find(f => f.bizId == item.bizId);
- if ($"{type}".Equals("add"))
- {
- if (temp != null)
- noBizRel.Add(item);
- else
- {
- bizUsers.relation.Add(item);
- strMsg.Append($"{item.name}[{item.bizId}]|");
- }
- }
- else if ($"{type}".Equals("del"))
- {
- if (temp != null)
- {
- bizUsers.relation.Remove(temp);
- strMsg.Append($"{item.name}[{item.bizId}]|");
- }
- else
- noBizRel.Add(item);
- }
- }
- if (response.StatusCode == System.Net.HttpStatusCode.OK)
- {
- bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizUsers>(bizUsers, bizUsers.id, new PartitionKey("BizUsers"));
- }
- else
- {
- bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<BizUsers>(bizUsers, new PartitionKey("BizUsers"));
- }
- //保存操作记录
- await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "bizconfig-addSchool", strMsg.ToString(), _dingDing, httpContext: HttpContext);
- if (noBizRel.Count > 0)
- return Ok(new { state = RespondCode.Created, bizUsers, noBizRel });
- return Ok(new { state = RespondCode.Ok, bizUsers });
- }
- /// <summary>
- /// 通过企业Id查询用户信息
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-bizid")]
- public async Task<IActionResult> GetBizIdUsers(JsonElement jsonElement)
- {
- if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
- //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
- var cosmosClient = _azureCosmos.GetCosmosClient();
- ////分开部署,就不需要,一站多用时,取消注释
- //if ($"{site}".Equals(BIConst.Global))
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- List<BizUsers> businessUsers = new();
- string sql = $"select value(c) from c join s in c.relation where c.code='BizUsers' and s.bizId = '{id}'";
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryIteratorSql<BizUsers>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BizUsers") }))
- {
- businessUsers.Add(item);
- }
- return Ok(new { state = RespondCode.Ok, businessUsers });
- }
- }
- }
|