validCode.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <view class="flex" style="width:80%;margin:30px auto 0">
  3. <view class="flex-sub">
  4. <input type="text" class=" verifyInput">
  5. </view>
  6. <view class="ValidCode disabled-select flex-sub" :style="`width:${width}; height:${height}`" @click="refreshCode">
  7. <text v-for="(item, index) in codeList" :key="index" :style="getStyle(item)">{{item.code}}</text>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. name: 'validCode',
  14. props: {
  15. width: {
  16. type: String,
  17. default: '100px'
  18. },
  19. height: {
  20. type: String,
  21. default: '40px'
  22. },
  23. length: {
  24. type: Number,
  25. default: 4
  26. }
  27. },
  28. data () {
  29. return {
  30. codeList: []
  31. }
  32. },
  33. mounted () {
  34. this.createdCode()
  35. },
  36. methods: {
  37. refreshCode () {
  38. this.createdCode()
  39. },
  40. createdCode () {
  41. let len = this.length,
  42. codeList = [],
  43. chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz0123456789',
  44. charsLen = chars.length
  45. // 生成
  46. for (let i = 0; i < len; i++) {
  47. let rgb = [Math.round(Math.random() * 220), Math.round(Math.random() * 240), Math.round(Math.random() * 200)]
  48. codeList.push({
  49. code: chars.charAt(Math.floor(Math.random() * charsLen)),
  50. color: `rgb(${rgb})`,
  51. fontSize: `1${[Math.floor(Math.random() * 10)]}px`,
  52. padding: `${[Math.floor(Math.random() * 10)]}px`,
  53. transform: `rotate(${Math.floor(Math.random() * 90) - Math.floor(Math.random() * 90)}deg)`
  54. })
  55. }
  56. // 指向
  57. this.codeList = codeList
  58. // 将当前数据派发出去
  59. this.$emit('update:value', codeList.map(item => item.code).join(''))
  60. },
  61. getStyle (data) {
  62. return `color: ${data.color}; font-size: ${data.fontSize}; padding: ${data.padding}; transform: ${data.transform}`
  63. }
  64. }
  65. }
  66. </script>
  67. <style scoped lang="scss">
  68. .ValidCode{
  69. display: flex;
  70. justify-content: center;
  71. align-items: center;
  72. cursor: pointer;
  73. span{
  74. display: inline-block;
  75. }
  76. }
  77. .verifyInput{
  78. width: 180rpx;
  79. height: 56rpx;
  80. opacity: 1;
  81. background: #ffffff;
  82. border: 1rpx solid #bfbfbf;
  83. border-radius: 5rpx;
  84. }
  85. </style>