wzp
2022-02-21 2667d6931e0f85f3dda26238330d9c45385025cf
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
/**
 * @version 3.0.5
 * @Author lu-ch
 * @Email webwork.s@qq.com
 * 文档: https://www.quanzhan.co/luch-request/
 * github: https://github.com/lei-mu/luch-request
 * DCloud: http://ext.dcloud.net.cn/plugin?id=392
 * HBuilderX: beat-3.0.4 alpha-3.0.4
 */
 
 
import Request from '@/js_sdk/luch-request/luch-request/index.js'
export const apiBaseUrl = 'http://asset.mengdong.icu'
 
const http = new Request()
 
const getTokenStorage = () => {
    let Authorization = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NDU0MzgyMzEsInVzZXJuYW1lIjoiMTM2MTE0NTIxNzUifQ.4lP5CTT8Y_lX_Ez4qQamtWGXGUjW37n3h9brW-GW2cI'
    // let Authorization="";
    // try {
    //     Authorization = uni.getStorageSync('token')
    // } catch (e) {}
    return Authorization;
}
 
 
http.setConfig((config) => {
    /* 设置全局配置 */
    config.baseURL = apiBaseUrl /* 根域名不同 */
    config.header = {
        ...config.header,
        // a: 1, // 演示
        // b: 2 // 演示
    }
    return config
})
 
 
http.interceptors.request.use((config) => {
    /* 请求之前拦截器。可以使用async await 做异步操作 */
    config.header = {
        ...config.header,
        Authorization: getTokenStorage()
    }
    /*
 if (!token) { // 如果token不存在,return Promise.reject(config) 会取消本次请求
   return Promise.reject(config)
 }
 */
    return config
}, (config) => {
    return Promise.reject(config)
})
 
 
http.interceptors.response.use(async (response) => {
    /* 请求之后拦截器。可以使用async await 做异步操作  */
    // if (response.data.code !== 200) { // 服务端返回的状态码不等于200,则reject()
    //   return Promise.reject(response)
    // }
    return response
}, (response) => { // 请求错误做点什么。可以使用async await 做异步操作
    console.log(response.data)
    return Promise.reject(response)
})
 
export {
    http
}