request.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import config from "@/config";
  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: config.baseUrl + 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. if (res.data.code != 200) uni.showToast({
  18. title: res.data.message,
  19. icon: 'none'
  20. })
  21. } else {
  22. // 可以根据项目要求修改错误处理
  23. reject(res.data);
  24. uni.hideLoading();
  25. }
  26. },
  27. fail: (err) => {
  28. // 请求失败处理
  29. uni.showToast({
  30. title: '出错了,请联系管理员~',
  31. icon: 'none'
  32. })
  33. reject(err);
  34. uni.hideLoading();
  35. },
  36. });
  37. });
  38. };