广州矛调粤政易端
xusd
7 days ago d27794814b69d18aeb8ee96a46cae91d5613570c
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
105
106
107
108
109
110
/*
 * @Company: hugeInfo
 * @Author: ldh
 * @Date: 2022-08-08 16:44:29
 * @LastEditTime: 2025-04-07 15:35:24
 * @LastEditors: lwh
 * @Version: 1.0.0
 * @Description: axios处理方法
 */
import axios from 'axios';
import { catchApiError, appUrl, getLocal, loginModal, getSessionStorage, confirmModal } from '../utils/utility';
 
// 网络异常
function networkException(err) {
    if (err.code >= 400) {
        catchApiError({ content: '抱歉! 网络异常请刷新' });
    } else {
        catchApiError({ content: '抱歉! 网络错误' });
    }
}
 
// api返回结果处理
function resHandle(resData) {
    const { code, data, msg } = resData;
    if (code === 0 || code === '0') {
        return { type: true, data };
    } else if (code === 401 || code === '401') {
        loginModal();
        return { type: false };
    } else if (code === -1) {
        if (msg) {
            catchApiError({ content: msg });
            return { type: false, content: msg };
        } else {
            return { type: false };
        }
    } else {
        return { type: code, data, msg };
    }
}
 
export const ax = axios;
 
export function request(value = {}) {
    let token = getSessionStorage('customerSystemToken');
 
    // 无token、非登录、非网格单点跳转时
    if (!value.isGrid && !token && value.url !== 'ctAccount/login') {
        catchApiError({
            content: '抱歉!登录状态已失效请重新登录',
            loginStatus: 'lose',
        });
        return { type: false };
    }
    let sessionacc = getLocal('sessionacc') || '';
    let data = value.data;
 
    // ocr接口单独调用其他服务
    const url =
        value.serviceType === 'thrid'
            ? `${appUrl.baseUrl}/${appUrl[value.service] || ''}/api/thrid/grid/${value.url}`
            : `${appUrl.baseUrl}/${appUrl[value.service] || ''}/api/web/${value.url}`;
    const urlAi = `${appUrl.fileUrl}/${appUrl[value.service] || ''}/api/ai/${value.urlAi}`;
 
    if (value.typeAi === 'get') {
        return ax
            .get(urlAi, { params: data, headers: { Authorization: token } })
            .then((response) => {
                return resHandle(response.data);
            })
            .catch((err) => {
                networkException(err);
                return { type: false };
            });
    }
 
    if (value.typeAi === 'post') {
        return ax
            .post(urlAi, data, { headers: { Authorization: token } })
            .then((response) => {
                return resHandle(response.data);
            })
            .catch((err) => {
                networkException(err);
                return { type: false };
            });
    }
 
    if (value.type === 'get') {
        return ax
            .get(url, { params: data, headers: { Authorization: token } })
            .then((response) => {
                return resHandle(response.data);
            })
            .catch((err) => {
                networkException(err);
                return { type: false };
            });
    } else {
        return ax
            .post(url, data, { headers: { Authorization: token } })
            .then((response) => {
                return resHandle(response.data);
            })
            .catch((err) => {
                networkException(err);
                return { type: false };
            });
    }
}