东风启辰小程序端
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

88 linhas
3.3KB

  1. module.exports = class Mcaptcha {
  2. constructor(options) {
  3. this.options = options;
  4. this.fontSize = options.height * 3 / 6;
  5. this.init();
  6. this.refresh();
  7. }
  8. init() {
  9. this.ctx = wx.createCanvasContext(this.options.el);
  10. this.ctx.setTextBaseline("middle");
  11. this.ctx.setFillStyle(this.randomColor(180, 240));
  12. }
  13. refresh() {
  14. var code = '';
  15. var txtArr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  16. for (var i = 0; i < 4; i++) {
  17. code += txtArr[this.randomNum(0, txtArr.length)];
  18. }
  19. this.options.createCodeImg = code;
  20. let arr = (code + '').split('');
  21. if (arr.length === 0) {
  22. arr = ['e', 'r', 'r', 'o', 'r'];
  23. };
  24. let offsetLeft = this.options.width * 0.6 / (arr.length - 1);
  25. let marginLeft = this.options.width * 0.2;
  26. arr.forEach((item, index) => {
  27. this.ctx.setFillStyle(this.randomColor(0, 180));
  28. let size = this.randomNum(24, this.fontSize);
  29. this.ctx.setFontSize(size);
  30. let dis = offsetLeft * index + marginLeft - size * 0.3;
  31. let deg = this.randomNum(-30, 30);
  32. this.ctx.translate(dis, this.options.height * 0.5);
  33. this.ctx.rotate(deg * Math.PI / 180);
  34. this.ctx.fillText(item, 0, 0);
  35. this.ctx.rotate(-deg * Math.PI / 180);
  36. this.ctx.translate(-dis, -this.options.height * 0.5);
  37. })
  38. for (var i = 0; i < 4; i++) {
  39. this.ctx.strokeStyle = this.randomColor(40, 180);
  40. this.ctx.beginPath();
  41. this.ctx.moveTo(this.randomNum(0, this.options.width), this.randomNum(0, this.options.height));
  42. this.ctx.lineTo(this.randomNum(0, this.options.width), this.randomNum(0, this.options.height));
  43. this.ctx.stroke();
  44. }
  45. for (var i = 0; i < this.options.width / 4; i++) {
  46. this.ctx.fillStyle = this.randomColor(0, 255);
  47. this.ctx.beginPath();
  48. this.ctx.arc(this.randomNum(0, this.options.width), this.randomNum(0, this.options.height), 1, 0, 2 * Math.PI);
  49. this.ctx.fill();
  50. }
  51. this.ctx.draw(false, setTimeout(() => {
  52. // this.ctx.vCodeImg
  53. wx.canvasToTempFilePath({
  54. width: this.options.width,
  55. height: this.options.height,
  56. canvasId: this.options.el,
  57. success: res => {
  58. var callUrl = this.options.callUrl;
  59. if (callUrl) {
  60. if (callUrl.that && callUrl.obj) {
  61. callUrl['obj'].call(callUrl.that,res);
  62. }
  63. }
  64. }
  65. })
  66. }),200);
  67. }
  68. validate(code) {
  69. var code = code.toLowerCase();
  70. var v_code = this.options.createCodeImg.toLowerCase();
  71. console.log(code)
  72. console.log(v_code.substring(v_code.length - 4))
  73. if (code == v_code.substring(v_code.length - 4)) {
  74. return true;
  75. } else {
  76. return false;
  77. }
  78. }
  79. randomNum(min, max) {
  80. return Math.floor(Math.random() * (max - min) + min);
  81. }
  82. randomColor(min, max) {
  83. let r = this.randomNum(min, max);
  84. let g = this.randomNum(min, max);
  85. let b = this.randomNum(min, max);
  86. return "rgb(" + r + "," + g + "," + b + ")";
  87. }
  88. }