SCimage.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <view>
  3. <!-- 图片 -->
  4. <view class="" style="margin-top: 40rpx">
  5. <view class="" style="margin-bottom: 20rpx">巡查图片{{ uploadNumber + '/' + 9 }}</view>
  6. <view class="">
  7. <!-- 上传照片 -->
  8. <view style="margin-left: 56rpx">
  9. <u-upload
  10. width="200rpx"
  11. height="200rpx"
  12. :maxCount="9"
  13. :deletable="true"
  14. :fileList="fileList1"
  15. @afterRead="afterRead"
  16. @delete="deletePic"
  17. multiple
  18. name="1"
  19. ></u-upload>
  20. </view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. name: 'SCimage',
  28. props: {
  29. fileList: {
  30. type: Array,
  31. default() {
  32. return [];
  33. }
  34. }
  35. },
  36. data() {
  37. return {
  38. // 上传的图片
  39. fileList1: []
  40. };
  41. },
  42. computed: {
  43. log() {
  44. // console.log(this.fileList, 'uuuu');
  45. return this.fileList;
  46. },
  47. uploadNumber() {
  48. this.fileList1 = this.fileList;
  49. return this.fileList1.length;
  50. }
  51. },
  52. methods: {
  53. async afterRead(event) {
  54. // console.log(event, '上传图片');
  55. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  56. let lists = [].concat(event.file);
  57. let fileListLen = this[`fileList${event.name}`].length;
  58. lists.map((item) => {
  59. this[`fileList${event.name}`].push({
  60. ...item,
  61. status: 'uploading',
  62. message: '上传中'
  63. });
  64. });
  65. for (let i = 0; i < lists.length; i++) {
  66. const result = await this.uploadFilePromise(lists[i].url);
  67. let item = this[`fileList${event.name}`][fileListLen];
  68. this[`fileList${event.name}`].splice(
  69. fileListLen,
  70. 1,
  71. Object.assign(item, {
  72. status: 'success',
  73. message: '',
  74. url: result
  75. })
  76. );
  77. fileListLen++;
  78. }
  79. // console.log(this[`fileList${event.name}`], 'jjjjj');
  80. this.$emit('childEvent', this[`fileList${event.name}`]);
  81. },
  82. // 删除图片
  83. deletePic(event) {
  84. console.log(event, 'event');
  85. this[`fileList${event.name}`].splice(event.index, 1);
  86. this.$emit('childEvent', this[`fileList${event.name}`]);
  87. },
  88. uploadFilePromise(url) {
  89. return new Promise((resolve, reject) => {
  90. let userToken = '';
  91. let auth = this.$db.get('auth');
  92. userToken = auth.token;
  93. let a = uni.uploadFile({
  94. url: this.$config.baseUrl + 'api/common/upload?token=' + userToken,
  95. // url: 'api/common/upload?token=' + userToken,
  96. filePath: url,
  97. name: 'file',
  98. formData: {},
  99. success: (res) => {
  100. setTimeout(() => {
  101. console.log(res);
  102. resolve(JSON.parse(res.data).data.fullurl);
  103. }, 1000);
  104. }
  105. });
  106. });
  107. }
  108. }
  109. };
  110. </script>
  111. <style></style>