This commit is contained in:
左哥 2025-10-19 21:56:31 +08:00
parent 7477992bc7
commit 1b61e3de12

View File

@ -84,7 +84,7 @@ Page({
// only auto-scroll if user is at bottom
scrollToId: this.data.isAtBottom ? (list[list.length - 1] ? list[list.length - 1].id : '') : this.data.scrollToId
});
console.log('messages',list);
console.log('messages', list);
})
},
@ -260,21 +260,16 @@ Page({
if (!text) return;
const now = new Date();
const id = 'u' + Date.now();
const timeStr = this.formatTime(now);
// 判断是否需要显示时间分割线
let showTime = false;
const lastMsg = this.data.messages.length ? this.data.messages[this.data.messages.length - 1] : null;
if (!lastMsg || now - (lastMsg._ts || now) > 5 * 60 * 1000) showTime = true;
// const timeStr = this.formatTime(now);
// // 判断是否需要显示时间分割线
// let showTime = false;
// const lastMsg = this.data.messages.length ? this.data.messages[this.data.messages.length - 1] : null;
// if (!lastMsg || now - (lastMsg._ts || now) > 5 * 60 * 1000) showTime = true;
const msg = {
id,
from: 'user',
type: 'text',
content: {
messages: text
},
showTime,
timeStr,
_ts: now,
"msg_type": 1,
receiver_id: '',
send_time: "刚刚",
@ -327,34 +322,37 @@ Page({
chooseImage() {
const that = this;
wx.chooseImage({
count: 1,
count: 1, // 选择图片的数量
mediaType: ['image'], // 仅选择图片
sizeType: ['compressed', 'original'],
sourceType: ['album', 'camera'],
success(res) {
const tempFilePaths = res.tempFilePaths;
console.log('选择的图片路径:', res);
if (tempFilePaths && tempFilePaths.length) {
const now = new Date();
const userInfo = wx.getStorageSync('user_info')
// const now = new Date();
const id = 'u' + Date.now();
const timeStr = that.formatTime(now);
let showTime = false;
const lastMsg = that.data.messages.length ? that.data.messages[that.data.messages.length - 1] : null;
if (!lastMsg || now - (lastMsg._ts || now) > 5 * 60 * 1000) showTime = true;
// const timeStr = that.formatTime(now);
// let showTime = false;
// const lastMsg = that.data.messages.length ? that.data.messages[that.data.messages.length - 1] : null;
// if (!lastMsg || now - (lastMsg._ts || now) > 5 * 60 * 1000) showTime = true;
const msg = {
id,
from: 'user',
msg_type: '2',
receiver_id: '',
content: {
messages:tempFilePaths[0]
messages: tempFilePaths[0]
},
showTime,
timeStr,
_ts: now,
sender_id: that.data.userInfo.openid,
sender_name: that.data.userInfo.user_name
sender_id: userInfo.openid,
sender_name: userInfo.user_name,
receiver_id: '',
send_time: "刚刚",
};
const list = that.data.messages
list.push(msg);
that.setData({
messages: that.data.messages.concat(msg)
messages: list
});
setTimeout(() => {
that.setData({
@ -362,6 +360,30 @@ Page({
});
}, 50);
wx.uploadFile({
filePath: tempFilePaths[0], // 图片临时文件路径
name: 'file', // 服务器接收文件的字段名,需与后端对应
url: 'https://mini-chat.1024tool.vip/admin/upload/image', // 服务器接收图片的接口地址
success: (res) => {
const data = JSON.parse(res.data);
console.log('上传成功', data);
const accountInfo = wx.getAccountInfoSync();
request('app/send_message', 'POST', {
"app_id": accountInfo.miniProgram.appId,
"content": JSON.stringify({
messages: 'https://mini-chat.1024tool.vip/'+ data.preview_image_url
}),
"from_user_id": userInfo.openid,
"from_user_name": userInfo.user_name,
"msg_type": 2
})
},
fail: (err) => {
console.error('上传失败', err);
}
});
// 模拟客服返回图片确认
// setTimeout(() => {
// const replyNow = new Date();
@ -421,7 +443,7 @@ Page({
// cleanup
this.stopPolling();
},
onPullDownRefresh() {},
onReachBottom() {},
onShareAppMessage() {}
onPullDownRefresh() { },
onReachBottom() { },
onShareAppMessage() { }
});