photo.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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" :readonly="readonly"></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. readonly: false
  27. },
  28. data() {
  29. return {
  30. fileList: []
  31. }
  32. },
  33. mounted() {
  34. if (this.value) {
  35. this.fileList = this.value
  36. }
  37. },
  38. methods: {
  39. // 获取上传状态
  40. select(e) {
  41. this.uploadFilePromise(e['tempFilePaths'][0]).then(res => {
  42. if (res.code === 200) {
  43. this.fileList.push({
  44. url: this.$BASE_URL + res.url,
  45. extname: res.extname,
  46. name: res.fileName
  47. })
  48. uni.showToast({
  49. title: "上传成功",
  50. icon: "none",
  51. })
  52. this.$emit("input", JSON.stringify(this.fileList))
  53. }
  54. })
  55. },
  56. deletePhoto(e) {
  57. const findex = this.fileList.map(f => f.url).indexOf(e.tempFilePath);
  58. if (findex > -1) {
  59. this.fileList.splice(findex, 1);
  60. this.$emit("input", JSON.stringify(this.fileList));
  61. }
  62. },
  63. uploadFilePromise(url) {
  64. return new Promise((resolve, reject) => {
  65. let a = uni.uploadFile({
  66. url: this.$BASE_URL + '/common/upload',
  67. filePath: url,
  68. name: 'file',
  69. header: {
  70. "Authorization": "Bearer " + uni.getStorageSync('token')
  71. },
  72. success: (res) => {
  73. // console.log(JSON.parse(res.data).url);
  74. setTimeout(() => {
  75. resolve(JSON.parse(res.data))
  76. }, 50)
  77. }
  78. });
  79. })
  80. }
  81. }
  82. }
  83. </script>
  84. <style>
  85. .desc {
  86. padding: 10rpx 15rpx 0 15rpx;
  87. color: #818181;
  88. font-size: 20rpx;
  89. }
  90. .example-body {
  91. padding: 10px;
  92. padding-top: 0;
  93. }
  94. .custom-image-box {
  95. /* #ifndef APP-NVUE */
  96. display: flex;
  97. /* #endif */
  98. flex-direction: row;
  99. justify-content: space-between;
  100. align-items: center;
  101. }
  102. .text {
  103. font-size: 14px;
  104. color: #333;
  105. }
  106. </style>