You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

form-upload-more.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const reqInterface = require("../../../api/models");
  2. const Base64 = require("../../../utils/base64");
  3. const {
  4. imagePrefix, getApi
  5. } = require("../../../utils/dataHelper");
  6. Component({
  7. /**
  8. * 组件的属性列表
  9. */
  10. properties: {
  11. images: {
  12. type: Array,
  13. value: []
  14. },
  15. imagesPath: {
  16. type: String,
  17. value: ''
  18. },
  19. uploadTxt: String,
  20. },
  21. observers: {
  22. 'images': function (val) {
  23. let self = this;
  24. if (val) {
  25. self.setData({
  26. imageList: val
  27. })
  28. }
  29. },
  30. 'imagesPath': function (val) {
  31. let self = this;
  32. if (val) {
  33. self.data.imagePathList = val.split(",")
  34. self.setData({
  35. imagePathList: self.data.imagePathList
  36. })
  37. }
  38. },
  39. },
  40. lifetimes: {
  41. attached() {
  42. this.setData({
  43. 'header.Blade-Auth': `bearer ${wx.getStorageSync('token')}`
  44. })
  45. this.setData({
  46. api: getApi()
  47. })
  48. }
  49. },
  50. data: {
  51. styleCss: 'padding: 20rpx 0; line-height: 36rpx font-size: 28rpx; border-bottom: 1rpx dashed #f0f0f0;',
  52. api: '',
  53. imageList: [],
  54. imagePathList: [],
  55. tempImageFiles: [],
  56. header: {
  57. 'Authorization': `Basic ${Base64.Base64.encode('miniapp:miniapp_secret')}`,
  58. 'Blade-Auth': `bearer ${wx.getStorageSync('token')}`
  59. }
  60. },
  61. /**
  62. * 组件的方法列表
  63. */
  64. methods: {
  65. uploadFiles(e) {
  66. let self = this;
  67. wx.showActionSheet({
  68. itemList: ['从相册中选择', '拍照'],
  69. success(res) {
  70. if (res.tapIndex == 0) {
  71. self.chooseWxImage('album');
  72. } else if (res.tapIndex == 1) {
  73. self.chooseWxImage('camera');
  74. }
  75. },
  76. fail(res) {
  77. console.log(res.errMsg)
  78. }
  79. })
  80. },
  81. chooseWxImage(type) {
  82. let self = this;
  83. wx.chooseMedia({
  84. mediaType: "image",
  85. sizeType: ['original', 'compressed'],
  86. sourceType: [type],
  87. async success(res) {
  88. self.setData({
  89. tempImageFiles: res.tempFiles
  90. })
  91. for (let i = 0; i < self.data.tempImageFiles.length; i++) {
  92. let res = await reqInterface.PostImageUpload(self.data.tempImageFiles[i].tempFilePath);
  93. self.data.imageList.push(res.data.url)
  94. self.data.imagePathList.push(res.data.path)
  95. }
  96. self.setData({
  97. imageList: self.data.imageList,
  98. imagePathList: self.data.imagePathList
  99. })
  100. self.triggerEvent('sumbitImageInfo', {
  101. images: self.data.imageList,
  102. imagesPath: self.data.imagePathList
  103. })
  104. }
  105. })
  106. },
  107. delImage(e) {
  108. let self = this;
  109. self.data.imageList.splice(e.currentTarget.dataset.index, 1)
  110. self.data.imagePathList.splice(e.currentTarget.dataset.index, 1)
  111. self.setData({
  112. imageList: self.data.imageList,
  113. imagePathList: self.data.imagePathList
  114. })
  115. self.triggerEvent('sumbitImageInfo', {
  116. images: self.data.imageList,
  117. imagesPath: self.data.imagePathList
  118. })
  119. },
  120. imgPreview(e) {
  121. let self = this;
  122. wx.previewImage({
  123. urls: [e.currentTarget.dataset.href]
  124. })
  125. },
  126. }
  127. })