合并代码
This commit is contained in:
parent
4ba7f85676
commit
85ed8ac740
|
@ -0,0 +1,16 @@
|
|||
import service from '@/utils/request'
|
||||
import { LoginVo } from '@/api/book/user'
|
||||
|
||||
/*
|
||||
* 用户登录
|
||||
* */
|
||||
export const useLogin = (data: LoginVo) => {
|
||||
return service.post('/maku/t_user/login', data)
|
||||
}
|
||||
|
||||
/*
|
||||
* 获取用户信息
|
||||
* */
|
||||
export const getUserInfo = () => {
|
||||
return service.get('/maku/t_user/info')
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
export interface LoginVo {
|
||||
username: string
|
||||
password: string
|
||||
}
|
|
@ -4,6 +4,7 @@ import { ElMessage } from 'element-plus'
|
|||
import { useUserStore } from '@/store/modules/user'
|
||||
import cache from '@/utils/cache'
|
||||
import { ElMessageBox } from 'element-plus/es'
|
||||
import { getCookie } from '@/utils/tokenUtil'
|
||||
|
||||
// axios实例
|
||||
const service = axios.create({
|
||||
|
@ -21,6 +22,11 @@ service.interceptors.request.use(
|
|||
config.headers.Authorization = userStore.token
|
||||
}
|
||||
|
||||
var token = getCookie('book_user')
|
||||
if (token) {
|
||||
config.headers['cookie'] = token
|
||||
}
|
||||
|
||||
config.headers['Accept-Language'] = cache.getLanguage()
|
||||
|
||||
// 追加时间戳,防止GET请求缓存
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
export function getCookie(name: string) {
|
||||
let nameEQ = name + '='
|
||||
let ca = document.cookie.split(';')
|
||||
for (let i = 0; i < ca.length; i++) {
|
||||
let c = ca[i]
|
||||
while (c.charAt(0) === ' ') {
|
||||
c = c.substring(1, c.length)
|
||||
}
|
||||
if (c.indexOf(nameEQ) === 0) {
|
||||
return c.substring(nameEQ.length, c.length)
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
|
@ -1,7 +1,8 @@
|
|||
<script setup lang="ts">
|
||||
import { useRouter } from 'vue-router'
|
||||
import { ref } from 'vue'
|
||||
|
||||
import { onMounted, ref } from 'vue'
|
||||
import { getCookie } from '@/utils/tokenUtil'
|
||||
import { getUserInfo } from '@/api/book'
|
||||
const router = useRouter()
|
||||
// 记录切换值
|
||||
const changeValue = ref('/book/index')
|
||||
|
@ -14,6 +15,13 @@ const changeRouters = () => {
|
|||
}
|
||||
router.push(changeValue.value)
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
console.log(getCookie('book_user'))
|
||||
// getUserInfo().then(res => {
|
||||
// console.log(res)
|
||||
// })
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
|
|
@ -1,15 +1,29 @@
|
|||
<script setup lang="ts">
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { onMounted, ref } from 'vue'
|
||||
import { useLogin } from '@/api/book'
|
||||
import { ElNotification } from 'element-plus'
|
||||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
const loginForm = ref({
|
||||
username: 'admin1',
|
||||
password: '123456',
|
||||
rePassword: '123456'
|
||||
})
|
||||
const handleLogin = () => {
|
||||
if (active.value == '0') {
|
||||
console.log('登录')
|
||||
if (route.query.returnUrl) {
|
||||
router.push(route.query.returnUrl as string)
|
||||
}
|
||||
useLogin(loginForm.value).then(res => {
|
||||
ElNotification({
|
||||
title: '登录成功!!',
|
||||
type: 'success'
|
||||
})
|
||||
if (route.query.returnUrl) {
|
||||
router.push(route.query.returnUrl as string)
|
||||
}
|
||||
window.document.cookie = 'book_user=' + res.data
|
||||
})
|
||||
} else if (active.value == '1') {
|
||||
active.value = '0'
|
||||
}
|
||||
|
@ -32,9 +46,9 @@ const active = ref('0')
|
|||
<div :class="{ active: active == '0' }" @click="active = '0'">用户登录</div>
|
||||
<div :class="{ active: active == '1' }" @click="active = '1'">用户注册</div>
|
||||
</div>
|
||||
<input class="fm-text" placeholder="用户名" />
|
||||
<input class="fm-text" placeholder="密码" type="password" />
|
||||
<input v-if="active == '1'" class="fm-text" placeholder="确认密码" type="password" />
|
||||
<input v-model="loginForm.username" class="fm-text" placeholder="用户名" />
|
||||
<input v-model="loginForm.password" class="fm-text" placeholder="密码" type="password" />
|
||||
<input v-if="active == '1'" v-model="loginForm.rePassword" class="fm-text" placeholder="确认密码" type="password" />
|
||||
<button @click.prevent="handleLogin">{{ active == '0' ? '登录' : '注册' }}</button>
|
||||
</form>
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue
Block a user