feat: 完成登录界面与主界面的连接
This commit is contained in:
parent
3fd7e4608d
commit
4e9945d9a8
3
.env.development
Normal file
3
.env.development
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
ENV = 'development'
|
||||||
|
|
||||||
|
VUE_APP_BASE_API = '/api'
|
3
.env.production
Normal file
3
.env.production
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
ENV = 'production'
|
||||||
|
|
||||||
|
VUE_APP_BASE_API = '/prod-api'
|
@ -1,5 +1,7 @@
|
|||||||
// 导入 axios
|
// 导入 axios
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
|
// 引入 ElMessage 消息提示
|
||||||
|
import { ElMessage } from 'element-plus'
|
||||||
// axios 基础配置
|
// axios 基础配置
|
||||||
const service = axios.create({
|
const service = axios.create({
|
||||||
// 基础路径
|
// 基础路径
|
||||||
@ -7,5 +9,39 @@ const service = axios.create({
|
|||||||
// 超时时间
|
// 超时时间
|
||||||
timeout: 5000
|
timeout: 5000
|
||||||
})
|
})
|
||||||
// 导入以供外界使用
|
|
||||||
|
// 响应拦截器
|
||||||
|
service.interceptors.response.use(
|
||||||
|
// 请求响应时
|
||||||
|
(response) => {
|
||||||
|
const { data, meta } = response.data
|
||||||
|
if (meta.status === 200 || meta.status === 201) {
|
||||||
|
return data
|
||||||
|
} else {
|
||||||
|
// meta.msg = '拦截器出错'
|
||||||
|
ElMessage.error(meta.msg)
|
||||||
|
return Promise.reject(new Error(meta.msg))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// 无响应时
|
||||||
|
(error) => {
|
||||||
|
// console.log(error.response)
|
||||||
|
error.response.data = '后端服务器无响应或者URL错误'
|
||||||
|
error.response && ElMessage.error(error.response.data)
|
||||||
|
return Promise.reject(new Error(error.response.data))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// 请求拦截器
|
||||||
|
service.interceptors.request.use(
|
||||||
|
(config) => {
|
||||||
|
config.headers.Authorization = localStorage.getItem('token')
|
||||||
|
return config
|
||||||
|
},
|
||||||
|
(error) => {
|
||||||
|
return Promise.reject(new Error(error))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// 导出以供外界使用
|
||||||
export default service
|
export default service
|
||||||
|
@ -5,6 +5,8 @@ import store from './store'
|
|||||||
import '@/styles/index.scss'
|
import '@/styles/index.scss'
|
||||||
import SvgIcon from '@/icons/iconIndex'
|
import SvgIcon from '@/icons/iconIndex'
|
||||||
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
|
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
|
||||||
|
import 'element-plus/dist/index.css'
|
||||||
|
import '@/router/premission'
|
||||||
|
|
||||||
const app = createApp(App)
|
const app = createApp(App)
|
||||||
SvgIcon(app)
|
SvgIcon(app)
|
||||||
|
20
src/router/premission.js
Normal file
20
src/router/premission.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import router from './index'
|
||||||
|
import store from '@/store'
|
||||||
|
|
||||||
|
// 白名单 无登录时可访问
|
||||||
|
const whiteList = ['/login']
|
||||||
|
router.beforeEach((to, from, next) => {
|
||||||
|
if (store.getters.token) {
|
||||||
|
if (to.path === '/login') {
|
||||||
|
next('/')
|
||||||
|
} else {
|
||||||
|
next()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (whiteList.includes(to.path)) {
|
||||||
|
next()
|
||||||
|
} else {
|
||||||
|
next('/login')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
5
src/store/getters.js
Normal file
5
src/store/getters.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export default {
|
||||||
|
token: (state) => state.app.token,
|
||||||
|
siderType: (state) => state.app.siderType,
|
||||||
|
lang: (state) => state.app.lang
|
||||||
|
}
|
@ -1,14 +1,8 @@
|
|||||||
import { createStore } from 'vuex'
|
import { createStore } from 'vuex'
|
||||||
|
import app from './modules/app'
|
||||||
|
import getters from './getters'
|
||||||
|
|
||||||
export default createStore({
|
export default createStore({
|
||||||
state: {
|
modules: { app },
|
||||||
},
|
getters
|
||||||
getters: {
|
|
||||||
},
|
|
||||||
mutations: {
|
|
||||||
},
|
|
||||||
actions: {
|
|
||||||
},
|
|
||||||
modules: {
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
29
src/store/modules/app.js
Normal file
29
src/store/modules/app.js
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import { login as loginApi } from '@/api/login'
|
||||||
|
import router from '@/router'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
namespaced: true,
|
||||||
|
state: () => ({ token: localStorage.getItem('token') || '' }),
|
||||||
|
mutations: {
|
||||||
|
setToken(state, token) {
|
||||||
|
state.token = token
|
||||||
|
localStorage.setItem('token', token)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
actions: {
|
||||||
|
login({ commit }, userInfo) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
loginApi(userInfo)
|
||||||
|
.then((res) => {
|
||||||
|
console.log(res)
|
||||||
|
commit('setToken', res.token)
|
||||||
|
router.replace('/')
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
reject(err)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -34,11 +34,12 @@
|
|||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { login } from '@/api/login'
|
import { useStore } from 'vuex'
|
||||||
|
const store = useStore()
|
||||||
// 默认显示账密
|
// 默认显示账密
|
||||||
const form = ref({
|
const form = ref({
|
||||||
username: '123456',
|
username: 'admin',
|
||||||
password: '1111',
|
password: '123456',
|
||||||
nametips: '请输入用户名',
|
nametips: '请输入用户名',
|
||||||
pwdtips: '请输入密码'
|
pwdtips: '请输入密码'
|
||||||
})
|
})
|
||||||
@ -64,8 +65,7 @@ const formRef = ref(null)
|
|||||||
const handleLogin = () => {
|
const handleLogin = () => {
|
||||||
formRef.value.validate(async (valid) => {
|
formRef.value.validate(async (valid) => {
|
||||||
if (valid) {
|
if (valid) {
|
||||||
const res = await login(form.value)
|
store.dispatch('app/login', form.value)
|
||||||
console.log(res)
|
|
||||||
} else {
|
} else {
|
||||||
console.log('error submit!')
|
console.log('error submit!')
|
||||||
return false
|
return false
|
||||||
|
@ -1,8 +1,3 @@
|
|||||||
// const { defineConfig } = require('@vue/cli-service')
|
|
||||||
// module.exports = defineConfig({
|
|
||||||
// transpileDependencies: true
|
|
||||||
// })
|
|
||||||
|
|
||||||
const AutoImport = require('unplugin-auto-import/webpack')
|
const AutoImport = require('unplugin-auto-import/webpack')
|
||||||
const Components = require('unplugin-vue-components/webpack')
|
const Components = require('unplugin-vue-components/webpack')
|
||||||
const { ElementPlusResolver } = require('unplugin-vue-components/resolvers')
|
const { ElementPlusResolver } = require('unplugin-vue-components/resolvers')
|
||||||
@ -75,15 +70,27 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
devServer: {
|
devServer: {
|
||||||
https: false,
|
https: false,
|
||||||
hot: false
|
hot: false,
|
||||||
// proxy: {
|
proxy: {
|
||||||
// '/api': {
|
'/api': {
|
||||||
// target: 'http://118.31.125.74:8000/admin/',
|
// 网上找的模拟接口
|
||||||
// changeOrigin: true,
|
target: 'https://lianghj.top:8888/api/private/v1/',
|
||||||
// pathRewrite: {
|
changeOrigin: true,
|
||||||
// '^/api': ''
|
pathRewrite: {
|
||||||
|
'^/api': ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// css: {
|
||||||
|
// loaderOptions: {
|
||||||
|
// sass: {
|
||||||
|
// // 8版本用prependData:
|
||||||
|
// prependData: `
|
||||||
|
// @import "@/styles/variables.scss"; // scss文件地址
|
||||||
|
// @import "@/styles/mixin.scss"; // scss文件地址
|
||||||
|
// `
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user