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
145
146
147
148
149
150
151
152
153
154
155
// http客户端组件
import { httpClass } from './libvbar-m-dxhttp.so'
 
const httpObj = new httpClass();
 
const http = {
 
    HTTP_METHOD : {
        GET: "GET",
        POST: "POST",
        PUT: "PUT",
        DELETE: "DELETE",
        HEAD: "HEAD",
        OPTIONS: "OPTIONS",
        PATCH: "PATCH"
    },
    HTTP_FORMAT : {
        JSON: "JSON",
        FORM: "FORM",
        URLENCODE: "URLENCODE"
    },
    HTTP_FORM_TYPE : {
        STRING: "STRING",
        FILE: "FILE"
    },
    /**
     * get请求
     * @param {string} url 
     * @param {array} headers 非必填,会有默认填充 request headers
     * @param {number} timeout 非必填, 超时时间
     * @returns 
     */
    get: function (url, headers, timeout) {
        if (!url) {
            throw new Error("url should not be null or empty")
        }
        return httpObj.request({ method: 0, url: url, headers: headers, timeout: timeout})
    },
    /**
     * post请求,data为json/form表单数组格式
     * @param {string} url 
     * @param {array} data
     * @param {array} headers 非必填,会有默认填充 request headers 
     * @param {number} timeout 非必填, 超时时间
     * @returns 
     */
    post: function (url, data, headers, timeout, format = "JSON") {
        if (!url) {
            throw new Error("url should not be null or empty")
        }
        if (!data) {
            throw new Error("data should not be null or empty")
        }
        if (typeof data != 'string' && format != "FORM") {
            data = JSON.stringify(data)
        }
        if(format == "JSON"){
            return httpObj.request({ method: 1, url: url, data: data, headers: headers, timeout: timeout})
        }else{
            return httpObj.request({ method: 1, url: url, formData: data, headers: headers, timeout: timeout})
        }
    },
    /**
     * put请求,data为json/form表单数组格式
     * @param {string} url 
     * @param {array} data
     * @param {array} headers 非必填,会有默认填充 request headers 
     * @param {number} timeout 非必填, 超时时间
     * @returns 
     */
    put: function (url, data, headers, timeout, format = "JSON") {
        if (!url) {
            throw new Error("url should not be null or empty")
        }
        if (!data) {
            throw new Error("data should not be null or empty")
        }
        if (typeof data != 'string' && format != "FORM") {
            data = JSON.stringify(data)
        }
        if(format == "JSON"){
            return httpObj.request({ method: 2, url: url, data: data, headers: headers, timeout: timeout})
        }else{
            return httpObj.request({ method: 2, url: url, formData: data, headers: headers, timeout: timeout})
        }
    },
    /**
     * delete请求
     * @param {string} url 
     * @param {array} headers 非必填,会有默认填充 request headers
     * @param {number} timeout 非必填, 超时时间
     * @returns 
     */
    delete: function (url, headers, timeout) {
        if (!url) {
            throw new Error("url should not be null or empty")
        }
        return httpObj.request({ method: 3, url: url, headers: headers, timeout: timeout})
    },
    /**
     * 下载文件,本质是get请求
     * @param {string} url 
     * @param {string} path 目标路径(绝对路径)
     * @param {array} headers 非必填,会有默认填充 request headers
     * @param {number} timeout 非必填, 超时时间
     * @returns 下载文件有可能返回null,但是下载是成功的
     */
    download: function (url, path, headers, timeout) {
        if (!url) {
            throw new Error("url should not be null or empty")
        }
        if (!path) {
            throw new Error("path should not be null or empty")
        }
        return httpObj.request({ method: 0, url: url, headers: headers, download: path, timeout: timeout })
    },
 
    /**
     * 上传文件,本质是post请求
     * @param {string} url 
     * @param {string} path 源路径(绝对路径)
     * @returns 
     */
    upload: function (url, path) {
        if (!url) {
            throw new Error("url should not be null or empty")
        }
        if (!path) {
            throw new Error("path should not be null or empty")
        }
        return httpObj.request({
            method: 1,
            url: url,
            headers: ["application/x-www-form-urlencoded; charset=UTF-8"],
            upload: path
        })
    },
    /**
     * 原生方式
     * 必填参数:method(0:get请求,1:post请求)、url
     * 可选参数:headers(字符串数组,覆盖默认header)、download(文件下载地址)、data(请求报文,post请求必填)、timeout(超时时间/ms,缺省:5000)、dns(缺省:"114.114.114.114,8.8.8.8")、upload
     * 默认header:Accept-Charset:utf-8、Content-Type:application/json;charset=utf-8、Connection:close
     * @param {object} param json
     *  如:let param={
                method:0,
                url:"http://192.168.10.122:8000/DW200_1_0.zip",
                download:"/testNet/aaa"
            }
     * @returns 
     */
    request: function (param) {
        return httpObj.request(param)
    }
}
export default http;