怎么在vue中封装一个随机验证码组件?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
成都创新互联公司-专业网站定制、快速模板网站建设、高性价比大箐山网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式大箐山网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖大箐山地区。费用合理售后完善,10年实体公司更值得信赖。
<template> <div > <canvas :width="contentWidth" :height="contentHeight" id="cav" @click="next()"></canvas> </div> </template> <script> // import num from './components/cc' export default { name: 'verificationCode', props: { fontSize: { type: Number, default: 20 }, contentWidth: { type: Number, default: 100 }, contentHeight: { type: Number, default: 40 }, verification: { type: String }, refreshCode: { type: Boolean } }, watch: { verification: function (newVal) { if (newVal.toLowerCase() === this.identify.toLowerCase()) { this.$emit('handleIdentify', true) } else { this.$emit('handleIdentify', false) } }, refreshCode: function (newVal) { this.draw() } }, mounted () { this.draw() }, data () { return { identify: '', letter: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, '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'] } }, methods: { draw () { let self = this let text = this.randomNum() this.identify = text.join('') let ctx = document.getElementById('cav') let cav = ctx.getContext('2d') cav.clearRect(0, 0, self.contentWidth, self.contentHeight) let w = ctx.width let h = ctx.height cav.textBaseline = 'bottom' self.drawText(cav, text[0], 10, 10, self.randomDeg(0, 0.1), 20) self.drawText(cav, text[1], 20, 10, self.randomDeg(0, 0.1)) self.drawText(cav, text[2], 25, 10, self.randomDeg(0, 0.1)) self.drawText(cav, text[3], 30, 10, self.randomDeg(0, 0.1)) let i for (i = 0; i < 3; i++) { self.drawLine(cav, self.randomDeg(0, 0.3 * w, 1), self.randomDeg(0, h, 1), self.randomDeg(0, w, 1), self.randomDeg(0, h, 1), self.randomDeg(0.8 * w, w, 1), self.randomDeg(0, h, 1)) } }, drawText (cav, text, x1, y1, route) { cav.beginPath() cav.fillStyle = '#ffe9db' cav.font = this.fontSize + 'px' + ' ' + '黑体' cav.translate(x1, y1) cav.rotate(Math.PI * route) cav.fillText(text, x1, y1) cav.fill() cav.rotate(-Math.PI * route) cav.translate(-(x1), -(y1)) }, drawLine (cav, x1, y1, x2, y2, x3, y3) { cav.beginPath() cav.strokeStyle = 'rgb(155, 204, 237)' cav.bezierCurveTo(x1, y1, x2, y2, x3, y3) cav.stroke() }, randomNum () { // 生成随机字 let i let letter = this.letter let text = [] for (i = 0; i < 4; i++) { text.push(letter[Math.floor(Math.random() * 36)]) } return text }, randomDeg (min, max, f) { // 设置文字倾斜角度 f = undefined ? Math.random() > 0.5 ? 1 : -1 : 1 return f * (Math.random() * (max - min) + min) }, next () { this.draw() } } } </script> <style scoped> </style>
样式什么的自己可以调一调,效果如下:
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
分享文章:怎么在vue中封装一个随机验证码组件
当前路径:https://www.cdcxhl.com/article32/jhsipc.html
成都网站建设公司_创新互联,为您提供网站收录、网站建设、网站策划、关键词优化、虚拟主机、营销型网站建设
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联