以下是项目的基本文件结构:
代码拆分为四个模块,分别为主界面逻辑(index.ets
)、API 服务类(apiService.ets
)、数据模型(exchange.ets
)以及模块配置(module.json5
)。
jsDemo1125/
├── entry/
│ ├── src/
│ │ ├── main/
│ │ │ ├── ets/
│ │ │ │ ├── pages/
│ │ │ │ │ ├── index.ets # 主界面逻辑
│ │ │ │ │ ├── apiService.ets # API 服务类
│ │ │ │ │ ├── exchange.ets # 数据模型
│ │ │ │ │
│ │ │ │ ├── module.json5 # 模块配置
│ │ │ ├── resources/
│ │ │ │ ├── base/
│ │ │ │ ├── en/
│ │ │ │ ├── zh/
├── .hvigor/
├── .idea/
├── build/
主页面(index.ets): 用户输入美元金额后,点击或自动触发查询按钮,将调用在线汇率 API 查询并返回实时结果。
API 服务(apiService.ets): 提供与 API 后端交互的逻辑,负责发送请求并返回结果。
数据模型(exchange.ets): 处理 API 返回的 JSON 数据,转化为可用的对象数据。
模块配置(module.json5): 包含权限申请、页面路径配置等基础信息。
jsimport { ApiService } from './apiService';
import { ExchangeResponse, ExchangeData } from './exchange';
@Entry
@Component
export struct Index {
@State value: number = 0; // 用户输入的美元金额
@State exchangeRate: string = "等待输入..."; // 显示兑换结果
@State updateTime: string = "暂无数据"; // 汇率更新时间
@State errorMessage: string = ""; // 错误提示信息
private apiService = new ApiService();
// 获取汇率逻辑
getExchangeRate(amount: number): void {
this.apiService.getExchangeData("USD", "CNY", (response, error) => {
if (response && response.code === 200 && response.data) {
const exchangeData: ExchangeData = response.data as ExchangeData;
this.exchangeRate = ((exchangeData.exchange ?? 0) * amount).toFixed(2);
this.updateTime = exchangeData.update_time ?? "未知时间";
this.errorMessage = "";
} else {
this.errorMessage = error || "API请求失败";
}
});
}
// 输入防抖逻辑
handleInput(value: string): void {
const inputValue = parseFloat(value);
if (!isNaN(inputValue) && inputValue > 0) {
this.value = inputValue;
this.getExchangeRate(inputValue);
} else {
this.errorMessage = "请输入有效金额";
}
}
build() {
Column() {
Text("汇率转换器")
.fontSize(24)
.fontWeight(FontWeight.Bold)
.margin(20);
TextInput({
placeholder: "请输入兑换的美元金额",
text: this.value.toString(),
})
.type(InputType.Number)
.onChange((e: string) => {
this.handleInput(e);
})
.padding({ left: 20, right: 20 })
.backgroundColor("#f0f0f0")
.borderRadius(8);
if (this.errorMessage) {
Text(this.errorMessage)
.fontColor("#FF0000")
.margin(10);
} else {
Text(`兑换金额: ${this.exchangeRate} 人民币`).margin(10);
Text(`更新时间: ${this.updateTime}`).margin(10);
}
}
.height("100%")
.width("100%")
.backgroundColor("#FFFFFF");
}
}
对于index的修改:
jsprivate debounceTimeout: number | null = null;
handleInput(value: string): void {
if (this.debounceTimeout) {
clearTimeout(this.debounceTimeout);
}
this.debounceTimeout = setTimeout(() => {
const inputValue = parseFloat(value);
if (!isNaN(inputValue) && inputValue > 0) {
this.value = inputValue;
this.getExchangeRate(inputValue);
} else {
this.errorMessage = "请输入有效的金额";
}
}, 500); // 防抖延迟 500 毫秒
}
jsimport http from '@ohos.net.http';
import { ExchangeResponse } from './exchange';
export class ApiService {
private readonly API_URL = "https://v2.alapi.cn/api/exchange";
private readonly API_KEY = "4YxBbmkhGAWA9BcW";
public getExchangeData(
from: string,
to: string,
callback: (response: ExchangeResponse | null, error: string | null) => void
): void {
const httpRequest = http.createHttp();
httpRequest.request(
this.API_URL,
{
method: http.RequestMethod.POST,
header: { 'Content-Type': 'application/json' },
extraData: {
token: this.API_KEY,
from: from,
to: to,
money: 1,
},
expectDataType: http.HttpDataType.STRING,
},
(err, data) => {
if (!err) {
try {
const response: ExchangeResponse = JSON.parse(data.result.toString());
callback(response, null);
} catch (parseError) {
callback(null, "数据解析错误");
}
} else {
callback(null, "网络请求失败");
}
httpRequest.destroy();
}
);
}
}
对于apiService.ets的修改:
jstry {
const response: ExchangeResponse = JSON.parse(data.result.toString());
if (response.code === 200 && response.data) {
// 确保数据是字符串或 JSON 对象
if (typeof response.data === "string") {
response.data = JSON.parse(response.data) as ExchangeData;
}
callback(response, null);
} else {
callback(null, response.msg || "API返回错误");
}
} catch (parseError) {
callback(null, "数据解析错误");
}
jsexport class ExchangeResponse {
code: number = 0; // 状态码
msg: string = ""; // 返回消息
data?: string | ExchangeData = undefined; // 返回的数据
}
export class ExchangeData {
exchange?: number = 0; // 汇率
update_time?: string = ""; // 更新时间
}
js{
"module": {
"name": "entry",
"type": "entry",
"description": "$string:module_desc",
"mainElement": "EntryAbility",
"deviceTypes": ["phone", "tablet", "2in1"],
"requestPermissions": [
{ "name": "ohos.permission.INTERNET" }
],
"pages": [
{ "srcEntry": "./ets/pages/index.ets", "name": "Index" }
]
}
}
本文作者:Reeshiram
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!