yangan
13 小时以前 a28d0135ee42809b2c5863609da37155d3ecba5b
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
import { fetch,fetchId } from '@/api/request.js'
 
const typeObj = {
    headerGET: { 'Content-type': 'application/x-www-from-urlencoded' },
    headerPOST: { 'Content-type': 'application/x-www-from-urlencoded' },
    jsonPOST: { 'Content-type': 'application/json' },
    utfPOSt: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8' }
}
 
// 通用接口请求
export const reqAll = (url, params, opt = {}) => {
    opt.data = params
    opt.header = typeObj['headerPOST']
    return fetch(url, opt)
}
export const reqAllJson = (url, params, opt = {}) => {
    opt.data = params
    opt.header = typeObj['jsonPOST']
    return fetch(url, opt)
}
// deltete请求
export const reqDelete = (url, params, opt = {}) => {
    opt.header = typeObj['headerGET']
    opt.method = 'DELETE'
    opt.data = params
    return fetch(url, opt)
}
// get请求
// urlParam是拼接路径的参数
export const reqGet = (url, params, opt = {}) => {
    opt.header = typeObj['headerGET']
    opt.method = 'GET'
    opt.data = params
    return fetch(url, opt)
}
export const reqGetId = (url, params, opt = {}) => {
    opt.header = typeObj['headerGET']
    opt.method = 'GET'
    opt.data = params
    return fetchId(url, opt)
}
 
// urlParam是拼接路径的参数
export const reqGet2 = (url, params, opt = {}) => {
    // opt.header = typeObj['headerGET']
    // opt.method = 'GET'
    // let newUrl = `${url}/${params}`
    // return fetch(newUrl, opt)
}
 
// post请求
// urlType是指定拼接路径还是传json格式参数
export const reqPost = (url, params, urlType, opt = {}) => {
    opt.method = 'POST'
    if (urlType == 'params') {
        Object.keys(params).map(item => {
            // 这里面不能用params.item,点运算符加标识符的形式取值,因为item是Object.keys生成的每一项键都是字符串,不是标识符,可以用[]的形式访问
            if (params[item] == null) {
                params[item] = ''
            }
        })
        opt.params = params
        opt.header = typeObj['headerPOST']
    } else if (urlType == 'json') {
        opt.header = typeObj['jsonPOST']
        opt.data = params
    } else if (urlType == 'utf8') {
        opt.header = typeObj['utfPOSt']
        opt.data = params
    } else {
        opt.data = params
        opt.header = typeObj['headerPOST']
    }
    return fetch(url, opt, urlType)
}
 
export const reqPut = (url, params, urlType, opt = {}) => {
    opt.method = 'Put'
    if (urlType == 'params') {
        Object.keys(params).map(item => {
            // 这里面不能用params.item,点运算符加标识符的形式取值,因为item是Object.keys生成的每一项键都是字符串,不是标识符,可以用[]的形式访问
            if (params[item] == null) {
                params[item] = ''
            }
        })
        opt.params = params
        opt.header = typeObj['headerPut']
    } else if (urlType == 'json') {
        opt.header = typeObj['jsonPut']
        opt.data = params
    } else if (urlType == 'utf8') {
        opt.header = typeObj['utfPOSt']
        opt.data = params
    } else {
        opt.data = params
        opt.header = typeObj['headerPut']
    }
    return fetch(url, opt, urlType)
}