2026-04-27 16:09:26 +08:00

238 lines
5.3 KiB
JavaScript

'use strict';
const MainService = require('../../main');
class platformService extends MainService {
/**
* 初始化平台设置模块的权限配置。
*
* @param {Egg.Context} ctx 上下文对象
*/
constructor(ctx) {
super(ctx);
const _noNeedLogin = '';
const _noNeedRight = 'list,create,update,delete';
const _noNeedLog = '*';
const _noNeedAuth = 'list,create,update,delete';
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
}
/**
* 调用平台设置系统服务。
*
* @param {string} methodName 系统服务方法名
* @param {Object} data 请求参数
* @param {string} defaultMessage 默认错误信息
* @return {Promise<Object>} 统一返回结果
*/
async callSystemPlatform(methodName, data, defaultMessage) {
const { service } = this;
try {
const result = await service.systems.setting.platform[methodName](data);
return this.success('ok', result);
} catch (error) {
return this.fail(error.message || defaultMessage, {
code: Number(error.code) || 500,
});
}
}
/**
* 获取平台设置列表。
*
* @param {Object} data 请求参数
* @return {Promise<Object>} 列表结果
*/
async list(data = {}) {
const {
where = {},
pagination: inputPagination,
page,
page_size: pageSize,
pageSize: camelPageSize,
...rest
} = data || {};
const pagination = {
...(inputPagination && typeof inputPagination === 'object' ? inputPagination : {}),
};
if (page !== undefined) {
pagination.page = page;
}
const resolvedPageSize = pageSize !== undefined ? pageSize : camelPageSize;
if (resolvedPageSize !== undefined) {
pagination.pageSize = resolvedPageSize;
}
const payload = {
...rest,
where,
pagination,
};
this.prepareWhereContext(payload, {
specialFieldMap: {
id: {
field: 'id',
},
code: {
field: 'code',
},
name: {
field: 'name',
},
platform_code: {
field: 'platform_code',
},
quickWhere: {
bucket: 'quick',
},
},
});
return await this.callSystemPlatform('list', payload, '获取平台设置列表失败');
}
/**
* 创建平台设置。
*
* @param {Object} data 请求参数
* @return {Promise<Object>} 创建结果
*/
async create(data = {}) {
const {
code,
name,
platform_code: platformCode,
platformCode: camelPlatformCode,
...rest
} = data || {};
const payload = {
...rest,
code,
name,
platform_code: platformCode !== undefined ? platformCode : camelPlatformCode,
};
return await this.callSystemPlatform('create', payload, '创建平台设置失败');
}
/**
* 修改平台设置。
*
* @param {Object} data 请求参数
* @return {Promise<Object>} 修改结果
*/
async update(data = {}) {
const {
id,
setting_id: settingId,
settingId: camelSettingId,
code,
name,
platform_code: platformCode,
platformCode: camelPlatformCode,
...rest
} = data || {};
const payload = {
...rest,
};
payload.id = id !== undefined ? id : (settingId !== undefined ? settingId : camelSettingId);
if (code !== undefined) {
payload.code = code;
}
if (name !== undefined) {
payload.name = name;
}
if (platformCode !== undefined || camelPlatformCode !== undefined) {
payload.platform_code = platformCode !== undefined ? platformCode : camelPlatformCode;
}
return await this.callSystemPlatform('update', payload, '更新平台设置失败');
}
/**
* 归一化删除 ID 列表。
*
* 优先读取 `ids`,其次兼容单个 `id`、`setting_id`、`settingId`。
*
* @param {Object} data 请求参数
* @return {Array<number>} 删除 ID 列表
*/
resolveDeleteIds(data = {}) {
const {
ids,
id,
setting_id: settingId,
settingId: camelSettingId,
} = data || {};
const rawIds = ids !== undefined
? ids
: (id !== undefined ? id : (settingId !== undefined ? settingId : camelSettingId));
const values = Array.isArray(rawIds)
? rawIds
: String(rawIds || '')
.split(',')
.map(item => item.trim())
.filter(Boolean);
const normalizedIds = [];
const seenIds = new Set();
for (const value of values) {
const normalized = Number(value);
if (!Number.isInteger(normalized) || normalized <= 0 || seenIds.has(normalized)) {
continue;
}
seenIds.add(normalized);
normalizedIds.push(normalized);
}
return normalizedIds;
}
/**
* 删除平台设置。
*
* @param {Object} data 请求参数
* @return {Promise<Object>} 删除结果
*/
async delete(data = {}) {
const {
ids,
id,
setting_id: settingId,
settingId: camelSettingId,
...rest
} = data || {};
const normalizedIds = this.resolveDeleteIds({
ids,
id,
setting_id: settingId,
settingId: camelSettingId,
});
const payload = {
...rest,
ids: normalizedIds,
};
return await this.callSystemPlatform('delete', payload, '删除平台设置失败');
}
}
module.exports = platformService;