request.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // 配置请求根路径
  2. const BASE_URL = process.env.UNI_BASE_URL;
  3. const request = (url, data, method = 'GET') => {
  4. return new Promise((resolve, reject) => {
  5. let header = {
  6. "Content-Type": "application/json"
  7. }
  8. const res = uni.getStorageSync('storage_key');
  9. const token = res.token
  10. if (token) {
  11. header['Authorization'] = token
  12. }
  13. uni.request({
  14. url: url,
  15. data: data,
  16. method: method,
  17. header: header,
  18. success: res => {
  19. if (res.statusCode == 200) {
  20. if (res.data.code === "200" || res.data.code === "") {
  21. resolve(res.data)
  22. } else if (res.data.code === "20004" || res.data.code === "20005") {
  23. uni.removeStorageSync("storage_key")
  24. uni.navigateTo({
  25. url: '/pages/login/index'
  26. })
  27. } else {
  28. uni.showToast({
  29. title: res.data.message,
  30. icon: "none"
  31. })
  32. }
  33. } else {
  34. reject(res)
  35. }
  36. },
  37. fail: res => {
  38. reject(res)
  39. },
  40. })
  41. });
  42. }
  43. //不带token接口请求,首页
  44. const request1 = (url, data, method = 'GET') => {
  45. return new Promise((resolve, reject) => {
  46. let header = {
  47. 'Content-Type': 'application/json',
  48. 'tenant': 'MDAwMA==',
  49. }
  50. uni.request({
  51. url: url,
  52. data: data,
  53. method: method,
  54. header: header,
  55. success: res => {
  56. if (res.data.code === "200" || res.data.code == "" || data.code === "") {
  57. resolve(res.data)
  58. } else {
  59. reject(res)
  60. }
  61. },
  62. fail: res => {
  63. reject(res)
  64. }
  65. })
  66. });
  67. }
  68. export {
  69. request,
  70. request1,
  71. BASE_URL
  72. }