request.js 2.2 KB

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