request.js 980 B

1234567891011121314151617181920212223242526272829303132333435
  1. const BASE_URL = 'https://www.waywish.com';
  2. export function request(options) {
  3. return new Promise((resolve, reject) => {
  4. let header = options.header || {
  5. 'Content-Type': 'application/json'
  6. }
  7. if (uni.getStorageSync('token')) header['token'] = uni.getStorageSync('token');
  8. uni.request({
  9. url: BASE_URL + options.url, // 服务器url
  10. method: options.method || 'GET', // 请求方法,默认为GET
  11. data: options.data || {}, // 请求参数
  12. header: header, // 设置请求的 header
  13. success: (res) => {
  14. // 请求成功
  15. console.log(res);
  16. if (res.statusCode === 200) {
  17. resolve(res.data);
  18. } else {
  19. // 可以根据项目要求修改错误处理
  20. reject(res.data);
  21. uni.hideLoading();
  22. }
  23. },
  24. fail: (err) => {
  25. // 请求失败处理
  26. uni.showToast({
  27. title: '出错了,请联系管理员~',
  28. icon: 'none'
  29. })
  30. reject(err);
  31. uni.hideLoading();
  32. },
  33. });
  34. });
  35. };