UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

333 lines 17.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CardPushHandler = void 0; const base_handler_1 = require("./base.handler"); const errors_1 = require("../utils/errors"); const card_push_service_1 = require("../services/card-push-service"); const api_command_1 = require("../utils/api-command"); class CardPushHandler extends base_handler_1.BaseHandler { constructor(authConfig, serviceConfig) { super(); this.cardPushService = new card_push_service_1.CardPushServiceSdk(authConfig, serviceConfig); } async listCardPushes(options) { const format = options.output || 'table'; this.validateListOptions(options); let cardPushes; try { cardPushes = await this.cardPushService.listCardPushes(options.channelId); } catch (error) { const msg = error instanceof Error ? error.message : String(error); if (/存在非法频道ID|illegal channelId/i.test(msg)) { throw new Error(`频道 ${options.channelId} 在卡片推送接口中被判定为不可访问(后端:${msg})。\n` + `可能原因:该频道/账号不在卡片推送的归属校验范围内(与 \`channel get\` 的可见范围不同)。\n` + `建议:确认当前账号(appId/userId)拥有该频道的卡片推送权限,或换用支持该频道的账号。`); } throw error; } if (format === 'json') { this.displayData(cardPushes, 'json'); } else { this.displayCardPushesTable(cardPushes); } } async createCardPush(options) { const format = options.output || 'table'; this.validateCreateOptions(options); const createdCardPush = await this.cardPushService.createCardPush({ channelId: options.channelId, cardType: options.cardType, imageType: options.imageType, title: options.title, link: options.link, duration: options.duration, durationPosition: options.durationPosition, showCondition: options.showCondition, conditionValue: options.conditionValue, conditionUnit: options.conditionUnit, countdownMsg: options.countdownMsg, enterEnabled: options.enterEnabled, linkEnabled: options.linkEnabled, redirectType: options.redirectType, }); if (format === 'json') { this.displayData(createdCardPush, 'json'); } else { this.displayCreatedCardPushTable(createdCardPush); } } async updateCardPush(options) { const format = options.output || 'table'; this.validateUpdateOptions(options); await this.cardPushService.updateCardPush({ channelId: options.channelId, cardPushId: options.cardPushId, cardType: options.cardType, imageType: options.imageType, title: options.title, link: options.link, duration: options.duration, durationPosition: options.durationPosition, showCondition: options.showCondition, conditionValue: options.conditionValue, conditionUnit: options.conditionUnit, countdownMsg: options.countdownMsg, enterEnabled: options.enterEnabled, linkEnabled: options.linkEnabled, redirectType: options.redirectType, }); const successMsg = 'Card-push updated successfully (卡片推送更新成功)'; if (format === 'json') { this.displayData({ success: true, message: successMsg }, 'json'); } else { console.log(successMsg); } } async pushCard(options) { const format = options.output || 'table'; this.validatePushOptions(options); await this.cardPushService.pushCard({ channelId: options.channelId, cardPushId: options.cardPushId, }); const successMsg = 'Card pushed successfully (卡片推送成功)'; if (format === 'json') { this.displayData({ success: true, message: successMsg }, 'json'); } else { console.log(successMsg); } } async cancelPush(options) { const format = options.output || 'table'; this.validateCancelOptions(options); await this.cardPushService.cancelPush({ channelId: options.channelId, cardPushId: options.cardPushId, }); const successMsg = 'Card push cancelled successfully (卡片推送已取消)'; if (format === 'json') { this.displayData({ success: true, message: successMsg }, 'json'); } else { console.log(successMsg); } } async deleteCardPush(options) { const format = options.output || 'table'; this.validateDeleteOptions(options); await this.cardPushService.deleteCardPush({ channelId: options.channelId, cardPushId: options.cardPushId, }); const successMsg = 'Card-push deleted successfully (卡片推送已删除)'; if (format === 'json') { this.displayData({ success: true, message: successMsg }, 'json'); } else { console.log(successMsg); } } async getShare(options) { const format = options.output || 'table'; this.validateListOptions(options); const result = await this.cardPushService.getShare(options.channelId); this.displayData(result, format); } async updateShare(options) { const format = options.output || 'table'; this.validateShareUpdateOptions(options); await (0, api_command_1.confirmWrite)(options.force, `Update share settings for channel ${options.channelId}?`); const result = await this.cardPushService.updateShare(options); if (format === 'json') { this.displayData({ success: true, data: result }, 'json'); } else { this.displaySuccess('Share settings updated successfully', result, 'table'); } } validateListOptions(options) { if (!options.channelId || options.channelId.trim() === '') { throw new errors_1.PolyVValidationError('channelId is required (频道ID是必需的)', 'channelId', options.channelId, 'required'); } if (options.output && options.output !== 'table' && options.output !== 'json') { throw new errors_1.PolyVValidationError('output format must be table or json (输出格式必须是 table 或 json)', 'output', options.output, 'invalid_format'); } } validateCreateOptions(options) { if (!options.channelId || options.channelId.trim() === '') { throw new errors_1.PolyVValidationError('channelId is required (频道ID是必需的)', 'channelId', options.channelId, 'required'); } if (!options.imageType) { throw new errors_1.PolyVValidationError('imageType is required (卡片样式类型是必需的)', 'imageType', options.imageType, 'required'); } const validImageTypes = ['giftbox', 'redpack', 'custom', 'weixinWork']; if (!validImageTypes.includes(options.imageType)) { throw new errors_1.PolyVValidationError(`imageType must be one of: ${validImageTypes.join(', ')} (卡片样式类型必须是: ${validImageTypes.join(', ')})`, 'imageType', options.imageType, 'invalid_value'); } if (!options.title || options.title.trim() === '') { throw new errors_1.PolyVValidationError('title is required (卡片标题是必需的)', 'title', options.title, 'required'); } if (options.title.length > 16) { throw new errors_1.PolyVValidationError('title cannot exceed 16 characters (卡片标题不能超过16个字符)', 'title', options.title, 'invalid_length'); } if (!options.link || options.link.trim() === '') { throw new errors_1.PolyVValidationError('link is required (卡片链接是必需的)', 'link', options.link, 'required'); } if (!options.link.startsWith('http://') && !options.link.startsWith('https://')) { throw new errors_1.PolyVValidationError('link must start with http:// or https:// (卡片链接必须以 http:// 或 https:// 开头)', 'link', options.link, 'invalid_format'); } if (options.duration === undefined || options.duration === null) { throw new errors_1.PolyVValidationError('duration is required (卡片倒计时时长是必需的)', 'duration', options.duration, 'required'); } const validDurations = [0, 5, 10, 20, 30]; if (!validDurations.includes(options.duration)) { throw new errors_1.PolyVValidationError(`duration must be one of: ${validDurations.join(', ')} (卡片倒计时时长必须是: ${validDurations.join(', ')})`, 'duration', options.duration, 'invalid_value'); } if (!options.showCondition) { throw new errors_1.PolyVValidationError('showCondition is required (弹出方式是必需的)', 'showCondition', options.showCondition, 'required'); } const validShowConditions = ['PUSH', 'WATCH']; if (!validShowConditions.includes(options.showCondition)) { throw new errors_1.PolyVValidationError(`showCondition must be one of: ${validShowConditions.join(', ')} (弹出方式必须是: ${validShowConditions.join(', ')})`, 'showCondition', options.showCondition, 'invalid_value'); } if (options.showCondition === 'WATCH') { if (!options.conditionUnit) { throw new errors_1.PolyVValidationError('conditionUnit is required when showCondition is WATCH (弹出方式为WATCH时观看时长单位是必需的)', 'conditionUnit', options.conditionUnit, 'required'); } const validConditionUnits = ['SECONDS', 'MINUTES']; if (!validConditionUnits.includes(options.conditionUnit)) { throw new errors_1.PolyVValidationError(`conditionUnit must be one of: ${validConditionUnits.join(', ')} (观看时长单位必须是: ${validConditionUnits.join(', ')})`, 'conditionUnit', options.conditionUnit, 'invalid_value'); } } if (options.countdownMsg && options.countdownMsg.length > 8) { throw new errors_1.PolyVValidationError('countdownMsg cannot exceed 8 characters (倒计时文案不能超过8个字符)', 'countdownMsg', options.countdownMsg, 'invalid_length'); } } validateUpdateOptions(options) { if (!options.channelId || options.channelId.trim() === '') { throw new errors_1.PolyVValidationError('channelId is required (频道ID是必需的)', 'channelId', options.channelId, 'required'); } if (!options.cardPushId || options.cardPushId.trim() === '') { throw new errors_1.PolyVValidationError('cardPushId is required (卡片推送ID是必需的)', 'cardPushId', options.cardPushId, 'required'); } if (options.title && options.title.length > 16) { throw new errors_1.PolyVValidationError('title cannot exceed 16 characters (卡片标题不能超过16个字符)', 'title', options.title, 'invalid_length'); } if (options.link && !options.link.startsWith('http://') && !options.link.startsWith('https://')) { throw new errors_1.PolyVValidationError('link must start with http:// or https:// (卡片链接必须以 http:// 或 https:// 开头)', 'link', options.link, 'invalid_format'); } if (options.duration !== undefined) { const validDurations = [0, 5, 10, 20, 30]; if (!validDurations.includes(options.duration)) { throw new errors_1.PolyVValidationError(`duration must be one of: ${validDurations.join(', ')} (卡片倒计时时长必须是: ${validDurations.join(', ')})`, 'duration', options.duration, 'invalid_value'); } } if (options.showCondition) { const validShowConditions = ['PUSH', 'WATCH']; if (!validShowConditions.includes(options.showCondition)) { throw new errors_1.PolyVValidationError(`showCondition must be one of: ${validShowConditions.join(', ')} (弹出方式必须是: ${validShowConditions.join(', ')})`, 'showCondition', options.showCondition, 'invalid_value'); } } if (options.showCondition === 'WATCH' && options.conditionUnit) { const validConditionUnits = ['SECONDS', 'MINUTES']; if (!validConditionUnits.includes(options.conditionUnit)) { throw new errors_1.PolyVValidationError(`conditionUnit must be one of: ${validConditionUnits.join(', ')} (观看时长单位必须是: ${validConditionUnits.join(', ')})`, 'conditionUnit', options.conditionUnit, 'invalid_value'); } } if (options.countdownMsg && options.countdownMsg.length > 8) { throw new errors_1.PolyVValidationError('countdownMsg cannot exceed 8 characters (倒计时文案不能超过8个字符)', 'countdownMsg', options.countdownMsg, 'invalid_length'); } } validatePushOptions(options) { if (!options.channelId || options.channelId.trim() === '') { throw new errors_1.PolyVValidationError('channelId is required (频道ID是必需的)', 'channelId', options.channelId, 'required'); } if (!options.cardPushId || options.cardPushId.trim() === '') { throw new errors_1.PolyVValidationError('cardPushId is required (卡片推送ID是必需的)', 'cardPushId', options.cardPushId, 'required'); } } validateCancelOptions(options) { if (!options.channelId || options.channelId.trim() === '') { throw new errors_1.PolyVValidationError('channelId is required (频道ID是必需的)', 'channelId', options.channelId, 'required'); } if (!options.cardPushId || options.cardPushId.trim() === '') { throw new errors_1.PolyVValidationError('cardPushId is required (卡片推送ID是必需的)', 'cardPushId', options.cardPushId, 'required'); } } validateDeleteOptions(options) { if (!options.channelId || options.channelId.trim() === '') { throw new errors_1.PolyVValidationError('channelId is required (频道ID是必需的)', 'channelId', options.channelId, 'required'); } if (!options.cardPushId || options.cardPushId.trim() === '') { throw new errors_1.PolyVValidationError('cardPushId is required (卡片推送ID是必需的)', 'cardPushId', options.cardPushId, 'required'); } } validateShareUpdateOptions(options) { this.validateListOptions(options); if (options.shareBtnEnable !== 'Y' && options.shareBtnEnable !== 'N') { throw new errors_1.PolyVValidationError('shareBtnEnable must be Y or N', 'shareBtnEnable', options.shareBtnEnable, 'invalid_value'); } if (!options.titleType || options.titleType.trim() === '') { throw new errors_1.PolyVValidationError('titleType is required', 'titleType', options.titleType, 'required'); } for (const field of ['weixinShareCustomUrlWithParamEnabled', 'webShareCustomUrlWithParamEnabled']) { const value = options[field]; if (value !== undefined && value !== 'Y' && value !== 'N') { throw new errors_1.PolyVValidationError(`${field} must be Y or N`, field, value, 'invalid_value'); } } } displayCardPushesTable(cardPushes) { if (cardPushes.length === 0) { console.log('No card-pushes found (未找到卡片推送)'); return; } const tableData = cardPushes.map((cardPush) => { return { '卡片推送ID': cardPush.id, '卡片标题': cardPush.title, '卡片样式': cardPush.imageType, '推送状态': this.getPushStatusText(cardPush.pushStatus), '弹出方式': cardPush.showCondition, '创建时间': this.formatTimestamp(cardPush.createdTime), }; }); this.displayAsTable(tableData); } displayCreatedCardPushTable(cardPush) { const tableData = [ { '卡片推送ID': cardPush.id, '卡片标题': cardPush.title, '卡片样式': cardPush.imageType, '弹出方式': cardPush.showCondition, '创建时间': this.formatTimestamp(cardPush.createdTime), }, ]; this.displayAsTable(tableData); } getPushStatusText(status) { const statusMap = { Y: '推送中', N: '未推送', L: '上次推送', }; return statusMap[status]; } formatTimestamp(timestamp) { const date = new Date(timestamp); const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); const hours = String(date.getHours()).padStart(2, '0'); const minutes = String(date.getMinutes()).padStart(2, '0'); return `${year}-${month}-${day} ${hours}:${minutes}`; } } exports.CardPushHandler = CardPushHandler; //# sourceMappingURL=card-push.handler.js.map