1.封装request
//服务端路径
let serverPath = "http://172.30.2.113:3000";
const $request = (url, data, method) => {
return new Promise((resolve, reject) => {
uni.showLoading({
title: '加载中',
icon: 'loading',
mask: true
});
uni.request({
url: serverPath + url,
method: method,
data: data,
header: {
"authorization": uni.getStorageSync('token')
},
success(res) {
resolve(res)
uni.hideLoading(); //关闭loading
},
fail(error) {
reject(error);
// throw new Error(error);
uni.hideLoading(); //关闭loading
},
complete() {
uni.hideLoading(); //关闭loading
}
})
})
}
//get请求
const get = (url, data) => {
return $request(url, data, 'GET')
}
//post请求
const post = (url, data) => {
return $request(url, data, 'POST')
}
export default {
post,
get
}
2二次封装
//引入封装好的request
import request from '@/untils/request.js';
function search_(data){
return request.get("接口路径",data)
}
export {
search_ as search,
}