ll
liukangdong
2024-10-22 85b460b57cd7e9ad935042df74f40e1bbdfa3408
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
import { baseUrl } from "./config.js"
export const http = function (options) {
    {
        return new Promise((resolve, reject) => {
            // let token = uni.getStorageSync('token') || ''
            // 在登录的时候需要储存 token uni.setStorageSync("authorization","这里是登录获取的token值")
 
            // uni.showLoading({
            //     title:"加载中..."
            // })
            // uni.showLoading({
            //     title: '加载中',
            //     mask: true
            // })
            const param = uni.getStorageSync('param') || {}
            uni.request({
                url: (param.baseUrl || baseUrl) + options.url,
                // url: baseUrl + options.url,
                data: options.data || {},
                method: options.method || 'GET',
                header: options.header || {
                    // 根据实际接口设计 key 取 token 或者 authorization
                    // dm_user_token: token,
                    "content-type": 'application/json;charset=UTF-8'
                },
                success: (res) => {
                    let data = res.data
                    // 控制台显示数据信息
                    // uni.hideLoading()
                    // 登录过期
                    if (data.code === 5112) {
                        setTimeout(() => {
                            uni.showToast({
                                title: data.message,
                                icon: "none",
                                duration: 2000
                            })
                        })
                        uni.navigateTo({
                            url: '/pages/index/login'
                        })
                        return
                    }
                    if (data.code !== 200) {
                        setTimeout(() => {
                            uni.showToast({
                                title: data.message,
                                icon: "none",
                                duration: 2000
                            })
                        })
                        return
                    }
                    resolve(data)
                    // return response.data
                },
                fail: (err) => {
                    // 页面中弹框显示失败
                    uni.showToast({
                        title: '请求接口失败',
                        icon: "none",
                    })
                    console.log('err', err)
                    console.log('msg', baseUrl + options.url)
                    // 返回错误消息
                    reject(err)
                    // uni.hideLoading()
                },
                catch: (e) => {
                    console.log(e)
                }
            })
        })
    }
}