import 'package:autosos_flutter/util/sp_util.dart';
import 'package:autosos_flutter/util/xhttp.dart';

/// 提供已注入 token 的 XHttp（保证所有业务 API 自动带 token）
class ApiClient {
  ApiClient._();
  static final ApiClient instance = ApiClient._();

  XHttp get http => XHttp.getInstance();

  /// 登录态写入后调用，刷新后续请求头
  void setToken(String token) {
    XHttp.setHeader('Authorization', 'Bearer $token');
  }

  void clearToken() {
    XHttp.removeHeader('Authorization');
  }

  String? get token {
    final v = SPUtil().get('accessToken');
    return v is String ? v : null;
  }
}