teamBackgroundFile.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { BaseApi } from './baseApi.js'
  2. import api from './index.js'
  3. function parseResult(response) {
  4. let result = response && response.data
  5. if (typeof result === 'string') {
  6. try {
  7. result = JSON.parse(result)
  8. } catch (error) {
  9. throw new Error('上传失败,请重试')
  10. }
  11. }
  12. if (!response || response.statusCode !== 200 || !result || result.code !== 0) {
  13. throw new Error(result && result.msg ? result.msg : '上传失败,请重试')
  14. }
  15. return result.data
  16. }
  17. function unwrap(response) {
  18. const result = response && response.data
  19. if (!result || result.code !== 0) {
  20. throw new Error(result && result.msg ? result.msg : '请求失败,请重试')
  21. }
  22. return result.data
  23. }
  24. function getHeader(headers, name) {
  25. if (!headers || typeof headers !== 'object') return ''
  26. const expectedName = name.toLowerCase()
  27. for (const key of Object.keys(headers)) {
  28. if (key.toLowerCase() === expectedName) return String(headers[key] || '')
  29. }
  30. return ''
  31. }
  32. function isDocumentDownload(headers) {
  33. const contentType = getHeader(headers, 'content-type').split(';')[0].trim().toLowerCase()
  34. const disposition = getHeader(headers, 'content-disposition').trim()
  35. if (!contentType || !/^attachment(?:\s*;|$)/i.test(disposition)) return false
  36. if (contentType === 'application/json' || contentType === 'text/json'
  37. || contentType.endsWith('+json') || contentType === 'text/html'
  38. || contentType === 'application/xhtml+xml') return false
  39. return true
  40. }
  41. export function uploadTeamBackgroundFile(teamId, filePath, onProgress = () => {}) {
  42. return new Promise((resolve, reject) => {
  43. const task = uni.uploadFile({
  44. url: `${BaseApi}/core/user/team/${teamId}/background-files`,
  45. filePath,
  46. name: 'file',
  47. header: { token: uni.getStorageSync('token') },
  48. success: response => {
  49. try {
  50. resolve(parseResult(response))
  51. } catch (error) {
  52. reject(error)
  53. }
  54. },
  55. fail: reject
  56. })
  57. task.onProgressUpdate(progress => onProgress(progress.progress))
  58. })
  59. }
  60. export const listTeamBackgroundFiles = teamId => api
  61. .get(`/core/user/team/${teamId}/background-files`, {}, false)
  62. .then(unwrap)
  63. export const disableTeamBackgroundFile = (teamId, fileId) => api
  64. .del(`/core/user/team/${teamId}/background-files/${fileId}`, {}, false)
  65. .then(unwrap)
  66. export const createTeamBackgroundSession = teamId => api
  67. .post(`/core/user/team/${teamId}/background-upload-session`, {}, false)
  68. .then(unwrap)
  69. export function downloadTeamBackgroundFile(teamId, fileId) {
  70. return new Promise((resolve, reject) => {
  71. let responseHeaders
  72. const task = uni.downloadFile({
  73. url: `${BaseApi}/core/user/team/${teamId}/background-files/${fileId}/download`,
  74. header: { token: uni.getStorageSync('token') },
  75. success: result => {
  76. const resultHeaders = result && (result.header || result.headers)
  77. if (!result || result.statusCode !== 200
  78. || !isDocumentDownload(responseHeaders || resultHeaders)) {
  79. reject(new Error('下载失败,请重试'))
  80. return
  81. }
  82. wx.openDocument({
  83. filePath: result.tempFilePath,
  84. showMenu: true,
  85. success: resolve,
  86. fail: reject
  87. })
  88. },
  89. fail: reject
  90. })
  91. if (task && typeof task.onHeadersReceived === 'function') {
  92. task.onHeadersReceived(result => {
  93. responseHeaders = result && (result.header || result.headers)
  94. })
  95. }
  96. })
  97. }