190 lines
5.9 KiB
C#
190 lines
5.9 KiB
C#
using AllBase;
|
|
using AllBase.Model.Result;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using TemuKJD.BaseForms;
|
|
using Work;
|
|
|
|
namespace TemuKJD.KJDForms.SystemForms
|
|
{
|
|
public partial class SystemUploadConfigForm : BaseForm
|
|
{
|
|
private UserConfig config = new UserConfig();
|
|
private readonly bool IsMainUser;
|
|
private bool isBusy;
|
|
|
|
public SystemUploadConfigForm()
|
|
{
|
|
InitializeComponent();
|
|
IsMainUser = BaseClass.GroupId == 3;
|
|
InitializePolicy(cmbSensitiveWordsPolicy);
|
|
InitializePolicy(cmbReturnAddressPolicy);
|
|
UpdateAccess();
|
|
lblStatus.Text = IsMainUser ? "可编辑、保存和读取配置。" : "当前账号只读。";
|
|
Load += btnLoad_Click;
|
|
}
|
|
|
|
private void UpdateAccess()
|
|
{
|
|
bool canEdit = IsMainUser && !isBusy;
|
|
txtSensitiveWords.ReadOnly = !canEdit;
|
|
chkUseOrderReturnWarehouseAddress.Enabled = canEdit;
|
|
cmbSensitiveWordsPolicy.Enabled = canEdit;
|
|
cmbReturnAddressPolicy.Enabled = canEdit;
|
|
btnSave.Visible = IsMainUser;
|
|
btnSave.Enabled = canEdit;
|
|
btnLoad.Enabled = !isBusy;
|
|
}
|
|
|
|
private async void btnLoad_Click(object sender, EventArgs e)
|
|
{
|
|
if (isBusy) return;
|
|
if (string.IsNullOrWhiteSpace(BaseClass.UserName))
|
|
{
|
|
lblStatus.Text = "读取失败:当前登录账号为空。";
|
|
return;
|
|
}
|
|
|
|
isBusy = true;
|
|
UpdateAccess();
|
|
lblStatus.Text = "正在读取配置…";
|
|
try
|
|
{
|
|
ApplyConfig(await LoadConfigAsync());
|
|
lblStatus.Text = IsMainUser ? "配置已读取。" : "配置已读取,当前账号只读。";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
lblStatus.Text = "读取失败:" + ex.Message;
|
|
}
|
|
finally
|
|
{
|
|
isBusy = false;
|
|
UpdateAccess();
|
|
}
|
|
}
|
|
|
|
private void ApplyConfig(UserConfig loadedConfig)
|
|
{
|
|
loadedConfig.Configs.TryGetValue(UserConfig.SensitiveWordsKey, out var words);
|
|
loadedConfig.Configs.TryGetValue(UserConfig.UseOrderReturnWarehouseAddressKey, out var address);
|
|
if ((words != null && !Enum.IsDefined(typeof(UserConfig.InheritPolicy), words.Policy)) ||
|
|
(address != null && !Enum.IsDefined(typeof(UserConfig.InheritPolicy), address.Policy)))
|
|
throw new InvalidOperationException("云端配置包含无效的继承策略。");
|
|
|
|
bool useAddress = false;
|
|
if (address != null && !bool.TryParse(address.Value, out useAddress))
|
|
throw new InvalidOperationException("云端退货仓库地址开关值无效。");
|
|
|
|
txtSensitiveWords.Text = words?.Value ?? string.Empty;
|
|
cmbSensitiveWordsPolicy.SelectedValue = words?.Policy ?? UserConfig.InheritPolicy.Free;
|
|
chkUseOrderReturnWarehouseAddress.Checked = useAddress;
|
|
cmbReturnAddressPolicy.SelectedValue = address?.Policy ?? UserConfig.InheritPolicy.Free;
|
|
config = loadedConfig;
|
|
}
|
|
|
|
private async void Save_Click(object sender, EventArgs e)
|
|
{
|
|
if (!IsMainUser)
|
|
{
|
|
lblStatus.Text = "当前账号只读,只有主账号可以保存配置。";
|
|
return;
|
|
}
|
|
if (isBusy) return;
|
|
if (string.IsNullOrWhiteSpace(BaseClass.UserName))
|
|
{
|
|
lblStatus.Text = "保存失败:当前登录账号为空。";
|
|
return;
|
|
}
|
|
|
|
isBusy = true;
|
|
UpdateAccess();
|
|
lblStatus.Text = "正在保存配置…";
|
|
try
|
|
{
|
|
config.Configs[UserConfig.SensitiveWordsKey] = new UserConfig.ConfigItem
|
|
{
|
|
Key = UserConfig.SensitiveWordsKey,
|
|
Value = txtSensitiveWords.Text,
|
|
Policy = (UserConfig.InheritPolicy)cmbSensitiveWordsPolicy.SelectedValue
|
|
};
|
|
config.Configs[UserConfig.UseOrderReturnWarehouseAddressKey] = new UserConfig.ConfigItem
|
|
{
|
|
Key = UserConfig.UseOrderReturnWarehouseAddressKey,
|
|
Value = chkUseOrderReturnWarehouseAddress.Checked.ToString(),
|
|
Policy = (UserConfig.InheritPolicy)cmbReturnAddressPolicy.SelectedValue
|
|
};
|
|
await UploadConfigAsync(JsonConvert.SerializeObject(config));
|
|
lblStatus.Text = "配置已保存。";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
lblStatus.Text = "保存失败:" + ex.Message;
|
|
}
|
|
finally
|
|
{
|
|
isBusy = false;
|
|
UpdateAccess();
|
|
}
|
|
}
|
|
|
|
public static async Task<UserConfig> LoadConfigAsync()
|
|
{
|
|
var url = BaseClass.BuildObjString("user/getUserConfig");
|
|
var data = UserLogin.GetSignPostData(new { });
|
|
var html = await GetHtml.PostJsonAsync(url, data);
|
|
return ParseConfigResponse(html);
|
|
}
|
|
|
|
private static UserConfig ParseConfigResponse(string html)
|
|
{
|
|
var ret = JsonConvert.DeserializeObject<Result<string>>(html);
|
|
if (ret == null)
|
|
throw new InvalidOperationException("服务器返回为空。");
|
|
if (ret.Code != 200)
|
|
throw new InvalidOperationException(ret.Msg ?? "读取配置失败。");
|
|
if (ret.Data == null)
|
|
throw new InvalidOperationException("服务器未返回配置数据。");
|
|
if (string.IsNullOrWhiteSpace(ret.Data))
|
|
return new UserConfig();
|
|
|
|
var loadedConfig = JsonConvert.DeserializeObject<UserConfig>(ret.Data);
|
|
if (loadedConfig?.Configs == null)
|
|
throw new InvalidOperationException("云端配置格式无效。");
|
|
return loadedConfig;
|
|
}
|
|
|
|
private async Task UploadConfigAsync(string configJson)
|
|
{
|
|
if (!IsMainUser)
|
|
throw new InvalidOperationException("只有主账号可以保存配置。");
|
|
var url = BaseClass.BuildObjString("user/saveUserConfig");
|
|
var data = UserLogin.GetSignPostData(new { Config = configJson });
|
|
var html = await GetHtml.PostJsonAsync(url, data);
|
|
var ret = JsonConvert.DeserializeObject<Result>(html);
|
|
if (ret == null)
|
|
throw new InvalidOperationException("服务器返回为空。");
|
|
if (ret.Code != 200)
|
|
throw new InvalidOperationException(ret.Msg ?? "保存配置失败。");
|
|
}
|
|
|
|
private sealed class UserConfigData
|
|
{
|
|
[JsonProperty(Required = Required.AllowNull)]
|
|
public string Config { get; set; }
|
|
}
|
|
|
|
private static void InitializePolicy(ComboBox comboBox)
|
|
{
|
|
comboBox.DataSource = new[]
|
|
{
|
|
new { Text = "自由", Value = UserConfig.InheritPolicy.Free },
|
|
new { Text = "锁定", Value = UserConfig.InheritPolicy.Locked },
|
|
new { Text = "仅追加", Value = UserConfig.InheritPolicy.AppendOnly }
|
|
};
|
|
}
|
|
}
|
|
}
|