提交 be59d92a authored 作者: 王鹏飞's avatar 王鹏飞

chore: 新增权限方法和指令;优化store数据;新增401页面;

上级 b39cd786
...@@ -2,7 +2,7 @@ import httpRequest from '@/utils/axios' ...@@ -2,7 +2,7 @@ import httpRequest from '@/utils/axios'
// 获取用户信息 // 获取用户信息
export function getUser() { export function getUser() {
return httpRequest.get('/api/passport/account/get-user-info') return httpRequest.get('/api/resource/v1/util/info')
} }
// 退出登录 // 退出登录
...@@ -40,7 +40,7 @@ export function getMapList() { ...@@ -40,7 +40,7 @@ export function getMapList() {
} }
// 上传视频 // 上传视频
export function getCreateAuth(data: { title: string, file_name: string }) { export function getCreateAuth(data: { title: string; file_name: string }) {
return httpRequest.post('/api/resource/v1/resource/video/create-auth', data) return httpRequest.post('/api/resource/v1/resource/video/create-auth', data)
} }
...@@ -57,4 +57,4 @@ export function getCategoryList(params: { type: string; category_name?: string } ...@@ -57,4 +57,4 @@ export function getCategoryList(params: { type: string; category_name?: string }
// 获取项目列表 // 获取项目列表
export function getProjectList(params: { organization_id: string }) { export function getProjectList(params: { organization_id: string }) {
return httpRequest.get('/api/resource/v1/util/members', { params }) return httpRequest.get('/api/resource/v1/util/members', { params })
} }
\ No newline at end of file
...@@ -51,7 +51,7 @@ function genNavClassName(data: IMenuItem) { ...@@ -51,7 +51,7 @@ function genNavClassName(data: IMenuItem) {
<img :src="userInfo.avatar || 'https://webapp-pub.ezijing.com/website/base/images/avatar.svg'" /> <img :src="userInfo.avatar || 'https://webapp-pub.ezijing.com/website/base/images/avatar.svg'" />
</div> </div>
<div class="app-header-user-main"> <div class="app-header-user-main">
<h3>{{ userStore.userName }}</h3> <h3>{{ userInfo.name }}</h3>
<p>{{ userInfo.email || userInfo.mobile }}</p> <p>{{ userInfo.email || userInfo.mobile }}</p>
</div> </div>
<div class="app-header-user-buttons"> <div class="app-header-user-buttons">
......
...@@ -14,9 +14,13 @@ import AppList from '@/components/base/AppList.vue' ...@@ -14,9 +14,13 @@ import AppList from '@/components/base/AppList.vue'
import AppUpload from '@/components/base/AppUpload.vue' import AppUpload from '@/components/base/AppUpload.vue'
import modules from './modules' import modules from './modules'
import { permissionDirective } from '@/utils/permission'
const app = createApp(App) const app = createApp(App)
// 注册公共组件 // 注册公共组件
app.component('AppCard', AppCard).component('AppList', AppList).component('AppUpload', AppUpload) app.component('AppCard', AppCard).component('AppList', AppList).component('AppUpload', AppUpload)
app.directive('permission', permissionDirective)
// 注册模块 // 注册模块
modules({ router }) modules({ router })
......
const routes = [
{
path: '/401',
component: () => import('./views/401.vue')
}
]
export { routes }
差异被折叠。
...@@ -100,7 +100,7 @@ const handleUpdate = () => { ...@@ -100,7 +100,7 @@ const handleUpdate = () => {
<AppCard title="数据字典"> <AppCard title="数据字典">
<AppList v-bind="listOptions" ref="appList" border stripe> <AppList v-bind="listOptions" ref="appList" border stripe>
<el-row> <el-row>
<el-button type="primary" @click="handleAdd" style="margin-bottom: 20px">新增字典</el-button> <el-button type="primary" @click="handleAdd" style="margin-bottom: 20px" v-permission="'test1'">新增字典</el-button>
</el-row> </el-row>
<template v-slot:created_time_start="{ params }"> <template v-slot:created_time_start="{ params }">
<el-date-picker v-model="params.created_time_start" type="date" placeholder="开始时间"> </el-date-picker> <el-date-picker v-model="params.created_time_start" type="date" placeholder="开始时间"> </el-date-picker>
......
...@@ -7,14 +7,15 @@ const router = createRouter({ ...@@ -7,14 +7,15 @@ const router = createRouter({
}) })
router.beforeEach(async (to, from, next) => { router.beforeEach(async (to, from, next) => {
const whiteList = ['/401']
const user = useUserStore() const user = useUserStore()
if (!user.isLogin) { if (!user.isLogin && !whiteList.includes(to.path)) {
try { try {
await user.getUser() await user.getUser()
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
user.isLogin ? next() : (location.href = `${import.meta.env.VITE_LOGIN_URL}?rd=${encodeURIComponent(location.href)}`) user.isLogin ? next() : next('/401')
return return
} }
next() next()
......
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { getMapList } from '@/api/base' import { getMapList } from '@/api/base'
interface State {
mapList: IMapState[]
}
interface IMapState { interface IMapState {
id: string id: string
key: string key: string
...@@ -17,18 +20,23 @@ interface IValuesList { ...@@ -17,18 +20,23 @@ interface IValuesList {
sort: string sort: string
value: string value: string
} }
export const useMapStore = defineStore({ export const useMapStore = defineStore({
id: 'map', id: 'map',
state: () => { state: (): State => {
return { return {
mapList: [] as IMapState | any mapList: []
}
},
getters: {
getMapValuesByKey: state => {
return (key: string) => state.mapList.find(map => map.key === key)?.values || []
} }
}, },
getters: {},
actions: { actions: {
async getMapList() { async getMapList() {
const res = await getMapList() const res = await getMapList()
this.mapList = res.data this.mapList = res.data || []
} }
} }
}) })
......
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { getUser, logout } from '@/api/base' import { getUser, logout } from '@/api/base'
import type { UserType, ProjectType, OrganizationType, RoleType, PermissionType } from '@/types'
interface IUserState { interface State {
id: string user: UserType | null
avatar: string project: ProjectType | null
mobile: string organization: OrganizationType | null
realname: string roles: RoleType[]
nickname: string permissions: PermissionType[]
username: string
email: string
} }
export const useUserStore = defineStore({ export const useUserStore = defineStore({
id: 'user', id: 'user',
state: () => { state: (): State => ({
return { user: null,
user: null as IUserState | null organization: null,
} project: null,
}, roles: [],
permissions: []
}),
getters: { getters: {
isLogin: state => !!state.user, isLogin: state => !!state.user
userName: ({ user }) => {
if (!user) return ''
return user.realname || user.nickname || user.username || ''
}
}, },
actions: { actions: {
async getUser() { async getUser() {
const res = await getUser() const res = await getUser()
this.user = res.data const { info } = res.data
console.log(res.data, 'res.data') const { organization, project, roles, permissions } = res.data.permissions
this.user = info
this.organization = organization
this.project = project
this.roles = roles
this.permissions = permissions
}, },
async logout() { async logout() {
await logout() await logout()
......
...@@ -6,3 +6,48 @@ export interface IMenuItem { ...@@ -6,3 +6,48 @@ export interface IMenuItem {
icon?: Component icon?: Component
children?: IMenuItem[] children?: IMenuItem[]
} }
// 用户信息
export interface UserType {
id: string
mobile: string
name: string
email: string
username: string
avatar: string
}
// 项目信息
export interface ProjectType {
id: string
tab: string
name: string
}
// 机构信息
export interface OrganizationType {
id: string
name: string
contact_name: string
contact_information: string
validity_date: string
is_valid: 1 | 2
}
// 角色信息
export interface RoleType {
id: string
name: string
desc: string
}
// 权限信息
export interface PermissionType {
desc: string
effect_uris: string
id: string
name: string
parent_id: string
system_tag: number
type: number
tag: string
}
...@@ -4,7 +4,6 @@ import { ElMessage } from 'element-plus' ...@@ -4,7 +4,6 @@ import { ElMessage } from 'element-plus'
import router from '@/router' import router from '@/router'
const httpRequest = axios.create({ const httpRequest = axios.create({
// baseURL: 'https://project-api.ezijing.com',
timeout: 60000, timeout: 60000,
withCredentials: true, withCredentials: true,
headers: { headers: {
...@@ -52,7 +51,7 @@ httpRequest.interceptors.response.use( ...@@ -52,7 +51,7 @@ httpRequest.interceptors.response.use(
// 未登录 // 未登录
if (status === 403) { if (status === 403) {
location.href = `${import.meta.env.VITE_LOGIN_URL}?rd=${encodeURIComponent(location.href)}` location.href = `${import.meta.env.VITE_LOGIN_URL}?rd=${encodeURIComponent(location.href)}`
} else if (status === 400) { } else if (status === 400 || status === 402) {
// 未授权 // 未授权
router.push('/401') router.push('/401')
} else { } else {
......
import { useUserStore } from '@/stores/user'
import type { DirectiveBinding } from 'vue'
// 判断是否有权限
export function checkPermission(value: string | string[]): boolean {
const userStore = useUserStore()
const permissions = userStore.permissions
if (Array.isArray(value)) {
return permissions.some(item => value.includes(item.tag))
} else {
return !!permissions.find(item => item.tag === value)
}
}
// 权限指令
export function permissionDirective(el: HTMLElement, binding: DirectiveBinding) {
const { value } = binding
if (!value) return
if (!checkPermission(value)) {
el.parentNode && el.parentNode.removeChild(el)
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论