在 OpenHarmony 中,“验证码如何更新”通常出现在登录/注册界面,常见做法是点击验证码图片或按钮重新请求接口。下面从常见场景 + 示例代码说明。
// ets 页面
@State captchaUrl: string = ''
aboutToAppear() {
this.refreshCaptcha()
}
refreshCaptcha() {
const timestamp = Date.now()
this.captchaUrl = `https://api.example.com/captcha?t=${timestamp}`
}
Image(this.captchaUrl)
.width(120)
.height(40)
.onClick(() => {
this.refreshCaptcha()
})
✅ 原理:URL 变化 → 浏览器/系统重新请求 → 获取新验证码
如果后端返回 Base64:
refreshCaptcha() {
httpRequest({
url: 'https://api.example.com/captcha',
method: 'GET'
}).then(res => {
this.captchaUrl = 'data:image/png;base64,' + res.data
})
}
@State countdown: number = 0
timer: number = 0
sendCode() {
if (this.countdown > 0) return
// 调用发送验证码接口
sendSmsCode().then(() => {
this.countdown = 60
this.timer = setInterval(() => {
this.countdown--
if (this.countdown <= 0) {
clearInterval(this.timer)
}
}, 1000)
})
}
Button(this.countdown > 0 ? `${this.countdown}s` : '获取验证码')
.enabled(this.countdown === 0)
.onClick(() => this.sendCode())
网络权限
"module": {
"reqPermissions": [
{ "name": "ohos.permission.INTERNET" }
]
}
ArkUI 状态更新
@State接口防刷
可以告诉我:
我可以直接给你对应完整示例。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。