348 lines
10 KiB
Vue
Raw Normal View History

2022-11-17 17:49:19 +08:00
<template>
<el-form
:model="loginData.loginForm"
:rules="LoginRules"
label-position="top"
class="login-form"
label-width="120px"
size="large"
v-show="getShow"
ref="formLogin"
>
<el-row style="maring-left: -10px; maring-right: -10px">
<el-col :span="24" style="padding-left: 10px; padding-right: 10px">
<el-form-item>
<LoginFormTitle style="width: 100%" />
</el-form-item>
</el-col>
<el-col :span="24" style="padding-left: 10px; padding-right: 10px">
2022-12-06 21:30:44 +08:00
<el-form-item prop="tenantName" v-if="loginData.tenantEnable === 'true'">
2022-11-17 17:49:19 +08:00
<el-input
type="text"
v-model="loginData.loginForm.tenantName"
:placeholder="t('login.tenantNamePlaceholder')"
:prefix-icon="iconHouse"
/>
</el-form-item>
</el-col>
<el-col :span="24" style="padding-left: 10px; padding-right: 10px">
<el-form-item prop="username">
<el-input
v-model="loginData.loginForm.username"
:placeholder="t('login.usernamePlaceholder')"
:prefix-icon="iconAvatar"
/>
</el-form-item>
</el-col>
<el-col :span="24" style="padding-left: 10px; padding-right: 10px">
<el-form-item prop="password">
<el-input
v-model="loginData.loginForm.password"
type="password"
:placeholder="t('login.passwordPlaceholder')"
show-password
@keyup.enter="getCode()"
:prefix-icon="iconLock"
/>
</el-form-item>
</el-col>
<el-col
:span="24"
style="padding-left: 10px; padding-right: 10px; margin-top: -20px; margin-bottom: -20px"
>
<el-form-item>
<el-row justify="space-between" style="width: 100%">
<el-col :span="6">
<el-checkbox v-model="loginData.loginForm.rememberMe">
{{ t('login.remember') }}
</el-checkbox>
</el-col>
<el-col :span="12" :offset="6">
<el-link type="primary" style="float: right">{{ t('login.forgetPassword') }}</el-link>
</el-col>
</el-row>
</el-form-item>
</el-col>
<el-col :span="24" style="padding-left: 10px; padding-right: 10px">
<el-form-item>
2022-12-06 21:30:44 +08:00
<XButton
:loading="loginLoading"
type="primary"
class="w-[100%]"
:title="t('login.login')"
@click="getCode()"
/>
2022-11-17 17:49:19 +08:00
</el-form-item>
</el-col>
<Verify
ref="verify"
mode="pop"
:captchaType="captchaType"
:imgSize="{ width: '400px', height: '200px' }"
@success="handleLogin"
/>
<el-col :span="24" style="padding-left: 10px; padding-right: 10px">
<el-form-item>
<el-row justify="space-between" style="width: 100%" :gutter="5">
<el-col :span="8">
2022-12-06 21:30:44 +08:00
<XButton
class="w-[100%]"
:title="t('login.btnMobile')"
@click="setLoginState(LoginStateEnum.MOBILE)"
/>
2022-11-17 17:49:19 +08:00
</el-col>
<el-col :span="8">
2022-12-06 21:30:44 +08:00
<XButton
class="w-[100%]"
:title="t('login.btnQRCode')"
@click="setLoginState(LoginStateEnum.QR_CODE)"
/>
2022-11-17 17:49:19 +08:00
</el-col>
<el-col :span="8">
2022-12-06 21:30:44 +08:00
<XButton
class="w-[100%]"
:title="t('login.btnRegister')"
@click="setLoginState(LoginStateEnum.REGISTER)"
/>
2022-11-17 17:49:19 +08:00
</el-col>
</el-row>
</el-form-item>
</el-col>
<el-divider content-position="center">{{ t('login.otherLogin') }}</el-divider>
<el-col :span="24" style="padding-left: 10px; padding-right: 10px">
<el-form-item>
<div class="flex justify-between w-[100%]">
<Icon
2022-12-06 21:30:44 +08:00
v-for="(item, key) in socialList"
:key="key"
:icon="item.icon"
2022-11-23 11:45:57 +08:00
:size="30"
2022-11-17 17:49:19 +08:00
class="cursor-pointer anticon"
2022-11-23 11:45:57 +08:00
color="#999"
2022-12-06 21:30:44 +08:00
@click="doSocialLogin(item.type)"
2022-11-17 17:49:19 +08:00
/>
</div>
</el-form-item>
</el-col>
</el-row>
</el-form>
</template>
2022-11-16 09:30:57 +08:00
<script setup lang="ts">
2022-11-22 21:41:59 +08:00
import { reactive, ref, unref, onMounted, computed, watch } from 'vue'
2022-07-18 19:06:37 +08:00
import LoginFormTitle from './LoginFormTitle.vue'
import {
ElForm,
ElFormItem,
ElInput,
ElCheckbox,
ElCol,
ElLink,
ElRow,
2022-11-21 16:53:15 +08:00
ElDivider,
ElLoading
} from 'element-plus'
2022-07-18 19:06:37 +08:00
import { useRouter } from 'vue-router'
2022-11-22 21:41:59 +08:00
import type { RouteLocationNormalizedLoaded } from 'vue-router'
2022-07-18 19:06:37 +08:00
import { useI18n } from '@/hooks/web/useI18n'
2022-11-22 21:41:59 +08:00
import { useIcon } from '@/hooks/web/useIcon'
2022-12-06 21:30:44 +08:00
import { useMessage } from '@/hooks/web/useMessage'
2022-07-18 19:06:37 +08:00
import { required } from '@/utils/formRules'
2022-12-06 16:33:46 +08:00
import * as authUtil from '@/utils/auth'
2022-12-06 16:12:54 +08:00
import { decrypt } from '@/utils/jsencrypt'
import { Verify } from '@/components/Verifition'
2022-11-22 21:41:59 +08:00
import { usePermissionStore } from '@/store/modules/permission'
import * as LoginApi from '@/api/login'
import { LoginStateEnum, useLoginState, useFormValid } from './useLogin'
2022-07-18 19:06:37 +08:00
2022-11-22 21:41:59 +08:00
const { t } = useI18n()
2022-12-06 21:30:44 +08:00
const message = useMessage()
const iconHouse = useIcon({ icon: 'ep:house' })
const iconAvatar = useIcon({ icon: 'ep:avatar' })
const iconLock = useIcon({ icon: 'ep:lock' })
2022-07-18 19:06:37 +08:00
const formLogin = ref()
const { validForm } = useFormValid(formLogin)
const { setLoginState, getLoginState } = useLoginState()
2022-11-23 11:45:57 +08:00
const { currentRoute, push } = useRouter()
const permissionStore = usePermissionStore()
2022-07-18 19:06:37 +08:00
const redirect = ref<string>('')
2022-11-22 21:41:59 +08:00
const loginLoading = ref(false)
2022-11-23 11:45:57 +08:00
const verify = ref()
const captchaType = ref('blockPuzzle') // blockPuzzle 滑块 clickWord 点击文字
const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN)
const LoginRules = {
tenantName: [required],
username: [required],
password: [required]
}
2022-07-18 19:06:37 +08:00
const loginData = reactive({
isShowPassword: false,
2022-09-04 12:35:23 +08:00
captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE,
tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE,
2022-07-18 19:06:37 +08:00
token: '',
loading: {
signIn: false
},
loginForm: {
2022-12-06 16:12:54 +08:00
tenantName: '芋道源码',
username: 'admin',
password: 'admin123',
2022-08-03 09:54:18 +08:00
captchaVerification: '',
2022-08-02 17:07:05 +08:00
rememberMe: false
2022-07-18 19:06:37 +08:00
}
})
2022-11-23 11:45:57 +08:00
2022-12-06 21:30:44 +08:00
const socialList = [
{
icon: 'ant-design:github-filled',
type: 0
},
{
icon: 'ant-design:wechat-filled',
type: 30
},
{
icon: 'ant-design:alipay-circle-filled',
type: 0
},
{
icon: 'ant-design:dingtalk-circle-filled',
type: 20
}
]
2022-07-18 19:06:37 +08:00
// 获取验证码
const getCode = async () => {
2022-09-04 12:35:23 +08:00
// 情况一,未开启:则直接登录
if (loginData.captchaEnable === 'false') {
2022-09-04 12:35:23 +08:00
await handleLogin({})
2022-11-23 11:45:57 +08:00
} else {
// 情况二,已开启:则展示验证码;只有完成验证码的情况,才进行登录
// 弹出验证码
verify.value.show()
2022-09-04 12:35:23 +08:00
}
2022-07-18 19:06:37 +08:00
}
//获取租户ID
const getTenantId = async () => {
2022-12-06 21:30:44 +08:00
if (loginData.tenantEnable === 'true') {
const res = await LoginApi.getTenantIdByNameApi(loginData.loginForm.tenantName)
authUtil.setTenantId(res)
}
2022-07-18 19:06:37 +08:00
}
2022-07-20 00:07:16 +08:00
// 记住我
const getCookie = () => {
2022-12-06 16:33:46 +08:00
const username = authUtil.getUsername()
const password = authUtil.getPassword()
? decrypt(authUtil.getPassword() as unknown as string)
: undefined
const rememberMe = authUtil.getRememberMe()
const tenantName = authUtil.getTenantName()
2022-07-20 00:07:16 +08:00
loginData.loginForm = {
...loginData.loginForm,
username: username ? username : loginData.loginForm.username,
password: password ? password : loginData.loginForm.password,
rememberMe: rememberMe ? true : false,
2022-07-20 00:07:16 +08:00
tenantName: tenantName ? tenantName : loginData.loginForm.tenantName
}
}
2022-07-18 19:06:37 +08:00
// 登录
2022-08-03 09:54:18 +08:00
const handleLogin = async (params) => {
loginLoading.value = true
2022-10-17 13:02:23 +08:00
try {
await getTenantId()
const data = await validForm()
if (!data) {
return
}
loginData.loginForm.captchaVerification = params.captchaVerification
const res = await LoginApi.loginApi(loginData.loginForm)
if (!res) {
return
}
2022-11-21 16:53:15 +08:00
ElLoading.service({
lock: true,
text: '正在加载系统中...',
background: 'rgba(0, 0, 0, 0.7)'
})
if (loginData.loginForm.rememberMe) {
2022-12-06 16:33:46 +08:00
authUtil.setUsername(loginData.loginForm.username)
authUtil.setPassword(loginData.loginForm.password)
authUtil.setRememberMe(loginData.loginForm.rememberMe)
authUtil.setTenantName(loginData.loginForm.tenantName)
} else {
2022-12-06 16:33:46 +08:00
authUtil.removeUsername()
authUtil.removePassword()
authUtil.removeRememberMe()
authUtil.removeTenantName()
}
2022-12-06 16:33:46 +08:00
authUtil.setToken(res)
2022-10-17 13:02:23 +08:00
if (!redirect.value) {
redirect.value = '/'
}
push({ path: redirect.value || permissionStore.addRouters[0].path })
2022-11-07 13:25:23 +08:00
} catch {
2022-08-03 09:54:18 +08:00
loginLoading.value = false
2022-11-21 16:53:15 +08:00
} finally {
setTimeout(() => {
const loadingInstance = ElLoading.service()
loadingInstance.close()
}, 400)
2022-08-03 12:38:58 +08:00
}
2022-07-18 19:06:37 +08:00
}
2022-07-21 18:58:52 +08:00
// 社交登录
2022-12-06 21:30:44 +08:00
const doSocialLogin = async (type: number) => {
if (type === 0) {
message.error('此方式未配置')
} else {
loginLoading.value = true
if (loginData.tenantEnable === 'true') {
await message.prompt('请输入租户名称', t('common.reminder')).then(async ({ value }) => {
const res = await LoginApi.getTenantIdByNameApi(value)
authUtil.setTenantId(res)
})
}
// 计算 redirectUri
const redirectUri =
location.origin + '/social-login?type=' + type + '&redirect=' + (redirect.value || '/')
// 进行跳转
const res = await LoginApi.socialAuthRedirectApi(type, encodeURIComponent(redirectUri))
window.location.href = res
}
2022-07-21 18:58:52 +08:00
}
2022-07-18 19:06:37 +08:00
watch(
() => currentRoute.value,
(route: RouteLocationNormalizedLoaded) => {
redirect.value = route?.query?.redirect as string
},
{
immediate: true
}
)
2022-08-02 17:07:05 +08:00
onMounted(() => {
2022-07-20 00:07:16 +08:00
getCookie()
2022-07-18 19:06:37 +08:00
})
</script>
2022-11-17 17:49:19 +08:00
2022-11-23 15:18:59 +08:00
<style lang="scss" scoped>
2022-07-20 09:12:20 +08:00
:deep(.anticon) {
&:hover {
color: var(--el-color-primary) !important;
}
}
2022-07-18 19:06:37 +08:00
.login-code {
width: 100%;
height: 38px;
float: right;
img {
cursor: pointer;
width: 100%;
max-width: 100px;
height: auto;
vertical-align: middle;
}
}
</style>