const base_url = 'http://localhost:8890'
const timeout = 5000
export default (params) => {
let url = params.url;
let method = params.method || "get";
let data = params.data || {};
let header = {
'Blade-Auth': uni.getStorageSync('token') || '',
'Content-Type': 'application/json;charset=UTF-8',
'Authorization': 'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
'Tenant-Id': uni.getStorageSync('tenantId') || 'xxx',
...params.header
}
if (method == "post") {
header = {
'Content-Type': 'application/json'
};
}
return new Promise((resolve, reject) => {
uni.request({
url: base_url + url,
method: method,
header: header,
data: data,
timeout ,
success(response) {
const res = response
if (res.statusCode == 200) {
resolve(res.data);
} else {
uni.clearStorageSync()
switch (res.statusCode) {
case 401:
uni.showModal({
title: "提示",
content: "请登录",
showCancel: false,
success() {
setTimeout(() => {
uni.navigateTo({
url: "/pages/login/index",
})
}, 1000);
},
});
break;
case 404:
uni.showToast({
title: '请求地址不存在...',
duration: 2000,
})
break;
default:
uni.showToast({
title: '请重试...',
duration: 2000,
})
break;
}
}
},
fail(err) {
console.log(err)
if (err.errMsg.indexOf('request:fail') !== -1) {
uni.showToast({
title: '网络异常',
icon: "error",
duration: 2000
})
} else {
uni.showToast({
title: '未知异常',
duration: 2000
})
}
reject(err);
},
complete() {
uni.hideLoading();
uni.hideToast();
}
});
}).catch(() => {});
};
import request from './index.js'
export const pageStudyInfo = (params) => {
return request({
url: '/study/studyInfo/page',
method: 'get',
data: params,
header: {}
})
}
export const studyInfoById = (id) => {
return request({
url: `/study/studyInfo/${id}`,
method: 'get',
})
}
import { pageStudyInfo } from '@/request/api.js'
pageStudyInfo (data).then((res) => {
console.log('成功', res);
}).catch((err) => {
console.error('失败', err);
});
export function vrequest(url, method ="GET", data ={},headerSoft={}) {
console.log('req___',headerSoft);
return new Promise((resolve, reject) => {
uni.request({
url: url,
method: method,
data: data,
header: headerSoft,
success: (res) => {
resolve(res.data);
},
fail: (err) => {
reject(err);
}
});
});
}