226 lines
6.6 KiB
JavaScript
226 lines
6.6 KiB
JavaScript
/**
|
|
* 配置管理服务
|
|
* 统一处理账户配置的解析和验证
|
|
*/
|
|
class ConfigManager {
|
|
/**
|
|
* 解析 JSON 配置字符串
|
|
* @param {string|object} config - 配置字符串或对象
|
|
* @param {object} defaultValue - 默认值
|
|
* @returns {object} 解析后的配置对象
|
|
*/
|
|
static parseConfig(config, defaultValue = {}) {
|
|
if (!config) {
|
|
return defaultValue;
|
|
}
|
|
|
|
if (typeof config === 'object') {
|
|
return { ...defaultValue, ...config };
|
|
}
|
|
|
|
if (typeof config === 'string') {
|
|
try {
|
|
const parsed = JSON.parse(config);
|
|
return { ...defaultValue, ...parsed };
|
|
} catch (error) {
|
|
console.warn('[配置管理] JSON 解析失败:', error.message);
|
|
return defaultValue;
|
|
}
|
|
}
|
|
|
|
return defaultValue;
|
|
}
|
|
|
|
/**
|
|
* 解析投递配置
|
|
* @param {string|object} deliverConfig - 投递配置
|
|
* @returns {object} 标准化的投递配置
|
|
*/
|
|
static parseDeliverConfig(deliverConfig) {
|
|
const defaultConfig = {
|
|
deliver_interval: 30, // 投递间隔(分钟)
|
|
min_salary: 0, // 最低薪资
|
|
max_salary: 0, // 最高薪资
|
|
page_count: 3, // 搜索页数
|
|
max_deliver: 10, // 最大投递数
|
|
filter_keywords: [], // 过滤关键词
|
|
exclude_keywords: [], // 排除关键词
|
|
time_range: null, // 时间范围
|
|
priority_weights: null // 优先级权重
|
|
};
|
|
|
|
return this.parseConfig(deliverConfig, defaultConfig);
|
|
}
|
|
|
|
/**
|
|
* 解析搜索配置
|
|
* @param {string|object} searchConfig - 搜索配置
|
|
* @returns {object} 标准化的搜索配置
|
|
*/
|
|
static parseSearchConfig(searchConfig) {
|
|
const defaultConfig = {
|
|
search_interval: 60, // 搜索间隔(分钟)
|
|
page_count: 3, // 搜索页数
|
|
keywords: [], // 搜索关键词
|
|
exclude_keywords: [], // 排除关键词
|
|
time_range: null // 时间范围
|
|
};
|
|
|
|
return this.parseConfig(searchConfig, defaultConfig);
|
|
}
|
|
|
|
/**
|
|
* 解析沟通配置
|
|
* @param {string|object} chatStrategy - 沟通策略
|
|
* @returns {object} 标准化的沟通配置
|
|
*/
|
|
static parseChatStrategy(chatStrategy) {
|
|
const defaultConfig = {
|
|
chat_interval: 30, // 沟通间隔(分钟)
|
|
auto_reply: false, // 是否自动回复
|
|
reply_template: '', // 回复模板
|
|
time_range: null // 时间范围
|
|
};
|
|
|
|
return this.parseConfig(chatStrategy, defaultConfig);
|
|
}
|
|
|
|
/**
|
|
* 解析活跃配置
|
|
* @param {string|object} activeStrategy - 活跃策略
|
|
* @returns {object} 标准化的活跃配置
|
|
*/
|
|
static parseActiveStrategy(activeStrategy) {
|
|
const defaultConfig = {
|
|
active_interval: 120, // 活跃间隔(分钟)
|
|
actions: ['view_jobs'], // 活跃动作
|
|
time_range: null // 时间范围
|
|
};
|
|
|
|
return this.parseConfig(activeStrategy, defaultConfig);
|
|
}
|
|
|
|
/**
|
|
* 获取优先级权重配置
|
|
* @param {object} config - 投递配置
|
|
* @returns {object} 优先级权重
|
|
*/
|
|
static getPriorityWeights(config) {
|
|
const defaultWeights = {
|
|
salary: 0.4, // 薪资匹配度
|
|
keyword: 0.3, // 关键词匹配度
|
|
company: 0.2, // 公司活跃度
|
|
distance: 0.1 // 距离(未来)
|
|
};
|
|
|
|
if (!config.priority_weights) {
|
|
return defaultWeights;
|
|
}
|
|
|
|
return { ...defaultWeights, ...config.priority_weights };
|
|
}
|
|
|
|
/**
|
|
* 获取排除关键词列表
|
|
* @param {object} config - 配置对象
|
|
* @returns {string[]} 排除关键词数组
|
|
*/
|
|
static getExcludeKeywords(config) {
|
|
if (!config.exclude_keywords) {
|
|
return [];
|
|
}
|
|
|
|
if (Array.isArray(config.exclude_keywords)) {
|
|
return config.exclude_keywords.filter(k => k && k.trim());
|
|
}
|
|
|
|
if (typeof config.exclude_keywords === 'string') {
|
|
return config.exclude_keywords
|
|
.split(/[,,、]/)
|
|
.map(k => k.trim())
|
|
.filter(k => k);
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* 获取过滤关键词列表
|
|
* @param {object} config - 配置对象
|
|
* @returns {string[]} 过滤关键词数组
|
|
*/
|
|
static getFilterKeywords(config) {
|
|
if (!config.filter_keywords) {
|
|
return [];
|
|
}
|
|
|
|
if (Array.isArray(config.filter_keywords)) {
|
|
return config.filter_keywords.filter(k => k && k.trim());
|
|
}
|
|
|
|
if (typeof config.filter_keywords === 'string') {
|
|
return config.filter_keywords
|
|
.split(/[,,、]/)
|
|
.map(k => k.trim())
|
|
.filter(k => k);
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* 获取薪资范围
|
|
* @param {object} config - 配置对象
|
|
* @returns {{min: number, max: number}} 薪资范围
|
|
*/
|
|
static getSalaryRange(config) {
|
|
return {
|
|
min: parseInt(config.min_salary) || 0,
|
|
max: parseInt(config.max_salary) || 0
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 获取时间范围
|
|
* @param {object} config - 配置对象
|
|
* @returns {object|null} 时间范围配置
|
|
*/
|
|
static getTimeRange(config) {
|
|
return config.time_range || null;
|
|
}
|
|
|
|
/**
|
|
* 验证配置完整性
|
|
* @param {object} config - 配置对象
|
|
* @param {string[]} requiredFields - 必需字段
|
|
* @returns {{valid: boolean, missing?: string[]}} 验证结果
|
|
*/
|
|
static validateConfig(config, requiredFields = []) {
|
|
const missing = [];
|
|
|
|
for (const field of requiredFields) {
|
|
if (config[field] === undefined || config[field] === null) {
|
|
missing.push(field);
|
|
}
|
|
}
|
|
|
|
if (missing.length > 0) {
|
|
return { valid: false, missing };
|
|
}
|
|
|
|
return { valid: true };
|
|
}
|
|
|
|
/**
|
|
* 合并配置(用于覆盖默认配置)
|
|
* @param {object} defaultConfig - 默认配置
|
|
* @param {object} userConfig - 用户配置
|
|
* @returns {object} 合并后的配置
|
|
*/
|
|
static mergeConfig(defaultConfig, userConfig) {
|
|
return { ...defaultConfig, ...userConfig };
|
|
}
|
|
}
|
|
|
|
module.exports = ConfigManager;
|