子钦加油

扩大
缩小

uni-app中封装统一请求函数(转载)

封装统一请求函数有利于项目的维护

整体功能简单实用,但小编遇到一个巨坑,项目中在vue文件使用跳转方法,url参数输入 "/" 后工具提示的路径为 "/pages/login/login",

但是在外部js文件中使用uni跳转的api,快捷提示的路径为 "/pages/login/login.vue" , 这就导致实际使用找不到了,类似的情况要注意一下。

 

参考如下:在common文件夹下面建立一个util.js,内容如下

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
import {getHttpUrl} from "common/server.js"
 
const domain = getHttpUrl() + "/api/instructor.php/"
 
const req = function(a){
    //console.log(a.url);
    a = a || {};
    var b = {
        url:  domain + (a.url || ""),
        method: a.method || "POST",
        dataType: a.dataType || "json",
        header: a.header || {},
        data: a.data || {},
        timeout: a.timeout || 30000,
        success: a.success || undefined,
        fail: a.fail || undefined,
        complete: a.complete || undefined,
        toLogin:a.toLogin || "no"
    };
     
    if(a.loading){
        uni.showLoading({
            title:a.loadingTitle || "加载中",
            mask: a.loadingMask || true
        })
    }
    uni.request({
        url:b.url,
        method:b.method,
        data:b.data,
        header:b.header,
        dataType:b.dataType,
        timeout:b.timeout,
        success:function(res){
            if(res.statusCode != 200){
                uni.showModal({
                    title:"提示",
                    content:"服务器繁忙,请稍后再试",
                    confirmColor:"#009714",
                    showCancel:false
                })
                return;
            }
            //console.log(res);
            if(res.data.code == 0){
                //console.log(res.data);
                if(b.success){
                    b.success(res)
                }
            }else{
                if(res.data.code == "-1" && res.data.msg == "未登录"){
                    if(b.toLogin == "yes"){
                        setTimeout(function(){
                            uni.redirectTo({
                                url:"/pages/login/login"
                            })
                        },1000)
                    }else{
                        try{
                            uni.removeStorageSync("userInfo");
                        }catch(e){
                            //TODO handle the exception
                        }
                        uni.showModal({
                            title:"提示",
                            content:"您未登录,请登录后再试",
                            showCancel:false,
                            confirmText:"去登陆",
                            confirmColor:"#FF0000",
                            success(e) {
                                if(e.confirm){
                                    uni.redirectTo({
                                        url:"/pages/login/login"
                                    })
                                }
                            }
                        })
                    }
                    return;
                }
                var tipMsg = res.data.msg ? res.data.msg : "暂无数据";
                setTimeout(function(){
                    uni.showToast({
                        title:tipMsg,
                        icon:"none",
                        mask:true,
                        duration:1500
                    })
                },200)
            }
        },fail:function(err){
            if(b.fail){
                b.fail(err);
            }else{
                uni.showModal({
                    title:"提示",
                    content:"服务器繁忙,请稍后再试",
                    confirmColor:"#009714",
                    showCancel:false
                })
            }
        },complete:function(com){
            //关闭加载提示
            if(a.loading){
                uni.hideLoading();
            }
             
            if(b.complete){
                b.complete(com);
            }
        }
         
    })
}
 
module.exports = {
    req:req
}

  

使用方法:

1、在要使用的vue页面中引入

2、注册到全局vue方法

 

1
2
3
4
import util from 'common/util.js'
 
//统一接口请求函数
Vue.prototype.req = util.req;

  

posted on   子钦加油  阅读(476)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2020-02-26 django项目中遇到要实现定时任务
2020-02-26 【crontab】“bad minute”及“errors in crontab file, can't install”错误处理
2020-02-26 django 定时任务 django-crontab 的使用

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
返回顶部
点击右上角即可分享
微信分享提示