request.js 977 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 = '/api' + url;
  10. url = 'http://localhost:8080' + url;
  11. // 添加token
  12. header['Authorization'] =
  13. 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjIsImV4cCI6MTczNTI2MDg5M30.wfoJ22OryiQtQRwuhZNKKEvYjS-ux-bvWyluw8w6LnE';
  14. return new Promise((resolve, reject) => {
  15. uni.request({
  16. url,
  17. data,
  18. method,
  19. header,
  20. withCredentials: true,
  21. success: res => {
  22. if (res.data.code === '0') {
  23. resolve(res.data.data)
  24. } else if (res.data.errCode === '999') {
  25. uni.showModal({
  26. title: "错误提示",
  27. content: res.data.msg,
  28. showCancel: false
  29. })
  30. reject(res.data)
  31. } else {
  32. uni.showToast({
  33. title: res.data.msg,
  34. icon: "none"
  35. })
  36. reject(res.data)
  37. }
  38. },
  39. fail: err => {
  40. reject(err)
  41. }
  42. })
  43. })
  44. }