zhangzengfei
2021-09-16 38430ddb8612fce15a2f1c940f9bd57d4da3e70b
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
export default class Api {
    syncRequest = function(option){
        //正式
        const BASEURL = 'http://111.205.161.131:5116';
        //测试
        //const BASEURL = 'http://192.168.20.10:7006';
        //处理请求url
 
        if(option.url.indexOf('weather')<0 && option.url.indexOf('smartai')<0){
            option.url = BASEURL+option.url;
        }
        option.method = option.method || "GET"
        option.header = option.header || {}
        option.data = option.data || {}
        option.loading = option.loading ? true : false
        option.loadtext = option.loadtext || '加载中...'
        if(option.method == 'GET'){
            option.header['Content-Type'] = 'application/json;charset=UTF-8'
        }else{
            option.header['Content-Type'] = 'application/x-www-form-urlencoded'
            option.header['Content-Type'] = 'application/json'
        }
        if(option.loading){
            uni.showLoading({
                mask: true,
                title: option.loadtext
            });
        }
        return new Promise((resolve,reject)=>{
            uni.request({
                url: option.url,
                method: option.method,
                header: option.header,
                data: option.data,
                success: (res)=>{
                    if(option.loading){
                        uni.hideLoading()
                    }
                    console.log(res)
                    if(res.statusCode==200){
                        resolve(res)
                    }else{
                        uni.showToast({
                            icon: "none",
                            title: "数据获取失败,请稍后重试"
                        });    
                    }
                },
                fail: (err)=>{
        
                    console.log(err)
                    if (option.loading) {
                        uni.hideLoading()
                    }
                    uni.showToast({
                        icon: "none",
                        title: "数据获取失败,请稍后重试"
                    })
                    reject(err)
                }
            })
        })
    }
}