index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const app = getApp()
  2. Component({
  3. properties: {
  4. list: { type: Array, value: [] },
  5. name: { type: String },
  6. count: { type: Number, value: 1 },
  7. },
  8. data: {},
  9. methods: {
  10. // 图片上传
  11. imgUpload() {
  12. const that = this;
  13. const name = that.data.name;
  14. wx.chooseImage({
  15. count: 1,
  16. sizeType: ['original', 'compressed'],
  17. sourceType: ['album', 'camera'],
  18. success(res) {
  19. const file = res.tempFilePaths;
  20. wx.uploadFile({
  21. url: `${app.globalData.serverUrl}/files/test/upload`,
  22. filePath: file[0],
  23. name: 'file',
  24. formData: {},
  25. success: (arr) => {
  26. let aee = JSON.parse(arr.data);
  27. if (arr.statusCode == 200) {
  28. that.triggerEvent('imgUpload', { data: { name: aee.name, url: `${app.globalData.serverUrl}` + aee.uri, uri: aee.uri }, name: name })
  29. } else {
  30. wx.showToast({ title: `${arr.errMsg}`, icon: 'error', duration: 2000 })
  31. }
  32. }
  33. })
  34. }
  35. })
  36. },
  37. imgDel(e) {
  38. const that = this;
  39. const name = that.data.name;
  40. const { item, index } = e.currentTarget.dataset;
  41. that.triggerEvent('imgDel', { data: { file: item, index: index }, name: name })
  42. },
  43. // 预览
  44. toView: function (e) {
  45. const { item } = e.currentTarget.dataset;
  46. wx.previewImage({ current: '', urls: [item.url] })
  47. }
  48. }
  49. })