Login.vue 2.65 KB
<template>
    <a-form :model="loginForm">
        <a-form-item label="用户名">
            <a-input v-model:value="loginForm.username"/>
        </a-form-item>
        <a-form-item label="密码">
            <a-input v-model:value="loginForm.password"/>
        </a-form-item>
        <a-form-item>
            <a-button type="primary" @click="onSubmit">登录</a-button>
        </a-form-item>
    </a-form>
</template>

<script>
import {defineComponent, reactive, toRaw} from 'vue'
import {getAccessedMenu,  setAccessedMenu} from '@/PublicUtil/PublicUtil'
import loginApi from '@/views/Login/LoginApi'
import Cookies from "js-cookie"
import {useRouter} from 'vue-router'
import {getUrlByTitle} from '@/router'
import {ElMessage} from 'element-plus'

export default defineComponent({
    setup() {
        const router = useRouter()

        const loginForm = reactive(
            {
                username: window.location.hostname === "localhost" ? 'vion' : '',
                password: window.location.hostname === "localhost" ? 'VION20050620' : '',
            }
        )

        const onSubmit = () => {
            const data = {
                username: loginForm.username,
                password: loginForm.password
            }

            loginApi.login(data).then(
                (r) => {
                    const message = r.msg

                    if (message === 'success')
                    {
                        const data = r.data
                        const token = data.tokenInfo.tokenValue
                        Cookies.set("token", token)

                        // store.commit("setAccessedRouteList", getAccessedRouteList(r.data.menu))
                        setAccessedMenu(data.menu)
                        localStorage.setItem('currentUserId', data.id)
                        localStorage.setItem('currentUserType', data.type)
                        localStorage.setItem('currentAccountId', data.accountId)

                        const accessedMenu = getAccessedMenu()
                        const firstTitle = accessedMenu[Object.keys(accessedMenu)[0]][0]

                        router.push(
                            getUrlByTitle(firstTitle)
                        )
                    }
                    else
                    {
                        ElMessage(
                            {
                                message: `${message}`,
                                type: 'error'
                            }
                        )
                    }
                }
            )
        }

        return {
            loginForm,
            onSubmit,
        }
    },
})
</script>

<style scoped>

</style>