request.js 3.0 KB

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