UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

178 lines 7.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WatchConditionHandler = void 0; const fs_1 = require("fs"); const base_handler_1 = require("./base.handler"); const watch_condition_service_1 = require("../services/watch-condition-service"); const watch_condition_1 = require("../types/watch-condition"); const errors_1 = require("../utils/errors"); class WatchConditionHandler extends base_handler_1.BaseHandler { constructor(authConfig, serviceConfig) { super(); this.watchConditionService = new watch_condition_service_1.WatchConditionServiceSdk(authConfig, serviceConfig); } async getWatchCondition(options) { return this.executeWithErrorHandling(async () => { this.validateGetOptions(options); const params = {}; if (options.channelId) { params.channelId = options.channelId; } const result = await this.watchConditionService.getWatchCondition(params); this.displayGetResult(result, options); }, 'watch-condition.get'); } async setWatchCondition(options) { return this.executeWithErrorHandling(async () => { const authSettings = this.validateAndBuildAuthSettings(options); const params = { authSettings, }; if (options.channelId) { params.channelId = options.channelId; } await this.watchConditionService.setWatchCondition(params); this.displaySetResult(options); }, 'watch-condition.set'); } validateGetOptions(options) { const errors = []; if (options.output && !['table', 'json'].includes(options.output)) { errors.push('输出格式必须是 "table" 或 "json"'); } if (errors.length > 0) { throw new errors_1.PolyVValidationError(errors.join(', '), 'options', options, 'validation_failed'); } } validateAndBuildAuthSettings(options) { if (options.configFile) { return this.loadAuthSettingsFromConfig(options.configFile); } return this.buildAuthSettingsFromOptions(options); } loadAuthSettingsFromConfig(configPath) { if (!(0, fs_1.existsSync)(configPath)) { throw new errors_1.PolyVValidationError('配置文件不存在', 'config', configPath, 'file_not_found'); } let configContent; try { configContent = (0, fs_1.readFileSync)(configPath, 'utf8'); } catch (error) { throw new errors_1.PolyVValidationError('无法读取配置文件', 'config', configPath, 'file_read_error'); } let config; try { config = JSON.parse(configContent); } catch { throw new errors_1.PolyVValidationError('配置文件 JSON 格式无效', 'config', configPath, 'invalid_json'); } if (!config.authSettings || !Array.isArray(config.authSettings)) { throw new errors_1.PolyVValidationError('配置文件必须包含 authSettings 字段', 'config', configPath, 'invalid_structure'); } return config.authSettings; } buildAuthSettingsFromOptions(options) { const errors = []; if (options.rank !== undefined && ![1, 2].includes(options.rank)) { errors.push('rank 必须是 1 (主要条件) 或 2 (次要条件)'); } if (options.authType !== undefined && !watch_condition_1.VALID_AUTH_TYPES.includes(options.authType)) { errors.push('无效的认证类型,支持的类型: none, code, pay, phone, info, custom, external, direct'); } if (options.enabled !== undefined && !['Y', 'N'].includes(options.enabled)) { errors.push('enabled 必须是 Y 或 N'); } if (options.output && !['table', 'json'].includes(options.output)) { errors.push('输出格式必须是 "table" 或 "json"'); } if (errors.length > 0) { throw new errors_1.PolyVValidationError(errors.join(', '), 'options', options, 'validation_failed'); } const authSetting = { rank: options.rank, }; if (options.enabled !== undefined) { authSetting.enabled = options.enabled; } if (options.authType !== undefined && options.authType !== 'none') { authSetting.authType = options.authType; } if (options.authCode !== undefined) { authSetting.authCode = options.authCode; } if (options.price !== undefined) { authSetting.payAmount = Math.round(options.price * 100); } return [authSetting]; } displayGetResult(result, options) { if (!result || result.length === 0) { this.displayInfo('未找到观看条件配置'); return; } const output = options.output || 'table'; if (output === 'json') { this.displayData(result, 'json'); } else { console.log(`找到 ${result.length} 条观看条件配置`); const tableData = result.map((item, index) => { const authType = item.authType; const rank = item.rank ?? (index === 0 ? 1 : 2); const row = { '条件级别': rank === 1 ? '主要 (1)' : '次要 (2)', '状态': item.enabled === 'Y' ? '启用 (Y)' : '禁用 (N)', '认证类型': (authType && watch_condition_1.AUTH_TYPE_LABELS[authType]) || authType || '-', }; const details = this.getAuthTypeDetails(item); row['详细配置'] = details; return row; }); if (tableData.length > 0) { this.displayAsTable(tableData); } } } getAuthTypeDetails(item) { switch (item.authType) { case 'code': return item.code ? `密码: ${item.code}` : '-'; case 'pay': return item.payAmount ? `价格: ${(item.payAmount / 100).toFixed(2)} 元` : '-'; case 'external': return item.externalUri ? `链接: ${this.truncate(item.externalUri, 30)}` : '-'; case 'custom': return item.customUri ? `链接: ${this.truncate(item.customUri, 30)}` : '-'; case 'info': return item.infoFields ? `字段数: ${item.infoFields.length}` : '-'; case 'direct': return item.directKey ? `Key: ${this.truncate(item.directKey, 20)}` : '-'; default: return '-'; } } displaySetResult(options) { const output = options.output || 'table'; if (output === 'json') { this.displayData({ success: true, channelId: options.channelId || 'global', }, 'json'); } else { const target = options.channelId ? `channel ${options.channelId}` : 'global settings'; this.displaySuccess(`Successfully updated watch condition for ${target}`); } } truncate(str, maxLength) { if (!str) return '-'; return str.length > maxLength ? str.substring(0, maxLength) + '...' : str; } } exports.WatchConditionHandler = WatchConditionHandler; //# sourceMappingURL=watch-condition.handler.js.map