shuishen
2023-01-06 dbd6d2ca8cb996d49593c19f2e07eb54f31d1d1a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * @Author: shuishen 1109946754@qq.com
 * @Date: 2022-07-29 15:19:13
 * @LastEditors: shuishen 1109946754@qq.com
 * @LastEditTime: 2023-01-06 17:13:56
 * @FilePath: \srs-police-affairs\src\router\axios.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/**
 * 全站http配置
 *
 * axios参数说明
 * isSerialize是否开启form表单提交
 * isToken是否需要token
 */
import axios from "axios"
import router from "@/router/page/index.js"
import { Message } from "element-ui"
 
// 获取公共配置域名
 
import { Base64 } from 'js-base64'
import * as auth from '@/utils/auth'
 
// 调用后台管理的
const service = axios.create({
    timeout: 600000, // request timeout
})
 
// 返回其他状态码
service.defaults.validateStatus = function (status) {
    return status >= 200 && status <= 500
}
 
// 跨域请求,允许保存cookie
// service.defaults.withCredentials = true
 
// http request拦截
service.interceptors.request.use(
    (config) => {
        // 调用监控平台的
        if (config.requestBaseUrl == "outside") {
            // 内网部署地址
            config.baseURL = 'http://10.141.11.11:18185/api'
            // config.baseURL = "http://192.168.0.129:18185/api"
        } else if (config.requestBaseUrl == "fengt") {
            // feng tu 数据
            config.baseURL = "http://10.141.11.11:18080"
            // config.baseURL = "http://192.168.0.112:18080"
        } else if (config.requestBaseUrl == "fengtDS") {
            config.baseURL = "http://192.168.0.112:9091"
        }
 
        //headers判断是否需要
        // const authorization = config.authorization === false
        // if (!authorization) {
        //     config.headers["Authorization"] = `Basic ${Base64.encode("saber:saber_secret")}`
        // }
        //让每个请求携带token
        const meta = config.meta || {}
        const isToken = meta.isToken === false
        if (auth.getToken() && !isToken) {
            config.headers['Blade-Auth'] = "bearer " + auth.getToken()
        }
        //headers中配置text请求
        if (config.text === true) {
            config.headers["Content-Type"] = "text/plain"
        }
 
        // const token = auth.getToken()
 
        // 添加token
        // token && (config.headers.token = token)
 
        return config
    },
    (error) => {
        return Promise.reject(error)
    }
)
 
// http response 拦截
// service.interceptors.response.use(
//     (res) => {
//         const code = res.data.code
//         if (code == 401 || code == 403 || res.data.data == "token 验证失败!") {
//             // sessionStorage.removeItem('token')
//             router.push("/login")
//         } else {
//             return res
//         }
//     },
//     (error) => {
//         Message({
//             message: error.message,
//             type: "error",
//             duration: 5 * 1000,
//         })
 
//         return Promise.reject(new Error(error))
//     }
// )
 
export default service