AppCompanyController.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. using 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.Text;
  10. using System.Text.Json;
  11. using System.Threading.Tasks;
  12. using TEAMModelBI.Filter;
  13. using TEAMModelBI.Models.Extension;
  14. using TEAMModelBI.Tool.Extension;
  15. using TEAMModelOS.Models;
  16. using TEAMModelOS.SDK.Context.BI;
  17. using TEAMModelOS.SDK.DI;
  18. using TEAMModelOS.SDK.Extension;
  19. using TEAMModelOS.SDK.Models.Cosmos.BI;
  20. using TEAMModelOS.SDK.Models.Service;
  21. namespace TEAMModelBI.Controllers.BINormal
  22. {
  23. [Route("appcompany")]
  24. [ApiController]
  25. public class AppCompanyController : ControllerBase
  26. {
  27. public readonly AzureCosmosFactory _azureCosmos;
  28. public readonly AzureStorageFactory _azureStorage;
  29. public readonly DingDing _dingDing;
  30. public readonly Option _option;
  31. private readonly IConfiguration _configuration;
  32. private readonly NotificationService _notificationService;
  33. public AppCompanyController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, NotificationService notificationService)
  34. {
  35. _azureCosmos = azureCosmos;
  36. _azureStorage = azureStorage;
  37. _dingDing = dingDing;
  38. _option = option?.Value;
  39. _configuration = configuration;
  40. _notificationService = notificationService;
  41. }
  42. /// <summary>
  43. /// 查询应用信息
  44. /// </summary>
  45. /// <param name="jsonElement"></param>
  46. /// <returns></returns>
  47. [ProducesDefaultResponseType]
  48. [HttpPost("get-info")]
  49. public async Task<IActionResult> GetInfo(JsonElement jsonElement)
  50. {
  51. jsonElement.TryGetProperty("appId", out JsonElement appId);
  52. jsonElement.TryGetProperty("eid", out JsonElement eid);
  53. jsonElement.TryGetProperty("audit", out JsonElement audit);
  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 c.id,c.pk,c.code,c.name,c.descr,c.picture,c.jwtKey,c.status,c.audit,c.refuseDesc,c.gateways,c.apis,c.webhookDomain,c.webHooks,c.schools from c where c.pk='App'");
  59. if (!string.IsNullOrEmpty($"{appId}"))
  60. {
  61. sqlTxt.Append($" and id='{appId}'");
  62. }
  63. if (!string.IsNullOrEmpty($"{audit}"))
  64. {
  65. sqlTxt.Append($" and audit='{audit}'");
  66. }
  67. List<ReadCompany> appCompanys = new();
  68. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryStreamIterator(queryText: sqlTxt.ToString(), requestOptions: string.IsNullOrEmpty($"{eid}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"App-{eid}") }))
  69. {
  70. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  71. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  72. {
  73. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  74. {
  75. ReadCompany readCompany = new()
  76. {
  77. id = obj.GetProperty("id").GetString(),
  78. pk = obj.GetProperty("pk").GetString(),
  79. code = obj.GetProperty("code").GetString(),
  80. name = obj.GetProperty("name").GetString(),
  81. descr = obj.GetProperty("descr").GetString(),
  82. picture = obj.GetProperty("picture").GetString(),
  83. jwtKey = obj.GetProperty("jwtKey").GetString(),
  84. status = obj.GetProperty("status").GetInt32(),
  85. audit = obj.GetProperty("audit").GetInt32(),
  86. refuseDesc = obj.GetProperty("refuseDesc").GetString(),
  87. gateways = obj.GetProperty("gateways").GetString(),
  88. apis = obj.GetProperty("apis").ToObject<List<AppApiState>>(),
  89. webhookDomain = obj.GetProperty("webhookDomain").GetString(),
  90. webHooks = obj.GetProperty("webHooks").ToObject<List<WebHookState>>(),
  91. schools = obj.GetProperty("schools").ToObject<List<ApplySchool>>()
  92. };
  93. appCompanys.Add(readCompany);
  94. }
  95. }
  96. }
  97. return Ok(new { state = 200, appCompanys });
  98. }
  99. /// <summary>
  100. /// 新增或者修改应用
  101. /// </summary>
  102. /// <param name="appCompany"></param>
  103. /// <returns></returns>
  104. [ProducesDefaultResponseType]
  105. [AuthToken(Roles = "admin,rdc,company")]
  106. [HttpPost("set-info")]
  107. public async Task<IActionResult> SetAppInfo(AppCompany appCompany, [FromHeader] string site)
  108. {
  109. try
  110. {
  111. var (loginId, loginName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  112. var cosmosClient = _azureCosmos.GetCosmosClient();
  113. var tableClient = _azureStorage.GetCloudTableClient();
  114. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  115. if ($"{site}".Equals(BIConst.Global))
  116. {
  117. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  118. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  119. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  120. }
  121. StringBuilder stringBuilder = new($"{loginName}【{loginId}】");
  122. string type = "";
  123. //新建
  124. if (string.IsNullOrEmpty($"{appCompany.id}"))
  125. {
  126. appCompany.id = GenerateRandom.StrRandom(8, large: true, small: true);
  127. appCompany.code = $"App-{appCompany.code}";
  128. appCompany.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  129. appCompany.status = -1;
  130. appCompany.audit = -1;
  131. appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<AppCompany>(appCompany, new PartitionKey(appCompany.code));
  132. stringBuilder.Append($"新增应用,应用ID:{appCompany.id},应用名称:{appCompany.name}");
  133. type = "appCompany-add";
  134. }
  135. //修改
  136. else
  137. {
  138. var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(appCompany.id, new PartitionKey(appCompany.code));
  139. if (response.Status == 200)
  140. {
  141. appCompany.pk = "App";
  142. appCompany.ttl = -1;
  143. appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<AppCompany>(appCompany, appCompany.id, new PartitionKey(appCompany.code));
  144. stringBuilder.Append($"修改应用,应用ID:{appCompany.id},应用名称:{appCompany.name}");
  145. type = "appCompany-update";
  146. }
  147. else return Ok(new { state = 404, msg = "未找到该id相关的企业应用信息" });
  148. }
  149. //保存操作记录
  150. //await _azureStorage.SaveBILog(type, stringBuilder.ToString(), _dingDing, httpContext: HttpContext);
  151. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, type, stringBuilder.ToString(), _dingDing, httpContext: HttpContext);
  152. return Ok(new { state = 200, appCompany });
  153. }
  154. catch (Exception e)
  155. {
  156. await _dingDing.SendBotMsg($"BI,{_option.Location} , /appcompany/set-info \n {e.Message}\n{e.StackTrace} \n ", GroupNames.成都开发測試群組);
  157. return BadRequest();
  158. }
  159. }
  160. /// <summary>
  161. /// 查询未审核的信息
  162. /// </summary>
  163. /// <param name="jsonElement"></param>
  164. /// <returns></returns>
  165. [ProducesDefaultResponseType]
  166. [HttpPost("get-noaudit")]
  167. public async Task<IActionResult> GetNoAudit(JsonElement jsonElement)
  168. {
  169. if(!jsonElement.TryGetProperty("operate", out JsonElement operate)) return BadRequest();
  170. jsonElement.TryGetProperty("site", out JsonElement site);
  171. var cosmosClient = _azureCosmos.GetCosmosClient();
  172. var tableClient = _azureStorage.GetCloudTableClient();
  173. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  174. if ($"{site}".Equals(BIConst.Global))
  175. {
  176. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  177. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  178. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  179. }
  180. StringBuilder sqlTxt = new();
  181. switch (operate.GetString())
  182. {
  183. case "api":
  184. sqlTxt.Append($"SELECT c.id, c.code,c.name,c.pk,c.audit,ARRAY(SELECT VALUE a FROM a in c.apis where a.status = -1) as operate FROM c where c.pk='App' and c.audit=1");
  185. break;
  186. case "school":
  187. sqlTxt.Append($"SELECT c.id, c.code,c.name,c.pk,c.audit,ARRAY(SELECT VALUE a FROM a in c.schools where a.status = -1) as operate FROM c where c.pk='App' and c.audit=1");
  188. break;
  189. default:
  190. sqlTxt.Append($"select c.id,c.code,c.name,c.pk,c.audit from c where c.audit=-1 and c.pk='App'");
  191. break;
  192. }
  193. List<NoAudit> noAudits = new();
  194. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryStreamIterator(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { }))
  195. {
  196. var json = await JsonDocument.ParseAsync(item.ContentStream);
  197. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt16() > 0)
  198. {
  199. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  200. {
  201. NoAudit noAudit = new();
  202. noAudit.id = obj.GetProperty("id").GetString();
  203. noAudit.code = obj.GetProperty("code").GetString();
  204. noAudit.pk = obj.GetProperty("pk").GetString();
  205. noAudit.name = obj.GetProperty("name").GetString();
  206. if (!string.IsNullOrEmpty($"{operate}"))
  207. {
  208. noAudit.operate = obj.GetProperty("operate").ToObject<List<object>>();
  209. }
  210. noAudit.audit = obj.GetProperty("audit").GetInt32();
  211. noAudits.Add(noAudit);
  212. }
  213. }
  214. }
  215. return Ok(new { state = 200, noAudits });
  216. }
  217. /// <summary>
  218. /// 审核应用是否通过
  219. /// </summary>
  220. /// <param name="jsonElement"></param>
  221. /// <returns></returns>
  222. [AuthToken(Roles = "admin,rdc")]
  223. [HttpPost("get-apply")]
  224. public async Task<IActionResult> SetAuditApp(JsonElement jsonElement)
  225. {
  226. try
  227. {
  228. var (loginId, loginName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  229. if (!jsonElement.TryGetProperty("appIds", out JsonElement appIds)) return BadRequest();
  230. if (!jsonElement.TryGetProperty("isAudit", out JsonElement isAudit)) return BadRequest();
  231. jsonElement.TryGetProperty("refuseDesc", out JsonElement refuseDesc);
  232. jsonElement.TryGetProperty("site", out JsonElement site);
  233. var cosmosClient = _azureCosmos.GetCosmosClient();
  234. var tableClient = _azureStorage.GetCloudTableClient();
  235. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  236. if ($"{site}".Equals(BIConst.Global))
  237. {
  238. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  239. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  240. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  241. }
  242. StringBuilder strMsg = new($"{loginName}【{loginId}】");
  243. List<AppIdOrCode> idOrCode = appIds.ToObject<List<AppIdOrCode>>();
  244. List<AppIdOrCode> haveIds = new();
  245. if (idOrCode.Count > 0)
  246. {
  247. foreach (var idCode in idOrCode)
  248. {
  249. AppCompany appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemAsync<AppCompany>(idCode.id, new PartitionKey(idCode.code));
  250. strMsg.Append($"审核应用{appCompany.name}【{appCompany.id}】,审核状态:");
  251. //var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(idCode.id, new PartitionKey(idCode.code));
  252. if (bool.Parse($"{isAudit}") == true)
  253. {
  254. appCompany.audit = 1;
  255. appCompany.jwtKey = JwtAuth.CreateApplyJwtKeyBI(_option.HostName, _option.JwtSecretKey, appCompany);
  256. strMsg.Append("通过。");
  257. }
  258. else
  259. {
  260. appCompany.audit = 0;
  261. appCompany.refuseDesc = $"{refuseDesc}";
  262. strMsg.Append("拒绝通过。");
  263. }
  264. try
  265. {
  266. await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<AppCompany>(appCompany, appCompany.id, new PartitionKey(idCode.code));
  267. }
  268. catch
  269. {
  270. haveIds.Add(idCode);
  271. strMsg.Append($"异常:id:{idCode.id},code:{idCode.code};");
  272. }
  273. }
  274. }
  275. else return Ok(new { state = 404, msg = "appIds参数错误" });
  276. //保存操作记录
  277. //await _azureStorage.SaveBILog("appCompany-update", strMsg.ToString(), _dingDing, httpContext: HttpContext);
  278. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "appCompany-update", strMsg.ToString(), _dingDing, httpContext: HttpContext);
  279. if (haveIds.Count > 0)
  280. return Ok(new { state = 201, msg = "部分应用审核失败!", haveIds });
  281. else return Ok(new { state = 200 });
  282. }
  283. catch (Exception e)
  284. {
  285. await _dingDing.SendBotMsg($"BI,{_option.Location} , /appcompany/get-applyapi \n {e.Message}\n{e.StackTrace} \n ", GroupNames.成都开发測試群組);
  286. return BadRequest();
  287. }
  288. }
  289. /// <summary>
  290. /// 应用申请Api接口信息
  291. /// 审核应用api接口信息
  292. /// </summary>
  293. /// <param name="jsonElement"></param>
  294. /// <returns></returns>
  295. [ProducesDefaultResponseType]
  296. [AuthToken(Roles = "admin,rdc,company")]
  297. [HttpPost("set-applyapi")]
  298. public async Task<IActionResult> SetApplyApi(JsonElement jsonElement)
  299. {
  300. try
  301. {
  302. var (loginId, loginName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  303. if (!jsonElement.TryGetProperty("applyApis", out JsonElement jsApplyApis)) return BadRequest();
  304. if (!jsonElement.TryGetProperty("operate", out JsonElement operate)) return BadRequest();
  305. jsonElement.TryGetProperty("site", out JsonElement site);
  306. StringBuilder strMsg = new($"{loginName}【{loginId}】");
  307. var cosmosClient = _azureCosmos.GetCosmosClient();
  308. var tableClient = _azureStorage.GetCloudTableClient();
  309. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  310. if ($"{site}".Equals(BIConst.Global))
  311. {
  312. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  313. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  314. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  315. }
  316. string bizcode = ""; //消息名称
  317. List<string> sendWhom = new();//消息分发给谁 待完善
  318. List<ApplyApi> applyApis = jsApplyApis.ToObject<List<ApplyApi>>();
  319. List<ApplyApi> haveApi = new(); //存在api接口
  320. Dictionary<string,string> noAudit = new();
  321. foreach (var tempApp in applyApis)
  322. {
  323. AppCompany appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemAsync<AppCompany>($"{tempApp.appId}", new PartitionKey($"{tempApp.appCode}"));
  324. if (appCompany != null || appCompany.audit != -1 || appCompany.audit != 0)
  325. {
  326. switch (operate.GetString())
  327. {
  328. case "apply":
  329. strMsg.Append($"申请:{appCompany.name}【{appCompany.id}】应用的Api:");
  330. if (!jsonElement.TryGetProperty("applyDesc", out JsonElement applyDesc)) return BadRequest();
  331. tempApp.apiIds.ForEach(x =>
  332. {
  333. var strt = appCompany.apis.Find(y => y.no.Equals($"{x}"));
  334. if (strt == null)
  335. {
  336. appCompany.apis.Add(new AppApiState() { no = $"{x}", applyDesc = $"{applyDesc}", status = -1 });
  337. strMsg.Append($"{x},");
  338. }
  339. else haveApi.Add(tempApp);
  340. });
  341. sendWhom.Add(appCompany.id);
  342. bizcode = "applyapi";
  343. if (haveApi.Count > 0) strMsg.Append($"已有存在的api:{haveApi.ToJsonString()}。");
  344. break;
  345. case "audit":
  346. if (!jsonElement.TryGetProperty("isAudit", out JsonElement isAudit)) return BadRequest();
  347. string refuseDesc = "";
  348. if (bool.Parse($"{isAudit}") == false)
  349. {
  350. if (!jsonElement.TryGetProperty("refuseDesc", out JsonElement jsonRefuseDesc)) return BadRequest();
  351. refuseDesc = jsonRefuseDesc.GetString();
  352. }
  353. strMsg.Append($"审核{appCompany.name}【{appCompany.id}】应用的Api:");
  354. tempApp.apiIds.ForEach(x =>
  355. {
  356. var temp = appCompany.apis.Find(n => n.no == x);
  357. if (temp != null)
  358. {
  359. AppApiState appApiState = appCompany.apis.Single(a => a.no == x);
  360. if (bool.Parse($"{isAudit}") == true)
  361. {
  362. appApiState.status = 1;
  363. appApiState.refuseDesc = null;
  364. appCompany.jwtKey = JwtAuth.CreateApplyJwtKeyBI(_option.HostName, _option.JwtSecretKey, appCompany);
  365. strMsg.Append($"{appApiState.no}通过,");
  366. }
  367. else
  368. {
  369. appApiState.status = 0;
  370. appApiState.refuseDesc = $"{refuseDesc}";
  371. strMsg.Append($"{appApiState.no}失败,");
  372. }
  373. }
  374. else haveApi.Add(tempApp);
  375. });
  376. if (haveApi.Count > 0) strMsg.Append($"该应用没有申请相关API接口:{haveApi.ToJsonString()}。");
  377. sendWhom.Add(appCompany.id);
  378. bizcode = "auditapi";
  379. break;
  380. default:
  381. return Ok(new { state = 400, msg = "operate参数错误" });
  382. }
  383. appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<AppCompany>(appCompany, appCompany.id, new PartitionKey(appCompany.code));
  384. }
  385. else noAudit.Add($"{appCompany.id}", $"{appCompany.name}");
  386. }
  387. //发送消息
  388. var location = _option.Location;
  389. Notification notification = new()
  390. {
  391. hubName = bizcode,
  392. type = "msg",
  393. from = $"BI:{_option.Location}:private",
  394. to = sendWhom,
  395. label = $"{bizcode}-appCompany",
  396. body = new { location = location, biz = bizcode, appid = sendWhom, appName = sendWhom, status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
  397. };
  398. var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  399. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  400. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  401. await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //站内发送消息
  402. //保存操作记录
  403. //await _azureStorage.SaveBILog("appCompany-update", strMsg.ToString(), _dingDing, httpContext: HttpContext);
  404. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "appCompany-update", strMsg.ToString(), _dingDing, httpContext: HttpContext);
  405. if (haveApi.Count > 0 || noAudit.Count > 0)
  406. return Ok(new { state = 201, msg = "部分成功", haveApi, noAudit });
  407. else return Ok(new { state = 200 });
  408. }
  409. catch (Exception e)
  410. {
  411. await _dingDing.SendBotMsg($"BI,{_option.Location} , /appcompany/get-applyapi \n {e.Message}\n{e.StackTrace} \n ", GroupNames.成都开发測試群組);
  412. return BadRequest();
  413. }
  414. }
  415. /// <summary>
  416. /// 应用申请学校
  417. /// 应用审核申请的学校
  418. /// </summary>
  419. /// <param name="jsonElement"></param>
  420. /// <returns></returns>
  421. [ProducesDefaultResponseType]
  422. [AuthToken(Roles = "admin,rdc,company")]
  423. [HttpPost("set-applyschool")]
  424. public async Task<IActionResult> SetAuditSchool(JsonElement jsonElement)
  425. {
  426. try
  427. {
  428. var (loginId, loginName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  429. if (!jsonElement.TryGetProperty("appId", out JsonElement appId)) return BadRequest();
  430. if (!jsonElement.TryGetProperty("appCode", out JsonElement appCode)) return BadRequest();
  431. if (!jsonElement.TryGetProperty("schooCode", out JsonElement schooCode)) return BadRequest();
  432. if (!jsonElement.TryGetProperty("operate", out JsonElement operate)) return BadRequest();
  433. jsonElement.TryGetProperty("site", out JsonElement site);
  434. var cosmosClient = _azureCosmos.GetCosmosClient();
  435. var tableClient = _azureStorage.GetCloudTableClient();
  436. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  437. if ($"{site}".Equals(BIConst.Global))
  438. {
  439. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  440. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  441. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  442. }
  443. StringBuilder strMsg = new($"{loginName}【{loginId}】操作:");
  444. List<string> haveSchool = new();
  445. List<string> sendWhom = new();//消息分发给谁 待完善
  446. string bizcode = ""; //消息名称
  447. AppCompany appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemAsync<AppCompany>($"{appId}", new PartitionKey($"{appCode}"));
  448. if (appCompany.audit == -1 || appCompany.audit == 0)
  449. {
  450. return Ok(new { state = 401, msg = "应用未审核请先审核应用程序" });
  451. }
  452. if (appCompany != null)
  453. {
  454. switch (operate.GetString())
  455. {
  456. case "apply":
  457. var aSchool = appCompany.schools.Find(x => x.id.Equals($"{schooCode}"));
  458. if (!jsonElement.TryGetProperty("name", out JsonElement name)) return BadRequest();
  459. if (aSchool == null)
  460. {
  461. jsonElement.TryGetProperty("picture", out JsonElement picture);
  462. strMsg.Append($"应用{appCompany.name}【{appCompany.id}】申请学校{name}【{schooCode}】,申请成功。");
  463. appCompany.schools.Add(new ApplySchool() { id = $"{schooCode}", name = $"{name}", picture = $"{picture}" });
  464. }
  465. else
  466. {
  467. haveSchool.Add(schooCode.GetString());
  468. strMsg.Append($"应用{appCompany.name}【{appCompany.id}】申请的学校{name}【{schooCode}】已存在。");
  469. }
  470. sendWhom = new List<string> { "1528783103", "1636016499" };
  471. bizcode = "applyschool";
  472. break;
  473. case "audit":
  474. if (!jsonElement.TryGetProperty("isAudit", out JsonElement isAudit)) return BadRequest();
  475. string refuseDesc = "";
  476. if (bool.Parse($"{isAudit}") == false)
  477. {
  478. if (!jsonElement.TryGetProperty("refuseDesc", out JsonElement jsonRefuseDesc)) return BadRequest();
  479. refuseDesc = jsonRefuseDesc.GetString();
  480. }
  481. var applySchool = appCompany.schools.Find(x => x.id.Equals($"{schooCode}"));
  482. strMsg.Append($"审核应用{appCompany.name}【{appCompany.id}】状态:");
  483. if (applySchool != null)
  484. {
  485. if (bool.Parse($"{isAudit}") == true)
  486. {
  487. applySchool.status = 1;
  488. applySchool.refuseDesc = null;
  489. appCompany.jwtKey = JwtAuth.CreateApplyJwtKeyBI(_option.HostName, _option.JwtSecretKey, appCompany);
  490. strMsg.Append($"审核成功。");
  491. }
  492. else
  493. {
  494. applySchool.status = 0;
  495. applySchool.refuseDesc = $"{refuseDesc}";
  496. strMsg.Append($"审核失败。");
  497. }
  498. }
  499. else
  500. {
  501. haveSchool.Add(schooCode.GetString());
  502. strMsg.Append($"已审核状态!");
  503. }
  504. sendWhom = new List<string> { "1528783103", "1636016499" };
  505. bizcode = "auditschool";
  506. break;
  507. default:
  508. return Ok(new { state = 400, msg = "operate参数错误" });
  509. }
  510. appCompany = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<AppCompany>(appCompany, appCompany.id, new PartitionKey(appCompany.code));
  511. }
  512. else return Ok(new { state = 404, msg = "未找到该应用" });
  513. //发送消息
  514. var location = _option.Location;
  515. Notification notification = new()
  516. {
  517. hubName = bizcode,
  518. type = "msg",
  519. from = $"BI:{_option.Location}:private",
  520. to = sendWhom,
  521. label = $"{bizcode}-appCompany",
  522. body = new { location = location, biz = bizcode, appid = appCompany.id, appName = appCompany.name, status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
  523. };
  524. var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  525. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  526. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  527. await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //站内发送消息
  528. //保存操作记录
  529. //await _azureStorage.SaveBILog("appCompany-update", strMsg.ToString(), _dingDing, httpContext: HttpContext);
  530. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "appCompany-update", strMsg.ToString(), _dingDing, httpContext: HttpContext);
  531. if (haveSchool.Count > 0) return Ok(new { state = 201, msg = "已存在学校,无须申请!", haveSchool });
  532. else return Ok(new { state = 200 });
  533. }
  534. catch (Exception e)
  535. {
  536. await _dingDing.SendBotMsg($"BI,{_option.Location} , /appcompany/set-auditschool \n {e.Message}\n{e.StackTrace}\n{e.StackTrace} \n ", GroupNames.成都开发測試群組);
  537. return BadRequest();
  538. }
  539. }
  540. /// <summary>
  541. /// 应用申请和审核api信息
  542. /// </summary>
  543. public record ApplyApi
  544. {
  545. public string appId { get; set; }
  546. public string appCode { get; set; }
  547. public List<string> apiIds { get; set; }
  548. }
  549. /// <summary>
  550. /// 审核应用
  551. /// </summary>
  552. public record AppIdOrCode
  553. {
  554. public string id { get; set; }
  555. public string code { get; set; }
  556. }
  557. /// <summary>
  558. /// 未审核应用
  559. /// </summary>
  560. public record NoAudit
  561. {
  562. public string id { get; set; }
  563. public string code { get; set; }
  564. public string pk { get; set; }
  565. public string name { get;set; }
  566. public List<object> operate { get; set; }
  567. public int audit { get; set; }
  568. }
  569. /// <summary>
  570. /// 显示应用
  571. /// </summary>
  572. public record ReadCompany
  573. {
  574. public string id { get; set; }
  575. public string pk { get; set; }
  576. public string code { get; set; }
  577. public string name { get; set; }
  578. public string descr { get; set; }
  579. public string picture { get; set; }
  580. public string jwtKey { get; set; }
  581. public int status { get; set; }
  582. public int audit { get; set; }
  583. public string refuseDesc { get; set; }
  584. public string gateways { get; set; }
  585. public List<AppApiState> apis { get; set; }
  586. public string webhookDomain { get; set; }
  587. public List<WebHookState> webHooks { get; set; }
  588. public List<ApplySchool> schools { get; set; }
  589. }
  590. }
  591. }