菜单跳转

This commit is contained in:
李鹏宇
2026-07-02 11:35:05 +08:00
parent 0c622a149d
commit d08f13cc39
+77 -50
View File
@@ -1,65 +1,92 @@
/** @odoo-module **/
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
* 拦截 action_test_guide 按钮点击,调用 RPC 后打开浮动聊天框
* 主动感知测试按钮
* 用法:<button type="button" onclick="window._testActiveGuide(this)"/>
*/
patch(FormController.prototype, "yuthon_ai_agent/FormController", {
setup() {
this._super(...arguments);
this.notificationService = useService("notification");
this.orm = useService("orm");
},
async onButtonAction(clickEvent, record, node) {
const methodName = node.getAttribute("name");
if (methodName === "action_test_guide") {
clickEvent.preventDefault();
clickEvent.stopPropagation();
(function () {
"use strict";
const buttonEl = clickEvent.currentTarget;
buttonEl.disabled = true;
window._testActiveGuide = function (btnEl) {
// 从 URL hash 获取当前 model 和 id
const hash = window.location.hash || "";
const modelMatch = hash.match(/model=([^&;]+)/);
const idMatch = hash.match(/id=(\d+)/);
if (!modelMatch || !idMatch) {
alert("请在表单页面点击此按钮");
return;
}
const resModel = decodeURIComponent(modelMatch[1]);
const recordId = parseInt(idMatch[1]);
try {
const result = await this.orm.call(
record.resModel,
"action_test_guide",
[record.resId]
);
btnEl.disabled = true;
const origText = btnEl.textContent;
btnEl.textContent = "测试中...";
// 用 Odoo 自带的 session rpc
const session = window.odoo?.session;
const rpc = session?.rpc || window.odoo?.__DEBUG__?.services["@web/core/network/rpc"];
if (!rpc) {
// fallback: 直接用 form 提交(传统方式)
_testActiveGuideFallback(btnEl, origText);
return;
}
rpc("/web/dataset/call_button", {
model: resModel,
method: "action_test_guide",
args: [[recordId]],
kwargs: {},
})
.then(function (result) {
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 },
})
);
showToast(result.message || "测试消息已创建");
} else if (result && result.error) {
this.notificationService.add(result.error, {
type: "danger",
});
alert(result.error);
} else if (result && result.warning) {
alert(result.warning.message);
}
} catch (e) {
this.notificationService.add(
_t("测试失败:") + e.message,
{ type: "danger" }
);
} finally {
buttonEl.disabled = false;
}
return;
})
.catch(function (err) {
alert("测试失败:" + (err.message || JSON.stringify(err)));
})
.finally(function () {
btnEl.disabled = false;
btnEl.textContent = origText;
});
};
function showToast(msg) {
var el = document.createElement("div");
el.style.cssText =
"position:fixed;top:20px;right:20px;z-index:9999;" +
"background:#28a745;color:#fff;padding:12px 20px;border-radius:6px;" +
"box-shadow:0 4px 12px rgba(0,0,0,.3);font-size:14px;";
el.textContent = msg;
document.body.appendChild(el);
setTimeout(function () {
el.remove();
}, 3000);
}
function _testActiveGuideFallback(btnEl, origText) {
// 传统 Odoo form 提交方式
var $form = btnEl.closest("form");
if ($form) {
// 设置按钮 name 让 form 提交时调用对应方法
btnEl.setAttribute("name", "action_test_guide");
btnEl.setAttribute("type", "submit");
// 实际上这个方法不可靠,还是提示用户
alert("无法初始化 RPC,请刷新页面后重试");
}
return this._super(...arguments);
},
});
btnEl.disabled = false;
btnEl.textContent = origText;
}
console.log("[yuthon_ai_agent] ai_rule_test.js loaded");
})();