refactor(harmony): 23 处废弃 API 换成 UIContext 写法(全局 promptAction.showToast 自 API 18 废弃)

SDK 里写得很清楚:`@ohos.promptAction.d.ts` 的全局 `showToast` 标着 `@deprecated since 18`,
替代品是 `UIContext.getPromptAction()`。仓库里有 23 处这种调用(6 个页面,历史遗留)——
这一轮既然在按"用系统方案"整理鸿蒙侧,就一次扫干净,并加判据挡住回潮。

- `promptAction.showToast(...)` → `this.getUIContext().getPromptAction().showToast(...)`(23 处)
- 清掉不再需要的 `promptAction` import(多个 → 只留 `router` 等)
- 新增判据:不得再用全局写法。防的不是这次,而是**新增页面照抄旧代码**这条回退路径 ——
  它编译照样通过、只在真机上行为不同。自检同时验"认得出旧写法"与"不误伤新写法"。
- 顺带把权限徽标那条"点它弹说明"的判据改成钉**非废弃**写法(原来是 `promptAction.showToast(`)。

变异验证:把 `SettingsPage` 的一处改回全局写法 → 判红并点出文件。

验证:`hvigorw assembleHap` BUILD SUCCESSFUL;`npm test` 退出码 0(harmony-logic 20 条)。
This commit is contained in:
2026-09-14 14:05:35 +08:00
parent 36f3183bba
commit c6aaf8c468
8 changed files with 71 additions and 27 deletions

View File

@ -9,7 +9,6 @@ import { MailApi } from '../api/MailApi';
import { AccountManager, AccountInfo } from '../api/AccountManager';
import { SendMailRequest } from '../model/Models';
import { ComposeParams } from '../model/RouteParams';
import { promptAction } from '@kit.ArkUI';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { picker } from '@kit.CoreFileKit';
@ -92,7 +91,7 @@ struct ComposePage {
const mailApi: MailApi | null = this.createSelectedMailApi(ctx);
if (mailApi === null) {
promptAction.showToast({ message: '请先选择发信账号' });
this.getUIContext().getPromptAction().showToast({ message: '请先选择发信账号' });
return;
}
this.uploading = true;
@ -108,7 +107,7 @@ struct ComposePage {
hilog.info(0x0001, 'Compose', 'uploaded: %{public}s → %{public}s', name, attachmentId);
} catch (e) {
const apiError = e as ApiError;
promptAction.showToast({ message: '上传失败: ' + name + ' - ' + apiError.message });
this.getUIContext().getPromptAction().showToast({ message: '上传失败: ' + name + ' - ' + apiError.message });
}
}
this.status = this.attachmentIds.length + ' 个附件已上传';
@ -133,11 +132,11 @@ struct ComposePage {
return;
}
if (this.to.length === 0) {
promptAction.showToast({ message: '请填写收件人' });
this.getUIContext().getPromptAction().showToast({ message: '请填写收件人' });
return;
}
if (this.subject.length === 0) {
promptAction.showToast({ message: '请填写主题' });
this.getUIContext().getPromptAction().showToast({ message: '请填写主题' });
return;
}
const ctx: Context | undefined = this.getUIContext().getHostContext();
@ -146,7 +145,7 @@ struct ComposePage {
}
const mailApi: MailApi | null = this.createSelectedMailApi(ctx);
if (mailApi === null) {
promptAction.showToast({ message: '请先选择发信账号' });
this.getUIContext().getPromptAction().showToast({ message: '请先选择发信账号' });
return;
}
@ -166,11 +165,11 @@ struct ComposePage {
request.attachment_ids = this.attachmentIds;
}
await mailApi.send(request);
promptAction.showToast({ message: '✅ 邮件已发送' });
this.getUIContext().getPromptAction().showToast({ message: '✅ 邮件已发送' });
this.getUIContext().getRouter().back();
} catch (e) {
const apiError = e as ApiError;
promptAction.showToast({ message: '发送失败: ' + apiError.message });
this.getUIContext().getPromptAction().showToast({ message: '发送失败: ' + apiError.message });
} finally {
this.sending = false;
}

View File

@ -6,7 +6,7 @@ import { ApiClient, ApiError } from '../api/ApiClient';
import { Theme } from '../common/Theme';
import { MailApi } from '../api/MailApi';
import { MailSummary, Me } from '../model/Models';
import { promptAction, router } from '@kit.ArkUI';
import { router } from '@kit.ArkUI';
@Entry
@Component

View File

@ -10,7 +10,6 @@ import { AccountManager } from '../api/AccountManager';
import { SseService } from '../api/SseService';
import { Me } from '../model/Models';
import { DEFAULT_API_BASE, EMULATOR_HOST_BASE } from '../common/Config';
import { promptAction } from '@kit.ArkUI';
import { hilog } from '@kit.PerformanceAnalysisKit';
@Entry
@ -125,7 +124,7 @@ struct LoginPage {
const ae = e as ApiError;
const msg: string = ae.code === 0 ? ae.message : (ae.message.length > 0 ? ae.message : '登录失败');
hilog.error(0x0001, 'LoginPage', 'login failed: code=%{public}d msg=%{public}s', ae.code, msg);
promptAction.showToast({ message: msg });
this.getUIContext().getPromptAction().showToast({ message: msg });
} finally {
this.loading = false;
}

