图书馆小程序
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.

45 lines
1.0 KiB

2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
  1. import store from '../store';
  2. import config from '@/utils/config.js'
  3. const BASE_URL = config.baseUrl
  4. function request({ url, data, method = 'GET' }) {
  5. return new Promise((resolve, reject) => {
  6. // 自动带上 token
  7. const header = {};
  8. if (store.state.user.token) {
  9. header.Authorization = store.state.user.token;
  10. }
  11. uni.request({
  12. url: BASE_URL + url,
  13. data,
  14. method: method.toUpperCase(),
  15. header,
  16. success: ({ data }) => {
  17. if (data.code === 200) {
  18. // 直接返回后端的 data 结构
  19. resolve(data);
  20. } else {
  21. uni.showToast({
  22. title: data.message || '请求失败',
  23. icon: 'none',
  24. mask: true,
  25. duration: 3000
  26. });
  27. reject(data.message);
  28. }
  29. },
  30. fail: (error) => {
  31. uni.showToast({
  32. title: '网络异常',
  33. icon: 'none'
  34. });
  35. reject(error);
  36. },
  37. complete: () => {
  38. uni.hideLoading();
  39. }
  40. });
  41. });
  42. }
  43. export default request;