单点登录

一、创建第三方用户跳转的页面 dynclogin

  • router.js
  1. {
  2. path: '/dynclogin',
  3. component: resolve => require(['../layout/dynclogin'], resolve),
  4. hidden: true
  5. },
  • dynclogin.vue
  1. <template>
  2. <div>第三方页面跳转</div>
  3. </template>
  4. <script>
  5. export default {
  6. name: "dynclogin",
  7. data(){
  8. return {
  9. token:''
  10. }
  11. },
  12. mounted() {
  13. var name = window.location.href;
  14. var questionIndex = name.indexOf('?');
  15. if(questionIndex != -1){
  16. var str = name.substring(questionIndex + 1)
  17. if(str){
  18. this.$store.dispatch('dyncLogin',str)
  19. location.reload()
  20. }
  21. }
  22. }
  23. }
  24. </script>
  25. <style scoped>
  26. </style>

vuex

  1. import { getToken, removeToken, setToken} from '../utils/auth'
  2. state: {
  3. token: getToken(),
  4. }
  5. mutations: {
  6. SET_TOKEN: (state, token) => {
  7. state.token = token
  8. },
  9. },
  10. actions: {
  11. dyncLogin({commit}, str) {
  12. var projectCodeIndex = str.indexOf('=');
  13. if(projectCodeIndex != -1){
  14. var token = str.substring(projectCodeIndex+1);
  15. setToken(token);
  16. commit('SET_TOKEN', token);
  17. }
  18. },
  19. }

最后部分路由拦截处理

  1. const whiteList = ['/login','/dynclogin'];// 不重定向白名单
  2. router.beforeEach((to, from, next) => {
  3. if (getToken()) {
  4. /*
  5. 路由拦截的时候看一下当前path路径是第三方跳转的路由页面
  6. 然后next匹配路由需要跳转的页面地址 否则next跳转默认当前地址会造成死循环
  7. */
  8. if(to.path == '/dynclogin'){
  9. next({path: '/home'});
  10. }else{
  11. next();
  12. }
  13. }else {
  14. if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
  15. next()
  16. } else {
  17. next('/login'); // 否则全部重定向到登录页
  18. }
  19. }
  20. });

auth.js

  • js-cookie cookie插件 script引入方式的话使用方法还是一样
  1. import Cookies from 'js-cookie'
  2. const TokenKey = 'Admin-Token'
  3. export function getToken() {
  4. return Cookies.get(TokenKey)
  5. }
  6. export function setToken(token) {
  7. return Cookies.set(TokenKey, token)
  8. }
  9. export function removetoken(){
  10. Cookies.remove(TokenKey)
  11. }

二、没有路由拦截的写法

  1. <template>
  2. <div>第三方页面跳转</div>
  3. </template>
  4. <script>
  5. import { setToken,getToken } from '../../utils/auth'
  6. export default {
  7. name: "dynclogin",
  8. data(){
  9. return {
  10. }
  11. },
  12. mounted() {
  13. var name = window.location.href;
  14. var questionIndex = name.indexOf('?');
  15. if(questionIndex != -1){
  16. let str = name.substring(questionIndex + 1)
  17. if(str){
  18. let projectCodeIndex = str.indexOf('=');
  19. if(projectCodeIndex != -1) {
  20. let token = str.substring(projectCodeIndex + 1);
  21. setToken(token);
  22. //看axios封装请求头带token情况做处理 如果判断$store.getters.token是否存在则必须写
  23. this.$store.commit('SET_TOKEN', token);
  24. if(getToken()){
  25. //跳转到指定页面
  26. this.$router.push({path: '/monitorManager/otaManager'})
  27. }
  28. }
  29. }
  30. }
  31. }
  32. }
  33. </script>
  34. <style scoped>
  35. </style>