|
|
|
@ -3,13 +3,26 @@ using Furion.DependencyInjection;
|
|
|
|
|
using Furion.DynamicApiController;
|
|
|
|
|
using Mapster;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using GDZZ.Application.Entity;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using K4os.Hash.xxHash;
|
|
|
|
|
using GDZZ.Core.Entity;
|
|
|
|
|
using Furion;
|
|
|
|
|
using GDZZ.Application.Help;
|
|
|
|
|
using Furion.LinqBuilder;
|
|
|
|
|
using System;
|
|
|
|
|
using static K4os.Compression.LZ4.Engine.Pubternal;
|
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
using GDZZ.Application.Service.LiveHistoryContacts.Dto;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Yitter.IdGenerator;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using ICacheService = GDZZ.Application.Help.ICacheService;
|
|
|
|
|
|
|
|
|
|
namespace GDZZ.Application
|
|
|
|
|
{
|
|
|
|
@ -19,19 +32,37 @@ namespace GDZZ.Application
|
|
|
|
|
[ApiDescriptionSettings("Application", Name = "LiveHistoryContacts", Order = 1)]
|
|
|
|
|
public class LiveHistoryContactsService : ILiveHistoryContactsService, IDynamicApiController, ITransient
|
|
|
|
|
{
|
|
|
|
|
private readonly SqlSugarRepository<LiveHistoryContacts> _rep;
|
|
|
|
|
private readonly SqlSugarRepository<LiveMessage> liveMessageRep;
|
|
|
|
|
private readonly SqlSugarRepository<LiveHistoryContacts> _rep; //聊天历史任务列表仓储
|
|
|
|
|
private readonly SqlSugarRepository<LiveMessage> liveMessageRep; //消息仓储
|
|
|
|
|
private readonly SqlSugarRepository<LiveUserFriend> liveUserFriend; //好友表仓储
|
|
|
|
|
private readonly SqlSugarRepository<SysUser> _sysUserRep; // 用户表仓储
|
|
|
|
|
private readonly SqlSugarRepository<OnlineUser> _sysOnlineUerRep; // 在线用户表仓储
|
|
|
|
|
private readonly ICacheService cacheService;
|
|
|
|
|
private readonly IHubContext<ChatHub, IChatClient> _chatHubContext;
|
|
|
|
|
private readonly UploadFileOptions _options;
|
|
|
|
|
|
|
|
|
|
public LiveHistoryContactsService(SqlSugarRepository<LiveHistoryContacts> rep,
|
|
|
|
|
ICacheService cacheService,
|
|
|
|
|
IOptions<UploadFileOptions> options,
|
|
|
|
|
SqlSugarRepository<SysUser> _sysUserRep,
|
|
|
|
|
SqlSugarRepository<OnlineUser> sysOnlineUerRep,
|
|
|
|
|
SqlSugarRepository<LiveUserFriend> liveUserFriend,
|
|
|
|
|
IHubContext<ChatHub, IChatClient> chatHubContext,
|
|
|
|
|
SqlSugarRepository<LiveMessage> liveMessageRep)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
_rep = rep;
|
|
|
|
|
_options = options.Value;
|
|
|
|
|
this.cacheService = cacheService;
|
|
|
|
|
this._chatHubContext= chatHubContext;
|
|
|
|
|
this._sysOnlineUerRep = sysOnlineUerRep;
|
|
|
|
|
this.liveUserFriend = liveUserFriend;
|
|
|
|
|
this.liveMessageRep = liveMessageRep;
|
|
|
|
|
this._sysUserRep = _sysUserRep;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region 聊天列表
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 新增联系人
|
|
|
|
|
/// </summary>
|
|
|
|
@ -39,10 +70,46 @@ namespace GDZZ.Application
|
|
|
|
|
[HttpPost("/LiveHistoryContacts/AddLive")]
|
|
|
|
|
public async Task AddLive(AddLiveHistoryContactsInput input)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var entity = input.Adapt<LiveHistoryContacts>();
|
|
|
|
|
await _rep.InsertAsync(entity);
|
|
|
|
|
await this.cacheService.DelLiveHistoryService(UserManager.UserId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 修改消息条数
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPut("/LiveHistoryContacts/UPLive")]
|
|
|
|
|
public async Task UPLive(UpdateLiveHistoryContactsInput input)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var entity = input.Adapt<LiveHistoryContacts>();
|
|
|
|
|
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
|
|
|
|
|
await this.cacheService.DelLiveHistoryService(UserManager.UserId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除联系人
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpDelete("/LiveHistoryContacts/DelLive")]
|
|
|
|
|
public async Task DelLive(long ID)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var entity = await this.liveUserFriend.SingleAsync(x => x.Id == ID);
|
|
|
|
|
await _rep.DeleteAsync(entity);
|
|
|
|
|
await this.cacheService.DelLiveHistoryService(UserManager.UserId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取联系人
|
|
|
|
|
/// </summary>
|
|
|
|
@ -51,37 +118,52 @@ namespace GDZZ.Application
|
|
|
|
|
[HttpGet("/LiveHistoryContacts/list")]
|
|
|
|
|
public async Task<List<LiveHistoryLists>> List()
|
|
|
|
|
{
|
|
|
|
|
List<LiveHistoryLists> historyLists= new List<LiveHistoryLists>();
|
|
|
|
|
|
|
|
|
|
var history = await this.cacheService.GetLiveHistoryService(UserManager.UserId);
|
|
|
|
|
if (!history.IsNullOrEmpty())
|
|
|
|
|
return history;
|
|
|
|
|
|
|
|
|
|
List<LiveHistoryLists> historyLists = new List<LiveHistoryLists>();
|
|
|
|
|
//获取好友列表
|
|
|
|
|
var friends = await this.liveUserFriend.Where(x => x.CreatedUserId == UserManager.UserId).ToListAsync();
|
|
|
|
|
var friends = await this.liveUserFriend.Where(x => x.CreatedUserId == UserManager.UserId).ToListAsync();
|
|
|
|
|
foreach (var item in friends)
|
|
|
|
|
{
|
|
|
|
|
var friend = await this._sysUserRep.SingleAsync(x => x.Id == item.FriendID);
|
|
|
|
|
if (friend == null)
|
|
|
|
|
|
|
|
|
|
//获取用户信息
|
|
|
|
|
var sysUser = await this._sysUserRep.SingleAsync(x => x.Id == item.FriendID);
|
|
|
|
|
if (sysUser == null)
|
|
|
|
|
continue;
|
|
|
|
|
historyLists.Add(new LiveHistoryLists()
|
|
|
|
|
var liveHis = new LiveHistoryLists()
|
|
|
|
|
{
|
|
|
|
|
Avatar = friend.Avatar,
|
|
|
|
|
DisplayName = friend.NickName,
|
|
|
|
|
Avatar = sysUser.Avatar,
|
|
|
|
|
DisplayName = sysUser.NickName,
|
|
|
|
|
Id = item.FriendID,
|
|
|
|
|
Index = friend.Name.Substring(0, 1),
|
|
|
|
|
Index = sysUser.Name.Substring(0, 1),
|
|
|
|
|
LastSendTime = new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds(),
|
|
|
|
|
Unread = 0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//rm -rf mk dir ls install cd home
|
|
|
|
|
|
|
|
|
|
}) ;
|
|
|
|
|
//查询聊天记录
|
|
|
|
|
//rm -rf mk dir ls install cd home //ru-rf
|
|
|
|
|
var live = await this.liveMessageRep.Where(x => x.CreatedUserId == UserManager.UserId && x.OtherUserID == item.FriendID || (x.CreatedUserId == item.FriendID && x.OtherUserID == UserManager.UserId)).OrderByDescending(x =>x.CreatedTime).FirstAsync();
|
|
|
|
|
|
|
|
|
|
if (live != null)
|
|
|
|
|
{
|
|
|
|
|
liveHis.LastContent = live.Content;
|
|
|
|
|
liveHis.Type = live.Type;
|
|
|
|
|
}
|
|
|
|
|
historyLists.Add(liveHis);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await this.cacheService.SetLiveHistoryService(UserManager.UserId, historyLists);
|
|
|
|
|
return historyLists;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 聊天详情
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取聊天信息列表
|
|
|
|
|
/// 获取聊天详情
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet("/LiveHistoryContacts/GetMessageLists")]
|
|
|
|
@ -92,24 +174,98 @@ namespace GDZZ.Application
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 增加聊天历史任务列表
|
|
|
|
|
/// 增加聊天详情
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost("/LiveHistoryContacts/addMessage")]
|
|
|
|
|
public async Task AddMessage(AddLiveMessageInput input)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var entity = input.Adapt<LiveMessage>();
|
|
|
|
|
var history = await this._rep.Where(x => (x.CreatedUserId == input.OtherUserID && x.UserID == UserManager.UserId) || (x.UserID == input.OtherUserID && x.CreatedUserId == UserManager.UserId)).SingleAsync();
|
|
|
|
|
var live = this.liveMessageRep.InsertReturnEntity(entity);
|
|
|
|
|
var user = await _sysOnlineUerRep.Where(m => m.UserId == entity.OtherUserID).OrderByDescending(x=>x.LastTime).FirstAsync();
|
|
|
|
|
var livemes = new LiveMessageInput()
|
|
|
|
|
{
|
|
|
|
|
id = live.Id.ToString(),
|
|
|
|
|
type = live.Type,
|
|
|
|
|
content = live.Content,
|
|
|
|
|
sendTime = new DateTimeOffset((DateTime)live.CreatedTime).ToUnixTimeSeconds(),
|
|
|
|
|
status = "succeed",
|
|
|
|
|
toContactId = entity.OtherUserID,
|
|
|
|
|
fromUser = new Fromuser()
|
|
|
|
|
{
|
|
|
|
|
avatar = this._sysUserRep.FirstOrDefaultAsync(x => x.Id == UserManager.UserId).Result.Avatar,
|
|
|
|
|
displayName = UserManager.Name,
|
|
|
|
|
id = UserManager.UserId
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
var livejson = livemes.ToJsonString();
|
|
|
|
|
await _chatHubContext.Clients.Client(user.ConnectionId).SendMessage(livejson, "SendMessage");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await this.liveMessageRep.InsertAsync(entity);
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 上传文件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="file"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost("/LiveHistoryContacts/upload")]
|
|
|
|
|
public async Task<LiveFileOutput> UploadFileDefault(IFormFile file)
|
|
|
|
|
{
|
|
|
|
|
// 可以读取系统配置来决定将文件存储到什么地方
|
|
|
|
|
return await UploadFile(file, _options.Default.path, FileLocation.LOCAL);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 上传文件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="file"></param>
|
|
|
|
|
/// <param name="pathType"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static async Task<LiveFileOutput> UploadFile(IFormFile file, string pathType, FileLocation fileLocation)
|
|
|
|
|
{
|
|
|
|
|
var fileSizeKb = (long)(file.Length / 1024.0); // 文件大小KB
|
|
|
|
|
var originalFilename = file.FileName; // 文件原始名称
|
|
|
|
|
var fileSuffix = Path.GetExtension(file.FileName).ToLower(); // 文件后缀
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
// 先存库获取Id
|
|
|
|
|
var id = YitIdHelper.NextId();
|
|
|
|
|
var newFile = new SysFile
|
|
|
|
|
{
|
|
|
|
|
Id = id,
|
|
|
|
|
FileLocation = (int)FileLocation.LOCAL,
|
|
|
|
|
FileBucket = FileLocation.LOCAL.ToString(),
|
|
|
|
|
FileObjectName = $"{YitIdHelper.NextId()}{fileSuffix}",
|
|
|
|
|
FileOriginName = originalFilename,
|
|
|
|
|
FileSuffix = fileSuffix.TrimStart('.'),
|
|
|
|
|
FileSizeKb = fileSizeKb.ToString(),
|
|
|
|
|
FilePath = pathType
|
|
|
|
|
};
|
|
|
|
|
newFile = await App.GetService<ISqlSugarClient>().Insertable(newFile).ExecuteReturnEntityAsync();
|
|
|
|
|
|
|
|
|
|
var finalName = newFile.FileObjectName; // 生成文件的最终名称
|
|
|
|
|
if (fileLocation == FileLocation.LOCAL) // 本地存储
|
|
|
|
|
{
|
|
|
|
|
var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, pathType);
|
|
|
|
|
if (!Directory.Exists(filePath))
|
|
|
|
|
Directory.CreateDirectory(filePath);
|
|
|
|
|
using (var stream = File.Create(Path.Combine(filePath, finalName)))
|
|
|
|
|
{
|
|
|
|
|
await file.CopyToAsync(stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (fileLocation == FileLocation.ALIYUN) // 阿里云OSS
|
|
|
|
|
{
|
|
|
|
|
var filePath = pathType + finalName;
|
|
|
|
|
OSSClientUtil.DeletefileCode(filePath);
|
|
|
|
|
|
|
|
|
|
var stream = file.OpenReadStream();
|
|
|
|
|
OSSClientUtil.PushMedia(stream, filePath);
|
|
|
|
|
}
|
|
|
|
|
newFile.FileObjectName = finalName;
|
|
|
|
|
return new LiveFileOutput() { Id = finalName, FileUrl= "/"+pathType + "/"+ finalName }; // 返回文件唯一标识
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|