tj
2025-05-26 f5f314e167906ec5b15d4f56568408982178caa1
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<template>
  <up-popup
    v-model:show="showPopup"
    mode="bottom"
    @open="handleOpen"
    @close="handleClose"
    close-on-click-overlay
  >
    <view class="comment-popup">
      <up-textarea
        v-model="commentContent"
        placeholder="请输入内容"
        count
        :focus="isFocus"
        :cursor-spacing="200"
        
      />
 
      <view class="comment-btn-view">
        <view class="comment-btn-icon">
          <up-icon name="@" size="60rpx" color="#999999" />
          <up-icon name="/static/common/smile.png" size="60rpx" color="#999999" />
          <up-icon name="photo" size="60rpx" color="#999999" @click="chooseImage" />
          <up-icon name="plus-circle" size="60rpx" color="#999999" />
        </view>
        <view class="comment-btn">
          <up-button
            :disabled="!canSend"
            type="error"
            shape="circle"
            text="发送"
            size="mini"
            @click="sendComment"
          />
        </view>
      </view>
 
      <view class="comment-image-upload" v-if="fileList.length > 0">
        <up-upload
          :file-list="fileList"
          name="file"
          :auto-upload="false"
          :max-count="9"
          upload-text="上传图片"
          width="130rpx"
          height="130rpx"
          :cursor-spacing="200"
          multiple
          @delete="handleDelete"
        />
      </view>
    </view>
  </up-popup>
</template>
 
<script setup lang="ts">
import { ref, watch, computed } from 'vue';
 
interface FileItem {
  url: string;
  status: string;
}
 
const props = defineProps<{
  modelValue: boolean;  // 修改这里
}>();
 
const emit = defineEmits(['update:modelValue']);  // 修改这里
 
const showPopup = ref(props.modelValue);
 
const commentContent = ref('');
const isFocus = ref(false);
const focusLock = ref(false);
const fileList = ref<FileItem[]>([]);
 
const uploadUrl = 'https://your-api.com/upload'; // 替换成你的上传接口地址
 
// 监听外部传入的 modelValue,同步到内部 showPopup
watch(
  () => props.modelValue,
  (newVal) => {
    showPopup.value = newVal;
    if (!newVal) {
      isFocus.value = false;
    }
  }
);
 
// 监听内部 showPopup 变化,通知外部更新 modelValue
watch(showPopup, (val) => {
  emit('update:modelValue', val);
});
 
const canSend = computed(() => {
  return commentContent.value.trim() !== '' || fileList.value.length > 0;
});
 
const handleOpen = () => {
  if (focusLock.value) return;
  focusLock.value = true;
  setTimeout(() => {
    isFocus.value = true;
    focusLock.value = false;
  }, 300);
};
 
const handleClose = () => {
  isFocus.value = false;
  showPopup.value = false;
};
 
const chooseImage = () => {
  uni.chooseImage({
    count: 9 - fileList.value.length,
    sizeType: ['original', 'compressed'],
    sourceType: ['album', 'camera'],
    success: (res) => {
      res.tempFilePaths.forEach((filePath) => {
        const fileItem: FileItem = {
          url: filePath,
          status: 'ready',
        };
        fileList.value.push(fileItem);
        uploadImage(filePath);
      });
    },
    fail: (err) => {
      console.error('选择图片失败:', err);
    },
  });
};
 
const uploadImage = (filePath: string) => {
  uni.uploadFile({
    url: uploadUrl,
    filePath,
    name: 'file',
    success: (res) => {
      console.log('上传成功:', res);
      // 可解析 res 并更新 fileList 中的 fileItem.url 为服务器返回地址
    },
    fail: (err) => {
      console.error('上传失败:', err);
    },
  });
};
 
const handleDelete = (index: number) => {
  fileList.value.splice(index, 1);
};
 
const sendComment = () => {
  if (!canSend.value) return;
 
  console.log('发送评论:', commentContent.value);
  console.log('附带图片:', fileList.value.map((f) => f.url));
 
  // 提交评论逻辑
  commentContent.value = '';
  fileList.value = [];
  handleClose();
};
</script>
 
<style scoped lang="scss">
.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;
}
</style>