181 lines
5.2 KiB
C#
181 lines
5.2 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using XiangYi.Application.Interfaces;
|
|
using XiangYi.Core.Entities.Biz;
|
|
using XiangYi.Core.Interfaces;
|
|
|
|
namespace XiangYi.Application.Services;
|
|
|
|
/// <summary>
|
|
/// 系统配置服务实现
|
|
/// </summary>
|
|
public class SystemConfigService : ISystemConfigService
|
|
{
|
|
private readonly IRepository<SystemConfig> _configRepository;
|
|
private readonly ILogger<SystemConfigService> _logger;
|
|
|
|
/// <summary>
|
|
/// 默认头像配置键
|
|
/// </summary>
|
|
public const string DefaultAvatarKey = "default_avatar";
|
|
|
|
/// <summary>
|
|
/// 用户协议配置键
|
|
/// </summary>
|
|
public const string UserAgreementKey = "user_agreement";
|
|
|
|
/// <summary>
|
|
/// 隐私协议配置键
|
|
/// </summary>
|
|
public const string PrivacyPolicyKey = "privacy_policy";
|
|
|
|
/// <summary>
|
|
/// 会员权益长图配置键
|
|
/// </summary>
|
|
public const string MemberBenefitsImageKey = "member_benefits_image";
|
|
|
|
/// <summary>
|
|
/// 搜索页Banner配置键
|
|
/// </summary>
|
|
public const string SearchBannerKey = "search_banner";
|
|
|
|
/// <summary>
|
|
/// 管家二维码配置键
|
|
/// </summary>
|
|
public const string ButlerQrcodeKey = "butler_qrcode";
|
|
|
|
public SystemConfigService(
|
|
IRepository<SystemConfig> configRepository,
|
|
ILogger<SystemConfigService> logger)
|
|
{
|
|
_configRepository = configRepository;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<string?> GetConfigValueAsync(string key)
|
|
{
|
|
var configs = await _configRepository.GetListAsync(c => c.ConfigKey == key);
|
|
return configs.FirstOrDefault()?.ConfigValue;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> SetConfigValueAsync(string key, string value, string? description = null)
|
|
{
|
|
try
|
|
{
|
|
var configs = await _configRepository.GetListAsync(c => c.ConfigKey == key);
|
|
var config = configs.FirstOrDefault();
|
|
|
|
if (config != null)
|
|
{
|
|
config.ConfigValue = value;
|
|
config.UpdateTime = DateTime.Now;
|
|
if (description != null)
|
|
{
|
|
config.Description = description;
|
|
}
|
|
await _configRepository.UpdateAsync(config);
|
|
}
|
|
else
|
|
{
|
|
config = new SystemConfig
|
|
{
|
|
ConfigKey = key,
|
|
ConfigValue = value,
|
|
ConfigType = "system",
|
|
Description = description,
|
|
CreateTime = DateTime.Now,
|
|
UpdateTime = DateTime.Now
|
|
};
|
|
await _configRepository.AddAsync(config);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "设置配置失败: {Key}", key);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<string?> GetDefaultAvatarAsync()
|
|
{
|
|
return await GetConfigValueAsync(DefaultAvatarKey);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> SetDefaultAvatarAsync(string avatarUrl)
|
|
{
|
|
return await SetConfigValueAsync(DefaultAvatarKey, avatarUrl, "新用户默认头像");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<Dictionary<string, string>> GetAllConfigsAsync()
|
|
{
|
|
var configs = await _configRepository.GetListAsync(c => true);
|
|
return configs.ToDictionary(c => c.ConfigKey, c => c.ConfigValue);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<string?> GetUserAgreementAsync()
|
|
{
|
|
return await GetConfigValueAsync(UserAgreementKey);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> SetUserAgreementAsync(string content)
|
|
{
|
|
return await SetConfigValueAsync(UserAgreementKey, content, "用户协议内容");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<string?> GetPrivacyPolicyAsync()
|
|
{
|
|
return await GetConfigValueAsync(PrivacyPolicyKey);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> SetPrivacyPolicyAsync(string content)
|
|
{
|
|
return await SetConfigValueAsync(PrivacyPolicyKey, content, "隐私协议内容");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<string?> GetMemberBenefitsImageAsync()
|
|
{
|
|
return await GetConfigValueAsync(MemberBenefitsImageKey);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> SetMemberBenefitsImageAsync(string imageUrl)
|
|
{
|
|
return await SetConfigValueAsync(MemberBenefitsImageKey, imageUrl, "会员权益长图URL");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<string?> GetSearchBannerAsync()
|
|
{
|
|
return await GetConfigValueAsync(SearchBannerKey);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> SetSearchBannerAsync(string imageUrl)
|
|
{
|
|
return await SetConfigValueAsync(SearchBannerKey, imageUrl, "搜索页Banner图URL");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<string?> GetButlerQrcodeAsync()
|
|
{
|
|
return await GetConfigValueAsync(ButlerQrcodeKey);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> SetButlerQrcodeAsync(string imageUrl)
|
|
{
|
|
return await SetConfigValueAsync(ButlerQrcodeKey, imageUrl, "管家指导二维码URL");
|
|
}
|
|
}
|