vue2设置自定义域名跳转

需求:首次登录域名为aa.com,之后登录系统后在系统内某个模块设置三级域名为second,之后退出登录到aa.com,登录进入系统后域名自动变为second.aa.com最后退出的域名也是second.aa.com,通过不同的域名配置动态的登录页面和系统内布局样式等

1.效果

原域名

输入账号登录后,添加了自定义的域名test在最前面

2.环境配置

要实现这个功能需要区分生产环境(本地)development和开发环境(线上)production

3.登录页请求

需要在登录页http://test.xx.com.cn/#/login页面直接调用后端接口,后端会返回登录页的公司图标等样式文件。在登录页肯定没有token,需要后端根据域名判断

4.登录系统后

登录系统后需要根据登录返回的domain域名来跳转,需要区分环境判断。例子如下

javascript 复制代码
this.$store
            .dispatch("userInfo/Login", this.userLoginForm)
            .then((domain) => {

              if (process.env.NODE_ENV === "development") {
                this.$router.push({ path: "/" });
              } else {
                // 构建跳转URL
                const targetUrl = this.buildRedirectUrl(domain);
                // // 跳转到目标地址
                window.location.href = targetUrl;
              }
            })
            .catch(() => {
   
            });
    // 构建跳转URL的方法
    buildRedirectUrl(domain) {

      const primaryDomain = domain
        ? `http://${domain}.xx.com.cn/`
        : `http://xx.com.cn/`;
      // 构建完整URL
      return primaryDomain;
    },

5.axios设置

跳转后会出现跨域的问题,所以axios配置也要添加判断,

javascript 复制代码
const instance = axios.create({
    baseURL: process.env.NODE_ENV === 'production' ? undefined : 'http://xx.com.cn/',
    withCredentials: process.env.NODE_ENV === 'production' ? true : false  // 线上必须要设置允许跨域
})

6.退出登录

在清空了token后直接跳转到login即可,路由拦截的话也是直接跳转到/login就行

javascript 复制代码
       this.$router.replace("/login");

文章到此结束,希望对你有所帮助~

相关推荐
冬奇Lab2 小时前
AI Workflow 定义的四次演进:从 Markdown 到 JS 脚本,再到分布式多 Agent
javascript·人工智能·agent
zhangxingchao2 小时前
Kotlin常用的Flow 操作符整理
前端
IT_陈寒3 小时前
React的useState居然还有这种坑?我差点删库跑路
前端·人工智能·后端
Pedantic5 小时前
SwiftUI 手势笔记
前端·后端
橙子家5 小时前
浏览器缓存之【结构化数据库与缓存】: IndexedDB、Cache storage 和 Storage buckets
前端
user20585561518135 小时前
X6 中边悬浮置顶,规避 `mouseleave` 事件丢失问题
前端
李明卫杭州5 小时前
CSS aspect-ratio 属性完全指南
前端
Pedantic7 小时前
SwiftUI 手势层级(Gesture Hierarchy)详解
前端
飘尘7 小时前
前端转型全栈(Java后端)的快速上手指引
前端·后端·全栈