fix(contact): 修复消息内容字段从message改为messages的兼容性问题

refactor(upload): 重构文件上传逻辑,改用OSS直传方案
refactor(vendor): 禁用开发模式下的日志WebSocket连接
style: 统一文件末尾换行符和代码格式
chore: 更新项目配置中的appid和编译设置
This commit is contained in:
邹方成 2025-11-06 16:24:44 +08:00
parent 5fc7573af2
commit 1d4ab75441
11 changed files with 219 additions and 120 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -1 +1,20 @@
function _typeof(o){return module.exports=_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(o){return typeof o}:function(o){return o&&"function"==typeof Symbol&&o.constructor===Symbol&&o!==Symbol.prototype?"symbol":typeof o},_typeof(o)}module.exports=_typeof; function _typeof2(o) {
"@babel/helpers - typeof";
return (_typeof2 = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
return typeof o;
} : function(o) {
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
})(o);
}
function _typeof(o) {
return "function" == typeof Symbol && "symbol" === _typeof2(Symbol.iterator) ? module.exports = _typeof = function(o) {
return _typeof2(o);
} : module.exports = _typeof = function(o) {
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : _typeof2(o);
}, _typeof(o);
}
module.exports = _typeof;

View File

@ -1,5 +1,21 @@
const baseUrl = 'https://mini-chat.1024tool.vip/api/'; const baseUrl = 'https://mini-chat.1024tool.vip/api/'
// const baseUrl = 'https://dsjhd9s.tbmw.cn/api/' /**
* 发送 HTTP 请求到后端接口
*
* 功能描述
* - 封装 wx.request统一设置 JSON 请求头与基础地址
* - 成功时返回后端 res.data已由微信自动 JSON.parse
* - 业务错误res.data.code 存在统一弹 Toast reject
* - 网络错误或服务器异常走 fail 并直接 reject
*
* 参数说明
* @param {string} url - 相对接口路径例如 'app/send_message'
* @param {string} [method='GET'] - HTTP 方法支持 'GET''POST'
* @param {Object} [data={}] - 请求参数对象将作为请求体/查询参数发送
*
* 返回值
* @returns {Promise<any>} - 成功时 resolve 为后端返回的数据对象失败时 reject 错误信息
*/
function request(url, method = 'GET', data = {}) { function request(url, method = 'GET', data = {}) {
const header = { const header = {
@ -40,5 +56,5 @@ function request(url, method = 'GET', data = {}) {
}); });
} }
// 导出请求和服务地址CommonJS 兼容 // 导出请求函数CommonJS 导出,兼容微信小程序解析环境
module.exports = request; module.exports = request;

View File

@ -1,31 +1,106 @@
const request = require('./request.js')
const uploadFile = async (file, callback) => {
function uploadFile(filePath) { const policyData = await request('admin/policy_token', 'post')
return new Promise((resolve, reject) => { const res = JSON.parse(policyData.token)
if (!filePath) {
const err = new Error('uploadFile requires a filePath parameter');
console.error('上传失败:未提供文件路径', err);
return reject(err);
}
const fileName = file.tempFilePath.split('/').pop(); // hello.png
// const fileName = fileNameWithExt.split('.').slice(0, -1).join('.'); // hello
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({ wx.uploadFile({
filePath: filePath, // 图片临时文件路径(由调用方传入) url: 'https://image-fudan.oss-cn-beijing.aliyuncs.com/',
name: 'file', // 服务器接收文件的字段名,需与后端对应 method: 'put',
url: 'https://mini-chat.1024tool.vip/api/admin/upload/image', // 服务器接收图片的接口地址 filePath: file.tempFilePath,
success: (res) => { name: 'file', //固定值为file
const data = JSON.parse(res.data); formData: formData,
resolve('https://mini-chat.1024tool.vip/' + data.preview_image_url); 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) => { fail(err) {
console.error('上传失败', err); console.error('上传失败:', err); // 输出错误信息
reject(err); wx.showToast({
title: '上传失败,请重试!',
icon: 'none'
});
callback(err); // 调用回调处理错误
} }
}); });
})
} }
// 导出请求和服务地址CommonJS 兼容) const uploadFileToOSS = () => {
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;

16
app.js
View File

