using ClockSnowFlake; using dy.net.dto; using dy.net.model; using dy.net.service; using dy.sync.lib; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using Quartz.Util; using System.Text; using System.Text.Json; using System.Xml.Linq; using static Dm.net.buffer.ByteArrayBuffer; namespace dy.net.Controllers { [Route("api/[controller]")] [ApiController] [Authorize] public class ConfigController : ControllerBase { private readonly DouyinCookieService dyCookieService; private readonly DouyinCommonService commonService; private readonly DouyinQuartzJobService quartzJobService; private readonly DouyinFollowService douyinFollowService; public ConfigController(DouyinCookieService dyCookieService, DouyinCommonService commonService, DouyinQuartzJobService quartzJobService, DouyinFollowService douyinFollowService) { this.dyCookieService = dyCookieService; this.commonService = commonService; this.quartzJobService = quartzJobService; this.douyinFollowService = douyinFollowService; } /// /// 导出配置 /// /// [HttpGet("exportConf")] public async Task ExportConf() { // 1. 组装导出数据 var dto = new AppConfigImportDto() { follows = await douyinFollowService.GetHandFollows(), conf = commonService.GetConfig() }; return Ok(new { code = 0, data = dto }); } /// /// 导入配置 /// /// /// [HttpPost("importConf")] public async Task ImportConf(AppConfigImportDto dto) { if (dto == null) return BadRequest("json数据为空"); var follows = dto.follows; if (follows != null && follows.Count > 0) { var add = await douyinFollowService.AddHandFollows(follows); if (add) Serilog.Log.Debug("关注列表导入成功"); } var conf = dto.conf; if (conf != null) { var update = await commonService.UpdateConfig(conf); if (update) Serilog.Log.Debug("配置导入成功"); } return Ok(new {code=0,data=true}); } /// /// 分页查询 /// /// 分页结果 [HttpPost("paged")] public async Task GetPagedAsync( PageRequestDto dto) { var (list, totalCount) = await dyCookieService.GetPagedAsync(dto.PageIndex, dto.PageSize); return Ok(new { code = 0, data = new { data = list, total = totalCount, pageIndex = dto.PageIndex, pageSize = dto.PageSize } } ); } /// /// 查询所有用户Cookie /// /// [HttpGet("list")] public async Task GetAllList() { var follows = await douyinFollowService.GetGroupByCookieAsync(); return Ok(new { code = 0, data = follows }); } /// /// 新增用户Cookie /// [HttpPost("add")] public async Task AddAsync([FromBody] DouyinCookie dyUserCookies) { var result = await dyCookieService.Add(dyUserCookies); if (result) { await ReStartJob(); return Ok(new { code = 0 }); } return BadRequest(new { code = -1, message = "添加失败" }); } /// /// 更新用户Cookie /// [HttpPost("update")] public async Task UpdateAsync([FromBody] DouyinCookie dyUserCookies) { if (dyUserCookies.Id == "0") { dyUserCookies.Id = IdGener.GetLong().ToString(); var result = await dyCookieService.Add(dyUserCookies); if (result) { await ReStartJob(); return Ok(new { code = 0 }); } return BadRequest(new { code = -1, message = "添加失败" }); } else { var result = await dyCookieService.UpdateAsync(dyUserCookies); if (result) { await ReStartJob(); return Ok(new { code = 0 }); } return BadRequest(new { code = -1, message = "更新失败" }); } } /// /// 批量删除用户Cookie /// [HttpGet("delete")] public async Task DeleteAsync(string id) { var count = await dyCookieService.DeleteByIdsAsync(new List { id }); if (count > 0) { await ReStartJob(); } return Ok(new { code = 0, deletedCount = count }); } [HttpGet("GetConfig")] public IActionResult GetConfig() { var data = commonService.GetConfig(); return Ok(new { code = 0, data = data }); } [HttpPost("UpdateConfig")] public async Task UpdateConfig(AppConfig config) { var data = await commonService.UpdateConfig(config); if (data) { await ReStartJob(); } return Ok(new { code = 0, data = data }); } /// /// /// /// [HttpGet("ExecuteJobNow")] //[Authorize] public async Task ExecuteJobNow() { var config = commonService.GetConfig(); if (config != null) await quartzJobService.InitOrReStartAllJobs(config.Cron.ToString()); return Ok(new { code = 0, error = "" }); } private async Task ReStartJob() { var config = commonService.GetConfig(); if (config != null) quartzJobService.InitOrReStartAllJobs(config.Cron.ToString()); //避免前端等待 } /// /// 镜像标签 /// /// [HttpGet("mytag")] public async Task GetMyTag() { return Ok(new { code = 0, data = Appsettings.Get("tagName") }); } [HttpGet("checktag")] public async Task CheckTag() { var data = await DouyinHttpHelper.GetTenImage(Appsettings.Get("tagName")); if (data.IsSuccessStatusCode) { var content = await data.Content.ReadAsStringAsync(); return Ok(content); } else { return BadRequest(new { code = -1, message = "请求失败" }); } } } }