AppCompanyController.cs 30 KB

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