Skip to content

Commit

Permalink
feat: 权限接口请求优化以及修复其他问题 (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
skique committed Jul 5, 2024
1 parent 9240ee1 commit 4ce51e7
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 70 deletions.
5 changes: 2 additions & 3 deletions web/src/management/api/space.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import axios from './base'

// 空间
export const createSpace = ({ name, description, members }: any) => {
return axios.post('/workspace', { name, description, members })
Expand Down Expand Up @@ -29,7 +28,7 @@ export const getUserList = (username: string) => {
})
}

// 协作权限列表
// 获取协作权限下拉框枚举
export const getPermissionList = () => {
return axios.get('collaborator/getPermissionList')
}
Expand Down Expand Up @@ -72,4 +71,4 @@ export const getCollaboratorPermissions = (surveyId: string) => {
surveyId
}
})
}
}
14 changes: 7 additions & 7 deletions web/src/management/components/LeftMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions web/src/management/pages/list/components/SpaceList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down
97 changes: 50 additions & 47 deletions web/src/management/router/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'),
Expand Down Expand Up @@ -94,7 +94,7 @@ const routes: RouteRecordRaw[] = [
name: 'analysisPage',
meta: {
needLogin: true,
premissions: [SurveyPermissions.DataManage]
permissions: [SurveyPermissions.DataManage]
},
component: () => import('../pages/analysis/AnalysisPage.vue')
},
Expand All @@ -103,7 +103,7 @@ const routes: RouteRecordRaw[] = [
name: 'publish',
meta: {
needLogin: true,
premissions: [SurveyPermissions.SurveyManage]
permissions: [SurveyPermissions.SurveyManage]
},
component: () => import('../pages/publish/PublishPage.vue')
},
Expand Down Expand Up @@ -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()
} else {
ElMessage.warning('您没有该问卷的相关协作权限')
next({
name: 'survey'
})
}
await handleLoginGuard(to, from, next, store);
} else {
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) },
});
}
}

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 {
next()
ElMessage.warning('您没有该问卷的相关协作权限');
next({ name: 'survey' });
}
} else {
next({
name: 'login',
query: {
redirect: encodeURIComponent(to.path)
}
})
next();
}
} else {
next()
}
})
}

function hasRequiredPermissions(requiredPermissions: string[], userPermissions: string[]) {
return requiredPermissions.some(permission => userPermissions.includes(permission));
}



// 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'
// })
// }
// }
// })
export default router
1 change: 0 additions & 1 deletion web/src/management/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 0 additions & 3 deletions web/src/render/components/MaterialGroup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 0 additions & 1 deletion web/src/render/components/QuestionWrapper.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<template>
<QuestionRuleContainer
v-if="visible"
v-bind="$attrs"
:moduleConfig="questionConfig"
:indexNumber="indexNumber"
:showTitle="true"
Expand Down
1 change: 0 additions & 1 deletion web/src/render/pages/IndexPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 0 additions & 1 deletion web/src/render/store/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default {
const questionArr = []

item.forEach((questionKey) => {
console.log('题目重新计算')
const question = { ...questionData[questionKey] }
// 开启显示序号
if (question.showIndex) {
Expand Down

0 comments on commit 4ce51e7

Please sign in to comment.