request.js 857 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. export default function request(config = {}) {
  2. let {
  3. url,
  4. data = {},
  5. method = "GET",
  6. header = {}
  7. } = config
  8. // 拼接url TODO 这里要修改,改成不同环境取不同的url
  9. url = process.env.BASE_API_URL + url;
  10. // 添加token
  11. header['Authorization'] = uni.getStorageSync('token');
  12. return new Promise((resolve, reject) => {
  13. uni.request({
  14. url,
  15. data,
  16. method,
  17. header,
  18. withCredentials: true,
  19. success: res => {
  20. if (res.data.code === '0') {
  21. resolve(res.data.data)
  22. } else if (res.data.errCode === '999') {
  23. uni.showModal({
  24. title: "错误提示",
  25. content: res.data.msg,
  26. showCancel: false
  27. })
  28. reject(res.data)
  29. } else {
  30. uni.showToast({
  31. title: res.data.msg,
  32. icon: "none"
  33. })
  34. reject(res.data)
  35. }
  36. },
  37. fail: err => {
  38. reject(err)
  39. }
  40. })
  41. })
  42. }