c-code.vue 2.0 KB

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