using LearningOfficer.OA.Common.Attributes;
using LearningOfficer.OA.Common.Configs;
using LearningOfficer.OA.Common.Dtos.TencentIM;
using LearningOfficer.OA.Common.Exceptions;
using Masuit.Tools;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Serilog.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UserCenter.Model;
namespace LearningOfficer.OA.Common.Helpers
{
///
/// 管理员操作腾讯IM的帮助类
///
[Inject(Lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Transient)]
public class TencentIMAdminHelper
{
private readonly IOptionsMonitor _imConfig;
private readonly ILogger _logger;
public TencentIMAdminHelper(IOptionsMonitor imConfig,
ILogger logger)
{
this._imConfig = imConfig;
this._logger = logger;
}
///
/// 获取管理员UserSig签名
///
///
public string GetAdminUserSig()
{
TLSSigAPIv2 tLSSigAPIv2 = new TLSSigAPIv2(
_imConfig.CurrentValue.SDKAppID,
_imConfig.CurrentValue.Key);
return tLSSigAPIv2.genUserSig(_imConfig.CurrentValue.AdminUserId.ToString(), _imConfig.CurrentValue.ExpiredTime);
}
///
/// 单个账号导入
///
///
///
public async Task Account_import(Account_importModel account_ImportModel)
{
var UserSig = GetAdminUserSig();
var avatar = string.IsNullOrWhiteSpace(account_ImportModel.FaceUrl) ? _imConfig.CurrentValue.DefaultAvater : account_ImportModel.FaceUrl;
var random = new Random().NextInt64(0, 4294967295);
var ApiUrl = _imConfig.CurrentValue.ApiHeadUrl + $"v4/im_open_login_svc/account_import?sdkappid={_imConfig.CurrentValue.SDKAppID}&identifier={_imConfig.CurrentValue.AdminUserId}&usersig={UserSig}&random={random}&contenttype=json";
var request = new
{
UserID = account_ImportModel.UserID,
Nick = account_ImportModel.Nick,
FaceUrl = avatar
};
var resultString = await new RestSharpHelper().PostAsync(ApiUrl, JsonConvert.SerializeObject(request));
if (!string.IsNullOrEmpty(resultString))
{
TencentResult result = JsonConvert.DeserializeObject(resultString);
if (result.ErrorCode == 0)
{
return true;
}
else
{
_logger.LogError($"导入账号失败,错误码:{result.ErrorCode},错误信息:{result.ErrorInfo}");
throw new BusinessException($"账号创建错误,请联系管理员!");
}
}
return false;
}
///
/// 批量账号导入
///
///
///
public async Task Multiaccount_import(List account_ImportModels)
{
var UserSig = GetAdminUserSig();
var random = new Random().NextInt64(0, 4294967295);
var ApiUrl = _imConfig.CurrentValue.ApiHeadUrl + $"v4/im_open_login_svc/multiaccount_import?sdkappid={_imConfig.CurrentValue.SDKAppID}&identifier={_imConfig.CurrentValue.AdminUserId}&usersig={UserSig}&random={random}&contenttype=json";
var request = new
{
AccountList = account_ImportModels.Select(x => new
{
UserID = x.UserID,
Nick = x.Nick,
FaceUrl = string.IsNullOrWhiteSpace(x.FaceUrl) ? _imConfig.CurrentValue.DefaultAvater : x.FaceUrl
}).ToArray()
};
var resultString = await new RestSharpHelper().PostAsync(ApiUrl, JsonConvert.SerializeObject(request));
if (!string.IsNullOrEmpty(resultString))
{
TencentResult result = JsonConvert.DeserializeObject(resultString);
if (result.ErrorCode == 0)
{
return true;
}
else
{
_logger.LogError($"导入账号失败,错误码:{result.ErrorCode},错误信息:{result.ErrorInfo}");
throw new BusinessException($"账号创建错误,请联系管理员!");
}
}
return false;
}
///
/// 服务端管理员用户设置用户资料
///
/// 要设置的用户id
/// 要设置的用户信息
///
public async Task SettingPortrait(string SettingUserId, TencentSettingModel SetetingUser)
{
var UserSig = GetAdminUserSig();
var random = new Random().NextInt64(0, 4294967295);
var ApiUrl = _imConfig.CurrentValue.ApiHeadUrl + $"v4/profile/portrait_set?sdkappid={_imConfig.CurrentValue.SDKAppID}&identifier={_imConfig.CurrentValue.AdminUserId}&usersig={UserSig}&random={random}&contenttype=json";
var profileItem = SetetingUser.GetType()
.GetProperties()
.Where(p => p.GetValue(SetetingUser) != null)
.Select(p => new
{
Tag = p.Name,
Value = p.GetValue(SetetingUser)
})
.ToArray();
var request = new
{
From_Account = SettingUserId,
ProfileItem = profileItem
};
var resultString = await new RestSharpHelper().PostAsync(ApiUrl, JsonConvert.SerializeObject(request));
if (!string.IsNullOrEmpty(resultString))
{
TencentResult result = JsonConvert.DeserializeObject(resultString);
if (result.ErrorCode == 0)
{
return true;
}
else
{
_logger.LogError($"用户资料设置失败,错误码:{result.ErrorCode},错误信息:{result.ErrorInfo}");
throw new BusinessException($"账号资料设置错误,请联系管理员!");
}
}
return false;
}
///
/// 批量获取账号信息
///
///
///
public async Task> GetAccountInfo(params long[] userIds)
{
var UserSig = GetAdminUserSig();
var random = new Random().NextInt64(0, 4294967295);
var ApiUrl = _imConfig.CurrentValue.ApiHeadUrl + $"v4/im_open_login_svc/account_check?sdkappid={_imConfig.CurrentValue.SDKAppID}&identifier={_imConfig.CurrentValue.AdminUserId}&usersig={UserSig}&random={random}&contenttype=json";
var CheckItem = userIds.ToList().ConvertAll(x => new
{
UserID = x.ToString()
});
var request = new
{
CheckItem = CheckItem
};
var resultString = await new RestSharpHelper().PostAsync(ApiUrl, JsonConvert.SerializeObject(request));
if (string.IsNullOrEmpty(resultString))
{
_logger.LogError("获取IM账号信息失败,请求失败");
throw new BusinessException("获取IM账号信息失败");
}
var resultResponse = resultString.FromJson>();
if (resultResponse.ErrorCode != 0)
{
_logger.LogError("获取IM账号信息失败," + resultResponse.ErrorInfo);
throw new BusinessException("获取IM账号信息失败," + resultResponse.ErrorInfo);
}
var result = resultResponse.ResultItem.Where(x => x.ResultCode != 0);
var errorList = result.Select(x => $@"UserId:{x.UserID},ErrorInfo{x.ResultInfo}").ToList();
if (errorList.Any())
{
_logger.LogError("获取IM账号信息失败," + string.Join(",", errorList));
throw new BusinessException("获取IM账号信息失败!");
}
return resultResponse.ResultItem;
}
}
}