StdPassword.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <script setup lang="ts">
  2. import { computed, ref } from 'vue'
  3. import { useGettext } from 'vue3-gettext'
  4. const props = defineProps<{
  5. value: string
  6. generate: boolean
  7. placeholder: string
  8. }>()
  9. const emit = defineEmits(['update:value'])
  10. const M_value = computed({
  11. get() {
  12. return props.value
  13. },
  14. set(v) {
  15. emit('update:value', v)
  16. },
  17. })
  18. const visibility = ref(false)
  19. const { $gettext } = useGettext()
  20. function handle_generate() {
  21. visibility.value = true
  22. M_value.value = 'xxxx'
  23. const chars = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*()ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  24. const passwordLength = 12
  25. let password = ''
  26. for (let i = 0; i <= passwordLength; i++) {
  27. const randomNumber = Math.floor(Math.random() * chars.length)
  28. password += chars.substring(randomNumber, randomNumber + 1)
  29. }
  30. M_value.value = password
  31. }
  32. </script>
  33. <template>
  34. <AInputGroup compact>
  35. <AInputPassword
  36. v-if="!visibility"
  37. v-model:value="M_value"
  38. :class="{ compact: generate }"
  39. :placeholoder="placeholder"
  40. />
  41. <AInput
  42. v-else
  43. v-model:value="M_value"
  44. :class="{ compact: generate }"
  45. :placeholoder="placeholder"
  46. />
  47. <AButton
  48. v-if="generate"
  49. type="primary"
  50. @click="handle_generate"
  51. >
  52. {{ $gettext('Generate') }}
  53. </AButton>
  54. </AInputGroup>
  55. </template>
  56. <style scoped>
  57. .compact {
  58. width: calc(100% - 91px)
  59. }
  60. </style>