tj
2025-05-21 7abdb24968bcb88f177851a30e41f282b9189a98
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<template>
  <u-popup :show="value" mode="bottom" @open="handleOpen" @close="handleClose" closeOnClickOverlay>
    <view class="comment-popup">
      <u-textarea v-model="commentContent" placeholder="请输入内容" count :focus="isFocus" :cursor-spacing="150"
        style="background-color: #999999;"></u-textarea>
 
      <view class="comment-btn-view">
        <view class="comment-btn-icon">
          <u-icon name="@" size="60" color="#999999"></u-icon>
          <u-icon name="/static/common/smile.png" size="60" color="#999999"></u-icon>
          <u-icon name="photo" size="60" color="#999999" @click="chooseImage"></u-icon>
          <u-icon name="plus-circle" size="60" color="#999999"></u-icon>
        </view>
        <view class="comment-btn">
          <u-button :disabled="!canSend" type="error" shape="circle" text="发送" size="mini"
            @click="sendComment"></u-button>
        </view>
      </view>
 
      <!-- 只有在图片选择后才显示上传组件 -->
      <view class="comment-image-upload" v-if="fileList.length > 0">
        <u-upload :file-list="fileList" name="file" :auto-upload="false" :max-count="9" @afterRead="afterRead"
          @delete="handleDelete" upload-text="上传图片" width="150" height="150" :cursor-spacing="150"
          multiple="true"></u-upload>
      </view>
 
    </view>
  </u-popup>
</template>
 
<script>
export default {
  name: "CommentPopup",
  props: {
    value: Boolean
  },
  data() {
    return {
      commentContent: "",
      isFocus: false,
      focusLock: false,
      fileList: [],
      uploadUrl: "https://your-api.com/upload", // 替换为你的上传接口
    };
  },
  watch: {
    value(newVal) {
      if (!newVal) {
        this.isFocus = false;
      }
    }
  },
  computed: {
    canSend() {
      return this.commentContent.trim() !== '' || this.fileList.length > 0;
    }
  },
  methods: {
    handleOpen() {
      if (this.focusLock) return;
      this.focusLock = true;
      setTimeout(() => {
        this.isFocus = true;
        this.focusLock = false;
      }, 300);
    },
    handleClose() {
      this.isFocus = false;
      this.$emit("input", false);
    },
 
 
    chooseImage() {
      uni.chooseImage({
        count: 9 - this.fileList.length,
        sizeType: ["original", "compressed"],
        sourceType: ["album", "camera"],
        success: (res) => {
          console.log("选择的图片路径:", res.tempFilePaths);
          res.tempFilePaths.forEach(filePath => {
            const fileItem = {
              url: filePath,
              status: 'ready'
            };
            this.fileList.push(fileItem);
            this.uploadImage(filePath); // 调用上传
          });
        },
        fail: (err) => {
          console.log("选择失败", err);
        }
      });
    },
    uploadImage(filePath) {
      uni.uploadFile({
        url: this.uploadUrl,
        filePath,
        name: 'file',
        success: (res) => {
          console.log('上传成功', res);
          // 你可以解析返回值,并更新 fileList 中对应的项
        },
        fail: (err) => {
          console.error('上传失败', err);
        }
      });
    },
    afterRead(file) {
      // 理论上这个不会触发了,因为你已经自己上传了
    },
    handleDelete(index, file) {
      this.fileList.splice(index, 1);
    },
    sendComment() {
      if (!this.canSend) return;
 
      console.log("发送评论:", this.commentContent);
      console.log("附带图片:", this.fileList.map(f => f.url));
 
      // 这里处理评论内容和图片上传路径的提交逻辑
      // 例如向后端接口提交
 
      // 清空状态
      this.commentContent = "";
      this.fileList = [];
      this.handleClose();
    }
  }
};
</script>
 
<style scoped>
.comment-popup {
  padding: 20rpx;
}
 
.comment-btn-view {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20rpx;
}
 
.comment-btn-icon {
  display: flex;
  gap: 20rpx;
}
 
.comment-btn {
  flex-shrink: 0;
}
 
.comment-image-upload {
  margin-top: 20rpx;
}
 
.horizontal-scroll {
  white-space: nowrap;
  overflow-x: auto;
  padding: 10rpx 0;
}
 
.scroll-content {
  display: inline-block;
}
</style>