All files index.ts

90.32% Statements 84/93
60.41% Branches 29/48
72.72% Functions 8/11
90.36% Lines 75/83

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228  1x 10x 10x 10x 10x   1x 1x 1x 1x 1938x 366x   6x 6x                                   6x 6x 6x 6x 6x 6x 6x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x   6x 6x 30x 30x 30x 30x 30x     6x 6x 300x 300x 300x 300x     6x   1x                                                                                                                                               6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x             1x     4x     1x 1x           1x           1x           1x           1x           1x           1x           1x           1x           1x    
import { defineComponentHOC, Doraemon, Component, Prop } from '@doraemon-ui/miniprogram.core-js'
import { useRef, getSystemInfoSync } from '@doraemon-ui/miniprogram.shared'
import type { MiniprogramNodeRef, MiniprogramPublicInstance } from '@doraemon-ui/miniprogram.shared'
I
const randomNum = (min: number, max: number) => Math.floorE(Math.random() * (max - min) + min)
const randomColor = (min: number, max: number) => `rgb(${randomNum(min, max)}, ${randomNum(min, max)}, ${randomNum(min, max)})`
 
async function toDataURL(width: number, height: number, canvas: WechatMiniprogram.Canvas) {
  if (typeof (canvas as any).toDataURL === 'function') return (canvas as any).toDataURL('image/png', 1)
  if (typeof wx !== 'undefined' && typeof wx.canvasToTempFilePath === 'function') {
    const ratio = (getSystemInfoSync(['window']).pixelRatio as number) || 1
    return await new Promise<string>((resolve) => {
      wx.canvasToTempFilePath({
        destWidth: width * ratio,
    E    destHeight: height * ratio,
        canvas,
        fileType: 'png',
        quality: 1,
        success: (res) => resolve(res.tempFilePath || ''),
        fail: () => resolve(''),
      })
    })
  }
  return ''
}

function render(ctx: any, props: Vcode) {
  const ratio = (getSystemInfoSync(['window']).pixelRatio as number) || 1
  let vcode = ''
  ctx.textBaseline = 'bottom'
  ctx.fillStyle = props.bgColor || randomColor(180, 240)
  ctx.scale(ratio, ratio)
  ctx.fillRect(0, 0, props.width, props.height)

  for (let i = 0; i < props.num; i++) {
    const x = ((props.width - 10) / props.num) * i + 10
    const y = randomNum(props.height / 2, props.height)
    const deg = randomNum(-45, 45)
    const txt = props.str[randomNum(0, props.str.length)]
    const fontSize = randomNum(16, 40)
    const halfHeight = parseInt(String(props.height / 2), 10)
    vcode += txt
    ctx.fillStyle = props.fontColor || randomColor(10, 100)
    ctx.font = `normal normal normal ${fontSize > halfHeight ? halfHeight : fontSize}px sans-serif`
    ctx.translate(x, y)
    ctx.rotate((deg * Math.PI) / 180)
    ctx.fillText(txt, 0, 0)
    ctx.rotate((-deg * Math.PI) / 180)
    ctx.translate(-x, -y)
  }
  if (props.hasLine) {
    for (let i = 0; i < props.num; i++) {
      ctx.strokeStyle = randomColor(90, 180)
      ctx.beginPath()
      ctx.moveTo(randomNum(0, props.width), randomNum(0, props.height))
    E  ctx.lineTo(randomNum(0, props.width), randomNum(0, props.height))
      ctx.stroke()
    }
  }
  if (props.hasPoint) {
    for (let i = 0; i < props.num * 10; i++) {
      ctx.fillStyle = randomColor(0, 255)
      ctx.beginPath()
      ctx.arc(randomNum(0, props.width), randomNum(0, props.height), 1, 0, 2 * Math.PI)
    E  ctx.fill()
    }
  }
  return vcode
}
 
@Component({
  props: {
    prefixCls: {
      type: String,
      default: 'dora-vcode',
    },
  },
})
class Vcode extends Doraemon {
  /**
   * 自定义类名前缀
   *
   * @type {string}
   * @memberof Vcode
   */
  prefixCls!: string
 
  /**
   * 验证码字符源
   *
   * @type {string}
   * @memberof Vcode
   */
  @Prop({
    type: String,
    default: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
  })
  str: string
 
  /**
   * 验证码长度
   *
   * @type {number}
   * @memberof Vcode
   */
  @Prop({
    type: Number,
    default: 6,
  })
  num: number
 
  /**
   * 画布宽度
   *
   * @type {number}
   * @memberof Vcode
   */
  @Prop({
    type: Number,
    default: 120,
  })
  width: number
 
  /**
   * 画布高度
   *
   * @type {number}
   * @memberof Vcode
   */
  @Prop({
    type: Number,
    default: 40,
  })
  height: number
 
  /**
   * 背景颜色
   *
   * @type {string}
   * @memberof Vcode
   */
  @Prop({
    type: String,
    default: '',
  })
  bgColor: string
 
  /**
   * 字体颜色
   *
   * @type {string}
   * @memberof Vcode
   */
  @Prop({
    type: String,
    default:E '',
  })
  fontColor: string
 
  /**
   * 是否绘制噪点
   *
   * @type {boolean}
   * @memberof Vcode
   */
  @Prop({
    type: Boolean,
    default: true,
  })
  hasPoint: boolean
 
  /**
   * 是否绘制干扰线
   *
   * @type {boolean}
   * @memberof Vcode
   */
  @Prop({
    type: Boolean,
    default: true,
  })
  hasLine: boolean
 
  /**
   * canvas 节点 id
   *
   * @type {string}
   * @memberof Vcode
   */
  @Prop({
    type: String,
    default: 'dora-vcode',
  })
  canvasId: string
 
  async createCanvasContext() {
    try {
      const ref = (await useRef(
        `#${this.canvasId}`,
        this._renderProxy as unknown as MiniprogramPublicInstance,
      )) as unknown as MiniprogramNodeRef
      const canvas = ref.node as unknown as WechatMiniprogram.Canvas
      const ctx = canvas.getContext('2d')
      const ratio = (getSystemInfoSync(['window']).pixelRatio as number) || 1
      canvas.width = this.width * ratio
      canvas.height = this.height * ratio
      const value = render(ctx, this)
      const base64Url = await toDataURL(this.width, this.height, canvas)
      if ((ctx as any).restore) (ctx as any).restore()
      this.$emit('change', { value, base64Url })
    } catch (err) {
      this.$emit('error', err)
    }
  }
 
  draw() {
    void this.createCanvasContext()
  }
 
  mounted() {
    void this.createCanvasContext()
  }
}
 
export { Vcode }
 
export default defineComponentHOC()(Vcode)