linwe
2024-08-08 3c738f4fe2762bba8087e5a22fc0dc06560eab0e
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import {
    clientId,
    clientSecret
} from '@/common/setting'
import {
    options
} from '@/http/config.js';
import {
    Base64
} from '@/utils/base64.js';
import Request from '@/utils/luch-request/index.js';
const http = new Request(options);
http.interceptors.request.use((config) => { // 可使用async await 做异步操作
    // 假设有token值需要在头部需要携带
    let accessToken = uni.getStorageSync('accessToken');
    //headers判断是否需要
    if (!config.custom.need) {
        // 客户端认证参数
        config.header['Authorization'] = 'Basic ' + Base64.encode(clientId + ':' + clientSecret);
    }
    if (config.custom.Authorization == true) {
        console.log(config.custom, "==========")
        config.header['Authorization'] =
            "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJBRE1JTiIsIlVzZXJJZCI6IjEiLCJzY29wZSI6WyJhbGwiXSwiVXNlclJlYWxOYW1lIjoi6LaF57qn566h55CG5ZGYIiwiVXNlclh6cWhkbSI6IjQyMDUiLCJleHAiOjI1MTk3NzIwMTAsImp0aSI6ImExNTcxYzk1LTBkNjMtNDkyYi1iOWEyLTE2ZTIzNTQ5ZTY1ZiIsImNsaWVudF9pZCI6InVzZXItc2VydmljZSJ9.ebNVZrw9LbhKaj2w6RR8b2wccQiDkhvBeq79SxxCK-yWiOlIFqBkotTN4TNJg8umcpyYvLILwvqXWRJhffEtgi25sX2y6MqLIWM4kMZ9d8ptdnSmTpBPhltSiQOM0KFa1kl5nSDCBwYOLn-pESJglam76cjpgZNoC88x3iNHacdiXDItY0rtY85HrQ26uyJu9UovKtmYmZRHsIbGMpDta5Q1p4vfaCIr-YUayDrCweZJiQDEEcOSpWJ7O7RMk3pRkX_4UmPHFzrOI2lMp1jQIhxnSTE7EVAz_4z8h8r46muSkpF54Ic4XSawHKqdSLHx8T05LB0MpvOzWMPS6c1uHA"
        config.header["Content-Type"] = "application/json";
    }
    if (accessToken) {
        config.header['Blade-Auth'] = 'bearer ' + accessToken;
        config.header['roleName'] = uni.getStorageSync("activeRole").roleAlias || "";
        let activeRole = uni.getStorageSync("activeRole")
        if (activeRole.roleAlias === 'inhabitant') {
            config.header['communityCode'] = uni.getStorageSync("siteInfo").neiCode || ""
        } else {
            config.header['communityCode'] = uni.getStorageSync("siteInfo").id || ""
        }
    }
    if (config.params.tenantId) {
        config.header['Tenant-Id'] = config.params.tenantId;
    }
    if (config.json === true) {
 
    }
 
    //数据中台请求头
    config.header['apikey'] =
        'F1DBECD719108635189480CF60E6553ADB3109616426BD537F25A430DFC613B491A025C4A51E77FD08C6E5B7CBE05917A461286E7B6D69F1AB1B14F946149D2065B0C675F8FEDF4B9B05C1496881BC5A'
    // #ifndef H5
    // let url = config.url
    // if (process.env.NODE_ENV == 'development' && !url.startsWith("http")) {
    //     url = url.substring(url.indexOf('/api') + 4)
    //     config.url = url
    // }
    // #endif
 
    // 额外参数
    // config.data = config.data || {};
    // config.data.pf = uni.getSystemInfoSync().platform;
    // config.data.sys = uni.getSystemInfoSync().system;
 
    // 演示custom 用处
    // if (config.custom.auth) {
    //   config.header.token = 'token'
    // }
    // if (config.custom.loading) {
    //  uni.showLoading()
    // }
    /**
     /* 演示
     if (!token) { // 如果token不存在,return Promise.reject(config) 会取消本次请求
        return Promise.reject(config)
      }
     **/
    return config
}, config => { // 可使用async await 做异步操作
    return Promise.reject(config)
})
 
// uni.showLoading({
//     title:"加载中..."
// })
 
http.interceptors.response.use((response) => {
 
    // 若有数据返回则通过
    if (response.data.access_token || response.data.key) {
        return response.data
    }
    // 服务端返回的状态码不等于200,则reject()
    if (response.data.code) {
        if (response.data.code !== 200) {
            uni.showToast({
                title: response.data.msg,
                icon: 'none'
            });
            return Promise.reject(response);
        }
    } else {
        if (response.data.error_code !== 200) {
            uni.showToast({
                title: response.data.msg,
                icon: 'none'
            });
            return Promise.reject(response);
        }
    }
 
    //数据中台返回的数据结果
    if (response.statusCode) {
        // uni.hideLoading()
        if (response.statusCode >= 300) {
            console.log(response.data.msg)
            uni.showToast({
                title: '系统繁忙,请稍后再试',
                icon: 'none'
            });
            return Promise.reject(response);
        }
    }
    return response.data;
}, (response) => {
    /*  对响应错误做点什么 (statusCode !== 200)*/
    // uni.showToast({
    //     title: response.data.msg,
    //     icon: 'none'
    // });
    if (response.statusCode == 401) {
        const pages = getCurrentPages()
        const currentPage = pages[pages.length - 1]
        uni.setStorageSync("responIndex", 0)
        uni.clearStorageSync()
        // uni.redirectTo({
        //     url: `/pages/login/login-account?redirect=/${currentPage.route}`
        // })
 
 
        uni.showModal({
            title: "提示",
            content: "请登录",
            // showCancel: false,
            success: res => {
                if (res.confirm) {
                    uni.redirectTo({
                        url: `/pages/login/login-account?redirect=/${currentPage.route}`
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消');
                }
            }
        })
    } else {
        uni.showToast({
            title: '系统繁忙,请稍后再试',
            icon: 'none'
        });
    }
    return Promise.reject(response)
})
export default http;