upload.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // import store from '@/store'
  2. import config from '@/config'
  3. import {
  4. getSession,getLoginInfo
  5. } from '@/utils/auth'
  6. import errorCode from '@/utils/errorCode'
  7. import {
  8. toast,
  9. showConfirm,
  10. tansParams
  11. } from '@/utils/common'
  12. import $tab from '@/plugins/tab.js'
  13. let timeout = config.timeout
  14. const baseUrl = config.baseUrl
  15. const upload = config => {
  16. const isSession = config.isSession || false
  17. config.header = config.header || {}
  18. if (isSession) {
  19. if (getSession()) {
  20. config.header['cookie'] = 'JSESSIONID=' + getSession()
  21. } else {
  22. uni.showModal({
  23. title: '系统提示',
  24. content: '登录状态已过期,您可以继续留在该页面,或者重新登录?',
  25. cancelText: '取消',
  26. confirmText: '确定',
  27. success: function(res) {
  28. console.log('showModal success');
  29. if (res.confirm) {
  30. const loginInfo = getLoginInfo()
  31. //如果有登录信息,就自动登录刷session
  32. if (Object.keys(loginInfo).length !== 0) {
  33. uni.reLaunch({
  34. url: '/pages/login?type=autoLogin'
  35. })
  36. } else {
  37. uni.reLaunch({
  38. url: '/pages/login'
  39. })
  40. }
  41. return
  42. }
  43. return
  44. }
  45. })
  46. }
  47. }
  48. // get请求映射params参数
  49. if (config.params) {
  50. let url = config.url + '?' + tansParams(config.params)
  51. url = url.slice(0, -1)
  52. config.url = url
  53. }
  54. console.log('upload.config', config);
  55. return new Promise((resolve, reject) => {
  56. uni.uploadFile({
  57. timeout: config.timeout || timeout,
  58. url: baseUrl + config.url,
  59. // files: config.files,
  60. filePath: config.filePath,
  61. name: config.name || 'file',
  62. header: config.header,
  63. formData: config.formData,
  64. success: (response) => {
  65. console.log('upload.response', response);
  66. const {
  67. statusCode,
  68. data
  69. } = response
  70. try {
  71. const result = JSON.parse(data)
  72. } catch (e) {
  73. toast('上传失败, 请重试')
  74. reject(-20201)
  75. return
  76. }
  77. const result = JSON.parse(data)
  78. const returnCode = result.returnCode
  79. const code = statusCode || 200
  80. const msg = errorCode[code] || result.returnMsg || errorCode['default']
  81. if (returnCode == -20201) {
  82. toast(msg)
  83. reject(returnCode)
  84. return
  85. }
  86. if (code === 200) {
  87. resolve(result)
  88. // } else if (code == 401) {
  89. // showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(response => {
  90. // if (response.confirm) {
  91. // store.dispatch('LogOut').then(response => {
  92. // uni.reLaunch({ url: '/pages/login' })
  93. // })
  94. // }
  95. // })
  96. // reject('无效的会话,或者会话已过期,请重新登录。')
  97. } else if (code === 500) {
  98. toast(msg)
  99. reject('500')
  100. } else if (code !== 200) {
  101. toast(msg)
  102. reject(code)
  103. }
  104. },
  105. fail: (error) => {
  106. let {
  107. errMsg
  108. } = error
  109. if (errMsg == 'Network Error') {
  110. errMsg = '后端接口连接异常'
  111. } else if (errMsg.includes('timeout')) {
  112. errMsg = '系统接口请求超时'
  113. } else if (errMsg.includes('Request failed with status code')) {
  114. errMsg = '系统接口' + errMsg.substr(errMsg.length - 3) + '异常'
  115. } else if (errMsg.includes('request:fail')) {
  116. errMsg = '网络请求失败'
  117. }
  118. toast(errMsg)
  119. reject(error)
  120. }
  121. })
  122. })
  123. }
  124. export default upload