You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OneAuth/oaf/src/views/login.vue

118 lines
3.1 KiB
Vue

<template>
<div class="flex items-center justify-center">
4 years ago
<div
:style="{background:Theme.me.lightBox, 'box-shadow': Theme.me.lightBoxShadow}"
class="px-10 pb-9 pt-28 rounded-xl w-96">
<n-form label-width="70px" label-align="left" :model="data" ref="form_ref" label-placement="left" :rules="rules">
<n-form-item required label="用户名" path="username">
<n-input @keydown.enter="divs[1].focus()" :ref="el => {if (el)divs[0]=el}"
v-model:value="data.username"></n-input>
</n-form-item>
4 years ago
<n-form-item required label="密码" path="password">
<n-input @keydown.enter="login" :ref="el => {if (el) divs[1]=el}" v-model:value="data.password"
type="password"></n-input>
</n-form-item>
4 years ago
<div class="flex justify-around mt-4">
<n-button @click="login"></n-button>
<n-button @click="router.push({name:'register'})"></n-button>
</div>
</n-form>
</div>
</div>
</template>
<script lang="ts" setup>
import {computed, onMounted, ref, watch} from 'vue'
import {Theme} from '@/theme'
4 years ago
import {useMessage} from 'naive-ui'
import api from '@/api'
import {useRoute, useRouter} from 'vue-router'
import {store} from '@/store'
import {modelsApp} from '@/models'
4 years ago
import util from '@/libs/util'
4 years ago
let msg = useMessage()
const route = useRoute()
const router = useRouter()
4 years ago
const divs = ref([])
let form_ref = ref(null)
let data = ref({
4 years ago
username: '',
password: '',
})
4 years ago
let rules = {
username: [
4 years ago
{
required: true,
validator(r: any, v: any) {
return (v && v.length >= 3 && v.length <= 16) || new Error('长度要求3~16')
},
trigger: ['input', 'blur'],
},
],
4 years ago
password: [{
required: true,
validator(r: any, v: any) {
return (v && v.length >= 6 && v.length <= 16) || new Error('长度要求6~16')
},
}],
}
4 years ago
let uuid = computed(() => {
return route.query.uuid
})
function login() {
redirect()
4 years ago
// @ts-ignore
form_ref.value.validate((e: any) => {
4 years ago
if (!e) {
api.user.login(data.value.username, data.value.password).Start((url: string) => {
4 years ago
msg.success('登录成功')
store.dispatch('user/fetchUserData')
4 years ago
redirect(route.query.redirect as string)
4 years ago
}, e => {
console.log(e)
msg.warning('登录失败:' + e)
})
}
})
}
4 years ago
4 years ago
function redirect(url?: string) {
4 years ago
console.log(util.checkLogin())
if (uuid.value && uuid.value !== store.state.oauuid) {
api.app.get(uuid.value as string).Start((app: modelsApp) => {
console.log(app.UserRefreshUrl)
api.token(uuid.value as string).get().Start(e => {
4 years ago
if (!url) {
url = app.UserRefreshUrl
}
e = encodeURIComponent(e)
url = url.replaceAll('$token', e)
window.location.href = url
})
4 years ago
}, e => {
})
4 years ago
} else if (util.checkLogin()) {
4 years ago
console.log(url)
4 years ago
if (url) {
router.push(url)
} else {
router.push({name: 'home'})
}
}
}
4 years ago
onMounted(() => {
redirect()
4 years ago
if (divs.value[0]) {
// @ts-ignore
divs.value[0].focus()
}
})
</script>
<style scoped>
</style>