BundlesService.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using Microsoft.EntityFrameworkCore;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Microsoft.AspNetCore.Http;
  7. using System.Threading.Tasks;
  8. using Winsoft.GOV.XF.WebApi.WXCore.Data;
  9. using Winsoft.GOV.XF.WebApi.WXCore.Models;
  10. using Winsoft.GOV.XF.WebApi.WXCore.Helpers;
  11. using Newtonsoft.Json;
  12. namespace Winsoft.GOV.XF.WebApi.WXCore.Services
  13. {
  14. public class BundlesService : BaseService
  15. {
  16. public BundlesService(XFDbContext context, ILoggerFactory loggerFactory):base(context, loggerFactory)
  17. {
  18. }
  19. public async Task<IEnumerable<Bundle>> GetByUserId(int userId)
  20. {
  21. return await _context.Bundles.AsNoTracking().Where(e => e.UserId == userId).ToListAsync<Bundle>();
  22. }
  23. public async Task<Bundle> Get(string id)
  24. {
  25. Guid g;
  26. if (!String.IsNullOrWhiteSpace(id) && Guid.TryParse(id,out g))
  27. return await _context.Bundles.AsNoTracking().SingleOrDefaultAsync<Bundle>(m => m.Id == g);
  28. else
  29. {
  30. Logger.LogError(String.Format("id:{0};id不存在或不能转换为guid",id));
  31. }
  32. return null;
  33. }
  34. public string GetURL(Bundle b, string absoluteUri)
  35. {
  36. //_httpContext.Request.GetAbsoluteUri()
  37. string s = absoluteUri + "/dist/web12345-pageView/index.html#!/pageView?bid={0}&m={1}";
  38. s = String.Format(s, b.Id.ToString(), b.Id.ToString().AESEncrypt(b.Key));
  39. return s;
  40. }
  41. public async Task<Bundle> GetDetail(string id)
  42. {
  43. Guid g;
  44. if (!String.IsNullOrWhiteSpace(id) && Guid.TryParse(id, out g))
  45. return await _context.Bundles.SingleOrDefaultAsync<Bundle>(m => m.Id == g);
  46. else
  47. {
  48. Logger.LogError(String.Format("id:{0};id不存在或不能转换为guid", id));
  49. }
  50. return null;
  51. }
  52. public async Task<string> Add(Bundle b)
  53. {
  54. if (b != null)
  55. {
  56. await _context.Bundles.AddAsync(b);
  57. await _context.SaveChangesAsync();
  58. return b.Id.ToString();
  59. }
  60. return String.Empty;
  61. }
  62. public async Task Delete(Bundle b)
  63. {
  64. if (b != null)
  65. {
  66. _context.Bundles.Remove(b);
  67. await _context.SaveChangesAsync();
  68. }
  69. }
  70. public void Update(Bundle b)
  71. {
  72. if (b != null)
  73. {
  74. _context.Bundles.Update(b);
  75. }
  76. }
  77. public string GenerateBaseURL(Bundle b, string absoluteUri)
  78. {
  79. BundleAccessToken a = new BundleAccessToken()
  80. {
  81. Expire = -1,
  82. BundleId = b.Id
  83. };
  84. string multimediaURL = string.Format("{0}dist/multimedia/index.html?bundleId={1}&ciphertext={2}",
  85. absoluteUri, b.Id, JsonConvert.SerializeObject(a).AESEncrypt(b.Key));
  86. return multimediaURL;
  87. }
  88. public string GenerateShareURL(Bundle b, BundleAccessToken a, string absoluteUri)
  89. {
  90. if (a.Expire != -1)
  91. {
  92. a.Expire = DateTime.Now.AddDays(1).ToTimeStamp();
  93. string multimediaURL = string.Format("{0}dist/multimedia/index.html?bundleId={1}&data={2}",
  94. absoluteUri, b.Id, JsonConvert.SerializeObject(a).AESEncrypt(b.Key));
  95. return multimediaURL;
  96. }
  97. return String.Empty;
  98. }
  99. }
  100. }