马宇豪
2024-07-24 33413c6be7f338ba1ee92b3218e5c078364675dc
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
import VUE_APP_BASE_URL from './constant.js';
 
let server_url = VUE_APP_BASE_URL;
let token = uni.getStorageSync('tk');
export function service(options = {}) {
    options.url = `${server_url}${options.url}`;
    //配置请求头
    if(uni.getStorageSync('tk')){
        options.header = {
            'Content-type': options.header || 'application/json',
            'Authorization':uni.getStorageSync('tk'),
            // 'uid': uni.getStorageSync('uid')
        };
    }
    else {
        options.header = {
            'Content-type': options.header || 'application/json',
            'Authorization': ''
        };
    }
    return new Promise((resolved, rejected) => {
        //成功
        options.success = (res) => {
            // if (Number(res.data.code) == 100) {
            if(Number(res.data.code) == 403 || Number(res.data.code) == 401){
                uni.showToast({
                    icon: 'none',
                    duration: 2000,
                    position: 'top',
                    title: `${res.data.message}`
                });
                rejected(res.data.msg);
                setTimeout(()=>{
                    uni.clearStorageSync();
                    uni.clearStorage();
                    uni.navigateTo({
                        url: '/pages/index/index'
                    })
                },2000)
            }else if(Number(res.data.code) == 200){
                
                if(res.data.msg == '您点击太快了,请稍后尝试'){
                    uni.showToast({
                        icon: 'none',
                        duration: 2000,
                        position: 'top',
                        title: `${res.data.message}`
                    });
                }
            }
                resolved(res.data);
            // } else {
            //     uni.showToast({
            //         icon: 'none',
            //         duration: 3000,
            //         position: 'top',
            //         title: `${res.data.msg}`
            //     });
            //     rejected(res.data.msg);
            //     setTimeout(()=>{
            //         uni.navigateTo({
            //             url: '/pages/index/index'
            //         })
            //     },2000)
            // }
        }
        options.fail = (err) => {
            uni.showToast({
                icon: "none",
                title: '服务响应失败'
            });
            uni.clearStorageSync();
            uni.clearStorage();
            uni.navigateTo({
                url: '/pages/index/index'
            })
            rejected(err); 
        }
        uni.request(options);
 
    });
}