XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

STHApplication

【MAUI】寄当家——安卓端交易管理系统

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Text.Json;
using JiDangJia.Contracts;
using JiDangJia.Services;

namespace JiDangJia.Views;

public sealed class CommunityAdminPage : ContentPage
{
    private readonly Editor notice=new(){MaxLength=2000,AutoSize=EditorAutoSizeOption.TextChanges,MinimumHeightRequest=180};
    private CommunitySettings? settings;
    private readonly Entry personal=new(){Placeholder="个人发布总上限",Keyboard=Keyboard.Numeric},enterprise=new(){Placeholder="企业发布总上限",Keyboard=Keyboard.Numeric};
    public CommunityAdminPage()
    {
        if(!AuthClient.Current.IsAdmin)throw new InvalidOperationException("需要管理员权限");Title="社群内容管理";var body=Ui.Stack(Ui.Card(Ui.Stack(Ui.Section("社群风险字幕"),Ui.Text("所有账户看到的风险提示由此处统一发布。",12,Ui.Muted),notice,Ui.Button("发布风险提示",Save))),Ui.Card(Ui.Stack(Ui.Section("服务与供需数量管理"),Ui.Text("合计限制展示中的服务与供需卡;下架和删除不占名额。降低上限保留已有卡片,后续发布或上架需有空位。",12,Ui.Muted),Ui.Text("个人用户上限",12,Ui.Muted),personal,Ui.Text("已认证企业上限",12,Ui.Muted),enterprise,Ui.Button("保存发布总上限",Save),Ui.Button("用户标签与特殊发布额度",()=>Navigation.PushAsync(new CommunityLabelsAdminPage()),false))),Ui.Card(Ui.Stack(Ui.Section("官方推荐"),Ui.Button("+ 发布推荐图文 / 视频",()=>Navigation.PushAsync(new PublishPostPage("RECOMMEND"))),Ui.Button("管理推荐与置顶",()=>Navigation.PushAsync(new PostsPage("RECOMMEND")),false))),Ui.Button("刷新后台",Load,false));body.Padding=18;Content=new ScrollView{Content=body};
    }
    protected override async void OnAppearing(){base.OnAppearing();try{await Load();}catch(Exception ex){await DisplayAlertAsync("无法加载",ex.Message,"知道了");}}
    private async Task Load(){using var data=await AuthClient.Current.AdminRequest("/community/settings");settings=data.RootElement.Deserialize<CommunitySettings>(new JsonSerializerOptions(JsonSerializerDefaults.Web));notice.Text=settings?.Notice;personal.Text=settings?.PersonalSupplyLimit.ToString();enterprise.Text=settings?.EnterpriseSupplyLimit.ToString();}
    private async Task Save(){if(settings is null)throw new InvalidOperationException("请先刷新后台");if(!int.TryParse(personal.Text,out var p)||!int.TryParse(enterprise.Text,out var e))throw new ArgumentException("数量上限请填写整数");using var data=await AuthClient.Current.AdminRequest("/community/settings",new UpdateCommunitySettingsRequest(settings.Revision,notice.Text?.Trim()??"",p,e));settings=data.RootElement.Deserialize<CommunitySettings>(new JsonSerializerOptions(JsonSerializerDefaults.Web));await DisplayAlertAsync("已保存","社群设置已更新,后续发布与上架按新上限执行。","完成");}
}

public sealed class CommunityLabelsAdminPage : ContentPage
{
    private readonly Entry query=new(){Placeholder="搜索账户 / 姓名",MaxLength=64};private readonly VerticalStackLayout list=Ui.Stack();
    public CommunityLabelsAdminPage(){if(!AuthClient.Current.IsAdmin)throw new InvalidOperationException("需要管理员权限");Title="用户标签与特殊发布额度";var body=Ui.Stack(Ui.Text("个人为蓝标,已通过企业认证为黄标。类型随认证状态更新;信用等级由管理员设置,默认良好。",12,Ui.Muted),Ui.Spread(query,Ui.Button("搜索 / 刷新",Load,false)),Ui.Text("显示前 100 个匹配账户,按账号可准确搜索。",11,Ui.Muted),list);body.Padding=18;Content=new ScrollView{Content=body};}
    protected override async void OnAppearing(){base.OnAppearing();try{await Load();}catch(Exception ex){await DisplayAlertAsync("加载失败",ex.Message,"知道了");}}
    private async Task Load()
    {
        using var data=await AuthClient.Current.AdminRequest("/community/labels?q="+Uri.EscapeDataString(query.Text?.Trim()??""));var users=data.RootElement.Deserialize<List<CommunityUserLabel>>(new JsonSerializerOptions(JsonSerializerDefaults.Web))??[];list.Clear();
        foreach(var user in users)
        {
            var credit=new Picker{Title="信用等级",ItemsSource=CommunityCatalog.CreditLevels.Values.ToList(),SelectedIndex=Math.Max(0,CommunityCatalog.CreditLevels.Keys.ToList().IndexOf(user.CreditLevel))};
            var limit=new Entry{Placeholder="特殊总额度(留空使用身份默认额度)",Keyboard=Keyboard.Numeric,Text=user.PublishLimitOverride?.ToString(),MaxLength=6};
            list.Add(Ui.Card(Ui.Stack(Ui.Text(user.Name+" · @"+user.Username,15,bold:true),CommunitySafetyUi.Badge(user.IdentityKind,user.Verified,user.CreditLevel),Ui.Text($"服务与供需合计 {user.ActiveSupply} / {user.SupplyLimit}"+(user.PublishLimitOverride.HasValue?" · 特殊额度":" · 默认额度"),12,Ui.Muted),credit,limit,Ui.Text("服务与供需共用此总额度,0 表示不可新发;留空恢复个人 / 企业默认额度。",11,Ui.Muted),Ui.Button("保存信用与发布额度",async()=>{int? count=null;if(!string.IsNullOrWhiteSpace(limit.Text)){if(!int.TryParse(limit.Text,out var n)||n is <0 or >100000)throw new ArgumentException("请填写 0–100000 的整数");count=n;}using var result=await AuthClient.Current.AdminRequest("/community/labels/"+user.Id,new UpdateUserLabelRequest(user.Revision,CommunityCatalog.CreditLevels.Keys.ElementAt(credit.SelectedIndex),count,true));await Load();},false))));
        }
    }
}