消息处理

This commit is contained in:
@zuopngfei 2025-11-06 17:19:33 +08:00
parent 1d4ab75441
commit bb20e4ea87
3 changed files with 96 additions and 158 deletions

View File

@ -1,106 +1,31 @@
const request = require('./request.js')
const uploadFile = async (file, callback) => {
const policyData = await request('admin/policy_token', 'post') function uploadFile(filePath) {
const res = JSON.parse(policyData.token) return new Promise((resolve, reject) => {
if (!filePath) {
const fileName = file.tempFilePath.split('/').pop(); // hello.png const err = new Error('uploadFile requires a filePath parameter');
// const fileName = fileNameWithExt.split('.').slice(0, -1).join('.'); // hello console.error('上传失败:未提供文件路径', err);
return reject(err);
const formData = {
key: 'upload_file/' + fileName, //上传文件名称
policy: res.policy, //表单域
'x-oss-signature-version': res.x_oss_signature_version, //指定签名的版本和算法
'x-oss-credential': res.x_oss_credential, //指明派生密钥的参数集
'x-oss-date': res.x_oss_date, //请求的时间
'x-oss-signature': res.signature, //签名认证描述信息
'x-oss-security-token': res.security_token, //安全令牌
success_action_status: "200" //上传成功后响应状态码
};
// console.log(filePath)
// return
// 发送请求上传文件
wx.uploadFile({
url: 'https://image-fudan.oss-cn-beijing.aliyuncs.com/',
method: 'put',
filePath: file.tempFilePath,
name: 'file', //固定值为file
formData: formData,
success(res) {
console.log('上传响应:', res);
if (res.statusCode === 200) {
callback(null, 'https://image-fudan.oss-cn-beijing.aliyuncs.com/upload_file/' + fileName); // 上传成功
} else {
console.error('上传失败,状态码:', res.statusCode);
console.error('失败响应:', res);
callback(res); // 上传失败,返回响应
}
},
fail(err) {
console.error('上传失败:', err); // 输出错误信息
wx.showToast({
title: '上传失败,请重试!',
icon: 'none'
});
callback(err); // 调用回调处理错误
} }
});
wx.uploadFile({
filePath: filePath, // 图片临时文件路径(由调用方传入)
name: 'file', // 服务器接收文件的字段名,需与后端对应
url: 'https://mini-chat.1024tool.vip/api/admin/upload/image', // 服务器接收图片的接口地址
success: (res) => {
const data = JSON.parse(res.data);
resolve('https://mini-chat.1024tool.vip/' + data.preview_image_url);
},
fail: (err) => {
console.error('上传失败', err);
reject(err);
}
});
})
} }
const uploadFileToOSS = () => { // 导出请求和服务地址CommonJS 兼容)
module.exports = uploadFile;
return new Promise((resolve, reject) => {
wx.chooseMedia({
count: 1, // 选择一个文件
mediaType: ['image'],
sourceType: ['album', 'camera'],
// type: 'all', // 支持所有类型的文件
success: (res) => {
wx.showToast({
title: '文件上传中,请稍等!',
icon: 'none'
});
console.log('选择的文件:', res.tempFiles); // 输出选择的文件信息
if (res.tempFiles.length > 0) {
const tempFilePath = res.tempFiles[0];
console.log('选择的文件路径:', tempFilePath); // 输出文件路径
uploadFile(tempFilePath, (error, data) => {
if (error) {
wx.showToast({
title: '上传失败!',
icon: 'none'
});
console.error('上传失败:', error); // 输出具体的错误信息
reject(error)
} else {
resolve(data)
console.log('上传成功:', data); // 输出上传成功后的数据
}
});
} else {
wx.showToast({
title: '未选择文件!',
icon: 'none'
});
reject('未选择文件!')
}
},
fail: (err) => {
wx.showToast({
title: '选择文件失败!',
icon: 'none'
});
console.error('选择文件失败:', err); // 输出选择文件的错误信息
reject('选择文件失败!')
}
});
});
};
// CommonJS 导出,适配微信开发者工具当前解析能力
module.exports = uploadFileToOSS;

View File

