Vue vue-element-admin中权限验证逻辑和富文本编辑器配置

247 阅读3分钟

「这是我参与11月更文挑战的第18天,活动详情查看:2021最后一次更文挑战

vue-element-admin 笔记

之前在做的一个项目要做后台,想用后台模板来做,然后选用了 vue-element-admin 。感叹大佬的技术,记录一下用这个模板的一点笔记。笔记在整理中,一边整理一边更新

官网: vue-element-admin.

一、权限验证

因为还没学关于token的权限验证方法,对内置的方法不熟悉,想先关闭进行重写,但是好像不太好关闭,因为还要和其他的模块兼容,先用了简单的方法绕过(感觉可能在其他模块会有问题)。

补充: 总体的逻辑是通过获取当前用户的权限去比对路由表,生成当前用户具有的权限可访问的路由表。 之前是在src\permission.js这个文件这里比较迷惑,弄明白了导航守卫的概念之后就好理解一点了。

具体的文件内容简化过了,只分析导航守卫这部分。

// src\permission.js

const whiteList = ['/login', '/auth-redirect'] // no redirect whitelist

router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start()

  // set page title
  document.title = getPageTitle(to.meta.title)

  // determine whether the user has logged in
  const hasToken = getToken()

  if (hasToken) {
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      next({ path: '/' })
      NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
    } else {
      // determine whether the user has obtained his permission roles through getInfo
      const hasRoles = store.getters.roles && store.getters.roles.length > 0
      if (hasRoles) {
        next()
      } else {
        try {
          // get user info
          // note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
          const { roles } = await store.dispatch('user/getInfo')

          // generate accessible routes map based on roles
          const accessRoutes = await store.dispatch('permission/generateRoutes', roles)

          // dynamically add accessible routes
          router.addRoutes(accessRoutes)

          // hack method to ensure that addRoutes is complete
          // set the replace: true, so the navigation will not leave a history record
          next({ ...to, replace: true })
        } catch (error) {
          // remove token and go to login page to re-login
          await store.dispatch('user/resetToken')
          Message.error(error || 'Has Error')
          next(`/login?redirect=${to.path}`)
          NProgress.done()
        }
      }
    }
  } else {
    /* has no token*/

    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next()
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      next(`/login?redirect=${to.path}`)
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})

这里最主要的是beforeEach(),这个是全局前置守卫,简单来说就是在进入到新路由的时候会进行拦截并执行相应的回调函数,在回调函数中调用next()钩子来让路由继续执行下去(继续跳转到目标路由或进行重定向)。

这里的大致逻辑是

  1. 获取token
  2. 如果token不存在,则判断跳转的路由是否在白名单中(不需要token验证),如果在就继续跳转,不在就重定向到登录页面登录获取token。
  3. 获取到token后进行进一步的身份(roles)分析,roles对应路由表。
  4. 如果所需的roles存在,则继续跳转。
  5. 如果所需的roles不存在,则重新通过getinfo和generateRoutes来获取对应的roles和相应的路由表。
  6. 继续进行跳转。

二、富文本编辑器

文件内容的编辑需要用到富文本编辑器,选用了 Tinymce 官网: Tinymce.

因为工具栏可以自由配置功能和图标的位置,项目里的工具栏是这样配置的,感觉比较整齐和简洁

const toolbar = ['searchreplace fontselect formatselect fontsizeselect blockquote removeformat subscript superscript code codesample bullist numlist undo redo ', 'bold italic underline strikethrough hr forecolor backcolor alignleft aligncenter alignright outdent indent  ', ' table link image charmap preview anchor pagebreak insertdatetime media emoticons fullscreen ']

三、富文本编辑器中上传本地图片

因为Tinymce在编辑的时候不支持上传本地图片(不只是Tinymce,好像大多数富文本编辑器都不支持,付费插件不讨论),要加入图片必须用在线图片(引用图片的网络地址)。 可以先将本地图片上传至服务器,在通过网络地址来加入图片。

在网上找到一个应该可行的解决方案 segmentfault.com/a/119000002….

**上传图片的思想:**先拿到本地的图片转换为base64 的格式,传递给后端,后端保存后再返回给我们一个图片地址,最终我们需要的就是这个地址做显示和保存。

补充: 最近有了解到一个叫github图床的东西,或许可以用来解决上传本地图片的问题。