request.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. // debugger
  29. uni.request({
  30. method: config.method || 'get',
  31. timeout: config.timeout || timeout,
  32. url: config.baseUrl || baseUrl + config.url,
  33. data: config.data,
  34. header: config.headers,
  35. dataType: 'json'
  36. }).then(response => {
  37. const res = response.data;
  38. // if (error) {
  39. // toast('后端接口连接异常')
  40. // reject('后端接口连接异常')
  41. // return
  42. // }
  43. const code = res.code
  44. const msg = res.msg
  45. // const msg = errorCode[code] || res.data.msg || errorCode['default']
  46. if (code === 401) {
  47. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
  48. if (res.confirm) {
  49. if(cacheManager.get('auth')){
  50. cacheManager.clearAll()
  51. }
  52. uni.reLaunch({
  53. url: '/pages/login/index'
  54. })
  55. }
  56. })
  57. reject('无效的会话,或者会话已过期,请重新登录。')
  58. } else if (code === 405) {
  59. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
  60. if (res.confirm) {
  61. if(cacheManager.get('auth')){
  62. cacheManager.clearAll()
  63. }
  64. uni.reLaunch({
  65. url: '/pages/login/index'
  66. })
  67. }
  68. })
  69. reject('无效的会话,或者会话已过期,请重新登录。')
  70. } else if (code === 500) {
  71. toast("数据异常-500")
  72. reject('500')
  73. }else if (code === 400) {
  74. toast("数据异常-400")
  75. reject('400')
  76. } else if (code !== 0) {
  77. toast(msg)
  78. reject(code)
  79. }
  80. resolve(res)
  81. })
  82. .catch(error => {
  83. let {
  84. message
  85. } = error
  86. if (message === 'Network Error') {
  87. message = '后端接口连接异常'
  88. } else if (message.includes('timeout')) {
  89. message = '系统接口请求超时'
  90. } else if (message.includes('Request failed with status code')) {
  91. message = '系统接口' + message.substr(message.length - 3) + '异常'
  92. }
  93. toast(message)
  94. reject(error)
  95. })
  96. })
  97. }
  98. export default request