request.js 939 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import dev from "@/env/dev.js"
  2. import prod from "@/env/prod.js"
  3. let baseUrl = ''
  4. if (process.env.NODE_ENV === 'development') {
  5. baseUrl = dev.baseUrl
  6. } else {
  7. baseUrl = prod.baseUrl
  8. }
  9. if(baseUrl==""){
  10. baseUrl="http://yigui.liliju.com";
  11. }
  12. export const myRequest = (options) => {
  13. return new Promise((resolve, reject) => {
  14. let obj_url = ''
  15. let obj_method = 'GET'
  16. let obj_data = {}
  17. if (options) {
  18. obj_url = baseUrl + options.url
  19. obj_method = options.method
  20. obj_data = options.data
  21. }
  22. uni.request({
  23. url: obj_url,
  24. method: obj_method,
  25. header: {
  26. 'token':uni.getStorageSync("token"),
  27. },
  28. data: obj_data,
  29. success: (res) => {
  30. if(res.data.msg=="登入已过期"){
  31. uni.redirectTo({
  32. url:"/pages/login/index"
  33. })
  34. }else{
  35. resolve(res)
  36. }
  37. },
  38. fail: (err) => {
  39. uni.showToast({
  40. title: "请求接口失败",
  41. icon: "error"
  42. })
  43. reject(err)
  44. }
  45. })
  46. })
  47. }