Files
harmony-client/entry/src/main/ets/data/api/UserApi.ets

34 lines
888 B
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { HttpClient, ApiResult, ApiError } from 'common';
/**
* 后端用户接口 DTO(与后端字段约定对齐)。
*/
export class UserDto {
id: string = '';
name: string = '';
email: string = '';
}
/**
* 用户相关网络请求服务:负责发请求、解析统一响应、返回 DTO。
* TODO: BASE_URL 应改为环境注入(debug/release 分离)。
*/
export class UserApi {
private static readonly BASE_URL: string = 'https://api.example.com';
private readonly http: HttpClient;
constructor(http: HttpClient) {
this.http = http;
}
async fetchUser(id: string): Promise<UserDto> {
const raw = await this.http.get(`${UserApi.BASE_URL}/api/user/${id}`);
const result = JSON.parse(raw) as ApiResult<UserDto>;
if (!result.isSuccess()) {
throw ApiError.business(result.code, result.message);
}
return result.data;
}
}