- 迁移:inbound_orders / outbound_orders 增加 subsidiary_id 与索引,按仓库回填 - 单据货主由仓库决定:创建写入、改仓库同步,接口不接受直写 subsidiary_id - 入库单/出库单列表返回 subsidiary_name 并新增货主筛选;详情/编辑/删除/审核/ 推送/完成/取消/恢复/作废/驳回与明细接口统一加货主越界 403 - 出入库记录按仓库归属货主:列表与导出收窄、返回 subsidiary_id/subsidiary_name、 新增货主列与筛选 - 导出:入库单/出库单/出入库记录新增货主列并按所在公司权限收窄 - 新增 resolveAccountNameMap:列表/详情返回 creator_name/auditor_name/voider_name, 创建人展示员工名称(principal_users.name),不再回退成邮箱账号 - 前端:三个列表新增货主列与筛选,创建人/作废人展示改为员工名称 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
118 lines
4.3 KiB
JavaScript
118 lines
4.3 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 账号在「主体维度」下的资料(名称 / 头像)
|
||
*
|
||
* 口径(2026-09-14 按用户要求):同一账号在不同主体下可以有不同名称与头像,
|
||
* 数据落在主体账号关联表 `principal_users.name` / `principal_users.avatar`:
|
||
* - 推送单据的操作人取该关联表的名称(入库 entryOrder.operatorName、发货单 deliveryOrder.operatorName 等)
|
||
* - 页面右上角展示:有关联头像显示头像 → 无头像显示名称首字符 → 无名称显示账号(username)首字符
|
||
*
|
||
* 回退顺序(关联表未配置时保持旧行为,避免历史账号显示空白):
|
||
* name:principal_users.name → users.nickname → users.username
|
||
* avatar:principal_users.avatar → users.avatar
|
||
*/
|
||
|
||
/**
|
||
* 取账号在指定主体下的资料
|
||
* @param {Object} ctx egg context
|
||
* @param {Number|String} userId 用户ID
|
||
* @param {Number|String} principalId 主体ID(缺省时只取全局 users 字段)
|
||
* @return {Promise<{name: string, avatar: string}>} 名称与头像(可能为空串)
|
||
*/
|
||
async function resolveAccountProfile(ctx, userId, principalId) {
|
||
const id = Number(userId);
|
||
if (!id) return { name: '', avatar: '' };
|
||
|
||
let relation = null;
|
||
if (Number(principalId)) {
|
||
relation = await ctx.model.PrincipalUser.findOne({
|
||
where: { user_id: id, principal_id: Number(principalId) },
|
||
attributes: [ 'name', 'avatar' ],
|
||
});
|
||
}
|
||
|
||
let user = null;
|
||
if (!relation || !relation.name || !relation.avatar) {
|
||
user = await ctx.model.User.findByPk(id, { attributes: [ 'nickname', 'username', 'avatar' ] });
|
||
}
|
||
|
||
const name = (relation && relation.name)
|
||
|| (user && (user.nickname || user.username))
|
||
|| '';
|
||
const avatar = (relation && relation.avatar)
|
||
|| (user && user.avatar)
|
||
|| '';
|
||
|
||
return { name: String(name), avatar: String(avatar) };
|
||
}
|
||
|
||
/**
|
||
* 取账号在指定主体下的显示名称(推送单据操作人用)
|
||
* @param {Object} ctx egg context
|
||
* @param {Number|String} userId 用户ID
|
||
* @param {Number|String} principalId 主体ID
|
||
* @return {Promise<String>} 名称(兜底为空串)
|
||
*/
|
||
async function resolveAccountName(ctx, userId, principalId) {
|
||
const { name } = await resolveAccountProfile(ctx, userId, principalId);
|
||
return name;
|
||
}
|
||
|
||
/**
|
||
* 取展示用首字符:名称首字符(支持中文)→ 账号首字符
|
||
* @param {String} name 关联表名称
|
||
* @param {String} username 账号
|
||
* @return {String} 单个字符(都没有时返回空串)
|
||
*/
|
||
function firstCharOf(name, username) {
|
||
const source = String(name || '').trim() || String(username || '').trim();
|
||
if (!source) return '';
|
||
// 取首个「字」,避免把 emoji/代理对切一半
|
||
return Array.from(source)[0];
|
||
}
|
||
|
||
/**
|
||
* 批量取账号在指定主体下的显示名称(列表展示创建人 / 审核人 / 作废人用)
|
||
*
|
||
* 与 resolveAccountName 同口径:principal_users.name → users.nickname → users.username,
|
||
* 一次查询完成,避免逐行查询 N+1。
|
||
* @param {Object} ctx egg context
|
||
* @param {Array<Number|String>} userIds 用户ID列表(可含重复/空值)
|
||
* @param {Number|String} principalId 主体ID
|
||
* @return {Promise<Map<Number, String>>} userId → 名称
|
||
*/
|
||
async function resolveAccountNameMap(ctx, userIds, principalId) {
|
||
const ids = [ ...new Set((userIds || []).map(Number).filter(id => Number.isInteger(id) && id > 0)) ];
|
||
const map = new Map();
|
||
if (!ids.length) return map;
|
||
|
||
const Op = ctx.app.Sequelize.Op;
|
||
const relations = Number(principalId)
|
||
? await ctx.model.PrincipalUser.findAll({
|
||
where: { user_id: { [Op.in]: ids }, principal_id: Number(principalId) },
|
||
attributes: [ 'user_id', 'name' ],
|
||
raw: true,
|
||
})
|
||
: [];
|
||
for (const row of relations) {
|
||
if (row.name) map.set(Number(row.user_id), String(row.name));
|
||
}
|
||
|
||
const missing = ids.filter(id => !map.has(id));
|
||
if (missing.length) {
|
||
const users = await ctx.model.User.findAll({
|
||
where: { id: { [Op.in]: missing } },
|
||
attributes: [ 'id', 'nickname', 'username' ],
|
||
raw: true,
|
||
});
|
||
for (const user of users) {
|
||
const name = user.nickname || user.username;
|
||
if (name) map.set(Number(user.id), String(name));
|
||
}
|
||
}
|
||
return map;
|
||
}
|
||
|
||
module.exports = { resolveAccountProfile, resolveAccountName, resolveAccountNameMap, firstCharOf };
|