2026-04-17 19:21:57 +08:00

187 lines
4.2 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, '更新平台设置失败');
}
/**
* 删除平台设置。
*
* @param {Object} data 请求参数
* @return {Promise<Object>} 删除结果
*/
async delete(data = {}) {
const {
id,
setting_id: settingId,
settingId: camelSettingId,
...rest
} = data || {};
const payload = {
...rest,
id: id !== undefined ? id : (settingId !== undefined ? settingId : camelSettingId),
};
return await this.callSystemPlatform('delete', payload, '删除平台设置失败');
}
}
module.exports = platformService;