uView框架2.0拦截器相关参数设置

uView2.0官网

1、拦截器 request.js

// 根域名
var baseUrl = 'https://xxx.com;

module.exports = (vm) => {
    // 初始化请求配置:config 为默认全局配置
    uni.$u.http.setConfig((config) => {
        config.baseURL = baseUrl; /* 根域名 */
        // 配置请求头信息
        config.header = {
            'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
            // 'content-type': 'application/json',
        };
        config.method = 'GET'; //支持post、get、put、delete
        config.dataType = 'json'; // 设置为json,返回后会对数据进行一次JSON.parse()
        config.timeout = 60000;
        return config
    })

    // 请求拦截
    uni.$u.http.interceptors.request.use((config) => { // 可使用async await 做异步操作
        // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
        config.data = config.data || {};

        // 设置token
        const token = uni.getStorageSync('token') || '';
        config.header['token'] = token;
        
        // 设置设备端类型
        // config.header['os'] = uni.$u.os() || '';

        return config;
    }, config => { // 可使用async await 做异步操作
        return Promise.reject(config)
    })

    // 响应拦截
    uni.$u.http.interceptors.response.use((response) => {
        /* 对响应成功做点什么 可使用async await 做异步操作*/
        const res = response.data;
        // console.log(res)

        // res为服务端返回值,可能有code,result等字段
        if (res.code == 0) {
            return res;
        } else if (res.code == 400) {
            uni.$u.toast(res.msg)
            return false;
        } else if (res.code == 401) {
            // 401为token失效
            uni.setStorageSync('token', ''); // 缓存token
            uni.$u.toast(res.msg)
            setTimeout(() => {
                vm.$u.route('/pages/login/login')
            }, 1500)
            return false;
        } else if (res.code == 405) {
            // 405为token为空
            return false;
        } else if (res.code == 1) {
            uni.$u.toast(res.msg)
            return false;
        } else {
            // 如果需要catch返回,则进行reject
            return Promise.reject(res)
        }
    }, (response) => {
        // 对响应错误做点什么 (statusCode !== 200)
        return Promise.reject(response)
    })
}

2、请求示例

// get请求
uni.$u.http.get('/api/xxx', {
	params: {}
}).then(res => {
	console.log("==get请求结果==");
	console.log(res);
}).catch(err => {
	console.log(err);
})

// post请求
uni.$u.http.post('/api/xxx', {
	
}).then(res => {
	console.log("==post请求结果==");
	console.log(res);
}).catch(err => {
	console.log(err);
})

// 临时调整post请求头格式
uni.$u.http.post('/api/xx', {
	id: '',
}, {
	header: {
		'content-type': 'application/json',
	}
}).then(res => {
	console.log("==自定义表单结果==");
	console.log(res);
}).catch(err => {
	console.log(err);
})

// upload上传请求
uni.$u.http.upload('/api/plus.Upload/imgUpload', {
	filePath: filePath,
	name: 'file',
    formData: {},
}).then(res => {
	console.log("==upload上传请求结果==");
	console.log(res);			
}).catch(err => {
	console.log(err);
})

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

 康 

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值