@page "/area/create/{countryId:long}" @page "/area/create/{countryId:long}/{stateProvinceId:long}" @page "/area/edit/{countryId:long}/{stateProvinceId:long}/{id:long}" @page "/{locale}/area/create/{countryId:long}" @page "/{locale}/area/create/{countryId:long}/{stateProvinceId:long}" @page "/{locale}/area/create/{countryId:long}/{stateProvinceId:long}/{id:long}" @inject ILogger Logger @attribute [Authorize] 管理后台 系统配置 国家管理 州/省管理
@code { [Parameter] public string Locale { get; set; } = string.Empty; [Parameter] public long CountryId { get; set; } [Parameter] public long StateProvinceId { get; set; } [Parameter] public long Id { get; set; } [SupplyParameterFromForm] AreaModel model { get; set; } = new(); Form editform = null!; List languageList = new(); Country country = new(); List stateProvinceList = new(); StateProvinceLocalizedModel stateProvince = new(); bool pageLoading = false; bool saving = false; protected override void OnInitialized() { base.OnInitialized(); } protected override void OnParametersSet() { model.CountryId = CountryId; _ = LoadLanguage(); _ = LoadCountry(); _ = LoadStateProvince(); if (Id > 0) { LoadData(); } base.OnParametersSet(); } async Task LoadCountry() { var url = $"/api/country/{CountryId}"; var apiResult = await HttpService.Get>(url); if (apiResult.Success) { if (apiResult.Data != null) { country = apiResult.Data; StateHasChanged(); } else { Navigation.NavigateTo($"/country/list"); } } } async Task LoadStateProvince() { var url = $"/api/stateprovince/select/{CountryId}"; var apiResult = await HttpService.Get>>(url); if (apiResult.Success) { if (apiResult.Data != null) { stateProvinceList = apiResult.Data; StateHasChanged(); } else { Navigation.NavigateTo($"/country/list"); } stateProvinceList.Insert(0, new KeyValue() { Key = "0", Value = "请选择州/省" }); } } async Task LoadStateProvince(long id) { var url = $"/api/stateprovince/select/{CountryId}"; var apiResult = await HttpService.Get>>(url); if (apiResult.Success) { if (apiResult.Data != null) { stateProvinceList = apiResult.Data; StateHasChanged(); } else { Navigation.NavigateTo($"/country/list"); } stateProvinceList.Insert(0, new KeyValue() { Key = "0", Value = "请选择州/省" }); } } async Task LoadLanguage() { var url = $"/api/language/enabled"; var apiResult = await HttpService.Get>>(url); if (apiResult.Success) { if (apiResult.Data == null) { languageList = new List(); } else { languageList = apiResult.Data; languageList.Insert(0, new KeyValue() { Key = "0", Value = "标准" }); } StateHasChanged(); } } async void LoadData() { pageLoading = true; var url = $"/api/stateprovince/detail?id={Id}"; var apiResult = await HttpService.Get>(url); if (apiResult.Success) { if (apiResult.Data == null) { Navigation.NavigateTo($"/country/list"); } else { stateProvince = apiResult.Data; model = apiResult.Data.Adapt(); } } else { Navigation.NavigateTo($"/country/list"); } pageLoading = false; StateHasChanged(); } async void OnFormFinishAsync() { if (editform.Validate()) { saving = true; var url = $"api/stateprovince/save"; var result = new ApiResult(); result = await HttpService.Post>(url, model); if (result.Code == (int)ResultCode.Success) { saving = false; await ModalService.InfoAsync(new ConfirmOptions() { Title = "提示", Content = "数据提交成功!" }); Navigation.NavigateTo($"/stateprovince/list/{CountryId}"); } else { saving = false; await ModalService.ErrorAsync(new ConfirmOptions() { Title = "服务异常", Content = result.Message }); } } } }