HarmonyOS 鸿蒙Next 图形验证码主动调用刷新问题
HarmonyOS 鸿蒙Next 图形验证码主动调用刷新问题
如何在Index页面主动调用验证码刷新,我试了调用 new CommonUtil().drawImgCode(this.context,this.canvas_width,this.canvas_height)画布不会刷新,测试代码如下
import ImageCode from './ImageCode';
@Entry
@Component
struct Index {
build() {
Row() {
TextInput({placeholder:'请输入验证码'})
.layoutWeight(1)
ImageCode()
.width(100)
.height(40)
}
.height('100%')
.width('100%')
}
}
export class CommonUtil{
//随机生成文本内容时使用的值
sCode: string = "1,2,3,4,5,6,7,8,9,0";
aCode: string[] = this.sCode.split(",");
aLength: number = this.aCode.length; //获取到数组的长度
/**
* 绘制图形验证码
* @param context
* @param canvas_width
* @param canvas_height
* @returns
*/
drawImgCode(context: CanvasRenderingContext2D, canvas_width: number = 100, canvas_height: number = 40): string {
//用于保存验证码
let showCode: string = ''
//清楚当前画布内容,用作刷新画布
context.clearRect(0, 0, canvas_width, canvas_height)
for (let i = 0; i < 4; i++) { //这里的for循环可以控制验证码位数
const j = Math.floor(Math.random() * this.aLength); //获取到随机的索引值
const deg = Math.random() - 0.5; //产生一个随机弧度
const txt = this.aCode[j]; //得到随机的一个内容
showCode += txt.toLowerCase(); //转小写
const x = 10 + i * 20; //文字在canvas上的x坐标
const y = canvas_height / 2 + Math.random() * 8; //文字在canvas上的y坐标
context.font = "20vp sans-serif";
context.translate(x, y);
context.rotate(deg);
context.fillStyle = this.getColor();
context.fillText(txt, 0, 0);
context.rotate(-deg);
context.translate(-x, -y);
}
for (let i = 0; i <= 5; i++) { //验证码上显示线条
context.strokeStyle = this.getColor();
context.beginPath();
context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
context.stroke();
}
for (let i = 0; i <= 20; i++) { //验证码上的小点
context.strokeStyle = this.getColor(); //随机生成
context.beginPath();
const x = Math.random() * canvas_width;
const y = Math.random() * canvas_height;
context.moveTo(x, y);
context.lineTo(x + 1, y + 1);
context.stroke();
}
return showCode
}
/**
* 获取随机颜色 rgb
* @returns
*/
getColor() {
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
return "rgb(" + r + "," + g + "," + b + ")";
}
}
import {CommonUtil } from '../pages/CommonUtil';
@Component
export default struct ImageCode {
//用来配置CanvasRenderingContext2D对象的参数,包括是否开启抗锯齿,true表明开启抗锯齿。
private settings: RenderingContextSettings = new RenderingContextSettings(true)
//用来创建CanvasRenderingContext2D对象,通过在canvas中调用CanvasRenderingContext2D对象来绘制。
private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
//指定canvas要绘制的图形的宽(可使用@Prop装饰器装饰,由调用此组件的父组件传递)
@State canvas_width: number = 100;
//指定canvas要绘制的图形的高
@State canvas_height: number = 40;
//用于接收图形验证码的文本值
@State showCode: string = ''
build() {
Row() {
//在canvas中调用CanvasRenderingContext2D对象。
Canvas(this.context)
.width(this.canvas_width)
.height(this.canvas_height)
.backgroundColor('#CCC')
.onReady(() => {
//在这里绘制图形
this.showCode = new CommonUtil().drawImgCode(this.context,this.canvas_width,this.canvas_height)
console.log(this.showCode)
})
.onClick(() => {
this.showCode = new CommonUtil().drawImgCode(this.context,this.canvas_width,this.canvas_height)
console.log(this.showCode)
})
}
.width('100%')
.height('100%')
}
}
更多关于HarmonyOS 鸿蒙Next 图形验证码主动调用刷新问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
请参考以下示例(CommonUtil不用动): 1.index。ets
import ImageCode , { ChildRef} from './ImageCode';
@Entry
@Component
struct Index {
private settings: RenderingContextSettings = new RenderingContextSettings(true)
//用来创建CanvasRenderingContext2D对象,通过在canvas中调用CanvasRenderingContext2D对象来绘制。
private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
@State showCode:string = ''
build() {
Row() {
TextInput({placeholder:'请输入验证码'})
.layoutWeight(1)
ImageCode({showCode:this.showCode,controller:ChildRef})
.width(140)
.height(40)
Text('刷新').onClick(()=>{
let sss = ChildRef.changeText('')
console.log('aaaaaaaa=',sss)
})
}
.height('100%')
.width('100%')
}
}
2.ImageCode.ets
import {CommonUtil } from '../pages/CommonUtil';
@Component
export default struct ImageCode {
//用来配置CanvasRenderingContext2D对象的参数,包括是否开启抗锯齿,true表明开启抗锯齿。
private settings: RenderingContextSettings = new RenderingContextSettings(true)
//用来创建CanvasRenderingContext2D对象,通过在canvas中调用CanvasRenderingContext2D对象来绘制。
private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
//指定canvas要绘制的图形的宽(可使用@Prop装饰器装饰,由调用此组件的父组件传递)
@State canvas_width: number = 100;
//指定canvas要绘制的图形的高
@State canvas_height: number = 40;
//用于接收图形验证码的文本值
@Link showCode: string
private controller: ImageCodeController = new ImageCodeController();
aboutToAppear(): void {
if(this.controller) {
this.controller.changeText = this.changeText
}
}
private changeText = (value: string) =>{
return new CommonUtil().drawImgCode(this.context,this.canvas_width,this.canvas_height)
console.log('bbb')
}
build() {
Row() {
//在canvas中调用CanvasRenderingContext2D对象。
Canvas(this.context)
.width(this.canvas_width)
.height(this.canvas_height)
.backgroundColor('#CCC')
.onReady(() => {
//在这里绘制图形
this.showCode = new CommonUtil().drawImgCode(this.context,this.canvas_width,this.canvas_height)
console.log(this.showCode)
})
.onClick(() => {
new CommonUtil().drawImgCode(this.context,this.canvas_width,this.canvas_height)
})
}
.width('100%')
.height('100%')
}
}
class ImageCodeController {
changeText = (value: string) => {
return value
console.log('11111')
}
}
export let ChildRef = new ImageCodeController()
更多关于HarmonyOS 鸿蒙Next 图形验证码主动调用刷新问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next系统中,图形验证码的主动调用刷新通常涉及UI组件的更新与事件处理机制。具体实现步骤如下:
-
UI布局定义:在XML或JSON布局文件中,定义验证码显示区域及刷新按钮。确保为验证码视图和刷新按钮分配唯一的ID。
-
数据绑定与事件监听:在页面的ViewModel或对应的逻辑层中,绑定验证码视图的数据源,并设置刷新按钮的点击事件监听器。
-
验证码生成与更新:在点击事件处理函数中,实现验证码的重新生成逻辑。这通常包括生成新的验证码字符串,并更新验证码视图显示的内容。同时,可能需要触发UI刷新以确保新验证码即时显示。
-
防刷机制(可选):为防止恶意刷验证码,可加入验证码刷新次数限制或时间间隔限制。
-
错误处理:处理验证码生成或更新过程中可能出现的异常,确保用户体验不受影响。
综上所述,通过定义UI布局、绑定数据、监听事件、生成与更新验证码,即可在HarmonyOS鸿蒙Next系统中实现图形验证码的主动调用刷新功能。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。