Login.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <form @submit.prevent="onsubmit">
  3. <va-input
  4. v-model="email"
  5. class="mb-4"
  6. type="email"
  7. :label="'用户名'"
  8. :error="email.length == 0"
  9. :error-messages="emailErrors"
  10. />
  11. <va-input
  12. v-model="password"
  13. class="mb-4"
  14. type="password"
  15. :label="'密码'"
  16. :error="password.length == 0"
  17. :error-messages="passwordErrors"
  18. />
  19. <div class="captchaBox">
  20. <va-input
  21. v-model="captcha"
  22. class="mb-4"
  23. type="text"
  24. :label="'验证码'"
  25. :error="captcha.length == 0"
  26. :error-messages="passwordErrors"
  27. />
  28. <img class="captchaImg" @click="getCaptcha" :src="img" alt="" />
  29. </div>
  30. <div class="auth-layout__options flex items-center justify-between">
  31. <va-checkbox v-model="keepLoggedIn" class="mb-0" :label="'记住我'" />
  32. <!-- <router-link class="ml-1 va-link" :to="{ name: 'recover-password' }">{{-->
  33. <!-- t('auth.recover_password')-->
  34. <!-- }}</router-link>-->
  35. </div>
  36. <VaInnerLoading :loading="loading">
  37. <div class="flex justify-center mt-4">
  38. <va-button class="my-0" @click="onsubmit">{{ t('auth.login') }}</va-button>
  39. </div>
  40. </VaInnerLoading>
  41. </form>
  42. </template>
  43. <style>
  44. .captchaBox {
  45. position: relative;
  46. }
  47. .captchaImg {
  48. position: absolute;
  49. right: 2px;
  50. top: 18px;
  51. cursor: pointer;
  52. }
  53. </style>
  54. <script setup lang="ts">
  55. import { getCurrentInstance } from 'vue'
  56. const { proxy } = getCurrentInstance()
  57. const { $api, $toast } = proxy
  58. import { computed, ref, onMounted } from 'vue'
  59. import { useRouter } from 'vue-router'
  60. import { useI18n } from 'vue-i18n'
  61. import { useToast } from 'vuestic-ui'
  62. const { t } = useI18n()
  63. const img = ref('')
  64. const captcha = ref('')
  65. const email = ref('')
  66. const loading = ref(false)
  67. const password = ref('')
  68. const keepLoggedIn = ref(false)
  69. const emailErrors = ref<string[]>([])
  70. const passwordErrors = ref<string[]>([])
  71. const captchaErrors = ref<string[]>([])
  72. const router = useRouter()
  73. onMounted(() => {
  74. getCaptcha()
  75. })
  76. function getCaptcha() {
  77. $api.getCaptcha().then((res) => {
  78. img.value = res.result
  79. })
  80. }
  81. function onsubmit() {
  82. emailErrors.value = email.value ? [] : ['Email is required']
  83. passwordErrors.value = password.value ? [] : ['Password is required']
  84. captchaErrors.value = captcha.value ? [] : ['Password is required']
  85. if (email.value == '' || password.value == '' || captcha.value == '') {
  86. alert(888)
  87. return
  88. }
  89. loading.value = true
  90. let params = {
  91. userName: email.value,
  92. password: password.value,
  93. captcha: captcha.value,
  94. rememberMe: keepLoggedIn.value,
  95. }
  96. $api
  97. .login(params)
  98. .then((res) => {
  99. localStorage.setItem('tokenName',res.result.tokenName)
  100. localStorage.setItem('tokenValue',res.result.tokenValue)
  101. loading.value = false
  102. $toast.success({ message: '登录成功' })
  103. setTimeout(() => {
  104. router.push({ name: 'dashboard' })
  105. }, 1000)
  106. })
  107. .catch((err) => {
  108. loading.value = false
  109. })
  110. }
  111. </script>