zhaojs
2023-04-27 582f536845628ffb8132aaed1885ee535fcf08ad
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
import config from "@/config";  // 配置文件
import storage from "./storage"; // 缓存封装
 
export default {
 
    domain(){
        return config.uni_app_web_api_url.replace("api","");
    },
    send(options={}){
        // loading加载
        uni.showLoading({
           title: '加载中'
        });
        
        // 拼接路劲,下面的配置文件会提到
        options.url = config.uni_app_web_api_url + '' + options.url;
        // 请求方式
        options.method = options.method || "GET";
        
        // 这里看项目的情况来定,如果是没有token,那就删除这里,上面的storage也不需要引入
        let users = storage.getJson("users");
        if(users != null){
            options.header = { "Auth-Token" : users.token };
        }
        
        
        // 发起Promise请求
        return new Promise((resolve, reject) =>{
            uni.request(options).then(res=>{
                    uni.hideLoading();
                if(!res.data.success){
                    reject(res.errMsg);
                }else{
                    // 相应拦截、根据后端的状态码来写,可以自行判断和封装
                    if(res.statusCode == '-1001'){
                        uni.hideLoading();
                        uni.navigateTo({
                            url: '/pages/Login/login/login'
                        });
                    }else{
                        resolve(res.data); 
                    }
                }
            });
        });
    },
    get(url="",data={}){
        return this.send({
            url: url,
            data: data
        });
    },
    post(url="",data={}){
        return this.send({
            url: url,
            data: data,
            method: "POST"
        });
    }
};