upload.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // import store from '@/store'
  2. import config from '@/config'
  3. import { getSession } from '@/utils/auth'
  4. import errorCode from '@/utils/errorCode'
  5. import { toast, showConfirm, tansParams } from '@/utils/common'
  6. let timeout = config.timeout
  7. const baseUrl = config.baseUrl
  8. const upload = config => {
  9. const isSession = config.isSession || false
  10. config.header = config.header || {}
  11. if (isSession) {
  12. if (getSession()) {
  13. config.header['cookie'] = 'JSESSIONID=' + getSession()
  14. } else {
  15. showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?')
  16. .then(() => {
  17. uni.reLaunch({ url: '/pages/login' })
  18. })
  19. .catch(() => {
  20. return
  21. })
  22. }
  23. }
  24. // get请求映射params参数
  25. if (config.params) {
  26. let url = config.url + '?' + tansParams(config.params)
  27. url = url.slice(0, -1)
  28. config.url = url
  29. }
  30. console.log('upload.config', config);
  31. return new Promise((resolve, reject) => {
  32. uni.uploadFile({
  33. timeout: config.timeout || timeout,
  34. url: baseUrl + config.url,
  35. // files: config.files,
  36. filePath: config.filePath,
  37. name: config.name || 'file',
  38. header: config.header,
  39. formData: config.formData,
  40. success: (response) => {
  41. console.log('upload.response', response);
  42. const { statusCode, data } = response
  43. try {
  44. const result = JSON.parse(data)
  45. } catch (e) {
  46. toast('上传失败, 请重试')
  47. reject(-20201)
  48. return
  49. }
  50. const result = JSON.parse(data)
  51. const returnCode = result.returnCode
  52. const code = statusCode || 200
  53. const msg = errorCode[code] || result.returnMsg || errorCode['default']
  54. if (returnCode == -20201) {
  55. toast(msg)
  56. reject(returnCode)
  57. return
  58. }
  59. if (code === 200) {
  60. resolve(result)
  61. // } else if (code == 401) {
  62. // showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(response => {
  63. // if (response.confirm) {
  64. // store.dispatch('LogOut').then(response => {
  65. // uni.reLaunch({ url: '/pages/login' })
  66. // })
  67. // }
  68. // })
  69. // reject('无效的会话,或者会话已过期,请重新登录。')
  70. } else if (code === 500) {
  71. toast(msg)
  72. reject('500')
  73. } else if (code !== 200) {
  74. toast(msg)
  75. reject(code)
  76. }
  77. },
  78. fail: (error) => {
  79. let { errMsg } = error
  80. if (errMsg == 'Network Error') {
  81. errMsg = '后端接口连接异常'
  82. } else if (errMsg.includes('timeout')) {
  83. errMsg = '系统接口请求超时'
  84. } else if (errMsg.includes('Request failed with status code')) {
  85. errMsg = '系统接口' + errMsg.substr(errMsg.length - 3) + '异常'
  86. } else if (errMsg.includes('request:fail')) {
  87. errMsg = '网络请求失败'
  88. }
  89. toast(errMsg)
  90. reject(error)
  91. }
  92. })
  93. })
  94. }
  95. export default upload