Przeglądaj źródła

feat(trial): 试用时长可配置为 10 分钟 + 提示词可随时取回

- TRIAL_MINUTES 常量,测试期 10 分钟,上线改回 48*60
- 云函数 op:instruction:按账号重生成提示词,到期返回 expired
- 前端已试用账号自动拉回提示词,可一直复制
- 恢复 18182795512 账号用于测试

Co-Authored-By: Claude Code <noreply@anthropic.com>
Huccct 12 godzin temu
rodzic
commit
d4c39f879c

+ 79 - 1
cloud/functions/fmode-provisioning.js

@@ -230,7 +230,12 @@ var QUOTA = { trial: 1, starter: 1, advanced: 3, enterprise: 5 };
  * 时钟从**首次创建**开始,同名复用不重置;入门版(starter)不限时。
  * App 侧拦截口径见 container.guard.ts;xlosi3m46q 派生 trialExpired 供列表/会话页使用。
  */
-var TRIAL_MS = 48 * 3600 * 1000;
+/*
+ * 试用时长(2026-12-04 用户要求先缩短到 10 分钟做「到期是否真失效」的实测)。
+ * 🔴 测试完成后改回 48 小时:只需把 TRIAL_MINUTES 改成 48*60 即可(其余逻辑不用动)。
+ */
+var TRIAL_MINUTES = 10;
+var TRIAL_MS = TRIAL_MINUTES * 60 * 1000;
 
 /*
  * 付费时长(2026-12-03 用户裁决「时长折算=固定天数」):
@@ -836,6 +841,78 @@ function _failedStep(steps) {
 }
 
 // ---------- 主流程 ----------
+/**
+ * 重新取回接入提示词(2026-12-04)。
+ *
+ * ## 为什么需要
+ *
+ * 用户点试用拿到提示词后**没复制就刷新**,原实现提示词只存在内存里,
+ * 回来后页面上什么都没有 —— 只能干看着「已使用过试用」。用户要求:
+ * 已用过的状态要能**一直看到并复制提示词**。
+ *
+ * ## 做法
+ *
+ * 按账号找到其试用记录(或指定 agentId),用**当前记录上的凭证**重新生成提示词。
+ * 已到期则按「已到期」返回,由前端提示去续费(续费走 reissue 换新凭证)。
+ */
+async function _instruction(user, p) {
+  var Agent = Parse.Object.extend('FmodeAgent');
+  var rec = null;
+  var agentId = String((p && p.agentId) || '').trim();
+  if (agentId) {
+    try { rec = await new Parse.Query(Agent).equalTo('agentId', agentId).first({ useMasterKey: true }); } catch (e) { rec = null; }
+    if (!rec) { try { rec = await new Parse.Query(Agent).get(agentId, { useMasterKey: true }); } catch (e) { rec = null; } }
+  } else {
+    var q = new Parse.Query(Agent);
+    q.equalTo('user', user);
+    q.equalTo('planCode', 'trial');
+    q.notEqualTo('isDeleted', true);
+    q.descending('createdAt');
+    q.limit(1);
+    rec = await q.first({ useMasterKey: true });
+  }
+  if (!rec || rec.get('isDeleted') === true) return _fail(404, '还没有可用的接入记录', { reasonCode: 'NO_RECORD' });
+  var owner = rec.get('user');
+  if (!owner || owner.id !== user.id) return _fail(403, '只能查看本人名下的接入提示词');
+
+  var exp = rec.get('accessExpiresAt') || rec.get('trialEndsAt');
+  var expMs = exp ? new Date(exp).getTime() : 0;
+  var expired = expMs > 0 && expMs <= Date.now();
+  if (expired || rec.get('accessState') === 'expired') {
+    return _ok({
+      mode: 'expired',
+      agentId: rec.id,
+      name: rec.get('name') || '',
+      accessExpiresAt: expMs ? new Date(expMs).toISOString() : null,
+      message: '已到期,请续费后获取新的接入提示词',
+    });
+  }
+
+  var prov = null;
+  try { prov = await _callFn(FN_PROVISION, _sessionToken(), { agentId: rec.id }); } catch (e) { prov = null; }
+  if (!prov || !prov.agentMxId) return _fail(500, 'Matrix 身份不可用,请稍后重试', { reasonCode: 'MATRIX_UNAVAILABLE', retryable: true });
+
+  var derivedPass = await _agentMxPassword(rec.id);
+  var expiresAt = new Date(expMs || Date.now());
+  var expiresText = isFinite(expiresAt.getTime())
+    ? expiresAt.toISOString().slice(0, 16).replace('T', ' ') + '(UTC)'
+    : '请以容器设置中的到期时间为准';
+  var ownerMx = '@' + String(owner.id).toLowerCase() + ':fmode.cn';
+  var instruction = _buildSelfHostedInstruction(rec.id, rec.get('name') || '', prov, derivedPass,
+    rec.get('accessToken'), expiresText, ownerMx, CONTAINER_ONBOARDING_BUNDLE);
+
+  return _ok({
+    mode: 'onboarding',
+    restored: true,
+    instruction: instruction,
+    agentId: rec.id,
+    name: rec.get('name') || '',
+    accessExpiresAt: expMs ? new Date(expMs).toISOString() : null,
+    onboardingBundle: CONTAINER_ONBOARDING_BUNDLE,
+    matrixInfo: { agentMxId: prov.agentMxId, roomId: prov.roomId, homeserver: prov.homeserver },
+  });
+}
+
 
 /**
  * 再开通(2026-12-03 用户裁决「再开通生成新密钥 + 新提示词」)。
@@ -1450,6 +1527,7 @@ async function handler(request, response) {
     if (op === 'retry') return await _retry(user, p);
     if (op === 'list') return await _list(user);
     if (op === 'reissue') return await _reissue(user, p);
+    if (op === 'instruction') return await _instruction(user, p);
 
     return _fail(400, '未知 op:' + op);
   } catch (e) {

+ 8 - 0
docs/00-tracking/操作追踪.md

@@ -10664,3 +10664,11 @@ Call log:
   - 附带修复:有效期可能是 DB 的 Parse Date 而非 Date 实例,`toISOString()` 会抛错 —— 统一转 Date 再格式化;错误信息改为输出异常的所有可读字段。
   - 验证:试用返回 200 + 10018 字符提示词;同账号换名再试返回 403 `TRIAL_USED`;测试记录已清理。
   - 说明:控制台里的 `mailto:contact@fmode.cn` 是**企业版「联系客服」被自动触发**(浏览器/无头环境会预先触发 mailto),与该报错无关;建议后续改为按钮点击后再跳转。
+
+- 2026-12-04 02:55:00+08:00:**试用时长改为 10 分钟(测试用)+ 提示词持久化**
+  - 试用时长:`TRIAL_MINUTES = 10`(原 48 小时)。🔴 测试完成后改回:把该值改成 `48*60` 即可,其余逻辑不动。
+  - **提示词持久化**(用户反馈「没复制就刷新再也看不到」):
+    - 云函数新增 `op:'instruction'`:按账号(或 agentId)找到试用记录,用记录上的凭证**重新生成提示词**;已到期返回 `mode:'expired'` 提示续费。
+    - 前端:`FmodeApi.fetchProvisionInstruction` / `ProvisionService.loadInstruction`;两端套餐页在「已试用过且当前无提示词」时**自动拉回**,用户可一直复制。
+  - 账号:18182795512 **已恢复**(手机号还原、`isDeleted=false`,用户名改为 `user_18182795512` 以避开唯一索引冲突);名下容器/工单仍为已删状态,相当于全新账号。
+  - 验证:构建通过;`op:'instruction'` 接口就绪(测试账号无记录时正确返回 404 NO_RECORD)。

+ 10 - 0
src/app/core/api/fmode-api.ts

@@ -124,6 +124,16 @@ export abstract class FmodeApi {
   /** 重试同一任务(不新建记录) */
   abstract retryProvision(jobId: string): Promise<ProvisionStartResult>;
 
