jesting0
2022-04-06 4a0fb138f5670809507c6483074622630e59eef3
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
import ViewUI from 'view-design';
 
class HttpRequest {
    constructor(baseUrl) {
        this.baseUrl = baseUrl;
    }
    get(url) {
        return new Promise((resolve, reject) => {
            try {
                fetch(this.baseUrl + url).then(res => {
                    if (res.ok)
                        return res.json()
                    else {
                        let err = JSON.stringify({
                            type: res.type,
                            status: res.status,
                            statusText: res.statusText
                        })
                        ViewUI.Message.error({
                            content: `调用${url}接口失败,` + err,
                            duration: 10
                        });
                        reject(err);
                    }
                }).then(res => {
                    if (res.success)
                        resolve(res.data, res);
                    else {
                        reject(`调用${url}接口失败,error_code=${res.error_code},` + res.msg);
                        if (res.error_code != '0404') {
                            ViewUI.Notice.error({
                                desc: `调用${url}接口失败,error_code=${res.error_code},` + res.msg
                            });
                        }
                    }
                }).catch(err => {
                    reject(`调用${url}接口失败,` + err);
                    ViewUI.Notice.error({
                        desc: `调用${url}接口失败,` + err
                    });
                });
            }
            catch (err) {
                reject(err);
            }
        });
    }
    post(url) {
        return new Promise((resolve, reject) => {
            try {
                fetch(this.baseUrl + url, {
                    method: 'POST'
                }).then(res => {
                    if (res.ok)
                        return res.json()
                    else {
                        let err = JSON.stringify({
                            type: res.type,
                            status: res.status,
                            statusText: res.statusText
                        })
                        ViewUI.Message.error({
                            content: `调用${url}接口失败,` + err,
                            duration: 10
                        });
                        reject(err);
                    }
                }).then(res => {
                    if (res.success)
                        resolve(res.data, res);
                    else {
                        reject(`调用${url}接口失败,error_code=${res.error_code},` + res.msg);
                        if (res.error_code != '0404') {
                            ViewUI.Notice.error({
                                desc: `调用${url}接口失败,error_code=${res.error_code},` + res.msg
                            });
                        }
                    }
                    // resolve(res);
                }).catch(err => {
                    reject(`调用${url}接口失败,` + err)
                })
            }
            catch (err) {
                reject(err);
            }
        })
    }
    form(url, data) {
        return new Promise((resolve, reject) => {
            try {
                fetch(this.baseUrl + url, {
                    method: 'POST',
                    body: JSON.stringify(data),
                    headers: {
                        'Content-Type': 'application/json',
                    },
                }).then(res => {
                    if (res.ok)
                        return res.json()
                    else {
                        let err = JSON.stringify({
                            type: res.type,
                            status: res.status,
                            statusText: res.statusText
                        })
                        ViewUI.Message.error({
                            content: `调用${url}接口失败,` + err,
                            duration: 10
                        });
                        // reject(err);
                        return res.json();
                    }
                }).then(res => {
                    if (res.success)
                        resolve(res.data, res);
                    else {
                        if (!res.error_code) res.error_code = '';
                        if (!res.msg) res.msg = JSON.stringify(res);
                        reject(`调用${url}接口失败,error_code=${res.error_code},` + res.msg);
                        if (res.error_code != '0404') {
                            ViewUI.Notice.error({
                                desc: `调用${url}接口失败,error_code=${res.error_code},` + res.msg,
                                duration: 20
                            });
                        }
                    }
                    // resolve(res);
                }).catch(err => {
                    ViewUI.Message.error({
                        content: `调用${url}接口失败,` + err,
                        duration: 10
                    });
                    reject(`调用${url}接口失败,` + err);
                })
            }
            catch (err) {
                reject(err);
            }
        })
    }
}
 
export default HttpRequest;