uni-app 封装请求

1、npm install uni-request --save

2、uniRequest.defaults.baseURL = '请求路径基地址';

3、uniRequest.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';

4、

// 添加请求拦截器

uniRequest.interceptors.request.use(function(config) {

// 在发送请求之前做些什么

if (uni.getStorageSync('atoken')) {

config.headers['Authorization'] = uni.getStorageSync('atoken')

}

return config;

}, function(error) {

// 对请求错误做些什么

return Promise.reject(error);

});

5、

// 添加响应拦截器

uniRequest.interceptors.response.use(function(response) {

// 对响应数据做点什么

if (response.data.code === 200) {

 

} else if (response.data.code === 500) {

uni.showToast({

    title: response.data.message,

    duration: 2000,

icon:"none"

});

return;

} else if (response.data.code === 404) {

uni.showToast({

    title: '参数检验失败',

    duration: 2000,

icon:"none"

});

return;

} else if (response.data.code === 401) {

uni.showToast({

    title: '暂未绑定手机号',

    duration: 2000,

icon:"none"

});

return;

}

return response;

}, function(error) {

// 对响应错误做点什么

return Promise.reject(error);

});

 

请求的api

import uniRequest from 'uni-request';

//get请求

export function content(id, type) {

return uniRequest({

url: '  接口   ' + id + '/' + type,

method: 'get'

})

}

//post 请求

export function getSettlementConfirmOrder(data) {

return uniRequest({

url: '  接口  ',

method: 'post',

data: data

})

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值