request.js 779 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const request = (url, data, method = 'GET') => {
  2. return new Promise((resolve, reject) => {
  3. let header = {
  4. 'Content-Type': 'application/json',
  5. }
  6. if (uni.getStorageSync('token')) {
  7. header.token = 'Bearer ' + uni.getStorageSync('token')
  8. }
  9. uni.request({
  10. url: url,
  11. data: data,
  12. method: method,
  13. header: header,
  14. success: res => {
  15. if (res.statusCode == 200) {
  16. let data = res.data
  17. if (data.code == 40001) {
  18. uni.navigateTo({
  19. url: '/pages/index/login'
  20. });
  21. return false
  22. }
  23. if (data.code == 0 || data.code == 200) {
  24. resolve(res.data)
  25. } else {
  26. reject(res)
  27. }
  28. } else {
  29. reject(res)
  30. }
  31. },
  32. fail: res => {
  33. reject(res)
  34. }
  35. })
  36. });
  37. }
  38. module.exports = {
  39. request: request
  40. }