index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <div class="login-box">
  3. <div class="form-content">
  4. <div class="logo-box">
  5. <Logo />
  6. </div>
  7. <a-form
  8. ref="formRef"
  9. name="form"
  10. :model="mainForm"
  11. :label-col="{ style: {width: '100px'}}"
  12. :wrapper-col="{ span: 16 }"
  13. autocomplete="off"
  14. @finish="onFinish"
  15. >
  16. <a-form-item
  17. label="账号"
  18. name="account"
  19. :rules="[{ required: true, message: '账号不能为空' }]"
  20. >
  21. <a-input
  22. v-model:value="mainForm.account"
  23. placeholder="请输入账号"
  24. allowClear
  25. />
  26. </a-form-item>
  27. <a-form-item
  28. label="密码"
  29. name="password"
  30. :rules="[{ required: true, message: '密码不能为空' }]"
  31. >
  32. <a-input-password
  33. v-model:value="mainForm.password"
  34. placeholder="请输入密码"
  35. allowClear
  36. />
  37. </a-form-item>
  38. <a-form-item
  39. label="验证码"
  40. name="codeValue"
  41. :rules="[
  42. { required: true, message: '验证码不能为空' }
  43. ]"
  44. >
  45. <a-input-group compact>
  46. <a-input
  47. v-model:value="mainForm.codeValue"
  48. placeholder="请输入验证码"
  49. allowClear
  50. style="width: calc(100% - 128px)"
  51. />
  52. <img :src="mainForm.pic" @click="reFetchCode" :style="{width: '128px', height: '32px', cursor: 'pointer'}" />
  53. </a-input-group>
  54. </a-form-item>
  55. <a-form-item label=" " :colon="false">
  56. <a-button
  57. type="primary"
  58. html-type="submit"
  59. size="large"
  60. block
  61. :disabled="mainForm.disabled"
  62. >登 录</a-button>
  63. </a-form-item>
  64. </a-form>
  65. </div>
  66. </div>
  67. </template>
  68. <script setup lang="js">
  69. import Logo from '@/components/logo/index.vue';
  70. import { ref, reactive, onMounted } from 'vue';
  71. import { useRouter } from 'vue-router';
  72. import { message } from 'ant-design-vue';
  73. import { getCode, userLogin } from '@/api/user';
  74. import { useUserStore } from '@/store/user';
  75. import { setToken } from '@/utils/token';
  76. const router = useRouter();
  77. const userStore = useUserStore();
  78. const formRef = ref(null)
  79. const mainForm = reactive({
  80. account: '',
  81. password: '',
  82. code: '',
  83. pic: '',
  84. codeValue: '',
  85. disabled: false
  86. })
  87. const fetchCode = async () => {
  88. mainForm.disabled = true
  89. getCode().then(res => {
  90. mainForm.code = res.data?.code || '';
  91. mainForm.pic = 'data:image/jpeg;base64,' + res.data?.pic;
  92. }).finally(() => {
  93. mainForm.disabled = false
  94. })
  95. }
  96. const fetchLogin = async () => {
  97. mainForm.disabled = true
  98. const params = {
  99. userName: mainForm.account,
  100. password: mainForm.password,
  101. code: mainForm.code,
  102. codeValue: mainForm.codeValue,
  103. clientType: '1'
  104. }
  105. userLogin(params).then(res => {
  106. setToken(res.data.token);
  107. message.success('登录成功');
  108. userStore.updateUserInfo(res.data);
  109. router.back();
  110. }).catch(() => {
  111. fetchCode();
  112. }).finally(() => {
  113. mainForm.disabled = false
  114. })
  115. }
  116. const onFinish = () => {
  117. fetchLogin();
  118. }
  119. const reFetchCode = () => {
  120. if (mainForm.disabled) return;
  121. fetchCode();
  122. }
  123. onMounted(() => {
  124. fetchCode()
  125. })
  126. </script>
  127. <style lang="less" scoped>
  128. .login-box {
  129. width: 100vw;
  130. height: 100vh;
  131. display: flex;
  132. align-items: center;
  133. justify-content: center;
  134. background: #f2f2f2;
  135. .form-content {
  136. width: 520px;
  137. padding: 24px;
  138. background: #fff;
  139. border-radius: 10px;
  140. .logo-box {
  141. width: 100px;
  142. height: 100px;
  143. margin: 0 auto;
  144. margin-bottom: 20px;
  145. }
  146. }
  147. }
  148. </style>