public async Task <Response <IList <EnumResponse> > > GetSignatureTypeAsync() { var response = new Response <IList <EnumResponse> >(); var result = Extension.EnumToList <SignatureEnum>(); response.Result = result; return(await Task.FromResult(response)); }
public async Task <Response <IList <EnumResponse> > > GetHotNewsSourceAsync() { var response = new Response <IList <EnumResponse> >(); var result = Extension.EnumToList <HotNewsSource>(); response.Result = result; return(await Task.FromResult(response)); }
public async Task <Response <string> > GenerateSignatureAsync(string name, int id) { var response = new Response <string>(); if (name.Length > 4) { response.Msg = "名字只支持1-4个字符"; return(response); } var type = Extension.EnumToList <SignatureEnum>().FirstOrDefault(x => x.Value.Equals(id))?.Description; if (string.IsNullOrEmpty(type)) { response.Msg = "不存在的签名类型"; return(response); } var signatureLog = await _context.SignatureLogs.FirstOrDefaultAsync(x => x.Name.Equals(name) && x.Type.Equals(type)); if (null != signatureLog) { response.Result = signatureLog.Url; return(response); } var ip = HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault(); if (string.IsNullOrEmpty(ip)) { ip = Request.HttpContext.Connection.RemoteIpAddress.ToString(); } var url = $"{(name + id).ToMd5()}.png"; #region 生成签名 var signatureUrl = AppSettings.Signature.Urls.OrderBy(x => Guid.NewGuid()).Select(x => new { Url = x.Key, Parameter = string.Format(x.Value, name, id) }).FirstOrDefault(); var content = new StringContent(signatureUrl.Parameter); content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); using var client = _httpClient.CreateClient(); client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0"); var httpResponse = await client.PostAsync(signatureUrl.Url, content); var result = await httpResponse.Content.ReadAsStringAsync(); var regex = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase); var imgUrl = regex.Match(result).Groups["imgUrl"].Value; // 生成签名路径 var signaturePath = Path.Combine(AppSettings.Signature.Path, url); // 下载签名图片至本地 await DownloadImg(client, signaturePath, imgUrl); // 添加水印并且保存图片 await AddWatermarkSaveItAsync(signaturePath); #endregion var entity = new SignatureLog { Name = name, Type = type, Url = url, Ip = ip, Date = DateTime.Now }; await _context.SignatureLogs.AddAsync(entity); await _context.SaveChangesAsync(); response.Result = url; return(response); }