修复用户端个人头像展示
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// 功能描述:封装用户端 HTTP 请求,并将服务端错误转换为安全、可读的中文提示。
|
||||
// 版本:1.1.0
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
@@ -104,6 +105,20 @@ class ApiClient {
|
||||
Future<Object?> get(String path, {bool authenticated = true}) =>
|
||||
_send('GET', path, authenticated: authenticated);
|
||||
|
||||
/// 读取需要鉴权的二进制资源;资源不存在时返回空值。
|
||||
Future<Uint8List?> getBytes(String path) async {
|
||||
final request = http.Request('GET', Uri.parse('$baseUrl$path'));
|
||||
request.headers['accept'] = 'image/jpeg, image/png';
|
||||
final token = _tokenProvider();
|
||||
if (token.isNotEmpty) request.headers['authorization'] = token;
|
||||
final response = await _sendRequest(request);
|
||||
if (response.statusCode == 404) return null;
|
||||
if (response.statusCode < 200 || response.statusCode >= 300) {
|
||||
throw ApiException(response.statusCode, '头像加载失败');
|
||||
}
|
||||
return response.bodyBytes.isEmpty ? null : response.bodyBytes;
|
||||
}
|
||||
|
||||
Future<Object?> post(
|
||||
String path, {
|
||||
Map<String, Object?>? body,
|
||||
|
||||
Reference in New Issue
Block a user