captcha.component.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import {
  2. Component,
  3. OnInit,
  4. Input,
  5. Output,
  6. EventEmitter,
  7. ViewChild,
  8. ElementRef,
  9. } from "@angular/core";
  10. import { CommonModule } from "@angular/common";
  11. import { FormsModule } from "@angular/forms";
  12. import { NzInputModule } from 'ng-zorro-antd/input';
  13. @Component({
  14. imports:[CommonModule,FormsModule,NzInputModule],
  15. standalone:true,
  16. selector: 'fm-captcha',
  17. templateUrl: './captcha.component.html',
  18. styleUrls: ['./captcha.component.scss']
  19. })
  20. export class CaptchaComponent implements OnInit {
  21. @ViewChild("canvasDom") canvasDom: ElementRef | any; //本地校验码绘画
  22. @Input() type: string = 'input'; //default & input 默认input方式
  23. @Input() canvas_id: string = 'canvasCode'; //元素id
  24. @Input() drawCode: Array<string> = []; //验证码数组
  25. @Input() vCode: string = ''; //用户输入code
  26. @Output() changeVal: EventEmitter<any> = new EventEmitter(); //改变时触发
  27. @Output() onEnter: EventEmitter<any> = new EventEmitter(); //enter按下触发
  28. code:string = ''
  29. constructor() {}
  30. ngOnInit() {}
  31. ngAfterViewInit(): void {
  32. //Called after ngAfterContentInit when the component's view has been initialized. Applies to components only.
  33. //Add 'implements AfterViewInit' to the class.
  34. this.updateDrawCode();
  35. }
  36. updateDrawCode() {
  37. let canvas_width = 100;
  38. let canvas_height = 38;
  39. let canv = document.getElementById(this.canvas_id);
  40. canv && this.canvasDom.nativeElement.removeChild(canv);
  41. let _a = document.getElementById(this.canvas_id + '_a');
  42. _a && this.canvasDom.nativeElement.removeChild(_a);
  43. this.canvasDom.nativeElement;
  44. let canvas = document.createElement("canvas");
  45. canvas.className = "canvas";
  46. canvas.setAttribute("id", this.canvas_id);
  47. canvas.addEventListener("click", () => {
  48. this.updateDrawCode();
  49. });
  50. let context:any = canvas.getContext("2d"); //获取到canvas画图的环境,演员表演的舞台
  51. canvas.width = canvas_width;
  52. canvas.height = canvas_height;
  53. let sCode =
  54. "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0,q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m";
  55. let aCode = sCode.split(",");
  56. let aLength = aCode.length; //获取到数组的长度
  57. for (let i = 0; i <= 3; i++) {
  58. let j = Math.floor(Math.random() * aLength); //获取到随机的索引值
  59. let deg = (Math.random() * 30 * Math.PI) / 180; //产生0~30之间的随机弧度
  60. let txt = aCode[j]; //得到随机的一个内容
  61. this.drawCode[i] = txt;
  62. let x = 10 + i * 20; //文字在canvas上的x坐标
  63. let y = 20 + Math.random() * 8; //文字在canvas上的y坐标
  64. context.font = "bold 23px 微软雅黑";
  65. context.translate(x, y);
  66. context.rotate(deg);
  67. context.fillStyle = this.randomColor();
  68. context.fillText(txt, 0, 0);
  69. context.rotate(-deg);
  70. context.translate(-x, -y);
  71. }
  72. for (let i = 0; i <= 5; i++) {
  73. //验证码上显示线条
  74. context.strokeStyle = this.randomColor();
  75. context.beginPath();
  76. context.moveTo(
  77. Math.random() * canvas_width,
  78. Math.random() * canvas_height
  79. );
  80. context.lineTo(
  81. Math.random() * canvas_width,
  82. Math.random() * canvas_height
  83. );
  84. context.stroke();
  85. }
  86. for (let i = 0; i <= 30; i++) {
  87. //验证码上显示小点
  88. context.strokeStyle = this.randomColor();
  89. context.beginPath();
  90. let x = Math.random() * canvas_width;
  91. let y = Math.random() * canvas_height;
  92. context.moveTo(x, y);
  93. context.lineTo(x + 1, y + 1);
  94. context.stroke();
  95. }
  96. this.code = this.drawCode.join("")
  97. this.changeVal.emit({
  98. code:this.code,
  99. vCode:this.vCode
  100. });
  101. this.canvasDom.nativeElement.appendChild(canvas);
  102. let a = document.createElement("a");
  103. a.setAttribute("id", this.canvas_id + '_a');
  104. a.innerText = "看不清,换一张";
  105. a.addEventListener("click", () => {
  106. this.updateDrawCode();
  107. });
  108. this.canvasDom.nativeElement.appendChild(a);
  109. }
  110. /* 得到随机的颜色值 */
  111. randomColor() {
  112. let r = Math.floor(Math.random() * 256);
  113. let g = Math.floor(Math.random() * 256);
  114. let b = Math.floor(Math.random() * 256);
  115. return "rgb(" + r + "," + g + "," + b + ")";
  116. }
  117. //按下确认键触发
  118. enter(e:any) {
  119. if (e.keyCode == 13) {
  120. this.changeVal.emit({
  121. code:this.code,
  122. vCode:this.vCode
  123. });
  124. this.onEnter.emit()
  125. }
  126. }
  127. onChange(){
  128. this.changeVal.emit({
  129. code:this.code,
  130. vCode:this.vCode
  131. });
  132. }
  133. }