electron-vue通过配置文件设置baseUrl

  • 在项目更目录下创建config.conf
    在这里插入图片描述
{
    "baseURL": "http://***.cn:8001"
}
  • 在package.json中配置build

在这里插入图片描述

"build": {
        ...
        "appId": "com.yuma.duifu",
        "extraResources": {
            "from": "./config.conf",
            "to": "../"
        },
        ...
    },
  • 获取安装路径地址和解析配置内容

在这里插入图片描述

const { app } = require("electron").remote
const path = require('path');
const fs = require('fs')

export function getSystem() {
    //这是mac系统
    if (process.platform == 'darwin') {
        return 1
    }
    //这是windows系统
    if (process.platform == 'win32') {
        return 2
    }
    //这是linux系统
    if (process.platform == 'linux') {
        return 3
    }
}
/**
 * 
 * @returns 获取安装路径
 */
export function getExePath() {
    return path.dirname(app.getPath('exe'));
}
/**
 * 
 * @returns 获取配置文件路径
 */
export function getConfigPath() {
    if (getSystem() === 1) {
        return getExePath() + '/config.conf'
    } else {
        return getExePath() + '\\config.conf'
    }
}
/**
 * 读取配置文件
 */
export function readConfig(callback) {
    fs.readFile(getConfigPath(), 'utf-8', (err, data) => {
        if (data) {
            //有值
            const config = JSON.parse(data)
            callback(config)
        }
    })
}
  • 调用
    在这里插入图片描述
<script>
import { readConfig } from "@/utils/index.js";
export default {
  name: "home",
  data() {
    return {
     
    };
  },
  mounted() {
    readConfig((res) => {
      const { baseURL } = res;
      this.$store.state.Counter.baseURL = baseURL;
      this._getLoginQrCode();
      this._getIntervalToken();
    });
  },
  methods: {
  
  },
};
</script>
  • 更换axios的baseUrl

在这里插入图片描述

import { Message } from 'element-ui'
import axios from 'axios'
import store from '@/store'
const service = axios.create({
    //服务器域名地址
    baseURL: "http://127.0.0.1:8001",
    //当跨域请求时发送cookie
    // withCredentials: true, 
    //超时时间
    timeout: 30000
})

service.interceptors.request.use(
        //请求配置
        config => {
            //添加请求头
            config.headers['token'] = store.state.Counter.token
            if (store.state.Counter.baseURL) {
                config.baseURL = store.state.Counter.baseURL
            }
            return config
        },
        error => {
            // 错误
            console.log(error)
            return Promise.reject(error)
        }
    )
    // 返回过滤器
service.interceptors.response.use(
    response => {
        const res = response.data
        if (process.env.NODE_ENV !== 'production') {
            console.log(res)
        }
        if (res.code !== 200) {
            const { config: { url } } = response
            if (url.endsWith('/Login') && res.code === 250) {
                return Promise.reject(res.message)
            }
            Message({
                message: res.message || 'Error',
                type: 'error',
                duration: 5 * 1000
            })
            if (res.code === 300 || res.code === 304 || res.code === 302) {
                store.state.Counter.token = ''
            }
            return Promise.reject(res.message)
        } else {
            return res
        }
    },
    error => {
        console.log('err==>' + error)
        Message({
            message: '服务器连接失败',
            type: 'error',
            duration: 5 * 1000
        })
        return Promise.reject(error)
    }
)
export default service
  • 完成
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值