+  /**
+   * 取回接入提示词(2026-12-04):用户没复制就刷新时,重新进入页面也能看到并复制。
+   * `null` 表示该账号还没有可用记录;`expired` 表示已到期需续费。
+   */
+  abstract fetchProvisionInstruction(input?: { agentId?: string }): Promise<
+    | { mode: 'onboarding'; instruction: string; agentId: string; name?: string; accessExpiresAt?: string | null }
+    | { mode: 'expired'; agentId: string; name?: string; accessExpiresAt?: string | null; message?: string }
+    | null
+  >;
+
   /** 当前用户的开通任务列表 */
   abstract listProvisions(): Promise<ProvisionStatus[]>;
 

+ 6 - 0
src/app/core/api/mock-api.ts

@@ -172,6 +172,12 @@ export class MockApi extends FmodeApi {
     return { mode: 'provision', jobId, status: 'provisioning' };
   }
 
+  /** 演示态没有真实记录:直接返回 null,页面按「无提示词」处理 */
+  async fetchProvisionInstruction(): Promise<null> {
+    await this.delay();
+    return null;
+  }
+
   async listProvisions(): Promise<ProvisionStatus[]> {
     await this.delay();
     return Array.from(this.mockProvisionJobs.values());

+ 18 - 0
src/app/core/api/parse-api.ts

@@ -445,6 +445,24 @@ export class ParseApi extends FmodeApi {
     return d;
   }
 
+  /**
+   * 取回接入提示词(2026-12-04):用户没复制就刷新时,重新进入页面也要能看到并复制。
+   * 服务端按账号已有的试用记录重新生成,凭证与到期时间以记录为准。
+   */
+  async fetchProvisionInstruction(input: { agentId?: string } = {}): Promise<
+    | { mode: 'onboarding'; instruction: string; agentId: string; name?: string; accessExpiresAt?: string | null }
+    | { mode: 'expired'; agentId: string; name?: string; accessExpiresAt?: string | null; message?: string }
+    | null
+  > {
+    const d = await this.callFunction<{ code: number; mode?: string; instruction?: string; agentId?: string; name?: string; accessExpiresAt?: string | null; message?: string; mess?: string }>(
+      FN.provisioning,
+      { op: 'instruction', ...input },
+    );
+    if (d.code === 404) return null;
+    if (d.code !== 200) throw new Error(pickMessage(d as never) ?? `获取接入提示词失败(${d.code})`);
+    return d as never;
+  }
+
   async fetchProvisionStatus(jobId: string): Promise<ProvisionStatus> {
     const d = await this.callFunction<ProvisionStatus & { code: number; mess?: string }>(
       FN.provisioning,

+ 23 - 0
src/app/core/provision.service.ts

@@ -319,6 +319,29 @@ export class ProvisionService {
    * 与 `start()` 的区别是不写 pending、不置 starting(不会和真正的开通互相干扰),
    * 只把结果写进 `instruction()` 供容器设置页展示。已过期的试用由云函数拒绝。
    */
+  /**
+   * 取回已生成的接入提示词(2026-12-04)。
+   *
+   * 场景:用户点试用拿到提示词后**没复制就刷新/关闭**。原来提示词只在内存里,
+   * 回来页面什么都没有。现在按账号已有记录重新生成,保证「已试用过」这个状态
+   * 下用户**一直能看到并复制**提示词。
+   *
+   * @returns `onboarding` 正常;`expired` 已到期(由页面提示续费);`null` 无记录
+   */
+  async loadInstruction(agentId?: string): Promise<{ mode: 'onboarding' | 'expired'; instruction?: string | null } | null> {
+    try {
+      const d = await this.api.fetchProvisionInstruction(agentId ? { agentId } : {});
+      if (!d) return null;
+      if (d.mode === 'onboarding' && d.instruction) {
+        this._instruction.set(d.instruction);
+        return { mode: 'onboarding', instruction: d.instruction };
+      }
+      return { mode: 'expired' };
+    } catch {
+      return null;
+    }
+  }
+
   async reissueInstruction(agent: { name: string; planCode?: string }): Promise<string | null> {
     const planCode = agent.planCode === 'starter' ? 'starter' : 'trial';
     try {

+ 10 - 0
src/app/desktop/pages/plans/plans.ts

@@ -819,6 +819,16 @@ export class DkPlansPage implements OnInit {
 
   ngOnInit(): void {
     void this.provision.restorePending();
+
+    /*
+     * 已试用过的账号:自动把提示词拉回来(2026-12-04 用户反馈「没复制就刷新,再也看不到」)。
+     * 拉不到(无记录/网络失败)不提示错误,界面保持原样。
+     */
+    if (!this.provision.instruction()) {
+      void this.live.ensureLoaded().then(() => {
+        if (this.trialUsed() && !this.provision.instruction()) void this.provision.loadInstruction();
+      });
+    }
     /*
      * 配额预检与重名校验都依赖容器列表(2026-12-03):
      * /plans 是顶层路由不在 shell 内,没人替本页拉 live,

+ 10 - 0
src/app/mobile/pages/plans/plans.ts

@@ -753,6 +753,16 @@ export class MbPlansPage implements OnInit {
 
   ngOnInit(): void {
     void this.provision.restorePending();
+
+    /*
+     * 已试用过的账号:自动把提示词拉回来(2026-12-04 用户反馈「没复制就刷新,再也看不到」)。
+     * 拉不到(无记录/网络失败)不提示错误,界面保持原样。
+     */
+    if (!this.provision.instruction()) {
+      void this.live.ensureLoaded().then(() => {
+        if (this.trialUsed() && !this.provision.instruction()) void this.provision.loadInstruction();
+      });
+    }
     /*
      * 配额预检与重名校验都依赖容器列表(2026-12-03):
      * /plans 是顶层路由不在 shell 内,没人替本页拉 live,