liufumingren 5ef99e3bed 1
2026-07-01 14:00:59 +08:00

173 lines
4.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* IM 相关服务
* @author 刘付明仁 2026-05-01 创建
* 返回通用格式:{ code: number, message: string, data: any }
* code: 200 表示成功,其他表示失败
* message: 错误或成功消息
* data: 具体数据内容,成功时通常包含请求结果,失败时可以为 null 或错误详情
*/
const Service = require('egg').Service;
class ImService extends Service {
async ping(data) {
const { requestId } = data || {};
return {
code: 200,
message: 'pong',
data: {
requestId,
},
};
}
async ssoCodeToId(data) {
const { code } = data || {};
const safeCode = String(code || '').trim();
if (!safeCode) {
return {
code: 400,
message: 'code不能为空',
data: null,
};
}
// 测试例外,如果code以"test-"开头,则直接返回模拟数据
if (safeCode.startsWith('test-')) {
return {
code: 200,
message: 'success',
data: {
employees_id: 4,
employees_name: '阿猫',
subject: {
id: 100000,
name: '测试仓',
type: 'warehouse',
},
avatar: '',
},
};
}
const cacheKey = `im:sso:code:${safeCode}`;
const cachedUser = await this.app.getCacheAsync(cacheKey);
if (!cachedUser || !cachedUser.employees_id || !cachedUser.employees_name || !cachedUser.subject) {
return {
code: 404,
message: 'code无效或已过期',
data: null,
};
}
await this.app.delCacheAsync(cacheKey);
const resultData = {
code: 200,
message: 'success',
data: {
employees_id: cachedUser.employees_id,
employees_name: cachedUser.employees_name,
subject: cachedUser.subject,
avatar: cachedUser.avatar,
},
};
console.log('SSO code converted to user info:', resultData);
return resultData;
}
/**
* 获取主体员工信息
* @param {Object} data 请求数据,包含 store_id
* @return {Promise<{code: number, message: string, data: Array}>} 响应数据,包含员工列表
*/
async getStoreEmployees(data) {
const { store_id } = data || {};
const safeStoreId = Number(store_id);
if (!safeStoreId) {
return {
code: 400,
message: 'store_id不能为空或无效',
data: null,
};
}
const store = await this.ctx.model.Store.findByPk(safeStoreId, {
attributes: [ 'id', 'company_name', 'project' ],
raw: true,
});
if (!store) {
return {
code: 404,
message: '主体不存在',
data: null,
};
}
const employees = await this.ctx.model.AccountStoreEmployee.findAll({
where: {
store_id: safeStoreId,
status: 1,
},
attributes: [ 'id', 'real_name' ],
order: [[ 'id', 'ASC' ]],
raw: true,
});
if (!employees || employees.length === 0) {
return {
code: 200,
message: 'success',
data: [],
};
}
const employeeIds = employees.map(item => String(item.id));
const avatarRefs = await this.ctx.model.FileReference.findAll({
where: {
business_type: 'avater',
business_id: employeeIds,
},
include: [{
model: this.ctx.model.File,
as: 'file',
attributes: [ 'file_url' ],
required: false,
}],
attributes: [ 'business_id', 'created_at' ],
order: [[ 'created_at', 'DESC' ]],
});
const avatarMap = new Map();
for (const ref of avatarRefs) {
const employeeId = String(ref.business_id || '').trim();
if (!employeeId || avatarMap.has(employeeId)) {
continue;
}
avatarMap.set(employeeId, ref.file && ref.file.file_url ? ref.file.file_url : '');
}
const subject = {
id: String(store.id),
name: String(store.company_name || ''),
type: String(store.project || ''),
};
const list = employees.map(item => {
const employeeId = String(item.id);
return {
employees_id: employeeId,
employees_name: String(item.real_name || ''),
subject,
avatar: avatarMap.get(employeeId) || '',
};
});
return {
code: 200,
message: 'success',
data: list,
};
}
}
module.exports = ImService;