This commit is contained in:
张成
2026-02-28 10:38:28 +08:00
parent 1a011bcc01
commit dfd3119163
44 changed files with 449 additions and 13555 deletions

View File

@@ -460,5 +460,82 @@ module.exports = {
});
return ctx.fail('保存投递配置失败');
}
},
/**
* 根据设备SN码获取账号全部功能配置投递、沟通、活跃
*/
'POST /user/account-config/get': async (ctx) => {
try {
const body = ctx.getBody();
const { sn_code } = body;
if (!sn_code) return ctx.fail('请提供设备SN码');
const { pla_account } = await Framework.getModels();
const user = await pla_account.findOne({ where: { sn_code } });
if (!user) return ctx.fail('用户不存在');
const u = user.toJSON ? user.toJSON() : user;
return ctx.success({
deliver_config: u.deliver_config || null,
chat_strategy: u.chat_strategy || null,
active_actions: u.active_actions || null,
auto_chat: u.auto_chat != null ? !!u.auto_chat : false,
auto_active: u.auto_active != null ? !!u.auto_active : false
});
} catch (error) {
console.error('[获取账号配置失败]', error);
return ctx.fail('获取账号配置失败');
}
},
/**
* 保存账号功能配置可只传需要更新的部分deliver_config / chat_strategy / active_actions
*/
'POST /user/account-config/save': async (ctx) => {
try {
const body = ctx.getBody();
const { sn_code, deliver_config, chat_strategy, active_actions } = body;
if (!sn_code) return ctx.fail('请提供设备SN码');
const { pla_account } = await Framework.getModels();
const user = await pla_account.findOne({ where: { sn_code } });
if (!user) return ctx.fail('用户不存在');
const updateData = {};
if (deliver_config !== undefined) {
const original = user.deliver_config || {};
const deepMerge = (t, s) => {
const r = { ...t };
Object.keys(s).forEach(k => {
const sv = s[k], tv = t[k];
if (sv && typeof sv === 'object' && !Array.isArray(sv) && tv && typeof tv === 'object' && !Array.isArray(tv)) {
r[k] = deepMerge(tv, sv);
} else {
r[k] = sv;
}
});
return r;
};
updateData.deliver_config = deepMerge(original, deliver_config);
if (deliver_config.auto_deliver !== undefined) updateData.auto_deliver = deliver_config.auto_deliver ? 1 : 0;
else if (deliver_config.auto_delivery !== undefined) updateData.auto_deliver = deliver_config.auto_delivery ? 1 : 0;
}
if (chat_strategy !== undefined) {
updateData.chat_strategy = chat_strategy;
if (chat_strategy.auto_chat !== undefined) updateData.auto_chat = chat_strategy.auto_chat ? 1 : 0;
}
if (active_actions !== undefined) {
updateData.active_actions = active_actions;
if (active_actions.auto_active !== undefined) updateData.auto_active = active_actions.auto_active ? 1 : 0;
}
if (Object.keys(updateData).length === 0) return ctx.success({ message: '无更新' });
await pla_account.update(updateData, { where: { id: user.id } });
return ctx.success({ message: '配置保存成功' });
} catch (error) {
console.error('[保存账号配置失败]', error);
return ctx.fail('保存账号配置失败');
}
}
}