You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.0 KiB
46 lines
1.0 KiB
import store from '../store';
|
|
import config from '@/utils/config.js'
|
|
const BASE_URL = config.baseUrl
|
|
|
|
function request({ url, data, method = 'GET' }) {
|
|
return new Promise((resolve, reject) => {
|
|
// 自动带上 token
|
|
const header = {};
|
|
if (store.state.user.token) {
|
|
header.Authorization = store.state.user.token;
|
|
}
|
|
|
|
uni.request({
|
|
url: BASE_URL + url,
|
|
data,
|
|
method: method.toUpperCase(),
|
|
header,
|
|
success: ({ data }) => {
|
|
if (data.code === 200) {
|
|
// 直接返回后端的 data 结构
|
|
resolve(data);
|
|
} else {
|
|
uni.showToast({
|
|
title: data.message || '请求失败',
|
|
icon: 'none',
|
|
mask: true,
|
|
duration: 3000
|
|
});
|
|
reject(data.message);
|
|
}
|
|
},
|
|
fail: (error) => {
|
|
uni.showToast({
|
|
title: '网络异常',
|
|
icon: 'none'
|
|
});
|
|
reject(error);
|
|
},
|
|
complete: () => {
|
|
uni.hideLoading();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
export default request;
|