irc-netcore-api/IRaCIS.Core.Application/Service/ImageAndDoc/ImageShareService.cs

105 lines
3.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using DocumentFormat.OpenXml.Bibliography;
using IRaCIS.Application.Contracts;
using IRaCIS.Core.Application.Auth;
using IRaCIS.Core.Application.Contracts.Dicom.DTO;
using IRaCIS.Core.Domain.Share;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace IRaCIS.Core.Application.Services
{
[AllowAnonymous, ApiExplorerSettings(GroupName = "HIR")]
public class ImageShareService(IRepository<ImageShare> _imageShareRepository,
ITokenService _tokenService, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService, IImageShareService
{
/// <summary>
/// 创建影像分享返回分享Id 和初始化随机密码
/// </summary>
/// <param name="imageShareCommand"></param>
/// <returns></returns>
[HttpPost]
public async Task<IResponseOutput> CreateImageShare(ImageShareCommand imageShareCommand)
{
var imageShare = _mapper.Map<ImageShare>(imageShareCommand);
var addEntity = await _imageShareRepository.AddAsync(imageShare);
//验证码 4位
int verificationPassWord = new Random().Next(1000, 10000);
addEntity.Password = verificationPassWord.ToString();
var success = await _imageShareRepository.SaveChangesAsync();
return ResponseOutput.Result(success, new { ResourceId = imageShare.Id, Password = verificationPassWord.ToString() });
}
/// <summary>
/// 更新影像分享 密码和授权时间
/// </summary>
/// <param name="inComand"></param>
/// <returns></returns>
[HttpPut]
public async Task<IResponseOutput> UpdateImageShare(UpdateImageShare inComand)
{
var find = await _imageShareRepository.FirstOrDefaultAsync(t => t.Id == inComand.Id);
find.ExpireTime = DateTime.Now.AddDays(inComand.ImageShareExpireDays);
find.Password = inComand.Password;
await _imageShareRepository.SaveChangesAsync();
return ResponseOutput.Ok();
}
[HttpGet, Route("{resourceId:guid}/{password}")]
public async Task<IResponseOutput> VerifyShareImage(Guid resourceId, string password)
{
var pWord = password.Trim();
var imageShare = await _imageShareRepository.FirstOrDefaultAsync(t => t.Id == resourceId);
if (imageShare == null)
{
//---资源不存在。
return ResponseOutput.NotOk(_localizer["ISS_ResNotExists"]);
}
if (pWord != imageShare.Password.Trim())
{
//---分享密码错误。
return ResponseOutput.NotOk(_localizer["ISS_SharePwdError"]);
}
if (DateTime.Now > imageShare.ExpireTime)
{
//---资源分享过期。
return ResponseOutput.NotOk(_localizer["ISS_ShareExpiration"]);
}
var resource = new ResourceInfo()
{
VisitTaskId = imageShare.VisitTaskId,
RouteUrl = imageShare.RouteUrl,
Token = _tokenService.GetToken(new UserTokenInfo()
{
IdentityUserId = Guid.NewGuid(),
FullName = "Share001",
UserName = "Share001",
UserTypeEnum = UserTypeEnum.ShareImage,
})
};
return ResponseOutput.Ok(resource);
}
}
}