智慧保安APP验收版本
zengh
2021-12-06 886a9f2deb311453f12cb0302bcb882a9d331d7a
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
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');
    if (accessToken) {
        config.header['Blade-Auth'] = 'bearer ' + accessToken;
    }
    // 客户端认证参数
    config.header['Authorization'] = 'Basic ' + Base64.encode(clientId + ':' + clientSecret);
 
    // 额外参数
    // 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)
})
http.interceptors.response.use((response) => {
    // 若有数据返回则通过
    if (response.data.access_token || response.data.key) {
        return response.data
    }
    // 服务端返回的状态码不等于200,则reject()
    if (response.data.code !== 200) {
        return Promise.reject(response);
    }
    return response.data;
}, (response) => {
    /*  对响应错误做点什么 (statusCode !== 200)*/
    uni.showToast({
        title: response.data.msg,
        icon: 'none'
    });
    return Promise.reject(response)
})
export default http;