zrlibs
2025-03-19 dad44b84ab21f9d1f860760a045015f1cab0aaec
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
/**
 * 使用es6-fetch发送http请求,不兼容旧版浏览器
 * 替换方案可选axios之类的库
 */
 
import { Message } from 'view-ui-plus'
 
class HttpRequest {
    constructor(apiRoot){
        this.apiRoot = apiRoot
    }
    get(url) {
        return new Promise((resolve, reject) => {
            try {
                const apiUrl = this.getUrl(url)
                fetch(apiUrl).then(res => {
                    if (res.ok)
                        return res.json()
                    else {
                        let err = JSON.stringify({
                            type: res.type,
                            status: res.status,
                            statusText: res.statusText
                        })
                        Message.error({
                            content: `调用${url}接口失败,` + err,
                            duration: 10
                        });
                        reject(err);
                    }
                }).then(res => {
                    if (res.err_code === 0)
                        resolve(res.result);
                    else
                        reject(`code=${res.err_code},` + res.err_msg);
                }).catch(err => {
                    reject(`调用${url}接口失败,` + err);
                });
            }
            catch (err) {
                reject(err);
            }
        });
    }
    post(url, data) {
        return new Promise((resolve, reject) => {
            try {
                const apiUrl = this.getUrl(url)
                fetch(apiUrl, {
                    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
                        })
                        Message.error({
                            content: `调用${url}接口失败,` + err,
                            duration: 10
                        });
                        reject(err);
                    }
                }).then(res => {
                    if (res.err_code === 0)
                        resolve(res.result);
                    else
                        reject(`code=${res.err_code},` + res.err_msg);
                    // resolve(res);
                }).catch(err => {
                    reject(`调用${url}接口失败,` + err)
                })
            }
            catch (err) {
                reject(err);
            }
        })
    }
    getUrl(url) {
        return `${this.apiRoot}/${url}`;
    }
}
 
export default HttpRequest;