LoginForm.vue
4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<template>
<LoginFormTitle v-show="getShow" class="enter-x" />
<Form
class="p-4 enter-x"
:model="formData"
:rules="getFormRules"
ref="formRef"
v-show="getShow"
@keypress.enter="handleLogin"
>
<FormItem name="account" class="enter-x">
<Input
size="large"
v-model:value="formData.account"
:placeholder="t('sys.login.userName')"
class="fix-auto-fill"
/>
</FormItem>
<FormItem name="password" class="enter-x">
<InputPassword
size="large"
visibilityToggle
v-model:value="formData.password"
:placeholder="t('sys.login.password')"
/>
</FormItem>
<ARow class="enter-x">
<ACol :span="12">
<FormItem>
<Checkbox v-model:checked="rememberMe" size="small">
{{ t('sys.login.rememberMe') }}
</Checkbox>
</FormItem>
</ACol>
<ACol :span="12">
<FormItem style="text-align: right">
<Button type="link" size="small" @click="setLoginState(LoginStateEnum.RESET_PASSWORD)">
{{ t('sys.login.forgetPassword') }}
</Button>
</FormItem>
</ACol>
</ARow>
<FormItem class="enter-x">
<Button type="primary" size="large" block @click="handleLogin" :loading="loading">
{{ t('sys.login.loginButton') }}
</Button>
</FormItem>
<ARow class="enter-x flex justify-between">
<ACol :md="11" :xs="24">
<Button block @click="setLoginState(LoginStateEnum.LOGIN)">
{{ t('sys.login.userNameInFormTitle') }}
</Button>
</ACol>
<ACol :md="11" :xs="24">
<Button block @click="setLoginState(LoginStateEnum.MOBILE)">
{{ t('sys.login.mobileSignInFormTitle') }}
</Button>
</ACol>
</ARow>
</Form>
</template>
<script lang="ts" setup>
import { reactive, ref, unref, computed } from 'vue';
import { Checkbox, Form, Input, Row, Col, Button } from 'ant-design-vue';
import LoginFormTitle from './LoginFormTitle.vue';
import { useI18n } from '/@/hooks/web/useI18n';
import { useMessage } from '/@/hooks/web/useMessage';
import { useUserStore } from '/@/store/modules/user';
import { LoginStateEnum, useLoginState, useFormRules, useFormValid } from './useLogin';
import { getPlatForm } from '/@/api/oem';
import { createLocalStorage } from '/@/utils/cache';
const ACol = Col;
const ARow = Row;
const FormItem = Form.Item;
const InputPassword = Input.Password;
const { t } = useI18n();
const { notification } = useMessage();
const userStore = useUserStore();
const { setLoginState, getLoginState } = useLoginState();
const { getFormRules } = useFormRules();
const storage = createLocalStorage();
const formRef = ref();
const loading = ref(false);
const rememberMe = ref(false);
const userInfo = storage.get('userInfo');
const formData = reactive({
account: userInfo?.account ?? '',
password: userInfo?.password ?? '',
});
const { validForm } = useFormValid(formRef);
const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN);
async function handleLogin() {
const data = await validForm();
if (!data) return;
if (unref(rememberMe)) {
storage.set('userInfo', formData);
} else {
storage.set('userInfo', null);
}
loading.value = true;
const userInfo = await userStore
.login({
password: data.password,
username: data.account,
mode: 'modal', //不要默认的错误提示
})
.catch(() => {
//登录失败返回的html,所以提示框什么都没有
//去掉提示框
// createMessage.error(data.message);
});
if (userInfo) {
notification.success({
message: t('sys.login.loginSuccessTitle'),
description: `${t('sys.login.loginSuccessDesc')}: ${userInfo.realName}`,
duration: 3,
});
const res = await getPlatForm();
storage.set('platformInfo', res);
userStore.setPlatInfo(res);
var _hmt = _hmt || [];
(function () {
var hm = document.createElement('script');
hm.src = 'https://hm.baidu.com/hm.js?909f8e22a361b08e4f5ea3918500aede';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(hm, s);
})();
}
loading.value = false;
}
</script>