群控体系服务端开发模式-应用开发-前端个人资料开发 ...

打印 上一主题 下一主题

主题 891|帖子 891|积分 2673

一、总结

        其实程序开发到现在,简朴的后端框架就只剩下获取登录账号信息及获取登录账号菜单这两个功能咯。具体见下图:
        1、未登录时总业务流程图


        2、登录后总业务流程图


二、获取登录账号信息对接

        在根目次下src文件夹下store文件夹下modules文件夹下的user.js文件中,修改代码如下:
  1. const state = {
  2.     token: getToken(),
  3.     username: '',
  4.     avatar: '',
  5.     email: '',
  6.     realname: '',
  7.     department_title: '',
  8.     grade_title: '',
  9.     rolename: '',
  10.     roles: [],
  11.     butts: []
  12. }
  13. const mutations = {
  14.     SET_TOKEN: (state, token) => {
  15.         state.token = token
  16.     },
  17.     SET_EMAIL: (state, email) => {
  18.         state.email = email
  19.     },
  20.     SET_USERNAME: (state, username) => {
  21.         state.username = username
  22.     },
  23.     SET_AVATAR: (state, avatar) => {
  24.         state.avatar = avatar
  25.     },
  26.     SET_REALNAME: (state, realname) => {
  27.         state.realname = realname
  28.     },
  29.     SET_DEPARTMENT_TITLE: (state, department_title) => {
  30.         state.department_title = department_title
  31.     },
  32.     SET_GRADE_TITLE: (state, grade_title) => {
  33.         state.grade_title = grade_title
  34.     },
  35.     SET_ROLENAME: (state, rolename) => {
  36.         state.rolename = rolename
  37.     },
  38.     SET_BUTTS: (state, butts) => {
  39.         state.butts = butts
  40.     },
  41.     SET_ROLES: (state, roles) => {
  42.         state.roles = roles
  43.     }
  44. }
  45. // get user info
  46. getInfo({commit, state}) {
  47.     return new Promise((resolve, reject) => {
  48.         getInfo().then(response => {
  49.             const {data} = response
  50.             if (!data) {
  51.                 reject('验证失败,请重新登录。')
  52.             }
  53.             const { butt, key, username, avatar, email, realname, department_title, grade_title, rolename } = data
  54.             if (!butt || butt.length <= 0) {
  55.                 reject('您权限不足,请联系系统管理员')
  56.             }
  57.             commit('SET_BUTTS', butt)
  58.             commit('SET_ROLES', key)
  59.             commit('SET_USERNAME', username)
  60.             commit('SET_AVATAR', avatar)
  61.             commit('SET_EMAIL', email)
  62.             commit('SET_REALNAME', realname)
  63.             commit('SET_DEPARTMENT_TITLE', department_title)
  64.             commit('SET_GRADE_TITLE', grade_title)
  65.             commit('SET_ROLENAME', rolename)
  66.             resolve(key)
  67.         }).catch(error => {
  68.             reject(error)
  69.         })
  70.     })
  71. },
