处理国家多语言数据

This commit is contained in:
yxw
2025-12-25 18:43:30 +08:00
parent f7bb6bb2dc
commit 46794708ff
14 changed files with 240 additions and 35 deletions

View File

@@ -27,6 +27,9 @@
<li>
<a href="/currency/list">货币设置</a>
</li>
<li>
<a href="/country/list">国家管理</a>
</li>
</PageContainer>
@code {

View File

@@ -10,7 +10,7 @@
<Breadcrumb>
<Breadcrumb>
<BreadcrumbItem Href="/">管理后台</BreadcrumbItem>
<BreadcrumbItem Href="/admin/list">系统配置</BreadcrumbItem>
<BreadcrumbItem Href="/settings">系统配置</BreadcrumbItem>
<BreadcrumbItem Href="/country/list">国家管理</BreadcrumbItem>
</Breadcrumb>
</Breadcrumb>
@@ -19,7 +19,7 @@
<Spin Spinning="pageLoading">
<Card Title="国家信息">
<Form @ref="editform" Model="@model" LabelColSpan="5" WrapperColSpan="14" OnFinish="OnFormFinishAsync">
@if (Id > 0 && languageList.Any())
@if (Id > 0 && languageList.Count() > 0)
{
<Tabs ActiveKey="@context.LanguageId" OnTabClick="OnLanguageTabChange">
<TabPane Key="0">
@@ -82,7 +82,7 @@
CountryModel model { get; set; } = new();
Form<CountryModel> editform = null!;
Country Country = new();
CountryLocalizedModel country = new();
List<KeyValue> languageList = new();
@@ -96,6 +96,7 @@
protected override void OnParametersSet()
{
_ = LoadLanguage();
if (Id > 0)
{
LoadData();
@@ -110,28 +111,30 @@
if (apiResult.Success)
{
languageList = apiResult.Data ?? new List<KeyValue>();
StateHasChanged();
}
}
async void LoadData()
{
pageLoading = true;
var url = $"/api/currency/{Id}";
var apiResult = await HttpService.Get<ApiResult<Currency>>(url);
var url = $"/api/country/detail?id={Id}";
var apiResult = await HttpService.Get<ApiResult<CountryLocalizedModel>>(url);
if (apiResult.Success)
{
if (apiResult.Data == null)
{
Navigation.NavigateTo($"/currency/create");
Navigation.NavigateTo($"/country/create");
}
else
{
country = apiResult.Data;
model = apiResult.Data.Adapt<CountryModel>();
}
}
else
{
Navigation.NavigateTo($"/currency/create");
Navigation.NavigateTo($"/country/create");
}
pageLoading = false;
@@ -143,16 +146,14 @@
if (editform.Validate())
{
saving = true;
var url = $"api/currency/save";
var url = $"api/country/save";
var result = new ApiResult<string>();
result = await HttpService.Post<ApiResult<string>>(url, model);
if (result.Code == (int)ResultCode.Success)
if (result.Success)
{
saving = false;
await ModalService.InfoAsync(new ConfirmOptions() { Title = "提示", Content = "数据提交成功!" });
Navigation.NavigateTo($"/currency/list");
Navigation.NavigateTo($"/country/list");
}
else
{
@@ -167,7 +168,7 @@
if (key != "0")
{
model.LanguageId = key;
var data = model.Localized.Where(p => p.LanguageId == key.ToInt()).ToList();
var data = country.Locales.Where(p => p.LanguageId == key.ToInt()).ToList();
if (data.Any())
{
var name = nameof(model.Name);
@@ -181,10 +182,8 @@
}
else
{
// model = Mapper.Map<CountryModel>(model);
// model.LanguageId = key;
// model.IsEdit = true;
model = country.Adapt<CountryModel>();
model.LanguageId = key;
}
}

View File

@@ -8,8 +8,8 @@
<PageContainer Title="国家管理">
<Breadcrumb>
<Breadcrumb>
<BreadcrumbItem>Home</BreadcrumbItem>
<BreadcrumbItem>系统配置</BreadcrumbItem>
<BreadcrumbItem Href="/">管理后台</BreadcrumbItem>
<BreadcrumbItem Href="/settings">系统配置</BreadcrumbItem>
<BreadcrumbItem>国家管理</BreadcrumbItem>
</Breadcrumb>
</Breadcrumb>
@@ -22,6 +22,8 @@
<FormItem Label="名称">
<Input @bind-Value="search.Name" Placeholder="名称" AllowClear />
</FormItem>
</Col>
<Col>
<div class="ant-form-item">
<Button Type="ButtonType.Primary" HtmlType="submit">查询</Button>
<Button Style="margin: 0 8px;" OnClick="OnSearchReset">重置</Button>
@@ -30,7 +32,8 @@
</Row>
</Form>
</Card>
<Card Title="" Class="hideborder">
<br />
<Card Title="国家列表" Class="hideborder">
<Extra>
<div class="extraContent">
<Button Type="ButtonType.Primary" HtmlType="submit" OnClick="HandleAddNew">新增国家</Button>
@@ -121,10 +124,10 @@
async Task LoadListAsync()
{
pageLoading = true;
try
{
pageLoading = true;
var url = "/api/country/search";
var url = "/api/country/searh";
var apiResult = await HttpService.GetPagingList<Country>(url, search, Page.GetValueOrDefault(1), PageSize.GetValueOrDefault(20));
if (apiResult.Success)
{

View File

@@ -7,8 +7,8 @@
<PageContainer Title="货币管理">
<Breadcrumb>
<Breadcrumb>
<BreadcrumbItem>Home</BreadcrumbItem>
<BreadcrumbItem>系统配置</BreadcrumbItem>
<BreadcrumbItem Href="/">管理后台</BreadcrumbItem>
<BreadcrumbItem Href="/settings">系统配置</BreadcrumbItem>
<BreadcrumbItem>货币管理</BreadcrumbItem>
</Breadcrumb>
</Breadcrumb>

View File

@@ -8,8 +8,8 @@
<PageContainer Title="州省管理">
<Breadcrumb>
<Breadcrumb>
<BreadcrumbItem>Home</BreadcrumbItem>
<BreadcrumbItem>系统配置</BreadcrumbItem>
<BreadcrumbItem Href="/">管理后台</BreadcrumbItem>
<BreadcrumbItem Href="/settings">系统配置</BreadcrumbItem>
<BreadcrumbItem>州省管理</BreadcrumbItem>
</Breadcrumb>
</Breadcrumb>

View File

@@ -101,6 +101,49 @@ namespace Atomx.Admin.Controllers
return new JsonResult(result);
}
/// <summary>
/// 通过ID获取数据
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
public IActionResult Get(long id)
{
var result = new ApiResult<Area>();
var data = _dbContext.Areas.SingleOrDefault(p => p.Id == id);
if (data == null)
{
return new JsonResult(new ApiResult<string>().IsFail("数据不存在", null));
}
result = result.IsSuccess(data);
return new JsonResult(result);
}
/// <summary>
/// 通过ID获取详情,含多语言
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("detail")]
public IActionResult Detail(long id)
{
var result = new ApiResult<AreaLocalizedModel>();
var data = _dbContext.Areas.SingleOrDefault(p => p.Id == id);
if (data == null)
{
return new JsonResult(new ApiResult<string>().IsFail("数据不存在", null));
}
var localizedList = _dbContext.LocalizedProperties.Where(p => p.EntityId == id).ToList();
var model = _mapper.Map<AreaLocalizedModel>(data);
model.Locales = localizedList;
result = result.IsSuccess(model);
return new JsonResult(result);
}
/// <summary>
/// 新增编辑数据

View File

@@ -1,5 +1,4 @@
using Atomx.Admin.Client.Models;
using Atomx.Admin.Client.Validators;
using Atomx.Admin.Services;
using Atomx.Common.Entities;
using Atomx.Common.Models;

View File

@@ -2,10 +2,12 @@
using Atomx.Admin.Services;
using Atomx.Common.Constants;
using Atomx.Common.Entities;
using Atomx.Common.Enums;
using Atomx.Common.Models;
using Atomx.Data;
using Atomx.Data.CacheServices;
using Atomx.Data.Services;
using Atomx.Utils.Extension;
using FluentValidation;
using MapsterMapper;
using Microsoft.AspNetCore.Authorization;
@@ -60,7 +62,6 @@ namespace Atomx.Admin.Controllers
/// <param name="size"></param>
/// <returns></returns>
[HttpPost("searh")]
[Authorize(Policy = Permissions.User.View)]
public IActionResult Search(CountrySearch search, int page, int size = 20)
{
if (page < 1)
@@ -88,11 +89,31 @@ namespace Atomx.Admin.Controllers
}
/// <summary>
/// 详情,含多语言
/// 通过ID获取数据
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost("detail")]
[HttpGet]
public IActionResult Get(long id)
{
var result = new ApiResult<Country>();
var data = _dbContext.Countries.SingleOrDefault(p => p.Id == id);
if (data == null)
{
return new JsonResult(new ApiResult<string>().IsFail("数据不存在", null));
}
result = result.IsSuccess(data);
return new JsonResult(result);
}
/// <summary>
/// 通过ID获取详情,含多语言
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("detail")]
public IActionResult Detail(long id)
{
var result = new ApiResult<CountryLocalizedModel>();
@@ -139,9 +160,74 @@ namespace Atomx.Admin.Controllers
return new JsonResult(new ApiResult<string>().IsFail("数据不存在", null));
}
if (model.LanguageId.ToInt() == 0)
{
data = _mapper.Map(model, data);
_dbContext.SaveChanges();
}
else
{
var localizedList = _dbContext.LocalizedProperties.Where(p => p.LanguageId == model.LanguageId.ToInt() && p.EntityId == model.Id).ToList();
var name = nameof(model.Name);
var nameData = localizedList.SingleOrDefault(p => p.Key == name);
var newLocalizedList = new List<LocalizedProperty>();
var addLocalizedList = new List<LocalizedProperty>();
if (nameData == null)
{
if (!string.IsNullOrEmpty(model.Name))
{
nameData = new LocalizedProperty()
{
Id = _idCreator.CreateId(),
EntityId = model.Id,
LanguageId = model.LanguageId.ToInt(),
Type = (int)LocalizedType.Country,
Key = name,
Value = model.Name
};
addLocalizedList.Add(nameData);
}
}
else
{
if (!string.IsNullOrEmpty(model.Name))
{
nameData.Value = model.Name;
}
else
{
_dbContext.LocalizedProperties.Remove(nameData);
}
}
if (nameData != null)
{
newLocalizedList.Add(nameData);
}
if (addLocalizedList.Any())
{
_dbContext.LocalizedProperties.AddRange(addLocalizedList);
}
data.Enabled = model.Enabled;
data.DisplayOrder = model.DisplayOrder;
try
{
_dbContext.SaveChanges();
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}");
}
}
}
else

View File

@@ -1,5 +1,4 @@
using Atomx.Admin.Client.Models;
using Atomx.Admin.Client.Validators;
using Atomx.Admin.Services;
using Atomx.Common.Entities;
using Atomx.Common.Enums;

View File

@@ -91,6 +91,49 @@ namespace Atomx.Admin.Controllers
return new JsonResult(result);
}
/// <summary>
/// 通过ID获取数据
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
public IActionResult Get(long id)
{
var result = new ApiResult<StateProvince>();
var data = _dbContext.StateProvinces.SingleOrDefault(p => p.Id == id);
if (data == null)
{
return new JsonResult(new ApiResult<string>().IsFail("数据不存在", null));
}
result = result.IsSuccess(data);
return new JsonResult(result);
}
/// <summary>
/// 通过ID获取详情,含多语言
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("detail")]
public IActionResult Detail(long id)
{
var result = new ApiResult<StateProvinceLocalizedModel>();
var data = _dbContext.StateProvinces.SingleOrDefault(p => p.Id == id);
if (data == null)
{
return new JsonResult(new ApiResult<string>().IsFail("数据不存在", null));
}
var localizedList = _dbContext.LocalizedProperties.Where(p => p.EntityId == id).ToList();
var model = _mapper.Map<StateProvinceLocalizedModel>(data);
model.Locales = localizedList;
result = result.IsSuccess(model);
return new JsonResult(result);
}
/// <summary>
/// 新增编辑数据

View File

@@ -3,5 +3,8 @@
public enum LocalizedType
{
None,
Country,
StateProvince,
AreaCity
}
}

View File

@@ -0,0 +1,9 @@
using Atomx.Common.Entities;
namespace Atomx.Common.Models
{
public class AreaLocalizedModel:Area
{
public List<LocalizedProperty> Locales { get; set; } = new List<LocalizedProperty>();
}
}

View File

@@ -0,0 +1,9 @@
using Atomx.Common.Entities;
namespace Atomx.Common.Models
{
public class CategoryLocalizedModel : Category
{
public List<LocalizedProperty> Locales { get; set; } = new List<LocalizedProperty>();
}
}

View File

@@ -0,0 +1,9 @@
using Atomx.Common.Entities;
namespace Atomx.Common.Models
{
public class StateProvinceLocalizedModel : StateProvince
{
public List<LocalizedProperty> Locales { get; set; } = new List<LocalizedProperty>();
}
}