LoginForm.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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="xs: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 { userStore } from '/@/store/modules/user';
  82. import { LoginStateEnum, useLoginState, useFormRules, useFormValid } from './useLogin';
  83. import { useDesign } from '/@/hooks/web/useDesign';
  84. export default defineComponent({
  85. name: 'LoginForm',
  86. components: {
  87. [Col.name]: Col,
  88. [Row.name]: Row,
  89. Checkbox,
  90. Button,
  91. Form,
  92. FormItem: Form.Item,
  93. Input,
  94. Divider,
  95. LoginFormTitle,
  96. InputPassword: Input.Password,
  97. GithubFilled,
  98. WechatFilled,
  99. AlipayCircleFilled,
  100. GoogleCircleFilled,
  101. TwitterCircleFilled,
  102. },
  103. setup() {
  104. const { t } = useI18n();
  105. const { notification } = useMessage();
  106. const { prefixCls } = useDesign('login');
  107. const { setLoginState, getLoginState } = useLoginState();
  108. const { getFormRules } = useFormRules();
  109. const formRef = ref<any>(null);
  110. const loading = ref(false);
  111. const rememberMe = ref(false);
  112. const formData = reactive({
  113. account: 'vben',
  114. password: '123456',
  115. });
  116. const { validForm } = useFormValid(formRef);
  117. const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN);
  118. async function handleLogin() {
  119. const data = await validForm();
  120. if (!data) return;
  121. try {
  122. loading.value = true;
  123. const userInfo = await userStore.login(
  124. toRaw({
  125. password: data.password,
  126. username: data.account,
  127. })
  128. );
  129. if (userInfo) {
  130. notification.success({
  131. message: t('sys.login.loginSuccessTitle'),
  132. description: `${t('sys.login.loginSuccessDesc')}: ${userInfo.realName}`,
  133. duration: 3,
  134. });
  135. }
  136. } finally {
  137. loading.value = false;
  138. }
  139. }
  140. return {
  141. t,
  142. prefixCls,
  143. formRef,
  144. formData,
  145. getFormRules,
  146. rememberMe,
  147. handleLogin,
  148. loading,
  149. setLoginState,
  150. LoginStateEnum,
  151. getShow,
  152. };
  153. },
  154. });
  155. </script>