using System.Net.Http.Json; using System.Text.Json; using JiDangJia.Contracts; namespace JiDangJia.Services; public sealed partial class AuthClient { #if !COMMUNITY_APP public async Task PurchaseUrl() {using var result=await Request("/purchase",auth:false);var url=result.RootElement.GetProperty("url").GetString()??"";return Uri.TryCreate(url,UriKind.Absolute,out var uri)&&uri.Scheme=="https"&&uri.UserInfo.Length==0&&!uri.IsLoopback?url:"";} #endif #if !ACCOUNTING_APP private static void PrivateTransport() {if(!ServerSettings.AllowPrivateHttpTrial&&!ServerSettings.BaseUrl.StartsWith("https://",StringComparison.OrdinalIgnoreCase))throw new InvalidOperationException("认证及证照互换需要 HTTPS 服务器地址");} public async Task SafetyRequest(string path,object? data=null,bool admin=false) { RequireAccess();PrivateTransport();if(admin&&!IsAdmin)throw new InvalidOperationException("需要管理员权限"); using var result=await Request((admin?"/admin/safety":"/safety")+path,data);return result.RootElement.Deserialize(communityJson)??throw new InvalidOperationException("资料响应无效"); } #endif public async Task AccountRequest(string path,object? data=null) {using var result=await Request("/user"+path,data);return result.RootElement.Deserialize(communityJson)??throw new InvalidOperationException("账户响应无效");} public async Task VerifyPassword(string password){using var result=await Request("/user/password/verify",new{password});} #if !ACCOUNTING_APP public async Task UploadDocument(byte[] bytes,string purpose,string nonce) { RequireAccess();PrivateTransport();var version=Volatile.Read(ref generation);using var request=new HttpRequestMessage(HttpMethod.Post,ServerUrl+"/api/safety/documents"); request.Headers.Authorization=new("Bearer",token);request.Headers.Add("X-Device-Id",DeviceId);request.Headers.Add("X-Document-Purpose",purpose);request.Headers.Add("X-Upload-Id",nonce);request.Content=new ByteArrayContent(bytes); using var response=await http.SendAsync(request);if(version!=Volatile.Read(ref generation)||Locked)throw new OperationCanceledException("账户已切换或锁定");var text=await response.Content.ReadAsStringAsync();if(version!=Volatile.Read(ref generation)||Locked)throw new OperationCanceledException("账户已切换或锁定");using var data=JsonDocument.Parse(text); if(!response.IsSuccessStatusCode)throw new InvalidOperationException(data.RootElement.TryGetProperty("error",out var error)?error.GetString():"资料上传失败");return data.RootElement.Deserialize(communityJson)!; } public async Task DownloadPrivate(string path,CancellationToken ct=default) { RequireAccess();PrivateTransport();var version=Volatile.Read(ref generation);using var request=new HttpRequestMessage(HttpMethod.Get,ServerUrl+"/api"+path);request.Headers.Authorization=new("Bearer",token);request.Headers.Add("X-Device-Id",DeviceId); using var response=await http.SendAsync(request,ct);if(!response.IsSuccessStatusCode)throw new InvalidOperationException("私密资料暂不可用或没有读取权限");var bytes=await response.Content.ReadAsByteArrayAsync(ct);if(version!=Volatile.Read(ref generation)||Locked)throw new OperationCanceledException("账户已切换或锁定");return bytes; } #endif }