request.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //import store from '@/store'
  2. import config from '../config.js'
  3. import cacheManager from '@/utils/cacheManager.js'
  4. import {
  5. Md5
  6. } from 'ts-md5/dist/md5';
  7. import errorCode from '@/utils/errorCode'
  8. import {
  9. toast,
  10. showConfirm,
  11. tansParams
  12. } from '@/utils/common'
  13. let timeout = 10000
  14. const baseUrl = config.baseUrl
  15. const request = config => {
  16. // 是否需要设置 token
  17. // const isToken = (config.headers || {}).isToken === false
  18. config.header = config.header || {}
  19. // if (getToken() && !isToken) {
  20. // config.headers['X-AUTH-SIGN'] = Md5.hashStr(JSON.stringify(config.data) +auth.secret);
  21. // config.headers['X-AUTH-TOKEN'] = auth.token;
  22. // }
  23. if (cacheManager.get('auth')) {
  24. config.headers['X-AUTH-SIGN'] = Md5.hashStr(JSON.stringify(config.data) + cacheManager.get('auth').secret);
  25. config.headers['X-AUTH-TOKEN'] = cacheManager.get('auth').token;
  26. }
  27. return new Promise((resolve, reject) => {
  28. if (config.isLoading) {
  29. uni.showLoading({
  30. title: '加载中',
  31. mask: true
  32. });
  33. }
  34. uni.request({
  35. method: config.method || 'get',
  36. timeout: config.timeout || timeout,
  37. url: config.baseUrl || baseUrl + config.url,
  38. data: config.data,
  39. header: config.headers,
  40. dataType: 'json'
  41. }).then(response => {
  42. if (config.isLoading) {
  43. uni.hideLoading()
  44. }
  45. const res = response.data;
  46. const code = res.code
  47. const msg = res.msg
  48. // const msg = errorCode[code] || res.data.msg || errorCode['default']
  49. if (code === 401) {
  50. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
  51. if (res.confirm) {
  52. const auth = cacheManager.get('auth')
  53. if (auth) {
  54. cacheManager.clearAll()
  55. }
  56. if (auth.type == 2 || auth.type == 5) {
  57. uni.reLaunch({
  58. url: '/pages/Login/index'
  59. })
  60. } else {
  61. uni.reLaunch({
  62. url: '/pages/Login/clientIndex'
  63. })
  64. }
  65. }
  66. })
  67. reject('无效的会话,或者会话已过期,请重新登录。')
  68. } else if (code === 405) {
  69. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
  70. if (res.confirm) {
  71. const auth = cacheManager.get('auth')
  72. if (auth) {
  73. cacheManager.clearAll()
  74. }
  75. if (auth.type == 2 || auth.type == 5) {
  76. uni.reLaunch({
  77. url: '/pages/Login/index'
  78. })
  79. } else {
  80. uni.reLaunch({
  81. url: '/pages/Login/clientIndex'
  82. })
  83. }
  84. }
  85. })
  86. reject('无效的会话,或者会话已过期,请重新登录。')
  87. } else if (code === 500) {
  88. toast("数据异常-500")
  89. reject('500')
  90. } else if (code === 400) {
  91. toast("数据异常-400")
  92. reject('400')
  93. } else if (code !== 0) {
  94. toast(msg)
  95. reject(code)
  96. }
  97. resolve(res)
  98. })
  99. .catch(error => {
  100. if (config.isLoading) {
  101. uni.hideLoading()
  102. }
  103. let {
  104. message
  105. } = error
  106. if (message === 'Network Error') {
  107. message = '后端接口连接异常'
  108. } else if (message.includes('timeout')) {
  109. message = '系统接口请求超时'
  110. } else if (message.includes('Request failed with status code')) {
  111. message = '系统接口' + message.substr(message.length - 3) + '异常'
  112. }
  113. toast(message)
  114. reject(error)
  115. })
  116. })
  117. }
  118. export default request