quick/utils/http/index.js
2022-10-26 11:14:13 +08:00

59 lines
1.3 KiB
JavaScript

import {
baseUrl
} from '@/config/index.js';
import store from 'store';
function service(options = {}) {
options.url = `${baseUrl}${options.url}`;
const uid = uni.getStorageSync('uid');
if (options.method === 'POST') {
options.header = {
'content-type': 'application/json',
};
} else {
options.header = {
'content-type': 'multipart/form-data',
};
}
options.data = {
uid: uid || '',
...options.data
};
return new Promise((resolved, rejected) => {
options.success = (res) => {
console.log(res.data);
// 如果请求回来的状态码不是200则执行以下操作
if (res.data.status === 1) {
resolved(res.data.data);
} else if (res.data.errorCode !== 200) {
// 非成功状态码弹窗
uni.showToast({
icon: 'none',
duration: 3000,
title: `${res.data.msg || res.data.errmsg}`
});
// 返回错误信息
rejected(res);
} else {
// 请求回来的状态码为200则返回内容
resolved(res.data.data);
}
};
options.fail = (err) => {
// 请求失败弹窗
uni.showToast({
icon: 'none',
duration: 3000,
title: '服务器错误,请稍后再试'
});
rejected(err);
};
uni.request(options);
});
}
export default service;