基础服务api
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

115 lines
5.1 KiB

  1. using BPA.SAAS.Manage.Application.System.Dtos;
  2. using BPA.SAAS.Manage.Application.System.Interface;
  3. using BPA.SAAS.Manage.Comm.Const;
  4. using BPA.SAAS.Manage.Core.Org;
  5. using COSXML;
  6. using COSXML.Auth;
  7. using COSXML.Model.Tag;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using System.Threading.Tasks;
  14. namespace BPA.SAAS.Manage.Application.System.Services
  15. {
  16. public class SystemConfigService: ISystemConfigService, ITransient
  17. {
  18. private readonly ISqlSugarClient _db;
  19. private CosXml cosXml;
  20. public SystemConfigService( ISqlSugarClient db)
  21. {
  22. _db=db;
  23. var cosConfig = CosConfig.cosInfoOptions;
  24. CosXmlConfig config = new CosXmlConfig.Builder()
  25. .SetRegion(cosConfig.Region) // 设置默认的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
  26. .Build();
  27. string secretId = cosConfig.SecretId; // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
  28. string secretKey = cosConfig.SecretKey; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
  29. long durationSecond = 6000; //每次请求签名有效时长,单位为秒
  30. QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,
  31. secretKey, durationSecond);
  32. this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);
  33. }
  34. public async Task<string> GetCosImgesURL(IFormFile file)
  35. {
  36. byte[] pReadByte = new byte[0];
  37. var pFileStream = file.OpenReadStream();
  38. BinaryReader r = new BinaryReader(pFileStream);
  39. r.BaseStream.Seek(0, SeekOrigin.Begin); //将文件指针设置到文件开
  40. pReadByte = r.ReadBytes((int)r.BaseStream.Length);
  41. TransferUploadObjectModel m = new TransferUploadObjectModel(_db);
  42. /// 高级接口上传对象
  43. var result = await m.TransferUploadFile(new FileInputInfoDto()
  44. {
  45. Directory = "goods",
  46. FileExtension = Path.GetExtension(file.FileName).Replace(".", ""),
  47. Method = "PUT",
  48. }, pReadByte);
  49. return result;
  50. }
  51. public FileOutInfoDto GetCosRequestSignURL(FileInputInfoDto inputDto)
  52. {
  53. return GetPresignUploadUrl(inputDto);
  54. }
  55. private FileOutInfoDto GetPresignUploadUrl(FileInputInfoDto inputDto)
  56. {
  57. var cosConfig = CosConfig.cosInfoOptions;
  58. string GroupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
  59. string baseUrl = "/Franchisee/";
  60. if (!string.IsNullOrWhiteSpace(GroupId))
  61. {
  62. var Companycheck = _db.Queryable<BPA_Company>().Where(x => x.Id == GroupId).First();
  63. baseUrl = baseUrl + Companycheck.Code + "/";
  64. }
  65. DateTime dt = DateTime.Now;
  66. baseUrl = baseUrl + inputDto.Directory + "/" + dt.ToFileTime().ToString() + "." + inputDto.FileExtension;
  67. try
  68. {
  69. PreSignatureStruct preSignatureStruct = new PreSignatureStruct();
  70. preSignatureStruct.appid = cosConfig.AppId;//腾讯云账号 APPID
  71. preSignatureStruct.region = cosConfig.Region; //存储桶地域
  72. preSignatureStruct.bucket = cosConfig.Bucket; //存储桶
  73. preSignatureStruct.key = baseUrl; //对象键+".png"
  74. preSignatureStruct.httpMethod = "PUT"; //HTTP 请求方法
  75. preSignatureStruct.isHttps = true; //生成 HTTPS 请求 URL
  76. preSignatureStruct.signDurationSecond = 6000; //请求签名时间为 600s
  77. preSignatureStruct.headers = null;//签名中需要校验的 header
  78. preSignatureStruct.queryParameters = null; //签名中需要校验的 URL 中请求参数
  79. //上传预签名 URL (使用永久密钥方式计算的签名 URL)
  80. string requestSignURL = cosXml.GenerateSignURL(preSignatureStruct);
  81. string[] urls = requestSignURL.Split('?');
  82. Regex reg = new Regex("(http|https)://.+?(?=/)");
  83. Match match = reg.Match(requestSignURL);
  84. FileOutInfoDto fileOut = new FileOutInfoDto();
  85. fileOut.Domain = match.Value;
  86. fileOut.FileUrl = urls[0].Replace(fileOut.Domain, "/cos");
  87. fileOut.Param = urls[1];
  88. fileOut.AllUrl = requestSignURL;
  89. fileOut.SeeUrl = urls[0];
  90. return fileOut;
  91. }
  92. catch (COSXML.CosException.CosClientException clientEx)
  93. {
  94. //请求失败
  95. // Console.WriteLine("CosClientException: " + clientEx);
  96. }
  97. catch (COSXML.CosException.CosServerException serverEx)
  98. {
  99. //请求失败
  100. //Console.WriteLine("CosServerException: " + serverEx.GetInfo());
  101. }
  102. return null;
  103. }
  104. }
  105. }