photo.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <view>
  3. <view class="desc container">
  4. <uni-section :title="label" type="line">
  5. <view class="example-body">
  6. <uni-file-picker :auto-upload="false" :value="fileList" :limit="limit" :title="'最多选择'+ limit +'张图片'"
  7. @select="select" @delete="deletePhoto"></uni-file-picker>
  8. </view>
  9. </uni-section>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. props: {
  16. value: [String, Object, Array],
  17. // 图片数量限制
  18. limit: {
  19. type: Number,
  20. default: 1,
  21. },
  22. label: {
  23. Type: String,
  24. default: ''
  25. },
  26. },
  27. data() {
  28. return {
  29. fileList: []
  30. }
  31. },
  32. methods: {
  33. // 获取上传状态
  34. select(e) {
  35. this.uploadFilePromise(e['tempFilePaths'][0]).then(res => {
  36. if (res.code === 200) {
  37. this.fileList.push({
  38. url: this.$BASE_URL + res.url,
  39. extname: res.extname,
  40. name: res.fileName
  41. })
  42. this.$emit("input", JSON.stringify(this.fileList))
  43. }
  44. })
  45. },
  46. deletePhoto(e) {
  47. const findex = this.fileList.map(f => f.url).indexOf(e.tempFilePath);
  48. if (findex > -1) {
  49. this.fileList.splice(findex, 1);
  50. this.$emit("input", JSON.stringify(this.fileList));
  51. }
  52. },
  53. uploadFilePromise(url) {
  54. return new Promise((resolve, reject) => {
  55. let a = uni.uploadFile({
  56. url: this.$BASE_URL + '/common/upload',
  57. filePath: url,
  58. name: 'file',
  59. header: {
  60. "Authorization": "Bearer " + uni.getStorageSync('token')
  61. },
  62. success: (res) => {
  63. // console.log(JSON.parse(res.data).url);
  64. setTimeout(() => {
  65. resolve(JSON.parse(res.data))
  66. }, 50)
  67. }
  68. });
  69. })
  70. }
  71. }
  72. }
  73. </script>
  74. <style>
  75. .desc {
  76. padding: 10rpx 15rpx 0 15rpx;
  77. color: #818181;
  78. font-size: 20rpx;
  79. }
  80. .example-body {
  81. padding: 10px;
  82. padding-top: 0;
  83. }
  84. .custom-image-box {
  85. /* #ifndef APP-NVUE */
  86. display: flex;
  87. /* #endif */
  88. flex-direction: row;
  89. justify-content: space-between;
  90. align-items: center;
  91. }
  92. .text {
  93. font-size: 14px;
  94. color: #333;
  95. }
  96. </style>