upload.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import env from '../config/env.js'
  2. /**
  3. * 文件上传工具
  4. * 对应PC端的 upload.js
  5. */
  6. const upload = {
  7. /**
  8. * 上传文件到服务器
  9. * @param {String} filePath - 文件临时路径
  10. * @param {Object} options - 配置选项
  11. * @param {String} options.name - 文件字段名,默认 'fileEdit'
  12. * @param {Object} options.formData - 额外的表单数据,默认 { application: '' }
  13. * @param {Boolean|String} options.loading - 是否显示loading,或loading文字
  14. * @returns {Promise} 返回服务器响应数据
  15. */
  16. uploadFile(filePath, options = {}) {
  17. const {
  18. name = 'fileEdit',
  19. formData = { application: '' },
  20. loading = true
  21. } = options
  22. // 显示 loading
  23. if (loading) {
  24. const loadingTitle = typeof loading === 'string' ? loading : '上传中...'
  25. uni.showLoading({
  26. title: loadingTitle,
  27. mask: true
  28. })
  29. }
  30. // 获取设备信息
  31. const deviceInfo = uni.getStorageSync("deviceInfo") || {}
  32. const devId = deviceInfo.deviceId || ''
  33. const sbmc = deviceInfo.model || ''
  34. // 构建上传URL(对应PC端的 /service?ssServ=ulByHttp&type=img)
  35. const url = '/service?ssServ=ulByHttp&type=img'
  36. const separator = url.includes('?') ? '&' : '?'
  37. const finalUrl = `${env.baseUrl}${url}${separator}devId=${devId}&sbmc=${sbmc}`
  38. return new Promise((resolve, reject) => {
  39. uni.uploadFile({
  40. url: finalUrl,
  41. filePath,
  42. name,
  43. formData,
  44. header: (() => {
  45. const headers = {}
  46. const jsessionId = uni.getStorageSync('JSESSIONID')
  47. if (jsessionId) {
  48. headers['Cookie'] = `JSESSIONID=${jsessionId}`
  49. }
  50. return headers
  51. })(),
  52. success: (res) => {
  53. if (res.statusCode === 200) {
  54. try {
  55. // 解析返回数据,对应PC端的格式:
  56. // { fileList: [{ path: "xxx.png", fileName: "xxx.png" }] }
  57. const data = JSON.parse(res.data)
  58. resolve(data)
  59. } catch (e) {
  60. // 如果解析失败,返回原始数据
  61. resolve(res.data)
  62. }
  63. } else {
  64. reject(res)
  65. }
  66. },
  67. fail: (err) => {
  68. uni.showToast({
  69. title: '上传失败',
  70. icon: 'none'
  71. })
  72. reject(err)
  73. },
  74. complete: () => {
  75. if (loading) {
  76. uni.hideLoading()
  77. }
  78. }
  79. })
  80. })
  81. },
  82. /**
  83. * 上传图片(快捷方法)
  84. * @param {String} filePath - 图片临时路径
  85. * @param {Object} options - 配置选项
  86. * @returns {Promise<String>} 返回服务器上的图片路径
  87. */
  88. async uploadImage(filePath, options = {}) {
  89. try {
  90. const result = await this.uploadFile(filePath, {
  91. ...options,
  92. loading: options.loading !== false ? '上传图片中...' : false
  93. })
  94. // 提取文件路径(对应PC端的 result.fileList[0].path)
  95. return result?.fileList?.[0]?.path || ''
  96. } catch (error) {
  97. console.error('上传图片失败:', error)
  98. throw error
  99. }
  100. },
  101. /**
  102. * 上传头像(专用方法)
  103. * @param {String} filePath - 头像临时路径
  104. * @returns {Promise<String>} 返回服务器上的头像路径
  105. */
  106. async uploadAvatar(filePath) {
  107. return this.uploadImage(filePath, {
  108. loading: '上传头像中...'
  109. })
  110. }
  111. }
  112. export default upload