charles
2024-07-18 1a0e437bd073d428e276a804c6846e409d92a99e
fix:与aps共享token
2个文件已修改
1个文件已添加
104 ■■■■■ 已修改文件
src/common/untils/index.js 86 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/common/untils/request.js 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/components/layout/components/appHeader/index.vue 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/common/untils/index.js
New file
@@ -0,0 +1,86 @@
import router from '@/router';
export const getPortFromUrl=(url) =>{
    try {
        const portPattern = /:\d+/;
        const urlObj = new URL(url);
        const portMatch = portPattern.exec(urlObj.host)[0].slice(1); // 去掉冒号
        return parseInt(portMatch, 10); // 转换为整数
    } catch (error) {
        console.error(error.message);
        return null;
    }
};
export const doHostName=(hostname)=>{
   /* if(hostname.includes('localhost')){
        return hostname;
    }*/
    return hostname.substring(hostname.indexOf(".")+1);
};
export const getApsPage = () => {
    // 首页部署在各个环境的端口
    /* const loginPathMap = {
       prod:`//${window.location.hostname}:9080`,
       test:`//192.168.20.119:9080`,
       // 想跳到本地启动的登录页的话需要把dev改成你本地项目路径
       dev: `//192.168.8.112:8080`
     };*/
    //return loginPathMap[environmentType()]
    let port=getPortFromUrl(window.location.href);//获得端口号
    let hostname=window.location.hostname;// 一级域名
    if(hostname.includes('fai365.com')||hostname.includes('smartai.com')||hostname.includes('navicat.com')) {// 代表存在域名
        hostname='aps.'+doHostName(hostname);
    }
    if(hostname.includes('localhost')){
        port=process.env.VUE_APP_APS_PORT;//端口号与 .env.development 中 VUE_APP_APS_PORT 跟本地aps项目的端口号保持一致
    }
    if(router.mode==='history'){
        if(port){
            return `${hostname}:${port}`;
        }else{
            return `${hostname}`;
        }
    }else{
        if(port){
            return `${hostname}:${port}`+'/#';
        }else{
            return `${hostname}`+'/#';
        }
    }
};
/**
 *  @param dataList 原列表数据
 *  @param childFieldName 子级属性名称
 *  @param pIdFieldName 关联上级节点属性名称
 *  @param idFieldName 节点属性名称
 *  @param rootValue 根节点的值(一般是小于等于0数)
 * */
export const getTreeData=(dataList,childFieldName='children',pIdFieldName='parentId',idFieldName = 'id',rootValue=0)=>{
    const rootArr=[];
    //1.先获取根节点列表
    dataList.forEach(item=>{
       if(item[pIdFieldName]===rootValue){//是根节点
           //1.1 递归获取根节点的所有下级节点
           const children= getChildrenNode(item[idFieldName],dataList);
           if(Array.isArray(children)&&children.length>0){
               item[childFieldName]=children;
           }
           rootArr.push(item)
       }
    });
    //2.递归获取根节点的所有下级节点
    function getChildrenNode(parentId,dataList) {
        //1.根据根节点获取 子节点列表
        const childrenArr=dataList.filter(item=>parentId===item[pIdFieldName]);
        //2.孩子有可能还有孩子 遍历孩子进行重新递归
        childrenArr.forEach(item=>{
            const children=getChildrenNode(item[idFieldName],dataList);
            if(children.length>0){
                item[childFieldName]=children;
            }
        });
        return childrenArr;
    }
    return rootArr;
};
src/common/untils/request.js
@@ -1,6 +1,6 @@
import axios from "axios"
import { Message } from "element-ui"
import {getApsPage} from './index.js';
// import router from '@/router'
// 开发环境下将自己的token复制到这里, 也可以在浏览器中手动添加token到cookie中,cookie中的token优先
@@ -27,7 +27,7 @@
  withCredentials: true // 是否允许带cookie这些
})
const getApsPage = () => {
/*const getApsPage = () => {
    // 首页部署在各个环境的端口
    const loginPathMap = {
        prod:`//${window.location.hostname}:9080`,
@@ -37,7 +37,7 @@
    }
    return loginPathMap[environmentType()]
}
}*/
/* //POST传参序列化(添加请求拦截器) */
Axios.interceptors.request.use(
src/components/layout/components/appHeader/index.vue
@@ -21,6 +21,7 @@
<script>
import Cookies from "js-cookie"
import UpdatePassWord from "./components/updatePassWord"
import {getApsPage} from '@/common/untils/index.js'
export default {
  name: "SalesLeads",
  props: {
@@ -67,7 +68,7 @@
      return type
    },
    getApsPage() {
   /* getApsPage() {
      // 首页部署在各个环境的端口
      const loginPathMap = {
        prod: `//${window.location.hostname}:9080`,
@@ -76,7 +77,7 @@
        dev: `//192.168.8.113:8080`
      }
      return loginPathMap[this.environmentType()]
    },
    },*/
    handleCommand(command) {
      if (command === "logout") {
        // this.$router.push({ path: "/login" })
@@ -89,11 +90,14 @@
          .then(() => {
            Cookies.remove("token")
            // window.location.href = 'http://localhost:8080/login'; //本地的智慧工厂-登录页
            window.location.href = "http:" + this.getApsPage() + "/login"
            //window.location.href = "http:" + this.getApsPage() + "/login"
            this.$message({
              type: "success",
              message: "注销成功!"
            })
            });
              setTimeout(()=>{
                  window.open(`//${getApsPage()}/login`);
              },500);
          })
          .catch(() => {
            this.$message({