菜单跳转

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': [ 'web.assets_backend': [
'yuthon_ai_agent/static/src/js/ai_chat_action.js', '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_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/js/ai_rule_test.js',
'yuthon_ai_agent/static/src/xml/ai_chat_template.xml', 'yuthon_ai_agent/static/src/xml/ai_chat_template.xml',
'yuthon_ai_agent/static/src/css/ai_chat.css', 'yuthon_ai_agent/static/src/css/ai_chat.css',
+7
View File
@@ -282,3 +282,10 @@ class AiRule(models.Model):
return conv.id 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 **/ /** @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;
/** /**
* 主动感知规则表单:测试按钮点击处理 * 主动感知测试按钮 patch
* 由 XML 里 onclick="window._testActiveGuide(this)" 调用 * 拦截 action_test_guide 按钮点击,调用 RPC 后打开浮动聊天框
*/ */
window._testActiveGuide = async function (btnEl) { patch(FormController.prototype, "yuthon_ai_agent/FormController", {
const formEl = btnEl.closest(".o_form_view"); setup() {
if (!formEl) return; this._super(...arguments);
this.notificationService = useService("notification");
this.orm = useService("orm");
},
// 从 URL 获取 record id async onButtonAction(clickEvent, record, node) {
const resId = (new URLSearchParams(window.location.search)).get("id"); const methodName = node.getAttribute("name");
if (!resId) { if (methodName === "action_test_guide") {
alert("无法获取规则 ID"); clickEvent.preventDefault();
return; clickEvent.stopPropagation();
}
btnEl.disabled = true; const buttonEl = clickEvent.currentTarget;
btnEl.textContent = "⏳ 测试中..."; buttonEl.disabled = true;
try { try {
const res = await jsonrpc("/ai/rule/test_guide", { rule_id: parseInt(resId) }); const result = await this.orm.call(
if (res.error) { record.resModel,
alert("错误: " + res.error); "action_test_guide",
} else { [record.resId]
window.dispatchEvent(new CustomEvent("yuthon:ai_open_chat", { );
detail: { forceOpen: true },
})); 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) { return this._super(...arguments);
alert("测试失败: " + e.message); },
} finally { });
btnEl.disabled = false;
}
};
+3 -3
View File
@@ -70,10 +70,10 @@
<page string="主动感知配置" name="active_guide" <page string="主动感知配置" name="active_guide"
invisible="rule_type != 'active_guide'"> invisible="rule_type != 'active_guide'">
<div class="oe_button_box" style="margin-bottom: 12px;"> <div class="oe_button_box" style="margin-bottom: 12px;">
<button name="action_test_guide" type="button" <button name="action_test_guide" type="object"
string="测试主动感知" string="测试主动感知"
class="btn btn-primary" class="btn btn-primary o_test_active_guide_btn"
onclick="this._testActiveGuide(this)"/> help="模拟触发此规则,查看引导内容和按钮效果"/>
</div> </div>
<group> <group>
<group string="引导内容"> <group string="引导内容">