request.js 3.0 KB

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