request.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. if (cacheManager.get('auth')) {
  53. cacheManager.clearAll()
  54. }
  55. uni.reLaunch({
  56. url: '/pages/Login/index'
  57. })
  58. }
  59. })
  60. reject('无效的会话,或者会话已过期,请重新登录。')
  61. } else if (code === 405) {
  62. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
  63. if (res.confirm) {
  64. if (cacheManager.get('auth')) {
  65. cacheManager.clearAll()
  66. }
  67. uni.reLaunch({
  68. url: '/pages/Login/index'
  69. })
  70. }
  71. })
  72. reject('无效的会话,或者会话已过期,请重新登录。')
  73. } else if (code === 500) {
  74. toast("数据异常-500")
  75. reject('500')
  76. } else if (code === 400) {
  77. toast("数据异常-400")
  78. reject('400')
  79. } else if (code !== 0) {
  80. toast(msg)
  81. reject(code)
  82. }
  83. resolve(res)
  84. })
  85. .catch(error => {
  86. if (config.isLoading) {
  87. uni.hideLoading()
  88. }
  89. let {
  90. message
  91. } = error
  92. if (message === 'Network Error') {
  93. message = '后端接口连接异常'
  94. } else if (message.includes('timeout')) {
  95. message = '系统接口请求超时'
  96. } else if (message.includes('Request failed with status code')) {
  97. message = '系统接口' + message.substr(message.length - 3) + '异常'
  98. }
  99. toast(message)
  100. reject(error)
  101. })
  102. })
  103. }
  104. export default request