| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- const { readQiweiAuthToken, readQiweiApiBase } = require('../core/credentials');
- const { callFmodeWecomGateway, redactSecret } = require('../providers/fmode-wecom-gateway');
- const { okResult, errorResult } = require('../core/result-envelope');
- function authRequiredResult() {
- return {
- status: 'needs_auth',
- assistantMessage: '还没有找到 Fmode 鉴权 token,无法调用由 Fmode 网关转发的企业微信接口。请配置 QIWEI_AUTH_TOKEN、FMODE_API_KEY 或平台 sessionToken 后重试。',
- summary: { errorKind: 'missing_auth_token', recoverable: true },
- data: {},
- files: [],
- nextActions: ['配置 Fmode 鉴权 token 后重试'],
- warnings: [],
- errors: []
- };
- }
- function subscriptionError(error, stage) {
- const safeMessage = redactSecret(error && (error.bizMessage || error.message));
- const kind = String((error && error.kind) || 'upstream');
- if (kind === 'auth') return authRequiredResult();
- if (kind === 'billing') {
- return {
- status: 'needs_recharge',
- assistantMessage: `飞马余额不足或订阅扣费失败(${safeMessage})。请充值后重新调用 qiwei_subscribe。`,
- summary: { stage, errorKind: kind, recoverable: true },
- data: {},
- files: [],
- nextActions: ['充值飞马余额', '重新调用 qiwei_subscribe'],
- warnings: [],
- errors: []
- };
- }
- return errorResult(`企微订阅操作失败:${safeMessage}`, {
- summary: { stage, errorKind: kind },
- nextActions: ['稍后重试', '若持续失败,请检查 Fmode 网关的企业微信接口状态']
- });
- }
- async function qiweiSubscriptionStatus(input = {}) {
- const token = readQiweiAuthToken(input);
- if (!token) return authRequiredResult();
- try {
- const result = await callFmodeWecomGateway({
- gatewayPath: '/subscribe/status',
- httpMethod: 'GET',
- token,
- apiBase: readQiweiApiBase(input),
- cacheBust: true
- });
- const data = result.data || {};
- return okResult({
- assistantMessage: data.subscribed
- ? `企微包月订阅有效:${data.usedSeats ?? 0}/${data.seats ?? 0} 个席位已使用,到期时间 ${data.expireAt || '未知'}。`
- : '企微包月订阅尚未开通或已到期,请调用 qiwei_subscribe 开通或续费。',
- summary: {
- subscribed: Boolean(data.subscribed),
- seats: data.seats ?? null,
- usedSeats: data.usedSeats ?? null,
- expireAt: data.expireAt || null,
- autoRenew: data.autoRenew ?? null,
- balance: data.balance
- },
- data,
- nextActions: data.subscribed ? [] : ['调用 qiwei_subscribe 开通或续费']
- });
- } catch (error) {
- return subscriptionError(error, 'subscriptionStatus');
- }
- }
- async function qiweiSubscribe(input = {}) {
- const token = readQiweiAuthToken(input);
- if (!token) return authRequiredResult();
- const seats = input.seats === undefined ? undefined : Number(input.seats);
- const months = input.months === undefined ? 1 : Number(input.months);
- if (seats !== undefined && (!Number.isInteger(seats) || seats < 1)) {
- return errorResult('seats 必须是大于等于 1 的整数。');
- }
- if (!Number.isInteger(months) || months < 1 || months > 12) {
- return errorResult('months 必须是 1-12 的整数。');
- }
- try {
- const result = await callFmodeWecomGateway({
- gatewayPath: '/subscribe',
- body: { ...(seats === undefined ? {} : { seats }), months },
- token,
- apiBase: readQiweiApiBase(input)
- });
- const data = result.data || {};
- return okResult({
- assistantMessage: `企微包月订阅已开通或续费:${data.seats ?? seats ?? 1} 个席位,购买 ${data.months ?? months} 个月,到期时间 ${data.expireAt || '未知'}。`,
- summary: {
- subscribed: true,
- seats: data.seats ?? seats ?? 1,
- months: data.months ?? months,
- amount: data.amount ?? null,
- expireAt: data.expireAt || null,
- autoRenew: data.autoRenew ?? null
- },
- data,
- nextActions: ['调用 qiwei_login_start 添加或重新登录企业微信账号']
- });
- } catch (error) {
- return subscriptionError(error, 'subscribe');
- }
- }
- async function qiweiSubscriptionAutoRenew(input = {}) {
- const token = readQiweiAuthToken(input);
- if (!token) return authRequiredResult();
- if (typeof input.autoRenew !== 'boolean') return errorResult('autoRenew 必须是 boolean。');
- try {
- const result = await callFmodeWecomGateway({
- gatewayPath: '/subscribe/autoRenew',
- body: { autoRenew: input.autoRenew },
- token,
- apiBase: readQiweiApiBase(input)
- });
- const data = result.data || {};
- return okResult({
- assistantMessage: `企微订阅自动续费已${data.autoRenew ? '开启' : '关闭'}。`,
- summary: { autoRenew: Boolean(data.autoRenew) },
- data
- });
- } catch (error) {
- return subscriptionError(error, 'subscriptionAutoRenew');
- }
- }
- module.exports = {
- qiweiSubscriptionStatus,
- qiweiSubscribe,
- qiweiSubscriptionAutoRenew
- };
|