菜单跳转

This commit is contained in:
李鹏宇
2026-07-02 11:29:18 +08:00
parent 5adf1077d9
commit d22433162c
4 changed files with 66 additions and 32 deletions
-1
View File
@@ -37,7 +37,6 @@
'web.assets_backend': [
'yuthon_ai_agent/static/src/js/ai_chat_action.js',
'yuthon_ai_agent/static/src/js/ai_floating.js',
'yuthon_ai_agent/static/src/js/ai_chat_open.js',
'yuthon_ai_agent/static/src/js/ai_rule_test.js',
'yuthon_ai_agent/static/src/xml/ai_chat_template.xml',
'yuthon_ai_agent/static/src/css/ai_chat.css',
+7
View File
@@ -282,3 +282,10 @@ class AiRule(models.Model):
return conv.id
def action_test_guide(self):
"""按钮调用:创建测试消息,返回 conv_id 供 JS 打开聊天框"""
try:
conv_id = self._do_test_guide()
return {'conv_id': conv_id, 'message': '测试消息已创建,请查看聊天框'}
except UserError as e:
return {'error': str(e)}
+56 -28
View File
@@ -1,37 +1,65 @@
/** @odoo-module **/
import { jsonrpc } from "@web/core/network/rpc_service";
import { patch } from "@web/core/utils/patch";
import { FormController } from "@web/views/form/form_controller";
import { _t } from "@web/core/l10n/translation";
import { useService } from "@web/core/utils/hooks";
const { onWillStart } = owl;
/**
* 主动感知规则表单:测试按钮点击处理
* 由 XML 里 onclick="window._testActiveGuide(this)" 调用
* 主动感知测试按钮 patch
* 拦截 action_test_guide 按钮点击,调用 RPC 后打开浮动聊天框
*/
window._testActiveGuide = async function (btnEl) {
const formEl = btnEl.closest(".o_form_view");
if (!formEl) return;
patch(FormController.prototype, "yuthon_ai_agent/FormController", {
setup() {
this._super(...arguments);
this.notificationService = useService("notification");
this.orm = useService("orm");
},
// 从 URL 获取 record id
const resId = (new URLSearchParams(window.location.search)).get("id");
if (!resId) {
alert("无法获取规则 ID");
return;
}
async onButtonAction(clickEvent, record, node) {
const methodName = node.getAttribute("name");
if (methodName === "action_test_guide") {
clickEvent.preventDefault();
clickEvent.stopPropagation();
btnEl.disabled = true;
btnEl.textContent = "⏳ 测试中...";
const buttonEl = clickEvent.currentTarget;
buttonEl.disabled = true;
try {
const res = await jsonrpc("/ai/rule/test_guide", { rule_id: parseInt(resId) });
if (res.error) {
alert("错误: " + res.error);
} else {
window.dispatchEvent(new CustomEvent("yuthon:ai_open_chat", {
detail: { forceOpen: true },
}));
try {
const result = await this.orm.call(
record.resModel,
"action_test_guide",
[record.resId]
);
if (result && result.conv_id) {
this.notificationService.add(
result.message || _t("测试消息已创建,请查看聊天框"),
{ type: "success" }
);
// 派发事件,通知浮动聊天框打开
window.dispatchEvent(
new CustomEvent("yuthon:ai_open_chat", {
detail: { conversation_id: result.conv_id },
})
);
} else if (result && result.error) {
this.notificationService.add(result.error, {
type: "danger",
});
}
} catch (e) {
this.notificationService.add(
_t("测试失败:") + e.message,
{ type: "danger" }
);
} finally {
buttonEl.disabled = false;
}
return;
}
} catch (e) {
alert("测试失败: " + e.message);
} finally {
btnEl.disabled = false;
}
};
return this._super(...arguments);
},
});
+3 -3
View File
@@ -70,10 +70,10 @@
<page string="主动感知配置" name="active_guide"
invisible="rule_type != 'active_guide'">
<div class="oe_button_box" style="margin-bottom: 12px;">
<button name="action_test_guide" type="button"
<button name="action_test_guide" type="object"
string="测试主动感知"
class="btn btn-primary"
onclick="this._testActiveGuide(this)"/>
class="btn btn-primary o_test_active_guide_btn"
help="模拟触发此规则,查看引导内容和按钮效果"/>
</div>
<group>
<group string="引导内容">