feat(entry): 建立 MVVM+data 分层骨架,接入 V2 状态管理与 Navigation 路由
This commit is contained in:
33
entry/src/main/ets/data/api/UserApi.ets
Normal file
33
entry/src/main/ets/data/api/UserApi.ets
Normal file
@@ -0,0 +1,33 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user