images.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <view class="imgs">
  3. <button class="btn" @click="choose()" v-if="!read">
  4. <text class="icon">&#xe658;</text>
  5. <text class="text">选择上传</text>
  6. </button>
  7. <view class="files omit" v-for="(item, index) in value" :key="index" @click="preview(item)">
  8. <view class="file omit">{{ index + 1 }}, {{ item.url }}</view>
  9. <view class="type">{{ item.type.toLowerCase() }}</view>
  10. <view class="look" v-if="read">查看</view>
  11. <view class="del" v-else @click.stop="del(item)">删除</view>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. name: 'images',
  18. props: {
  19. value: {
  20. type: Array
  21. },
  22. read: {
  23. type: Boolean,
  24. default: false
  25. }
  26. },
  27. data() {
  28. return {
  29. show: false,
  30. ip: this.http.ip,
  31. list: this.value,
  32. item: {},
  33. actions: [{ name: '图片' }, { name: '视频' }]
  34. };
  35. },
  36. methods: {
  37. getType(name) {
  38. return name.substring(name.lastIndexOf('.') + 1);
  39. },
  40. choose() {
  41. if (this.value.length >= 5) {
  42. uni.showModal({ content: '最多只能上传5个附件', showCancel: false });
  43. return;
  44. }
  45. uni.chooseFile({
  46. count: 5,
  47. success: e => {
  48. console.log('asd:' + JSON.stringify(e));
  49. e.tempFilePaths.forEach((item, index) => {
  50. uni.showLoading({ title: '正在上传...', mask: true });
  51. uni.uploadFile({
  52. url: this.http.urls.upload,
  53. filePath: item,
  54. name: 'file',
  55. header: { Authorization: this.getUser().token },
  56. success: res => {
  57. uni.hideLoading();
  58. let data = JSON.parse(res.data);
  59. if (data.code == 200) {
  60. if (this.value.length < 5) {
  61. this.value.push({ url: data.fileName, type: data.fileName.substring(data.fileName.lastIndexOf('.') + 1) });
  62. this.$forceUpdate();
  63. this.$emit('input', this.value);
  64. } else {
  65. uni.showModal({ content: '最多只能上传5个附件', showCancel: false });
  66. }
  67. } else {
  68. uni.showModal({ content: data.msg, showCancel: false });
  69. }
  70. },
  71. fail: res => {
  72. uni.hideLoading();
  73. uni.showModal({ content: '图片上传失败', showCancel: false });
  74. }
  75. });
  76. });
  77. }
  78. });
  79. },
  80. //预览资源
  81. preview(item) {
  82. if (item.type.toLowerCase() == 'jpg' || item.type.toLowerCase() == 'png') {
  83. uni.previewImage({
  84. urls: [this.ip + item.url]
  85. });
  86. }
  87. if (item.type.toLowerCase() == 'mp4') {
  88. this.item = item;
  89. this.show = true;
  90. }
  91. },
  92. del(item) {
  93. this.value.splice(this.value.indexOf(item), 1);
  94. }
  95. }
  96. };
  97. </script>
  98. <style lang="scss" scoped>
  99. .imgs {
  100. margin-top: -15px;
  101. .btn {
  102. background-color: white;
  103. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0);
  104. border-radius: 5px;
  105. color: #656363;
  106. .icon {
  107. padding-right: 5px;
  108. }
  109. margin-bottom: 10px;
  110. }
  111. }
  112. .popup_xz {
  113. overflow: hidden;
  114. ._video {
  115. width: 100%;
  116. }
  117. }
  118. </style>