| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- export default function request(config = {}) {
- let {
- url,
- data = {},
- method = "GET",
- header = {},
- loading = false,
- loadingText = "请求中..."
- } = config
- // 拼接url TODO 这里要修改,改成不同环境取不同的url
- url = process.env.BASE_API_URL + url;
- // 添加token
- header['Authorization'] = uni.getStorageSync('token');
- // 是否显示loading
- if (loading) {
- uni.showLoading({
- title: loadingText,
- mask: true
- });
- }
- return new Promise((resolve, reject) => {
- uni.request({
- url,
- data,
- method,
- header,
- withCredentials: true,
- success: res => {
- if (res.data.code === '0') {
- resolve(res.data.data)
- } else if (res.data.errCode === '999') {
- uni.showModal({
- title: "错误提示",
- content: res.data.msg,
- showCancel: false
- })
- reject(res.data)
- } else {
- uni.showToast({
- title: res.data.msg,
- icon: "none"
- })
- reject(res.data)
- }
- },
- fail: err => {
- reject(err)
- },
- complete: () => {
- if (loading) {
- uni.hideLoading();
- }
- }
- })
- })
- }
|