request.js 2.6 KB

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