{
"type": "page",
"pullRefresh": {
"disabled": true
},
"variables": [
{
"name": "chatMessages",
"type": "array",
"value": [] // 初始为空
}
],
"regions": [
"body"
],
"data": {
"chatMessages": [
{
"name": "助手",
"message": "您好,有什么事情可以帮助您?"
}
]
},
"asideResizor": false,
"onEvent": {
"init": {
"weight": 0,
"actions": [
{
"args": {},
"actionType": "custom",
"ignoreError": false,
"script": "// 从当前页面URL中提取基础信息,手动构造WebSocket地址\nconst protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; // 协议转换:https→wss,http→ws\nconst host = window.location.host; // 包含域名和端口(如:localhost:8080)\nconst contextPath = ''; // 若项目有上下文路径(如 /app),需手动添加,否则留空\nconst stompEndpoint = '/stomp'; // STOMP服务的端点路径\n\n// 拼接完整的WebSocket URL\nconst brokerURL = `${protocol}//${host}${contextPath}${stompEndpoint}`;\n\nconst stompClient = new StompJs.Client({\n brokerURL: brokerURL,\n onConnect: () => {\n console.log('STOMP连接成功');\n console.log(context.props)\n // 订阅消息主题\n stompClient.subscribe('/topic/hello', (response) => {\n context.props.data.chatMessages=[{\"name\":\"555\",\"message\":\"5555\"}]\n console.log(response.body)\n });\n },\n onStompError: (frame) => {\n console.error('STOMP错误: ' + frame.headers['message']);\n },\n reconnectDelay: 3000 // 自动重连延迟(毫秒)\n});\n\n// 保存到全局(关键:让后续操作能访问)\nwindow.stompClient = stompClient;\nstompClient.activate();\n// 监听消息\nstompClient.onmessage = function (event) {\nconsole.log('收到消息:', event.data);\n// 可通过 this.setState 更新 amis 页面数据\nthis.setState({ message: event.data });\n}.bind(this); // 绑定当前组件上下文,方便更新状态"
}
]
}
},
"id": 3079,
"body": [
{
"id": "u:3a4ee5d7decc",
"source": "$chatMessages",
"type": "cards",
"card": {
"body": [
{
"name": "name",
"label": "name",
"id": "u:0c7850ec5bef"
},
{
"name": "message",
"label": "message",
"id": "u:da07638c130d"
}
],
"isFixedWidth": true,
"style": {
"display": "block",
"width": "300px",
"overflowX": "visible",
"position": "static"
},
"id": "u:80a6edce35d6",
"actions": []
},
"placeholder": "暂无数据",
"columnsCount": 1
},
{
"labelAlign": "left",
"dsType": "api",
"style": {
"padding": "10px",
"borderTop": "1px solid [[eee]]"
},
"id": "u:a79eb8902249",
"type": "form",
"title": "",
"body": [
{
"minRows": 3,
"maxRows": 20,
"name": "message",
"id": "messageInput",
"label": false,
"placeholder": "请输入您的问题...",
"type": "textarea",
"rows": 2,
"required": true
}
],
"feat": "Insert",
"actions": [
{
"disabledOnAction": false,
"size": "xs",
"level": "primary",
"onEvent": {
"click": {
"actions": [
{
"args": {},
"actionType": "custom",
"script": "const messageInput = document.querySelector('textarea[name=\"message\"]');\nif (!messageInput) {\n console.error('未找到消息输入框');\n return;\n}\nconst message = messageInput.value.trim();\nconsole.log('获取到消息内容:', message);\nif (!message) {\n alert('请输入消息内容');\n return;\n}\n\nif (stompClient && stompClient.connected) {\n // 发送消息到/app/hello目的地\n stompClient.publish({\n destination: '/app/hello',\n body: JSON.stringify({\n content: message\n })\n })\n // 清空输入框并添加到聊天记录\n document.querySelector('textarea[name=\"message\"]').value = '';\n // addMessageToChat({content: content}, true);\n} else {\n showErrorMessage('WebSocket连接未建立,请稍后重试');\n}"
}
]
}
},
"label": "发送",
"id": "u:2d5143e1f69a",
"type": "submit"
}
],
"wrapWithPanel": true
}
]
}