request.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import {
  2. getToken
  3. } from '@/utils/auth'
  4. import {
  5. store
  6. } from '@/store/index.js'
  7. // import baseURL from '@/api/base/path.js'
  8. function behindRequist({
  9. url,
  10. data,
  11. method = "GET"
  12. }) {
  13. let token = 'Bearer ' + getToken();
  14. let tenantId = store.tenantId?store.tenantId:0
  15. let header = {
  16. Authorization: token,
  17. tenantId:tenantId
  18. }
  19. return new Promise((resolve, reject) => {
  20. const urlList = uni.getStorageSync('baseUrl')
  21. uni.request({
  22. url: JSON.parse(urlList).baseUrl + url,
  23. data,
  24. method,
  25. header,
  26. timeout: 600000,
  27. sslVerify: false,
  28. success: (res) => {
  29. if (res.statusCode === 200) {
  30. //请求成功
  31. resolve(res.data);
  32. } else if (res.statusCode === 401) {
  33. uni.showToast({
  34. icon: 'none',
  35. title: "未登录或登录状态已超时",
  36. duration: 1500
  37. });
  38. } else if (res.statusCode === 405) {
  39. uni.showToast({
  40. icon: 'none',
  41. title: "请求方法错误",
  42. duration: 1500
  43. });
  44. } else {
  45. uni.showToast({
  46. icon: 'none',
  47. title: "请求错误:" + res.statusCode,
  48. duration: 1500
  49. });
  50. }
  51. },
  52. fail: (err) => {
  53. uni.showToast({
  54. icon: 'none',
  55. title: err.errMsg,
  56. duration: 1500
  57. });
  58. reject(err);
  59. }
  60. })
  61. })
  62. }
  63. // 拦截请求的函数
  64. function request(options) {
  65. return preRequest().then(() => {
  66. // 前置请求成功,继续执行原请求
  67. return behindRequist(options);
  68. }).catch((error) => {
  69. // 前置请求失败,处理错误
  70. uni.showToast({
  71. icon: 'none',
  72. title: error.message || '前置请求失败',
  73. duration: 1500
  74. });
  75. return Promise.reject(error);
  76. });
  77. }
  78. // 前置请求函数
  79. function preRequest() {
  80. let token = 'Bearer ' + getToken();
  81. let header = {
  82. Authorization: token
  83. };
  84. return new Promise((resolve, reject) => {
  85. const urlList = uni.getStorageSync('baseUrl')
  86. console.log(urlList)
  87. uni.request({
  88. url: JSON.parse(urlList).baseUrl + '/business/notification/getHasNotification', // 前置请求的URL
  89. header,
  90. method: 'GET',
  91. success: (res) => {
  92. console.log(res)
  93. if (res.data.code == 200 && !store.isNotification) {
  94. uni.navigateTo({
  95. url: "/pages/notification/index",
  96. success: (resqust) => {
  97. // 通过eventChannel向被打开页面传送数据
  98. resqust.eventChannel.emit("notification", {
  99. data: res.data.data[0]
  100. })
  101. }
  102. })
  103. } else {
  104. // 前置请求成功,继续执行原请求
  105. resolve(true);
  106. }
  107. },
  108. fail: (err) => {
  109. // 前置请求失败,不执行原请求
  110. reject(err);
  111. }
  112. });
  113. });
  114. }
  115. export default {
  116. request
  117. }