120 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			120 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			C#
		
	
	
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 = "Image")]
 | 
						|
    public class ImageShareService(IRepository<ImageShare> _imageShareRepository,
 | 
						|
        IRepository<DicomStudy> _studyRepository,
 | 
						|
        ITokenService _tokenService, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService, IImageShareService
 | 
						|
    {
 | 
						|
 | 
						|
 | 
						|
 | 
						|
        [HttpPost]
 | 
						|
        public async Task<IResponseOutput> CreateImageShare(ImageShareCommand imageShareCommand)
 | 
						|
        {
 | 
						|
            if (imageShareCommand.StudyId == null)
 | 
						|
            {
 | 
						|
 | 
						|
                #region 上传不按照访视上传,基线没传,数据可能出错
 | 
						|
 | 
						|
                //var subjectVisit1 = _subjectVisitRepository.FirstOrDefault(t =>
 | 
						|
                //    t.TrialId == imageShareCommand.TrialId && t.SubjectId == imageShareCommand.SubjectId &&
 | 
						|
                //    t.VisitNum == 1);
 | 
						|
 | 
						|
                //if (subjectVisit1 == null)
 | 
						|
                //{
 | 
						|
                //    return ResponseOutput.NotOk("当前无影像数据,无法分享!");
 | 
						|
                //}
 | 
						|
 | 
						|
                //imageShareCommand.StudyId =
 | 
						|
                //    _studyRepository.GetAll().First(t => t.SubjectVisitId == subjectVisit1.Id&&t.Status != (int)StudyStatus.Abandon).Id;
 | 
						|
 | 
						|
                #endregion
 | 
						|
 | 
						|
                var studyIds = await _studyRepository.AsQueryable()
 | 
						|
                    .Where(t => t.TrialId == imageShareCommand.TrialId && t.SubjectId == imageShareCommand.SubjectId
 | 
						|
                                )
 | 
						|
                    .Select(u => u.Id).ToListAsync();
 | 
						|
 | 
						|
                if (!studyIds.Any())
 | 
						|
                {
 | 
						|
                    //---当前检查没有影像可以分享。 
 | 
						|
                    return ResponseOutput.NotOk(_localizer["ISS_NoImgToShare"]);
 | 
						|
                }
 | 
						|
 | 
						|
                imageShareCommand.StudyId = studyIds.First();
 | 
						|
 | 
						|
            }
 | 
						|
 | 
						|
            //验证码 4位
 | 
						|
            int verificationPassWord = new Random().Next(1000, 10000);
 | 
						|
 | 
						|
            imageShareCommand.Password = verificationPassWord.ToString();
 | 
						|
 | 
						|
            //配置文件读取过期时间
 | 
						|
 | 
						|
            var days = AppSettings.IRaCISBasicConfig.ImageShareExpireDays;
 | 
						|
 | 
						|
 | 
						|
            imageShareCommand.ExpireTime = DateTime.Now.AddDays(days);
 | 
						|
 | 
						|
            var imageShare = _mapper.Map<ImageShare>(imageShareCommand);
 | 
						|
 | 
						|
            await _imageShareRepository.AddAsync(imageShare);
 | 
						|
 | 
						|
            var success = await _imageShareRepository.SaveChangesAsync();
 | 
						|
 | 
						|
            return ResponseOutput.Result(success, new { ResourceId = imageShare.Id, Password = verificationPassWord.ToString() });
 | 
						|
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        [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()
 | 
						|
            {
 | 
						|
                StudyId = imageShare.StudyId,
 | 
						|
                Token = _tokenService.GetToken(new UserTokenInfo()
 | 
						|
                {
 | 
						|
                    IdentityUserId = Guid.NewGuid(),
 | 
						|
                    FullName = "Share001",
 | 
						|
                    UserName = "Share001",
 | 
						|
                    UserTypeEnum = UserTypeEnum.ShareImage,
 | 
						|
                })
 | 
						|
            };
 | 
						|
 | 
						|
 | 
						|
            return ResponseOutput.Ok(resource);
 | 
						|
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |