LoginForm.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <LoginFormTitle v-show="getShow" class="enter-x" />
  3. <Form class="p-4 enter-x" :model="formData" :rules="getFormRules" ref="formRef" v-show="getShow">
  4. <FormItem name="account" class="enter-x">
  5. <Input size="large" v-model:value="formData.account" :placeholder="t('sys.login.userName')" />
  6. </FormItem>
  7. <FormItem name="password" class="enter-x">
  8. <InputPassword
  9. size="large"
  10. visibilityToggle
  11. v-model:value="formData.password"
  12. :placeholder="t('sys.login.password')"
  13. />
  14. </FormItem>
  15. <ARow class="enter-x">
  16. <ACol :span="12">
  17. <FormItem>
  18. <!-- No logic, you need to deal with it yourself -->
  19. <Checkbox v-model:checked="rememberMe" size="small">
  20. {{ t('sys.login.rememberMe') }}
  21. </Checkbox>
  22. </FormItem>
  23. </ACol>
  24. <ACol :span="12">
  25. <FormItem :style="{ 'text-align': 'right' }">
  26. <!-- No logic, you need to deal with it yourself -->
  27. <Button type="link" size="small" @click="setLoginState(LoginStateEnum.RESET_PASSWORD)">
  28. {{ t('sys.login.forgetPassword') }}
  29. </Button>
  30. </FormItem>
  31. </ACol>
  32. </ARow>
  33. <FormItem class="enter-x">
  34. <Button type="primary" size="large" block @click="handleLogin" :loading="loading">
  35. {{ t('sys.login.loginButton') }}
  36. </Button>
  37. <!-- <Button size="large" class="mt-4 enter-x" block @click="handleRegister">
  38. {{ t('sys.login.registerButton') }}
  39. </Button> -->
  40. </FormItem>
  41. <ARow class="enter-x">
  42. <ACol :xs="24" :md="8">
  43. <Button block @click="setLoginState(LoginStateEnum.MOBILE)">
  44. {{ t('sys.login.mobileSignInFormTitle') }}
  45. </Button>
  46. </ACol>
  47. <ACol :md="8" :xs="24" class="!my-2 !md:my-0 xs:mx-0 md:mx-2">
  48. <Button block @click="setLoginState(LoginStateEnum.QR_CODE)">
  49. {{ t('sys.login.qrSignInFormTitle') }}
  50. </Button>
  51. </ACol>
  52. <ACol :md="7" :xs="24">
  53. <Button block @click="setLoginState(LoginStateEnum.REGISTER)">
  54. {{ t('sys.login.registerButton') }}
  55. </Button>
  56. </ACol>
  57. </ARow>
  58. <Divider class="enter-x">{{ t('sys.login.otherSignIn') }}</Divider>
  59. <div class="flex justify-evenly enter-x" :class="`${prefixCls}-sign-in-way`">
  60. <GithubFilled />
  61. <WechatFilled />
  62. <AlipayCircleFilled />
  63. <GoogleCircleFilled />
  64. <TwitterCircleFilled />
  65. </div>
  66. </Form>
  67. </template>
  68. <script lang="ts">
  69. import { defineComponent, reactive, ref, toRaw, unref, computed } from 'vue';
  70. import { Checkbox, Form, Input, Row, Col, Button, Divider } from 'ant-design-vue';
  71. import {
  72. GithubFilled,
  73. WechatFilled,
  74. AlipayCircleFilled,
  75. GoogleCircleFilled,
  76. TwitterCircleFilled,
  77. } from '@ant-design/icons-vue';
  78. import LoginFormTitle from './LoginFormTitle.vue';
  79. import { useI18n } from '/@/hooks/web/useI18n';
  80. import { useMessage } from '/@/hooks/web/useMessage';
  81. import { useUserStore } from '/@/store/modules/user';
  82. import { LoginStateEnum, useLoginState, useFormRules, useFormValid } from './useLogin';
  83. import { useDesign } from '/@/hooks/web/useDesign';
  84. import { useKeyPress } from '/@/hooks/event/useKeyPress';
  85. import { KeyCodeEnum } from '/@/enums/keyCodeEnum';
  86. export default defineComponent({
  87. name: 'LoginForm',
  88. components: {
  89. [Col.name]: Col,
  90. [Row.name]: Row,
  91. Checkbox,
  92. Button,
  93. Form,
  94. FormItem: Form.Item,
  95. Input,
  96. Divider,
  97. LoginFormTitle,
  98. InputPassword: Input.Password,
  99. GithubFilled,
  100. WechatFilled,
  101. AlipayCircleFilled,
  102. GoogleCircleFilled,
  103. TwitterCircleFilled,
  104. },
  105. setup() {
  106. const { t } = useI18n();
  107. const { notification } = useMessage();
  108. const { prefixCls } = useDesign('login');
  109. const userStore = useUserStore();
  110. const { setLoginState, getLoginState } = useLoginState();
  111. const { getFormRules } = useFormRules();
  112. const formRef = ref();
  113. const loading = ref(false);
  114. const rememberMe = ref(false);
  115. const formData = reactive({
  116. account: 'vben',
  117. password: '123456',
  118. });
  119. const { validForm } = useFormValid(formRef);
  120. useKeyPress(['enter'], (events) => {
  121. const keyCode = events.keyCode;
  122. if (keyCode === KeyCodeEnum.ENTER) {
  123. handleLogin();
  124. }
  125. });
  126. const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN);
  127. async function handleLogin() {
  128. const data = await validForm();
  129. if (!data) return;
  130. try {
  131. loading.value = true;
  132. const userInfo = await userStore.login(
  133. toRaw({
  134. password: data.password,
  135. username: data.account,
  136. })
  137. );
  138. if (userInfo) {
  139. notification.success({
  140. message: t('sys.login.loginSuccessTitle'),
  141. description: `${t('sys.login.loginSuccessDesc')}: ${userInfo.realName}`,
  142. duration: 3,
  143. });
  144. }
  145. } finally {
  146. loading.value = false;
  147. }
  148. }
  149. return {
  150. t,
  151. prefixCls,
  152. formRef,
  153. formData,
  154. getFormRules,
  155. rememberMe,
  156. handleLogin,
  157. loading,
  158. setLoginState,
  159. LoginStateEnum,
  160. getShow,
  161. };
  162. },
  163. });
  164. </script>