request.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import store from '@/store'
  2. import config from '@/config'
  3. import {
  4. AESDecrypt,
  5. AESEncypt,
  6. RSAEncrypt,
  7. RSADecrypt,
  8. createKey
  9. } from '@/utils/dataParams.js'
  10. import {
  11. getToken
  12. } from '@/utils/auth'
  13. import errorCode from '@/utils/errorCode'
  14. import {
  15. toast,
  16. showConfirm,
  17. tansParams
  18. } from '@/utils/common'
  19. let timeout = 10000
  20. const serverAddress = uni.getStorageSync('serverAddress')
  21. const baseUrlIp = serverAddress ? serverAddress : config.baseUrl
  22. const baseUrl = baseUrlIp + '/projects/outApi'
  23. const request = config => {
  24. // console.log(config.data || config.params)
  25. // 是否需要设置 token
  26. const isToken = (config.headers || {}).isToken === false
  27. config.header = config.header || {}
  28. if (getToken() && !isToken) {
  29. config.header['token'] = getToken()
  30. }
  31. // get请求映射params参数
  32. let urlOrginal = "" + config.url;
  33. if (config.params) {
  34. let url = config.url + '?' + tansParams(config.params)
  35. url = url.slice(0, -1)
  36. config.url = url
  37. }
  38. // console.log(config.url)
  39. // console.log(config.params || config.data)
  40. let pwdConfig = {}
  41. // 如果是对象形式传递的params
  42. if (config.formParams) {
  43. const encryptKey = createKey()
  44. pwdConfig.encryptKey = encryptKey
  45. for (var i in config.formParams) {
  46. config.formParams[i] = AESEncypt(config.formParams[i], encryptKey).data
  47. }
  48. config.header['GoWork'] = RSAEncrypt(encryptKey)
  49. config.data = config.formParams
  50. }
  51. if (config.data) {
  52. pwdConfig = AESEncypt(JSON.stringify(config.data))
  53. config.header['GoWork'] = RSAEncrypt(pwdConfig.encryptKey)
  54. config.data = pwdConfig.data;
  55. }
  56. if (!pwdConfig.encryptKey) {
  57. const encryptKey = createKey()
  58. pwdConfig.encryptKey = encryptKey
  59. config.header['GoWork'] = RSAEncrypt(encryptKey)
  60. }
  61. // console.log("params", config.params)
  62. // console.log("data", config.data)
  63. return new Promise((resolve, reject) => {
  64. uni.request({
  65. method: config.method || 'get',
  66. timeout: config.timeout || timeout,
  67. url: baseUrl + config.url,
  68. data: config.data,
  69. header: config.header,
  70. dataType: 'json',
  71. params: config.params
  72. }).then(response => {
  73. /* let [error, res] = response
  74. if (error) {
  75. toast('后端接口连接异常')
  76. reject('后端接口连接异常')
  77. return
  78. } */
  79. const res = response
  80. const code = res.data.code || 200
  81. const msg = errorCode[code] || res.data.msg || errorCode['default']
  82. if (code === 401) {
  83. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(confirmRes => {
  84. if (confirmRes.confirm) {
  85. store.dispatch('LogOut').then(storeRes => {
  86. uni.reLaunch({
  87. url: '/pages/login/index'
  88. })
  89. })
  90. }
  91. })
  92. reject('无效的会话,或者会话已过期,请重新登录。')
  93. } else if (code === 500) {
  94. toast(msg)
  95. reject('500')
  96. } else if (code !== 200) {
  97. toast(msg)
  98. reject(code)
  99. }
  100. if (res.data.encrypt) {
  101. if (pwdConfig.encryptKey == null) {
  102. pwdConfig.encryptKey = RSADecrypt(res.data.GoWork)
  103. }
  104. res.data.data = JSON.parse(AESDecrypt(res.data.data, pwdConfig.encryptKey))
  105. }
  106. console.log(urlOrginal)
  107. console.log(res.data)
  108. console.log("***************************************")
  109. resolve(res.data)
  110. })
  111. .catch(error => {
  112. let {
  113. message
  114. } = error
  115. if (message === 'Network Error') {
  116. message = '后端接口连接异常'
  117. } else if (message.includes('timeout')) {
  118. message = '系统接口请求超时'
  119. } else if (message.includes('Request failed with status code')) {
  120. message = '系统接口' + message.substr(message.length - 3) + '异常'
  121. }
  122. toast(message)
  123. reject(error)
  124. })
  125. })
  126. }
  127. export default request