@ -1,7 +1,7 @@
// pages/contact/index.js // pages/contact/index.js
const request = require('../../api/request.js'); const request = require('../../api/request.js');
const uploadFile = require('../../api/upload.js');
Page({ Page({
/** /**
* 页面的初始数据 * 页面的初始数据
@ -169,10 +169,12 @@ Page({
// 解析消息内容 // 解析消息内容
if (typeof item.content === 'string') { if (typeof item.content === 'string') {
try { try {
item.content = JSON.parse(item.content); item.content = {
message: JSON.parse(item.content).message || JSON.parse(item.content).messages
}
} catch (e) { } catch (e) {
console.warn('消息内容解析失败:', item.content); console.warn('消息内容解析失败:', item.content);
item.content = { messages: item.content }; item.content = { message: item.content };
} }
} }
@ -282,7 +284,7 @@ Page({
}); });
}); });
}, },
fail: function(err) { fail: function (err) {
console.error('wx.login失败:', err); console.error('wx.login失败:', err);
} }
}); });
@ -406,7 +408,7 @@ Page({
const msg = { const msg = {
id, id,
content: { content: {
messages: welcomeText message: welcomeText
}, },
"msg_type": 1, "msg_type": 1,
receiver_id: '', receiver_id: '',
@ -433,7 +435,7 @@ Page({
request('app/send_message', 'POST', { request('app/send_message', 'POST', {
"app_id": accountInfo.miniProgram.appId, "app_id": accountInfo.miniProgram.appId,
"content": JSON.stringify({ "content": JSON.stringify({
messages: welcomeText message: welcomeText
}), }),
"from_user_id": userInfo.openid, "from_user_id": userInfo.openid,
"from_user_name": userInfo.user_name, "from_user_name": userInfo.user_name,
@ -470,7 +472,7 @@ Page({
const msg = { const msg = {
id, id,
content: { content: {
messages: text message: text
}, },
"msg_type": 1, "msg_type": 1,
receiver_id: '', receiver_id: '',
@ -492,7 +494,7 @@ Page({
request('app/send_message', 'POST', { request('app/send_message', 'POST', {
"app_id": accountInfo.miniProgram.appId, "app_id": accountInfo.miniProgram.appId,
"content": JSON.stringify({ "content": JSON.stringify({
messages: text message: text
}), }),
"from_user_id": userInfo.openid, "from_user_id": userInfo.openid,
"from_user_name": userInfo.user_name, "from_user_name": userInfo.user_name,
@ -550,7 +552,7 @@ Page({
msg_type: '2', msg_type: '2',
receiver_id: '', receiver_id: '',
content: { content: {
messages: tempFilePaths[0] message: tempFilePaths[0]
}, },
sender_id: userInfo.openid, sender_id: userInfo.openid,
sender_name: userInfo.user_name, sender_name: userInfo.user_name,
@ -567,30 +569,41 @@ Page({
scrollToId: id scrollToId: id
}); });
}, 50); }, 50);
uploadFile(tempFilePaths[0]).then((res) => {
wx.uploadFile({ const accountInfo = wx.getAccountInfoSync();
filePath: tempFilePaths[0], // 图片临时文件路径
name: 'file', // 服务器接收文件的字段名,需与后端对应
url: 'https://dsjhd9s.tbmw.cn/admin/upload/image', // 服务器接收图片的接口地址
success: (res) => {
const data = JSON.parse(res.data);
console.log('上传成功', data);
const accountInfo = wx.getAccountInfoSync();
request('app/send_message', 'POST', { request('app/send_message', 'POST', {
"app_id": accountInfo.miniProgram.appId, "app_id": accountInfo.miniProgram.appId,
"content": JSON.stringify({ "content": JSON.stringify({
messages: 'https://dsjhd9s.tbmw.cn/'+ data.preview_image_url message: res
}), }),
"from_user_id": userInfo.openid, "from_user_id": userInfo.openid,
"from_user_name": userInfo.user_name, "from_user_name": userInfo.user_name,
"msg_type": 2 "msg_type": 2
}) }).catch((err) => {
}, wx.showToast({
fail: (err) => { title: '图片发送失败',
console.error('上传失败', err); icon: 'none'
} });
});
}).catch((err) => {
wx.showToast({
title: '图片发送失败',
icon: 'none'
});
}); });
// wx.uploadFile({
// filePath: tempFilePaths[0], // 图片临时文件路径
// name: 'file', // 服务器接收文件的字段名,需与后端对应
// url: 'https://mini-chat.1024tool.vip/api/admin/upload/image', // 服务器接收图片的接口地址
// success: (res) => {
// },
// fail: (err) => {
// console.error('上传失败', err);
// }
// });
// 模拟客服返回图片确认 // 模拟客服返回图片确认
// setTimeout(() => { // setTimeout(() => {

View File

@ -20,10 +20,10 @@
<view class="bubble-wrap"> <view class="bubble-wrap">
<view class="bubble"> <view class="bubble">
<block wx:if="{{item.msg_type == 1}}"> <block wx:if="{{item.msg_type == 1}}">
<text class="msg-text">{{item.content.messages}}</text> <text class="msg-text">{{item.content.message}}</text>
</block> </block>
<block wx:elif="{{item.msg_type == 2}}"> <block wx:elif="{{item.msg_type == 2}}">
<image src="{{item.content.messages}}" bindtap="previewImage" data-src="{{item.content.messages}}" class="msg-image" mode="aspectFill" /> <image src="{{item.content.message}}" bindtap="previewImage" data-src="{{item.content.message}}" class="msg-image" mode="aspectFill" />
</block> </block>
</view> </view>