| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Logging;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.AspNetCore.Http;
- using System.Threading.Tasks;
- using Winsoft.GOV.XF.WebApi.WXCore.Data;
- using Winsoft.GOV.XF.WebApi.WXCore.Models;
- using Winsoft.GOV.XF.WebApi.WXCore.Helpers;
- using Newtonsoft.Json;
- namespace Winsoft.GOV.XF.WebApi.WXCore.Services
- {
- public class BundlesService : BaseService
- {
- public BundlesService(XFDbContext context, ILoggerFactory loggerFactory):base(context, loggerFactory)
- {
-
- }
- public async Task<IEnumerable<Bundle>> GetByUserId(int userId)
- {
- return await _context.Bundles.AsNoTracking().Where(e => e.UserId == userId).ToListAsync<Bundle>();
- }
- public async Task<Bundle> Get(string id)
- {
- Guid g;
- if (!String.IsNullOrWhiteSpace(id) && Guid.TryParse(id,out g))
- return await _context.Bundles.AsNoTracking().SingleOrDefaultAsync<Bundle>(m => m.Id == g);
- else
- {
- Logger.LogError(String.Format("id:{0};id不存在或不能转换为guid",id));
- }
- return null;
- }
- public string GetURL(Bundle b, string absoluteUri)
- {
- //_httpContext.Request.GetAbsoluteUri()
- string s = absoluteUri + "/dist/web12345-pageView/index.html#!/pageView?bid={0}&m={1}";
- s = String.Format(s, b.Id.ToString(), b.Id.ToString().AESEncrypt(b.Key));
- return s;
- }
- public async Task<Bundle> GetDetail(string id)
- {
- Guid g;
- if (!String.IsNullOrWhiteSpace(id) && Guid.TryParse(id, out g))
- return await _context.Bundles.SingleOrDefaultAsync<Bundle>(m => m.Id == g);
- else
- {
- Logger.LogError(String.Format("id:{0};id不存在或不能转换为guid", id));
- }
- return null;
- }
- public async Task<string> Add(Bundle b)
- {
- if (b != null)
- {
- await _context.Bundles.AddAsync(b);
- await _context.SaveChangesAsync();
- return b.Id.ToString();
- }
- return String.Empty;
- }
- public async Task Delete(Bundle b)
- {
- if (b != null)
- {
- _context.Bundles.Remove(b);
- await _context.SaveChangesAsync();
- }
- }
- public void Update(Bundle b)
- {
- if (b != null)
- {
- _context.Bundles.Update(b);
- }
- }
- public string GenerateBaseURL(Bundle b, string absoluteUri)
- {
- BundleAccessToken a = new BundleAccessToken()
- {
- Expire = -1,
- BundleId = b.Id
- };
- string multimediaURL = string.Format("{0}dist/multimedia/index.html?bundleId={1}&ciphertext={2}",
- absoluteUri, b.Id, JsonConvert.SerializeObject(a).AESEncrypt(b.Key));
- return multimediaURL;
- }
- public string GenerateShareURL(Bundle b, BundleAccessToken a, string absoluteUri)
- {
- if (a.Expire != -1)
- {
- a.Expire = DateTime.Now.AddDays(1).ToTimeStamp();
- string multimediaURL = string.Format("{0}dist/multimedia/index.html?bundleId={1}&data={2}",
- absoluteUri, b.Id, JsonConvert.SerializeObject(a).AESEncrypt(b.Key));
- return multimediaURL;
- }
- return String.Empty;
- }
- }
- }
|