3***@qq.com
3***@qq.com
  • 发布:2018-09-20 15:01
  • 更新:2020-10-28 14:05
  • 阅读:17499

uni-app没有请求拦截器

分类:uni-app

uni.request(OBJECT)的请求拦截器怎么写呢?

2018-09-20 15:01 负责人:无 分享
已邀请:
1***@163.com

1***@163.com

兼容app吗

Neil_HL

Neil_HL

拦截器只能自己封装,uni-app仅提供网络请求的方法和中断连接的方法;
我们内置了flyio(https://wendux.github.io/dist/#/doc/flyio/readme),使用时用wx,如下示例:

var Fly=require("flyio/dist/npm/wx");  
var fly=new Fly;  
var server = "https://uniapp.dcloud.io/update";  
var req = {"appid":"123","version":"123"};  
fly.request(server,req,{  
    method:"get",  
    timeout:5000 //超时设置为5s  
})  
.then(d=>{ console.log("request result:",d)})  
.catch((e) => console.log("error", e));  
  • liuxmoo

    其他 api 的封装呢?

    2019-04-05 21:01

7***@qq.com

7***@qq.com

flyio 自带请求拦截设置,你可以看看它的文档

互帮互助

互帮互助

这么搞吧!已经在项目中使用了
// #ifdef APP-PLUS
import Fly from 'flyio/dist/npm/wx'
// #endif
// #ifdef MP-WEIXIN
import Fly from 'flyio/dist/npm/wx'
// #endif
const request = new Fly()

const errorPrompt = (err) => {
// #ifdef MP-WEIXIN
wx.showToast({
title: err.message || 'fetch data error.',
icon: 'none'
})
// #endif
// #ifdef APP-PLUS
uni.showToast({
title: err.message || 'fetch data error.',
icon: 'none'
})
// #endif
}

request.interceptors.request.use((request) => {
// wx.showNavigationBarLoading()
uni.showLoading({
title: '加载中'
});
return request
})

request.interceptors.response.use((response, promise) => {
// wx.hideNavigationBarLoading()
uni.hideLoading();
// if (!(response && response.data && response.data.res === 0)) {
// errorPrompt(response)
// }
return promise.resolve(response.data)
}, (err, promise) => {
// wx.hideNavigationBarLoading()
uni.hideLoading();

errorPrompt(err)  
return promise.reject(err)  

})

export default request


原文:https://blog.csdn.net/haoyuexihuai/article/details/86590587

liuxmoo

liuxmoo

uni-app 有对 其他的 api 进行 promise 封装么

hhyang

hhyang - 如有问题,请添加QQ1606726660 备注付费咨询

樱桃也疯狂

樱桃也疯狂 - 我不是搬码工,我不会武功

https://blog.csdn.net/qq_42618566/article/details/109308690

// 全局请求封装  
const token = uni.getStorageSync('token');  
//import service from "./service.js";  // 请求字典  

export default (url, method, params) => {  
    //拦截区  
    return new Promise((resolve, reject) => {  
        wx.request({  
            url: "https://www.piop.cn/api" + url,  
            method: method,  
            header: {  
                token: token  
            },  
            data: {  
                //serviceId: api[0].serviceId,  
                ...params  
            },  
            success(res) {  
                resolve(res.data);  
                               //响应区  
            },  
            fail(err) {  
                reject(err);  
                               //响应区  
            },  
            complete() {  
                uni.hideLoading();  
                                //响应区  
            }  
        });  
    });  
};
w***@foxmail.com

w***@foxmail.com - 全栈工程师

axios 都不支持?

  • Neil_HL

    不支持

    2018-12-06 19:51

4***@qq.com

4***@qq.com

兼容性呢?

该问题目前已经被锁定, 无法添加新回复