复制代码
三、获取登录账号菜单信息对接

       获取菜单这块,需要更改两部分,第一部分就是路由修改,第二部分才是菜单转换成路由
        1、路由修改

                在根目次下src文件夹下route文件夹下,修改index.js文件,代码如下
  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. /* Layout */
  4. import Layout from '@/layout'
  5. Vue.use(Router)
  6. /**
  7. * Note: sub-menu only appear when route children.length >= 1
  8. * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
  9. *
  10. * hidden: true                   if set true, item will not show in the sidebar(default is false)
  11. * alwaysShow: true               if set true, will always show the root menu
  12. *                                if not set alwaysShow, when item has more than one children route,
  13. *                                it will becomes nested mode, otherwise not show the root menu
  14. * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
  15. * name:'router-name'             the name is used by <keep-alive> (must set!!!)
  16. * meta : {
  17.     roles: ['admin','editor']    control the page roles (you can set multiple roles)
  18.     title: 'title'               the name show in sidebar and breadcrumb (recommend set)
  19.     icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
  20.     noCache: true                if set true, the page will no be cached(default is false)
  21.     affix: true                  if set true, the tag will affix in the tags-view
  22.     breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
  23.     activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  24.   }
  25. */
  26. /**
  27. * constantRoutes
  28. * a base page that does not have permission requirements
  29. * all roles can be accessed
  30. */
  31. export const constantRoutes = [
  32.   {
  33.     path: '/redirect',
  34.     component: Layout,
  35.     hidden: true,
  36.     children: [
  37.       {
  38.         path: '/redirect/:path(.*)',
  39.         component: () => import('@/views/redirect/index')
  40.       }
  41.     ]
  42.   },
  43.   {
  44.     path: '/login',
  45.     component: () => import('@/views/login/index'),
  46.     hidden: true
  47.   },
  48.   {
  49.     path: '/404',
  50.     component: () => import('@/views/error-page/404'),
  51.     hidden: true
  52.   },
  53.   {
  54.     path: '/401',
  55.     component: () => import('@/views/error-page/401'),
  56.     hidden: true
  57.   },
  58.   {
  59.     path: '/',
  60.     component: Layout,
  61.     redirect: '/dashboard',
  62.     children: [
  63.       {
  64.         path: 'dashboard',
  65.         component: () => import('@/views/dashboard/index'),
  66.         name: '首页',
  67.         meta: { title: '首页', icon: 'dashboard', affix: true }
  68.       }
  69.     ]
  70.   },
  71.   {
  72.     path: '/profile',
  73.     component: Layout,
  74.     redirect: '/profile/index',
  75.     hidden: true,
  76.     children: [
  77.       {
  78.         path: 'index',
  79.         component: () => import('@/views/profile/index'),
  80.         name: 'Profile',
  81.         meta: { title: 'Profile', icon: 'user', noCache: true }
  82.       }
  83.     ]
  84.   }
  85. ]
  86. /**
  87. * asyncRoutes
  88. * the routes that need to be dynamically loaded based on user roles
  89. */
  90. export const asyncRoutes = [
  91.   // 404 page must be placed at the end !!!
  92.   { path: '*', redirect: '/404', hidden: true }
  93. ]
  94. const createRouter = () => new Router({
  95.   // mode: 'history', // require service support
  96.   scrollBehavior: () => ({ y: 0 }),
  97.   routes: constantRoutes
  98. })
  99. const router = createRouter()
  100. // Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
  101. export function resetRouter() {
  102.   const newRouter = createRouter()
  103.   router.matcher = newRouter.matcher // reset router
  104. }
  105. export default router
