419 lines
12 KiB
JavaScript
419 lines
12 KiB
JavaScript
'use strict';
|
|
|
|
const Service = require('egg').Service;
|
|
|
|
const RESERVED_EMPLOYEE_NAME = '测试工号保留';
|
|
|
|
class SettingAccountService extends Service {
|
|
createError(message, code = 500) {
|
|
const error = new Error(message);
|
|
error.code = code;
|
|
return error;
|
|
}
|
|
|
|
getCurrentStoreId() {
|
|
const storeId = Number(this.ctx.user && this.ctx.user.store_id);
|
|
if (!Number.isFinite(storeId) || storeId <= 0) {
|
|
throw this.createError('未识别到当前主体,无法操作工号', 400);
|
|
}
|
|
return storeId;
|
|
}
|
|
|
|
getCurrentEmployeeId() {
|
|
const employeeId = Number(this.ctx.user && this.ctx.user.employees_id);
|
|
if (!Number.isFinite(employeeId) || employeeId <= 0) {
|
|
return null;
|
|
}
|
|
return employeeId;
|
|
}
|
|
|
|
getWmsClient() {
|
|
return this.ctx.app.mysql.get('wms');
|
|
}
|
|
|
|
normalizePhone(phone) {
|
|
return typeof phone === 'string' ? phone.trim() : '';
|
|
}
|
|
|
|
isValidPhone(phone) {
|
|
return /^1[3-9]\d{9}$/.test(phone);
|
|
}
|
|
|
|
normalizeName(name) {
|
|
return typeof name === 'string' ? name.trim() : '';
|
|
}
|
|
|
|
normalizeUsername(username) {
|
|
return typeof username === 'string' ? username.trim() : '';
|
|
}
|
|
|
|
generateUsername() {
|
|
const suffix = `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;
|
|
return `acct_${suffix}`;
|
|
}
|
|
|
|
async findRawAccountByPhone(phone) {
|
|
const rows = await this.getWmsClient().select('wms_account', {
|
|
where: { phone },
|
|
columns: [ 'id', 'username', 'phone', 'email', 'real_name', 'status' ],
|
|
limit: 1,
|
|
});
|
|
return rows && rows.length > 0 ? rows[0] : null;
|
|
}
|
|
|
|
async findRawAccountByUsername(username) {
|
|
const rows = await this.getWmsClient().select('wms_account', {
|
|
where: { username },
|
|
columns: [ 'id', 'username', 'phone', 'email', 'real_name', 'status' ],
|
|
limit: 1,
|
|
});
|
|
return rows && rows.length > 0 ? rows[0] : null;
|
|
}
|
|
|
|
async getAvatarMeta(employeeId) {
|
|
const { ctx } = this;
|
|
const avatarRef = await ctx.model.FileReference.findOne({
|
|
where: {
|
|
business_type: 'avater',
|
|
business_id: String(employeeId),
|
|
},
|
|
include: [
|
|
{
|
|
model: ctx.model.File,
|
|
as: 'file',
|
|
attributes: [ 'id', 'file_url', 'file_name' ],
|
|
required: false,
|
|
},
|
|
],
|
|
order: [[ 'created_at', 'DESC' ]],
|
|
});
|
|
|
|
return {
|
|
avatar_file_id: Number(avatarRef && avatarRef.file_id ? avatarRef.file_id : 0) || null,
|
|
avatar_url: avatarRef && avatarRef.file ? avatarRef.file.file_url : '',
|
|
avatar_name: avatarRef && avatarRef.file ? avatarRef.file.file_name : '',
|
|
};
|
|
}
|
|
|
|
async resolveRoleId(data = {}) {
|
|
const { ctx } = this;
|
|
const explicitRoleId = Number(data.role_id || data.roleId || 0);
|
|
if (Number.isFinite(explicitRoleId) && explicitRoleId > 0) {
|
|
const role = await ctx.model.Role.findOne({
|
|
where: {
|
|
id: explicitRoleId,
|
|
status: 1,
|
|
},
|
|
});
|
|
if (!role) {
|
|
throw this.createError('指定角色不存在或已禁用', 400);
|
|
}
|
|
return explicitRoleId;
|
|
}
|
|
|
|
const currentEmployeeId = this.getCurrentEmployeeId();
|
|
if (!currentEmployeeId) {
|
|
throw this.createError('未识别到当前操作人工号,无法继承角色', 400);
|
|
}
|
|
|
|
const currentRole = await ctx.model.EmployeeRole.findOne({
|
|
where: { employee_id: currentEmployeeId },
|
|
order: [[ 'id', 'ASC' ]],
|
|
});
|
|
if (!currentRole) {
|
|
throw this.createError('当前操作人工号未绑定角色,无法创建新工号', 400);
|
|
}
|
|
|
|
return Number(currentRole.role_id);
|
|
}
|
|
|
|
async formatEmployeeRow(employee) {
|
|
const account = await this.getWmsClient().get('wms_account', {
|
|
id: employee.account_id,
|
|
});
|
|
const avatar = await this.getAvatarMeta(employee.id);
|
|
|
|
return {
|
|
employee_id: Number(employee.id),
|
|
account_id: Number(employee.account_id),
|
|
store_id: Number(employee.store_id),
|
|
real_name: employee.real_name || '',
|
|
status: Number(employee.status),
|
|
created_at: employee.created_at,
|
|
updated_at: employee.updated_at,
|
|
username: account && account.username ? account.username : '',
|
|
phone: account && account.phone ? account.phone : '',
|
|
email: account && account.email ? account.email : '',
|
|
account_real_name: account && account.real_name ? account.real_name : '',
|
|
...avatar,
|
|
};
|
|
}
|
|
|
|
async list(data = {}) {
|
|
const { ctx } = this;
|
|
|
|
const storeId = this.getCurrentStoreId();
|
|
const statusFilter = data.includeDisabled === true
|
|
? undefined
|
|
: (data.status === 0 || data.status === '0' ? 0 : 1);
|
|
|
|
const where = {
|
|
store_id: storeId,
|
|
};
|
|
if (statusFilter === 0 || statusFilter === 1) {
|
|
where.status = statusFilter;
|
|
}
|
|
|
|
const employees = await ctx.model.AccountStoreEmployee.findAll({
|
|
where,
|
|
order: [[ 'created_at', 'DESC' ], [ 'id', 'DESC' ]],
|
|
});
|
|
|
|
const list = [];
|
|
for (const employee of employees) {
|
|
list.push(await this.formatEmployeeRow(employee));
|
|
}
|
|
|
|
return {
|
|
code: 200,
|
|
data: list,
|
|
};
|
|
}
|
|
|
|
async add(data = {}) {
|
|
const { ctx, service } = this;
|
|
let createdAccountId = 0;
|
|
|
|
try {
|
|
const storeId = this.getCurrentStoreId();
|
|
const phone = this.normalizePhone(data.phone);
|
|
const realName = this.normalizeName(data.real_name || data.realName);
|
|
const password = typeof data.password === 'string' && data.password.trim()
|
|
? data.password.trim()
|
|
: '111111';
|
|
|
|
if (!phone) {
|
|
throw this.createError('phone 参数不能为空', 400);
|
|
}
|
|
if (!this.isValidPhone(phone)) {
|
|
throw this.createError('phone 格式不正确', 400);
|
|
}
|
|
if (!realName) {
|
|
throw this.createError('real_name 参数不能为空', 400);
|
|
}
|
|
if (realName === RESERVED_EMPLOYEE_NAME) {
|
|
throw this.createError(`新增工号禁止直接使用保留名称 ${RESERVED_EMPLOYEE_NAME}`, 400);
|
|
}
|
|
|
|
const roleId = await this.resolveRoleId(data);
|
|
let account = await this.findRawAccountByPhone(phone);
|
|
|
|
if (account && Number(account.status) !== 1) {
|
|
throw this.createError('该手机号对应账号已禁用,无法新增工号', 409);
|
|
}
|
|
|
|
if (!account) {
|
|
let username = this.normalizeUsername(data.username);
|
|
if (!username) {
|
|
username = this.generateUsername();
|
|
}
|
|
|
|
const existingUsername = await this.findRawAccountByUsername(username);
|
|
if (existingUsername) {
|
|
throw this.createError('username 已存在', 409);
|
|
}
|
|
|
|
const salt = service.systems.auth.user.generateSalt();
|
|
const passwordHash = await service.systems.auth.user.encryptPassword(password, salt);
|
|
const email = typeof data.email === 'string' && data.email.trim()
|
|
? data.email.trim()
|
|
: `${username}@autotest.local`;
|
|
|
|
const insertRet = await this.getWmsClient().insert('wms_account', {
|
|
username,
|
|
phone,
|
|
email,
|
|
password_hash: passwordHash,
|
|
real_name: realName,
|
|
status: 1,
|
|
salt,
|
|
created_at: new Date(),
|
|
updated_at: new Date(),
|
|
});
|
|
|
|
createdAccountId = Number(insertRet && insertRet.insertId ? insertRet.insertId : 0);
|
|
account = await this.getWmsClient().get('wms_account', { id: createdAccountId });
|
|
}
|
|
|
|
const activeBinding = await ctx.model.AccountStoreEmployee.findOne({
|
|
where: {
|
|
account_id: account.id,
|
|
store_id: storeId,
|
|
status: 1,
|
|
},
|
|
});
|
|
if (activeBinding) {
|
|
throw this.createError('该手机号已绑定当前主体工号', 409);
|
|
}
|
|
|
|
const { employee, employeeRole } = await service.systems.store.ensureEmployeeRoleBinding({
|
|
accountId: Number(account.id),
|
|
storeId,
|
|
roleId,
|
|
realName,
|
|
});
|
|
|
|
await service.systems.auth.user.clearUserCache(Number(account.id));
|
|
|
|
const formatted = await this.formatEmployeeRow(employee);
|
|
return {
|
|
code: 200,
|
|
data: {
|
|
...formatted,
|
|
role_id: Number(employeeRole.role_id),
|
|
is_new_account: createdAccountId > 0,
|
|
},
|
|
};
|
|
} catch (error) {
|
|
if (createdAccountId > 0) {
|
|
await this.getWmsClient().delete('wms_account', { id: createdAccountId });
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async delete(data = {}) {
|
|
const { ctx, service } = this;
|
|
|
|
const storeId = this.getCurrentStoreId();
|
|
const currentEmployeeId = this.getCurrentEmployeeId();
|
|
const employeeId = Number(data.employee_id || data.id || 0);
|
|
|
|
if (!employeeId) {
|
|
throw this.createError('employee_id 参数不能为空', 400);
|
|
}
|
|
if (currentEmployeeId && employeeId === currentEmployeeId) {
|
|
throw this.createError('不允许删除当前登录工号', 400);
|
|
}
|
|
|
|
const employee = await ctx.model.AccountStoreEmployee.findOne({
|
|
where: {
|
|
id: employeeId,
|
|
store_id: storeId,
|
|
},
|
|
});
|
|
if (!employee) {
|
|
throw this.createError('工号不存在或无权限删除', 404);
|
|
}
|
|
|
|
if (Number(employee.status) !== 1) {
|
|
return {
|
|
code: 200,
|
|
data: {
|
|
employee_id: employeeId,
|
|
account_id: Number(employee.account_id),
|
|
status: 'deleted',
|
|
already_deleted: true,
|
|
},
|
|
};
|
|
}
|
|
|
|
await employee.update({
|
|
status: 0,
|
|
updated_at: new Date(),
|
|
});
|
|
|
|
await ctx.model.AccountPlatformActive.destroy({
|
|
where: {
|
|
employee_id: employeeId,
|
|
},
|
|
});
|
|
|
|
await service.systems.auth.user.clearUserCache(Number(employee.account_id));
|
|
|
|
return {
|
|
code: 200,
|
|
data: {
|
|
employee_id: employeeId,
|
|
account_id: Number(employee.account_id),
|
|
status: 'deleted',
|
|
},
|
|
};
|
|
}
|
|
|
|
async update(data = {}) {
|
|
const { ctx, service } = this;
|
|
|
|
const storeId = this.getCurrentStoreId();
|
|
const employeeId = Number(data.employee_id || data.id || 0);
|
|
const hasRealName = Object.prototype.hasOwnProperty.call(data, 'real_name') || Object.prototype.hasOwnProperty.call(data, 'realName');
|
|
const hasFileId = Object.prototype.hasOwnProperty.call(data, 'file_id') || Object.prototype.hasOwnProperty.call(data, 'avatar_file_id') || Object.prototype.hasOwnProperty.call(data, 'fileId');
|
|
const realName = this.normalizeName(data.real_name || data.realName);
|
|
const fileId = Number(data.file_id || data.avatar_file_id || data.fileId || 0);
|
|
|
|
if (!employeeId) {
|
|
throw this.createError('employee_id 参数不能为空', 400);
|
|
}
|
|
if (!hasRealName && !hasFileId) {
|
|
throw this.createError('至少需要更新 real_name 或 file_id', 400);
|
|
}
|
|
if (hasRealName && !realName) {
|
|
throw this.createError('real_name 参数不能为空', 400);
|
|
}
|
|
if (hasFileId && !fileId) {
|
|
throw this.createError('file_id 参数不能为空', 400);
|
|
}
|
|
|
|
const employee = await ctx.model.AccountStoreEmployee.findOne({
|
|
where: {
|
|
id: employeeId,
|
|
store_id: storeId,
|
|
status: 1,
|
|
},
|
|
});
|
|
if (!employee) {
|
|
throw this.createError('工号不存在或无权限更新', 404);
|
|
}
|
|
|
|
if (hasRealName && realName !== employee.real_name) {
|
|
await employee.update({
|
|
real_name: realName,
|
|
updated_at: new Date(),
|
|
});
|
|
}
|
|
|
|
if (hasFileId) {
|
|
const file = await ctx.model.File.findOne({
|
|
where: {
|
|
id: fileId,
|
|
is_deleted: false,
|
|
},
|
|
});
|
|
if (!file) {
|
|
throw this.createError('头像文件不存在', 404);
|
|
}
|
|
|
|
await service.systems.file.ReferenceFile({
|
|
id: employeeId,
|
|
type: 'avater',
|
|
images: [
|
|
{
|
|
file_id: fileId,
|
|
sort_order: 0,
|
|
},
|
|
],
|
|
});
|
|
}
|
|
|
|
await service.systems.auth.user.clearUserCache(Number(employee.account_id));
|
|
|
|
const refreshed = await ctx.model.AccountStoreEmployee.findByPk(employeeId);
|
|
return {
|
|
code: 200,
|
|
data: await this.formatEmployeeRow(refreshed),
|
|
};
|
|
}
|
|
}
|
|
|
|
module.exports = SettingAccountService;
|