207 lines
8.7 KiB
C#
207 lines
8.7 KiB
C#
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
|
||
{
|
||
/// <summary>
|
||
/// 管理员操作腾讯IM的帮助类
|
||
/// </summary>
|
||
[Inject(Lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Transient)]
|
||
public class TencentIMAdminHelper
|
||
{
|
||
private readonly IOptionsMonitor<ImConfig> _imConfig;
|
||
private readonly ILogger<TencentIMAdminHelper> _logger;
|
||
|
||
public TencentIMAdminHelper(IOptionsMonitor<ImConfig> imConfig,
|
||
ILogger<TencentIMAdminHelper> logger)
|
||
{
|
||
this._imConfig = imConfig;
|
||
this._logger = logger;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取管理员UserSig签名
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public string GetAdminUserSig()
|
||
{
|
||
TLSSigAPIv2 tLSSigAPIv2 = new TLSSigAPIv2(
|
||
_imConfig.CurrentValue.SDKAppID,
|
||
_imConfig.CurrentValue.Key);
|
||
return tLSSigAPIv2.genUserSig(_imConfig.CurrentValue.AdminUserId.ToString(), _imConfig.CurrentValue.ExpiredTime);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 单个账号导入
|
||
/// </summary>
|
||
/// <param name="account_ImportModel"></param>
|
||
/// <returns></returns>
|
||
public async Task<bool> 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<TencentResult>(resultString);
|
||
if (result.ErrorCode == 0)
|
||
{
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
_logger.LogError($"导入账号失败,错误码:{result.ErrorCode},错误信息:{result.ErrorInfo}");
|
||
throw new BusinessException($"账号创建错误,请联系管理员!");
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
/// <summary>
|
||
/// 批量账号导入
|
||
/// </summary>
|
||
/// <param name="account_ImportModels"></param>
|
||
/// <returns></returns>
|
||
public async Task<bool> Multiaccount_import(List<Account_importModel> 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<TencentResult>(resultString);
|
||
if (result.ErrorCode == 0)
|
||
{
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
_logger.LogError($"导入账号失败,错误码:{result.ErrorCode},错误信息:{result.ErrorInfo}");
|
||
throw new BusinessException($"账号创建错误,请联系管理员!");
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
/// <summary>
|
||
/// 服务端管理员用户设置用户资料
|
||
/// </summary>
|
||
/// <param name="SettingUserId">要设置的用户id</param>
|
||
/// <param name="SetetingUser">要设置的用户信息</param>
|
||
/// <returns></returns>
|
||
public async Task<bool> 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<TencentResult>(resultString);
|
||
if (result.ErrorCode == 0)
|
||
{
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
_logger.LogError($"用户资料设置失败,错误码:{result.ErrorCode},错误信息:{result.ErrorInfo}");
|
||
throw new BusinessException($"账号资料设置错误,请联系管理员!");
|
||
}
|
||
}
|
||
return false;
|
||
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 批量获取账号信息
|
||
/// </summary>
|
||
/// <param name="userIds"></param>
|
||
/// <returns></returns>
|
||
public async Task<List<TenantUserInfoModel>> 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<TenantItemResult<TenantUserInfoModel>>();
|
||
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;
|
||
}
|
||
|
||
}
|
||
}
|