复制代码
        2、登录者菜单转换成路由

                在根目次下src文件夹下store文件夹下modules文件夹下,修改permission.js文件,代码如下
  1. import {asyncRoutes, constantRoutes} from '@/router'
  2. import {getPersonalMenu} from '@/api/common'
  3. import {warn} from '@/utils/message'
  4. import Layout from '@/layout'
  5. /**
  6. * Use meta.role to determine if the current user has permission
  7. * @param roles
  8. * @param route
  9. */
  10. function hasPermission(roles, route) {
  11.     if (route.meta && route.meta.roles) {
  12.         return roles.some(role => route.meta.roles.includes(role))
  13.     } else {
  14.         return true
  15.     }
  16. }
  17. /**
  18. * Filter asynchronous routing tables by recursion
  19. * @param routes asyncRoutes
  20. * @param roles
  21. */
  22. export function filterAsyncRoutes(routes, roles) {
  23.     const res = []
  24.     routes.forEach(route => {
  25.         const tmp = {...route}
  26.         if (hasPermission(roles, tmp)) {
  27.             if (tmp.children) {
  28.                 tmp.children = filterAsyncRoutes(tmp.children, roles)
  29.             }
  30.             res.push(tmp)
  31.         }
  32.     })
  33.     return res
  34. }
  35. const state = {
  36.     routes: [],
  37.     addRoutes: []
  38. }
  39. const mutations = {
  40.     SET_ROUTES: (state, routes) => {
  41.         state.addRoutes = routes
  42.         state.routes = constantRoutes.concat(routes)
  43.     }
  44. }
  45. /**
  46. * 后台查询的菜单数据拼装成路由格式的数据
  47. * @param routes
  48. */
  49. export function generaMenu(routes, data) {
  50.     data.forEach(item => {
  51.         const menu = {
  52.             path: item.path === '#' ? item.id + '_key' : item.path,
  53.             component: item.component === '#' ? Layout : resolve => require([`@/views/${item.component}`], resolve),
  54.             hidden: item.is_hidden,
  55.             redirect: item.redirect,
  56.             children: [],
  57.             name: 'menu_' + item.menuname,
  58.             alwaysShow: item.always_show,
  59.             meta: {
  60.                 title: item.title,
  61.                 id: item.id,
  62.                 icon: item.is_icon === 1 ? item.icon : '',
  63.                 /* affix: item.affix,*/
  64.                 noCache: item.is_cache
  65.             }
  66.         }
  67.         // 一级路由
  68.         if (!item.children && item.pid === 0) {
  69.             menu.name = undefined
  70.             menu.children = [
  71.                 {
  72.                     path: item.path,
  73.                     component: resolve => require([`@/views/${item.path}`], resolve),
  74.                     name: 'menu_' + item.menuname,
  75.                     meta: {
  76.                         title: item.title,
  77.                         icon: item.is_icon === 1 ? item.icon : '',
  78.                         noCache: item.is_cache,
  79.                         //affix: item.affix
  80.                     }
  81.                 }
  82.             ]
  83.         }
  84.         // 二级路由
  85.         if (item.children) {
  86.             generaMenu(menu.children, item.children)
  87.         }
  88.         routes.push(menu)
  89.     })
  90. }
  91. function filterMenus(localMenus, remoteMenus) {
  92.     const res = []
  93.     localMenus.forEach(local => {
  94.         remoteMenus.forEach(remote => {
  95.             if (remote.path === local.path) {
  96.                 local.meta.roles = remote.roleSlugs
  97.                 if (local.children && remote.children) {
  98.                     local.children = filterMenus(local.children, remote.children)
  99.                 }
  100.                 res.push(local)
  101.             }
  102.         })
  103.     })
  104.     return res
  105. }
  106. const actions = {
  107.     generateRoutes({commit, state}, key) {
  108.         return new Promise(resolve => {
  109.             const loadMenuData = []
  110.             getPersonalMenu().then(res => {
  111.                 if (res.code === 50034) {
  112.                     reject(res.message)
  113.                 } else if (res.code === 50000) {
  114.                     warn(res.message)
  115.                 } else {
  116.                     const remoteRoutes = res.data.menu
  117.                     Object.assign(loadMenuData, remoteRoutes)
  118.                     const tempAsyncRoutes = Object.assign([], asyncRoutes)
  119.                     generaMenu(tempAsyncRoutes, loadMenuData)
  120.                     let accessedRoutes
  121.                     /*if (key == 'admin') {
  122.                         accessedRoutes = tempAsyncRoutes || []
  123.                     } else {
  124.                         accessedRoutes = filterAsyncRoutes(tempAsyncRoutes, key)
  125.                     }*/
  126.                     accessedRoutes = filterAsyncRoutes(tempAsyncRoutes, key)
  127.                     commit('SET_ROUTES', accessedRoutes)
  128.                     resolve(accessedRoutes)
  129.                 }
  130.             }).catch(error => {
  131.                 reject(error)
  132.             })
  133.         })
  134.     }
  135. }
  136. export default {
  137.     namespaced: true,
  138.     state,
  139.     mutations,
  140.     actions
  141. }
复制代码
四、提前说明

        此时就已经可以登录到体系内里去了,效果如下图。来日诰日将完成个人资料页面、退出及后端过期退出主动更新数据库的退出数据。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

杀鸡焉用牛刀

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表