upload.js 3.2 KB

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