View File

@ -9,7 +9,6 @@ import { MailApi } from '../api/MailApi';
import { AccountManager, AccountInfo } from '../api/AccountManager';
import { MailDetail, SendMailRequest } from '../model/Models';
import { MailDetailParams } from '../model/RouteParams';
import { promptAction } from '@kit.ArkUI';
@Entry
@Component
@ -103,10 +102,10 @@ struct MailDetailPage {
try {
await m.setPermissionMode(sid, mode);
this.permissionMode = mode;
promptAction.showToast({ message: '权限已切换为 ' + mode });
this.getUIContext().getPromptAction().showToast({ message: '权限已切换为 ' + mode });
} catch (e) {
const ae = e as ApiError;
promptAction.showToast({ message: '切换失败: ' + ae.message });
this.getUIContext().getPromptAction().showToast({ message: '切换失败: ' + ae.message });
} finally {
this.switchingPerm = false;
}
@ -306,10 +305,10 @@ struct MailDetailPage {
await m.send(req);
this.showReplyBox = false;
this.replyBody = '';
promptAction.showToast({ message: '回复已发送' });
this.getUIContext().getPromptAction().showToast({ message: '回复已发送' });
} catch (e) {
const ae = e as ApiError;
promptAction.showToast({ message: '发送失败: ' + ae.message });
this.getUIContext().getPromptAction().showToast({ message: '发送失败: ' + ae.message });
} finally {
this.sending = false;
}

View File

@ -26,7 +26,6 @@ import {
enforcementLabel
} from '../model/MailGrouping';
import { MailDetailParams, ComposeParams } from '../model/RouteParams';
import { promptAction } from '@kit.ArkUI';
/** 一页取多少封。取满了就要如实提示"可能还有更多"(服务端 total 是未读数,不是总封数)。 */
const INBOX_PAGE_SIZE: number = 50;
@ -89,7 +88,7 @@ struct InboxTab {
break;
}
}
promptAction.showToast({ message: sourceName.length > 0 ? '📨 ' + sourceName + ' 收到新邮件' : '📨 新邮件到达' });
this.getUIContext().getPromptAction().showToast({ message: sourceName.length > 0 ? '📨 ' + sourceName + ' 收到新邮件' : '📨 新邮件到达' });
this.loadData();
}
};
@ -737,7 +736,7 @@ struct ContactsTab {
.padding({ left: 5, right: 5, top: 1, bottom: 1 })
.margin({ right: 6 })
.onClick(() => {
promptAction.showToast({
this.getUIContext().getPromptAction().showToast({
message: permissionHint(c.permission_mode, c.permission_enforcement)
+ '(强制力:' + enforcementLabel(c.permission_enforcement) + '',
duration: 6000

View File

@ -7,7 +7,6 @@ import { Theme } from '../common/Theme';
import { AuthApi } from '../api/AuthApi';
import { AccountManager, AccountInfo } from '../api/AccountManager';
import { SseService } from '../api/SseService';
import { promptAction } from '@kit.ArkUI';
@Entry
@Component
@ -66,7 +65,7 @@ struct SettingsPage {
if (account !== null) {
client.setBase(account.server);
client.setToken(account.token);
promptAction.showToast({ message: '默认发信账号已设为 ' + account.displayName });
this.getUIContext().getPromptAction().showToast({ message: '默认发信账号已设为 ' + account.displayName });
}
this.refreshList();
}
@ -92,7 +91,7 @@ struct SettingsPage {
}
}
this.refreshList();
promptAction.showToast({ message: '账号已删除' });
this.getUIContext().getPromptAction().showToast({ message: '账号已删除' });
}
async addNewAccount(): Promise<void> {
@ -106,7 +105,7 @@ struct SettingsPage {
const token: string = this.newToken.trim();
const optionalUsername: string = this.newUsername.trim();
if (displayName.length === 0 || server.length === 0 || token.length === 0) {
promptAction.showToast({ message: '请填写显示名称、Gateway 地址和 user_key' });
this.getUIContext().getPromptAction().showToast({ message: '请填写显示名称、Gateway 地址和 user_key' });
return;
}
@ -119,7 +118,7 @@ struct SettingsPage {
const username: string = optionalUsername.length > 0 ? optionalUsername : user.username;
const account: AccountInfo = await manager.addAccount(server, username, token, displayName);
SseService.getInstance().connectForAccount(account.id, account.server, account.token);
promptAction.showToast({ message: '✅ 添加成功: ' + account.displayName });
this.getUIContext().getPromptAction().showToast({ message: '✅ 添加成功: ' + account.displayName });
this.showAddDialog = false;
this.newDisplayName = '';
this.newServer = '';
@ -128,7 +127,7 @@ struct SettingsPage {
this.refreshList();
} catch (e) {
const apiError = e as ApiError;
promptAction.showToast({ message: '验证失败: ' + apiError.message });
this.getUIContext().getPromptAction().showToast({ message: '验证失败: ' + apiError.message });
} finally {
this.adding = false;
}