request.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. export const BASE_URL = 'https://a.bianyifang.com';
  2. function request({ loading = true, url, method = 'GET', data = {}, header = {} }) {
  3. return new Promise((resolve, reject) => {
  4. if (loading) {
  5. wx.showLoading({
  6. title: '加载中...',
  7. mask: true
  8. });
  9. }
  10. wx.request({
  11. url: BASE_URL + url,
  12. method,
  13. data,
  14. header: {
  15. 'content-type': 'application/json',
  16. 'Authorization': wx.getStorageSync('token') || '',
  17. ...header
  18. },
  19. timeout: 10000,
  20. success(res) {
  21. if (loading) wx.hideLoading();
  22. if (res.statusCode === 200) {
  23. resolve(res.data);
  24. } else {
  25. wx.showToast({
  26. title: res.data?.message || '请求错误',
  27. icon: 'none',
  28. duration: 2000
  29. });
  30. reject(res.data);
  31. }
  32. },
  33. fail(err) {
  34. if (loading) wx.hideLoading();
  35. wx.showToast({
  36. title: '网络异常,请重试',
  37. icon: 'none',
  38. duration: 2000
  39. });
  40. reject(err);
  41. }
  42. });
  43. });
  44. }
  45. export default request;