feat: 权限接口请求优化以及修复其他问题 (#290)

This commit is contained in:
dayou 2024-06-14 19:05:23 +08:00 committed by sudoooooo
parent c122589a2e
commit 32b1c4888d
11 changed files with 67 additions and 70 deletions

View File

@ -1,5 +1,4 @@
import axios from './base'
// 空间
export const createSpace = ({ name, description, members }: any) => {
return axios.post('/workspace', { name, description, members })
@ -29,7 +28,7 @@ export const getUserList = (username: string) => {
})
}
// 协作权限列表
// 获取协作权限下拉框枚举
export const getPermissionList = () => {
return axios.get('collaborator/getPermissionList')
}

View File

@ -26,7 +26,7 @@
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { ref, watch } from 'vue'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
const route = useRoute()
@ -58,18 +58,18 @@ const tabArr = [
}
]
const tabs = ref([])
onMounted(async () => {
await store.dispatch('fetchCooperPermissions', route.params.id)
watch(() => store.state.cooperPermissions, (newVal) => {
tabs.value = []
//
if (store.state.cooperPermissions.includes(SurveyPermissions.SurveyManage)) {
if (newVal.includes(SurveyPermissions.SurveyManage)) {
tabs.value.push(tabArr[0])
tabs.value.push(tabArr[1])
}
//
if (store.state.cooperPermissions.includes(SurveyPermissions.DataManage)) {
if (newVal.includes(SurveyPermissions.DataManage)) {
tabs.value.push(tabArr[2])
}
})
}, { immediate: true })
</script>
<style lang="scss" scoped>
.nav {

View File

@ -5,7 +5,13 @@
</div>
</template>
<script setup lang="ts">
const handleNavigateHome = () => window.open('/survey', '_self')
import { useRouter } from 'vue-router'
const router = useRouter()
const handleNavigateHome = () => {
router.push({
name: 'survey'
})
}
</script>
<style lang="scss" scoped>
.back-btn {

View File

@ -10,7 +10,6 @@
row-class-name="tableview-row"
cell-class-name="tableview-cell"
style="width: 100%"
@row-click="onRowClick"
>
<el-table-column column-key="space" width="20" />
<el-table-column
@ -98,9 +97,7 @@ const isAdmin = (id: string) => {
UserRole.Admin
)
}
const onRowClick = () => {
console.log('onRowClick')
}
const handleModify = async (id: string) => {
await store.dispatch('list/getSpaceDetail', id)
modifyType.value = 'edit'

View File

@ -1,6 +1,6 @@
import { createRouter, createWebHistory } from 'vue-router'
import { createRouter, createWebHistory, type RouteLocationNormalized, type NavigationGuardNext } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { useStore } from 'vuex'
import { useStore, type Store } from 'vuex'
import { SurveyPermissions } from '@/management/utils/types/workSpace'
import { ElMessage } from 'element-plus'
import 'element-plus/theme-chalk/src/message.scss'
@ -23,7 +23,7 @@ const routes: RouteRecordRaw[] = [
path: '/survey/:id/edit',
meta: {
needLogin: true,
premissions: [SurveyPermissions.SurveyManage]
permissions: [SurveyPermissions.SurveyManage]
},
name: 'QuestionEdit',
component: () => import('../pages/edit/index.vue'),
@ -94,7 +94,7 @@ const routes: RouteRecordRaw[] = [
name: 'analysisPage',
meta: {
needLogin: true,
premissions: [SurveyPermissions.DataManage]
permissions: [SurveyPermissions.DataManage]
},
component: () => import('../pages/analysis/AnalysisPage.vue')
},
@ -103,7 +103,7 @@ const routes: RouteRecordRaw[] = [
name: 'publish',
meta: {
needLogin: true,
premissions: [SurveyPermissions.SurveyManage]
permissions: [SurveyPermissions.SurveyManage]
},
component: () => import('../pages/publish/PublishPage.vue')
},
@ -132,57 +132,60 @@ const router = createRouter({
})
router.beforeEach(async (to, from, next) => {
const store = useStore()
const store = useStore();
// 初始化用户信息
if (!store.state.user?.initialized) {
store?.dispatch('user/init')
await store.dispatch('user/init');
}
// 更新页面标题
if (to.meta.title) {
document.title = to.meta.title as string
document.title = to.meta.title as string;
}
if (to.meta.needLogin) {
if (store?.state?.user?.hasLogined) {
if (to.meta.premissions) {
const params = to.params
await store.dispatch('fetchCooperPermissions', params.id)
if (
(to.meta.premissions as []).some((permission) =>
store.state?.cooperPermissions?.includes(permission)
)
) {
next()
await handleLoginGuard(to, from, next, store);
} else {
ElMessage.warning('您没有该问卷的相关协作权限')
next({
name: 'survey'
})
}
} else {
next()
next();
}
});
async function handleLoginGuard(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext, store: Store<any>) {
if (store.state.user?.hasLogined) {
await handlePermissionsGuard(to, from, next, store);
} else {
next({
name: 'login',
query: {
redirect: encodeURIComponent(to.path)
query: { redirect: encodeURIComponent(to.path) },
});
}
})
}
async function handlePermissionsGuard(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext, store: Store<any>) {
const currSurveyId = to?.params?.id || ''
const prevSurveyId = from?.params?.id || ''
// 如果跳转页面不存在surveyId 或者不需要页面权限,则直接跳转
if (!to.meta.permissions || !currSurveyId) {
next()
} else {
// 如果跳转编辑页面且跳转页面和上一页的surveyId不同判断是否有对应页面权限
if (currSurveyId !== prevSurveyId) {
await store.dispatch('fetchCooperPermissions', currSurveyId)
if (hasRequiredPermissions(to.meta.permissions as string[], store.state.cooperPermissions)) {
next();
} else {
ElMessage.warning('您没有该问卷的相关协作权限');
next({ name: 'survey' });
}
} else {
next()
next();
}
}
}
})
// router.afterEach(async (to, from) => {
// const store = useStore()
// if (to.meta.premissions) {
// const params = to.params
// await store.dispatch('fetchCooperPermissions', params.id)
// if (!(to.meta.premissions as []).some((permission) => store.state?.cooperPermissions?.includes(permission))) {
// ElMessage.warning('您没有该问卷的相关协作权限')
// router.push({
// name: 'survey'
// })
// }
// }
// })
function hasRequiredPermissions(requiredPermissions: string[], userPermissions: string[]) {
return requiredPermissions.some(permission => userPermissions.includes(permission));
}
export default router

View File

@ -14,7 +14,6 @@ export default {
},
async fetchCooperPermissions({ commit }, id) {
const res = await getCollaboratorPermissions(id)
console.log(res.data)
if (res.code === CODE_MAP.SUCCESS) {
commit('setCooperPermissions', res.data.permissions)
}

View File

@ -79,7 +79,6 @@ const emit = defineEmits(['addOther', 'optionChange', 'change'])
const moduleConfig = inject('moduleConfig')
const optionConfigVisible = ref(false)
const openOptionConfig = () => {
console.log('open')
optionConfigVisible.value = true
}

View File

@ -3,11 +3,8 @@
<div v-for="item in renderData" :key="item.field">
<QuestionWrapper
class="gap"
v-bind="$attrs"
:moduleConfig="item"
:qIndex="item.qIndex"
:indexNumber="item.indexNumber"
:showTitle="true"
@change="handleChange"
></QuestionWrapper>
</div>

View File

@ -1,7 +1,6 @@
<template>
<QuestionRuleContainer
v-if="visible"
v-bind="$attrs"
:moduleConfig="questionConfig"
:indexNumber="indexNumber"
:showTitle="true"

View File

@ -102,7 +102,6 @@ const submitSurver = async () => {
}
try {
const params = normalizationRequestBody()
console.log(params)
const res: any = await submitForm(params)
if (res.code === 200) {
store.commit('setRouter', 'successPage')

View File

@ -10,7 +10,6 @@ export default {
const questionArr = []
item.forEach((questionKey) => {
console.log('题目重新计算')
const question = { ...questionData[questionKey] }
// 开启显示序号
if (question.showIndex) {