upload.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const { handleUpload } = require('../api/request')
  2. module.exports = {
  3. compressJs(file) {
  4. return new Promise(async (resolve, reject) => {
  5. try {
  6. const { errMsg, tempFilePath } = await wx.compressImage({
  7. src: file.filePath, // 图片路径
  8. quality: 80 // 压缩质量
  9. })
  10. if (errMsg === 'compressImage:ok') {
  11. resolve({ ...file, filePath: tempFilePath })
  12. return
  13. }
  14. resolve(file)
  15. } catch (err) {
  16. resolve(file)
  17. }
  18. })
  19. },
  20. async afterRead(event) {
  21. const { file } = event.detail
  22. const { formkey, maxsize = '2' } = event.currentTarget.dataset
  23. const maxSize = maxsize * 1024 * 1024
  24. let _file = file
  25. if (Object.prototype.toString.call(file) === '[object Object]') {
  26. _file = [file]
  27. }
  28. wx.showToast({
  29. title: '上传中',
  30. icon: 'loading',
  31. duration: 200000
  32. })
  33. const result = _file.map(async item => {
  34. // 压缩图片时传入的路径
  35. item.filePath = item.url
  36. item.formkey = formkey
  37. const compressResult = item.size > maxSize ? await this.compressJs(item) : item
  38. return handleUpload(compressResult)
  39. })
  40. try {
  41. const res = await Promise.all(result)
  42. if (this.uploadCallBack) {
  43. this.uploadCallBack(res)
  44. }
  45. wx.showToast({
  46. title: '上传成功',
  47. icon: 'success',
  48. duration: 2000
  49. })
  50. } catch (err) {
  51. console.log(err)
  52. wx.showToast({
  53. title: '上传出错',
  54. icon: 'none',
  55. duration: 2000
  56. })
  57. }
  58. },
  59. delete(event) {
  60. const { formkey } = event.currentTarget.dataset
  61. const { index } = event.detail
  62. this.data.form[formkey].splice(index, 1)
  63. this.setData({
  64. ['form.' + formkey]: this.data.form[formkey]
  65. })
  66. },
  67. async uploadImg(event) {
  68. const { formkey, maxsize = '2' } = event.currentTarget.dataset
  69. const maxSize = maxsize * 1024 * 1024
  70. try {
  71. const { errMsg, tempFiles } = await wx.chooseImage({
  72. count: 1,
  73. sizeType: ['original', 'compressed']
  74. })
  75. if (errMsg === 'chooseImage:ok') {
  76. wx.showToast({
  77. title: '上传中',
  78. icon: 'loading',
  79. duration: 200000
  80. })
  81. const result = tempFiles.map(async item => {
  82. item.filePath = item.path
  83. item.formkey = formkey
  84. const compressResult = item.size > maxSize ? await this.compressJs(item) : item
  85. return handleUpload(compressResult)
  86. })
  87. try {
  88. const res = await Promise.all(result)
  89. if (this.uploadCallBack) {
  90. this.uploadCallBack(res)
  91. }
  92. wx.showToast({
  93. title: '图片上传成功',
  94. icon: 'success',
  95. duration: 2000
  96. })
  97. } catch (err) {
  98. console.log(err)
  99. wx.showToast({
  100. title: '图片上传出错',
  101. icon: 'error',
  102. duration: 2000
  103. })
  104. }
  105. }
  106. } catch (e) {
  107. }
  108. }
  109. }