请求说明
// /根目录/api/index.js 基于第三方库uni-network封装(简介上面有文档地址)
# 发送get请求
uni.$http.get('/api/home/getBanner').then(res => {
}).catch(err => {
})
# 发送post请求
uni.$http.post('/api/home/getBanner', {
}).then(res => {
}).catch(err => {
})
# 图片上传
uni.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: res => {
uni.$http.upload({
url: '/api/file.File/add',
formData: {
type: 'upl',
file_type: 'image'
},
file: res.tempFiles[0]
}).then(res => {
if (res.code === 200) {
// 处理业务逻辑
}
})
}
})
自定义请求
如果您有自定义请求头或者其他方便的需求,可以直接修改/api/index.js
,如下
// /api/index.js
instance.interceptors.request.use(
function (config) {
const userStore = useUserStore()
// 传递语言code给后端
if (config.url.indexOf('?') !== -1) {
config.url = `${config.url}&lang=${userStore.languageCode}`
} else {
config.url = `${config.url}?lang=${userStore.languageCode}`
}
if (config.data && config.data.withoutToken) {
return config
}
config.headers.apitoken = uni.getStorageSync('token')
config.headers.key = `adsadasd`
return config
}
)