request.js 909 B

1234567891011121314151617181920212223242526272829303132
  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. if (res.statusCode === 200) {
  16. resolve(res.data);
  17. } else {
  18. // 可以根据项目要求修改错误处理
  19. reject(res.data);
  20. }
  21. },
  22. fail: (err) => {
  23. // 请求失败处理
  24. uni.showToast({
  25. title: '出错了,请联系管理员~',
  26. icon: 'none'
  27. })
  28. reject(err);
  29. },
  30. });
  31. });
  32. };