index.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <template>
  2. <div class="dynamic-code">
  3. <div class="code-container">
  4. <div @click="generateCode" class="code">{{ code }}</div>
  5. </div>
  6. <!-- <button @click="generateCode">看不清,换一张</button> -->
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. data() {
  12. return {
  13. code: '', // 存储验证码
  14. characters: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', // 允许的字符
  15. };
  16. },
  17. methods: {
  18. generateRandomCode() {
  19. // 生成随机四位数字加大小写字母验证码
  20. let result = '';
  21. for (let i = 0; i < 4; i++) {
  22. const randomIndex = Math.floor(Math.random() * this.characters.length);
  23. result += this.characters[randomIndex];
  24. }
  25. this.code = result;
  26. console.log(this.code);
  27. },
  28. generateCode() {
  29. this.generateRandomCode();
  30. },
  31. },
  32. mounted() {
  33. this.generateCode(); // 组件挂载后生成验证码
  34. },
  35. };
  36. </script>
  37. <style>
  38. .dynamic-code {
  39. display: flex;
  40. flex-direction: column;
  41. align-items: center;
  42. justify-content: center;
  43. height: 100vh;
  44. }
  45. .code-container {
  46. font-size: 24px;
  47. font-weight: bold;
  48. margin-bottom: 20px;
  49. padding: 10px 20px;
  50. border: 2px solid #333;
  51. border-radius: 5px;
  52. }
  53. button {
  54. font-size: 18px;
  55. padding: 10px 20px;
  56. background-color: #007aff;
  57. color: #fff;
  58. border: none;
  59. border-radius: 5px;
  60. cursor: pointer;
  61. }
  62. </style>