request.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import store from '@/store'
  2. import config from '@/config'
  3. import { getToken } from '@/utils/auth'
  4. import errorCode from '@/utils/errorCode'
  5. import { toast, showConfirm, tansParams } from '@/utils/common'
  6. import { encryptBase64, encryptWithAes, generateAesKey, decryptWithAes, decryptBase64 } from '@/utils/crypto'
  7. import { encrypt, decrypt } from '@/utils/jsEncrypt'
  8. let timeout = 10000
  9. const baseUrl = config.baseUrl
  10. const request = config => {
  11. // 是否需要设置 token
  12. const isToken = (config.headers || {}).isToken === false
  13. // 是否需要加密
  14. const isEncrypt = (config.headers || {}).isEncrypt === true
  15. // 是否需要ClientId
  16. const isClientId = (config.headers || {}).isClientId === true
  17. config.header = config.header || {}
  18. config.header['Content-Type'] = 'application/json;charset=utf-8'
  19. if (getToken() && !isToken) {
  20. config.header['Authorization'] = 'Bearer ' + getToken()
  21. }
  22. if (isClientId) {
  23. config.header['clientId'] = 'e5cd7e4891bf95d1d19206ce24a7b32e'
  24. }
  25. // 当开启参数加密
  26. if (isEncrypt && (config.method === 'post' || config.method === 'put')) {
  27. // 生成一个 AES 密钥
  28. const aesKey = generateAesKey()
  29. config.header['Encrypt-Key'] = encrypt(encryptBase64(aesKey))
  30. config.data = typeof config.data === 'object' ?
  31. encryptWithAes(JSON.stringify(config.data), aesKey) : encryptWithAes(config.data, aesKey)
  32. }
  33. // get请求映射params参数
  34. if (config.params) {
  35. let url = config.url + '?' + tansParams(config.params)
  36. url = url.slice(0, -1)
  37. config.url = url
  38. }
  39. return new Promise((resolve, reject) => {
  40. uni.request({
  41. method: config.method || 'get',
  42. timeout: config.timeout || timeout,
  43. url: config.baseUrl || baseUrl + config.url,
  44. data: config.data,
  45. header: config.header,
  46. dataType: 'json'
  47. }).then(response => {
  48. let [error, res] = response
  49. if (error) {
  50. toast('后端接口连接异常')
  51. reject('后端接口连接异常')
  52. return
  53. }
  54. const code = res.data.code || 200
  55. const msg = errorCode[code] || res.data.msg || errorCode['default']
  56. if (code === 401) {
  57. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
  58. if (res.confirm) {
  59. store.dispatch('LogOut').then(res => {
  60. uni.reLaunch({ url: '/pages/login' })
  61. })
  62. }
  63. })
  64. reject('无效的会话,或者会话已过期,请重新登录。')
  65. } else if (code === 500) {
  66. toast(msg)
  67. reject('500')
  68. } else if (code !== 200) {
  69. toast(msg)
  70. reject(code)
  71. }
  72. resolve(res.data)
  73. })
  74. .catch(error => {
  75. let { message } = error
  76. if (message === 'Network Error') {
  77. message = '后端接口连接异常'
  78. } else if (message.includes('timeout')) {
  79. message = '系统接口请求超时'
  80. } else if (message.includes('Request failed with status code')) {
  81. message = '系统接口' + message.substr(message.length - 3) + '异常'
  82. }
  83. toast(message)
  84. reject(error)
  85. })
  86. })
  87. }
  88. export default request