@ -1,10 +1,9 @@
Object.defineProperty(exports, Symbol.toStringTag, { Object.defineProperty(exports, Symbol.toStringTag, {
value: "Module" value: "Module"
});var common_vendor = require("./common/vendor.js");if (!Math) { });
"./pages/index/index.js"; var common_vendor = require("./common/vendor.js");
"./pages/index/detail.js";
"./pages/ming/index.js"; var _sfc_main = {
}var _sfc_main = {
onLaunch: function onLaunch() { onLaunch: function onLaunch() {
common_vendor.index.__f__("log", "at App.vue:4", "App Launch"); common_vendor.index.__f__("log", "at App.vue:4", "App Launch");
}, },
@ -14,9 +13,12 @@ Object.defineProperty(exports, Symbol.toStringTag, {
onHide: function onHide() { onHide: function onHide() {
common_vendor.index.__f__("log", "at App.vue:10", "App Hide"); common_vendor.index.__f__("log", "at App.vue:10", "App Hide");
} }
};function createApp() { };
function createApp() {
var app = common_vendor.createSSRApp(_sfc_main); var app = common_vendor.createSSRApp(_sfc_main);
return { return {
app: app app: app
}; };
}createApp().app.mount("#app");exports.createApp = createApp; }
createApp().app.mount("#app");
exports.createApp = createApp;

View File

@ -6320,9 +6320,14 @@ extend( /* @__PURE__ */Object.create(null), {
console.log = value; console.log = value;
return isWritable; return isWritable;
}function initRuntimeSocketService() { }function initRuntimeSocketService() {
var hosts = "127.0.0.1,192.168.1.88"; // 禁用开发模式下的日志 WebSocket 连接(按需关闭 ws 功能)
var port = "8090"; var hosts = "";
var id = "mp-weixin_s4ko-M"; var port = "";
var id = "";
// 若未配置 socket 参数,直接跳过所有处理,避免与开发者工具内部机制冲突
if (!hosts || !port || !id) {
return Promise.resolve(false);
}
var lazy = typeof swan !== "undefined"; var lazy = typeof swan !== "undefined";
var restoreError = lazy ? function () {} : initOnError(); var restoreError = lazy ? function () {} : initOnError();
var restoreConsole = lazy ? function () {} : rewriteConsole(); var restoreConsole = lazy ? function () {} : rewriteConsole();
@ -6333,13 +6338,9 @@ extend( /* @__PURE__ */Object.create(null), {
} }
return initRuntimeSocket(hosts, port, id).then(function (socket) { return initRuntimeSocket(hosts, port, id).then(function (socket) {
if (!socket) { if (!socket) {
// 禁用 ws 功能时,直接恢复错误与控制台并安静返回,不打印 ws 相关错误日志
restoreError(); restoreError();
restoreConsole(); restoreConsole();
originalConsole.error(wrapError("开发模式下日志通道建立 socket 连接失败。"));
{
originalConsole.error(wrapError("小程序平台,请勾选不校验合法域名配置。"));
}
originalConsole.error(wrapError("如果是运行到真机,请确认手机与电脑处于同一网络。"));
return false; return false;
} }
{ {

BIN
pages/.DS_Store vendored

Binary file not shown.

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,13 +169,10 @@ Page({
// 解析消息内容 // 解析消息内容
if (typeof item.content === 'string') { if (typeof item.content === 'string') {
try { try {
const text = JSON.parse(item.content) item.content = JSON.parse(item.content);
item.content = {
message: text.message || text.messages
};
} catch (e) { } catch (e) {
console.warn('消息内容解析失败:', item.content); console.warn('消息内容解析失败:', item.content);
item.content = { message: item.content }; item.content = { messages: item.content };
} }
} }
@ -409,7 +406,7 @@ Page({
const msg = { const msg = {
id, id,
content: { content: {
message: welcomeText messages: welcomeText
}, },
"msg_type": 1, "msg_type": 1,
receiver_id: '', receiver_id: '',
@ -436,7 +433,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({
message: welcomeText messages: welcomeText
}), }),
"from_user_id": userInfo.openid, "from_user_id": userInfo.openid,
"from_user_name": userInfo.user_name, "from_user_name": userInfo.user_name,
@ -473,7 +470,7 @@ Page({
const msg = { const msg = {
id, id,
content: { content: {
message: text messages: text
}, },
"msg_type": 1, "msg_type": 1,
receiver_id: '', receiver_id: '',
@ -495,7 +492,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({
message: text messages: text
}), }),
"from_user_id": userInfo.openid, "from_user_id": userInfo.openid,
"from_user_name": userInfo.user_name, "from_user_name": userInfo.user_name,
@ -553,7 +550,7 @@ Page({
msg_type: '2', msg_type: '2',
receiver_id: '', receiver_id: '',
content: { content: {
message: tempFilePaths[0] messages: tempFilePaths[0]
}, },
sender_id: userInfo.openid, sender_id: userInfo.openid,
sender_name: userInfo.user_name, sender_name: userInfo.user_name,
@ -570,41 +567,30 @@ Page({
scrollToId: id scrollToId: id
}); });
}, 50); }, 50);
uploadFile(tempFilePaths[0]).then((res) => {
wx.uploadFile({
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(); 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({
message: res messages: 'https://dsjhd9s.tbmw.cn/'+ data.preview_image_url
}), }),
"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({ },
title: '图片发送失败', fail: (err) => {
icon: 'none' console.error('上传失败', err);
}
}); });
});
}).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(() => {
@ -620,7 +606,7 @@ Page({
// _ts: replyNow // _ts: replyNow
// }; // };
// that.setData({ // that.setData({
// message: that.data.messages.concat(reply) // messages: that.data.messages.concat(reply)
// }); // });
// setTimeout(() => { // setTimeout(() => {
// that.setData({ // that.setData({

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.message}}</text> <text class="msg-text">{{item.content.messages}}</text>
</block> </block>
<block wx:elif="{{item.msg_type == 2}}"> <block wx:elif="{{item.msg_type == 2}}">
<image src="{{item.content.message}}" bindtap="previewImage" data-src="{{item.content.message}}" class="msg-image" mode="aspectFill" /> <image src="{{item.content.messages}}" bindtap="previewImage" data-src="{{item.content.messages}}" class="msg-image" mode="aspectFill" />
</block> </block>
</view> </view>

View File

@ -3,16 +3,16 @@
"__compileDebugInfo__": { "__compileDebugInfo__": {
"useSummer": true "useSummer": true
}, },
"appid": "wx26ad074017e1e63f", "appid": "wx75596d627154f6d0",
"libVersion": "3.10.3", "libVersion": "3.10.3",
"setting": { "setting": {
"es6": true, "es6": false,
"postcss": false, "postcss": false,
"compileWorklet": false, "compileWorklet": false,
"minified": false, "minified": false,
"uglifyFileName": false, "uglifyFileName": false,
"uploadWithSourceMap": true, "uploadWithSourceMap": true,
"enhance": true, "enhance": false,
"packNpmManually": false, "packNpmManually": false,
"packNpmRelationList": [], "packNpmRelationList": [],
"minifyWXSS": true, "minifyWXSS": true,