request.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import {
  2. server_host
  3. } from "@/config/system"
  4. import {
  5. getCache
  6. } from "./cache";
  7. const auth_whit_list = [
  8. '/auth/c/userCollect/appCarLogin',
  9. '/auth/c/userCollect/appCarEnroll',
  10. ]
  11. const getData = ({
  12. method,
  13. url,
  14. data,
  15. config
  16. }) => {
  17. return new Promise((resolve, reject) => {
  18. console.log('---', config);
  19. const host = config?.server_host || server_host
  20. console.log(`请求地址 - ${method} - ${host}${url}`);
  21. uni.request({
  22. url: `${host}${url}`,
  23. data,
  24. header: config?.header || {},
  25. method: method,
  26. dataType: config?.dataType || 'json',
  27. success(resp) {
  28. uni.hideLoading()
  29. const code = resp.data.code
  30. if (code == '200') {
  31. console.log('1', resp.data);
  32. resolve(resp.data)
  33. } else if (code == '404') {
  34. reject('找不到服务', code)
  35. } else if (code == '401') {
  36. reject('授权错误', code)
  37. } else {
  38. reject(resp.data.msg, code)
  39. }
  40. },
  41. fail(err) {
  42. uni.hideLoading()
  43. reject('无法连接服务器,请检查您的网络', '-1')
  44. },
  45. })
  46. })
  47. }
  48. const doRequest = ({
  49. method,
  50. url,
  51. data,
  52. config
  53. }) => {
  54. uni.showLoading({
  55. mask: true
  56. })
  57. if (auth_whit_list.includes(url)) {
  58. console.log('白名单 不检查 token');
  59. return getData({
  60. method,
  61. url,
  62. data,
  63. config
  64. }).catch((err, code) => {
  65. console.log('err', err, code);
  66. uni.showToast({
  67. title: err.errMsg || err,
  68. icon: 'error',
  69. })
  70. if (code == '401') {
  71. uni.redirectTo({
  72. url: '/pages/login/login'
  73. })
  74. }
  75. })
  76. }
  77. return getCache('token').then(resp => {
  78. console.log('doRequest - getCache', resp, config);
  79. const token = resp.data
  80. if (config) {
  81. if (config.header) {
  82. config.header.token = token
  83. } else {
  84. config.header = {
  85. token
  86. }
  87. }
  88. } else {
  89. config = {
  90. header: {
  91. token
  92. }
  93. }
  94. }
  95. console.log('122');
  96. return getData({
  97. method,
  98. url,
  99. data,
  100. config
  101. })
  102. }).catch((err, code) => {
  103. console.log('err', err, code);
  104. uni.showToast({
  105. title: err.errMsg || err,
  106. icon: 'error',
  107. })
  108. if (code == '401') {
  109. uni.redirectTo({
  110. url: '/pages/login/login'
  111. })
  112. }
  113. })
  114. }
  115. const get = ({
  116. url,
  117. data,
  118. config
  119. }) => {
  120. return doRequest({
  121. method: 'GET',
  122. url,
  123. data,
  124. config
  125. })
  126. }
  127. const post = ({
  128. url,
  129. data,
  130. config
  131. }) => {
  132. return doRequest({
  133. method: 'POST',
  134. url,
  135. data,
  136. config
  137. })
  138. }
  139. const request = {
  140. get,
  141. post
  142. }
  143. export default request