using COSSTS; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data; using System.Threading.Tasks; using Znyc.Recruitment.Admin.Commons.Cache; using Znyc.Recruitment.Admin.Commons.Core.App; using Znyc.Recruitment.Admin.Commons.Cos; using Znyc.Recruitment.Admin.Commons.Dtos; using Znyc.Recruitment.Admin.Commons.Entitys; using Znyc.Recruitment.Admin.Commons.Mapping; using Znyc.Recruitment.Admin.Commons.Pages; using Znyc.Recruitment.Admin.Commons.Services; using Znyc.Recruitment.Admin.Security.Dtos; using Znyc.Recruitment.Admin.Security.Dtos.Document; using Znyc.Recruitment.Admin.Security.Entitys; using Znyc.Recruitment.Admin.Security.IRepositories; using Znyc.Recruitment.Admin.Security.IServices; namespace Znyc.Recruitment.Admin.Security.Services { /// /// public class APPService : BaseService, IAPPService { private readonly IAPPRepository _appRepository; private static CosOptions _cosOptions; public APPService(IAPPRepository repository) : base(repository) { _appRepository = repository; _cosOptions = App.GetService(); if (_cosOptions == null) { throw new ArgumentNullException(nameof(_cosOptions)); } } /// /// 同步新增实体。 /// /// 实体 /// 事务对象 /// public override long Insert(APPEntity entity, IDbTransaction trans = null) { long result = repository.Insert(entity, trans); UpdateCacheAllowApp(); return result; } /// /// 异步更新实体。 /// /// 实体 /// 主键ID /// 事务对象 /// public override async Task UpdateAsync(APPEntity entity, long id, IDbTransaction trans = null) { bool result = await repository.UpdateAsync(entity, id, trans); UpdateCacheAllowApp(); return result; } /// /// 异步步新增实体。 /// /// 实体 /// 事务对象 /// public override async Task InsertAsync(APPEntity entity, IDbTransaction trans = null) { int result = await repository.InsertAsync(entity, trans); UpdateCacheAllowApp(); return result; } /// /// 获取app对象 /// /// 应用ID /// 应用密钥AppSecret /// public APPEntity GetAPP(string appid, string secret) { return _appRepository.GetAPP(appid, secret); } /// /// 获取app对象 /// /// 应用ID /// public APPEntity GetAPP(string appid) { return _appRepository.GetAPP(appid); } public IList SelectApp() { return _appRepository.SelectApp(); } /// /// 根据条件查询数据库,并返回对象集合(用于分页数据显示) /// /// 查询的条件 /// 指定对象的集合 public override async Task> FindWithPagerAsync(SearchInputDto search) { bool order = search.Order == "asc" ? false : true; string where = GetDataPrivilege(false); if (!string.IsNullOrEmpty(search.Keywords)) { @where += string.Format(" and (AppId like '%{0}%' or RequestUrl like '%{0}%')", search.Keywords); }; PagerInfo pagerInfo = new PagerInfo { CurrenetPageIndex = search.CurrenetPageIndex, PageSize = search.PageSize }; List list = await repository.FindWithPagerAsync(where, pagerInfo, search.Sort, order); PageResult pageResult = new PageResult { CurrentPage = pagerInfo.CurrenetPageIndex, Items = list.MapTo(), ItemsPerPage = pagerInfo.PageSize, TotalItems = pagerInfo.RecordCount }; return pageResult; } public void UpdateCacheAllowApp() { CacheHelper cacheHelper = new CacheHelper(); IEnumerable appList = repository.GetAllByIsNotDeleteAndEnabledMark(); cacheHelper.Add("AllowAppId", appList); } public CommonResult GetCosToken() { CommonResult result = new CommonResult(); Dictionary values = new() { ["bucket"] = _cosOptions.Bucket, ["region"] = _cosOptions.Region, ["allowPrefix"] = _cosOptions.AllowPrefix, ["allowActions"] = _cosOptions.AllowActions, ["durationSeconds"] = _cosOptions.DurationSeconds, ["secretId"] = _cosOptions.SecretId, ["secretKey"] = _cosOptions.SecretKey }; Dictionary credential = STSClient.genCredential(values); result.ResData = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(credential)); result.ErrCode = "0"; result.Success = true; return result; } } }