upload.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. const temp = JSON.parse(JSON.stringify(this.data.form[formkey]))
  63. temp.splice(index, 1)
  64. this.setData({
  65. [`form.${formkey}`]: temp
  66. })
  67. },
  68. async uploadImg(event) {
  69. const { formkey, maxsize = '2' } = event.currentTarget.dataset
  70. const maxSize = maxsize * 1024 * 1024
  71. try {
  72. const { errMsg, tempFiles } = await wx.chooseImage({
  73. count: 1,
  74. sizeType: ['original', 'compressed']
  75. })
  76. if (errMsg === 'chooseImage:ok') {
  77. wx.showToast({
  78. title: '上传中',
  79. icon: 'loading',
  80. duration: 200000
  81. })
  82. const result = tempFiles.map(async item => {
  83. item.filePath = item.path
  84. item.formkey = formkey
  85. const compressResult = item.size > maxSize ? await this.compressJs(item) : item
  86. return handleUpload(compressResult)
  87. })
  88. try {
  89. const res = await Promise.all(result)
  90. if (this.uploadCallBack) {
  91. this.uploadCallBack(res)
  92. }
  93. wx.showToast({
  94. title: '图片上传成功',
  95. icon: 'success',
  96. duration: 2000
  97. })
  98. } catch (err) {
  99. console.log(err)
  100. wx.showToast({
  101. title: '图片上传出错',
  102. icon: 'error',
  103. duration: 2000
  104. })
  105. }
  106. }
  107. } catch (e) {
  108. }
  109. }
  110. }