Files
test/utils/request.js

52 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const BASE_URL = "https://tea.qingnian8.com/api/bizhi";
// config是形参然后对其进行解构
export function request(config={}){
let {
url,
data={},
method="GET",
header={}
} = config
// 对url进行拼接
url = BASE_URL + url
//下面这种写法是针对header里面的属性进行赋值
header['access-key'] = "888888"
//你要加一个token的话就可以按下面的步骤进行
// header['token'] = '11212212'
return new Promise((resolve,reject)=>{
uni.request({
//url属性接收外面传入的url可以进行简写url:url, ==》 url
url,
data,
method,
header,
success:res =>{
if(res.data.errCode === 0){
//res返回是全部信息但是我们要的是res.data就可以了
// resolve(res)
resolve(res.data)
}else if(res.data.errCode === 400){
uni.showModal({
title:"错误提示",
content:res.data.errMsg,
// 不展示“取消”键
showCancel:false
})
reject(res.data)
}else{
uni.showToast({
title:res.data.errMsg,
icon:"none"
})
reject(res.data)
}
},
fail:err=>{
reject(err)
}
})
})
}