AppCompanyController.cs 33 KB

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