Skip to content

许可与角标 ​

@virt-table-pro/license✅ 已发布
许可产品名
license
样式
import '@virt-table-pro/license/index.css'
微应用尚未挂载。

其余付费包都 peer 依赖它。它做两件事:把授权范围如实解析出来,以及超范围时明确说出原因 (一行 console.warn + 右下角角标)。

模型与理由(为什么只 warn、为什么密钥不是安全边界)在 许可与授权, 这一页只讲怎么用。

快速开始 ​

密钥是模块级单例,整个应用设一次就够,页面上开十张表也只 warn 一次。

ts
// main.ts —— 应用启动时设一次
import { setLicenseKey } from '@virt-table-pro/license';
import '@virt-table-pro/license/index.css'; // 角标样式,忘了引就只有 console.warn

setLicenseKey(import.meta.env.VITE_VT_LICENSE);

之后任何付费插件都自动吃这份密钥,不需要逐个传:

ts
import { VirtTable } from '@virt-table/vanilla';
import { vtViews } from '@virt-table-pro/views';
import { vtUndo } from '@virt-table-pro/undo';

new VirtTable(el, { list: rows, columns, itemKey: 'id', estimatedSize: 40, plugins: [vtViews(), vtUndo()] });

或者写在 plugins 里 ​

喜欢把配置集中在表格 options 里的用 vtLicense,效果与 setLicenseKey 完全等价,选一种即可:

ts
import { vtLicense } from '@virt-table-pro/license';

const table = new VirtTable(el, {
  list: rows,
  columns,
  itemKey: 'id',
  estimatedSize: 40,
  plugins: [vtLicense({ key: import.meta.env.VITE_VT_LICENSE }), vtViews()],
});

table.getLicenseStatus(); // { valid: true, payload: { sub, exp, products, ... } }

装载顺序无所谓 —— 密钥后设也对先装的插件生效(角标下次装载即消失,或手动调 refreshLicenseBadge())。

vtLicense 是可选的

它自己既不校验也不挂角标(只装它而没装任何付费插件时完全无痕),校验是各付费插件在 setup 里调 guardProduct() 做的。它的用途只有两个:顺手在 plugins 里设密钥,以及把许可状态透到实例上。

拿到密钥之后想做点自己的提示 ​

getLicenseStatus() 把 payload 原样给出来,「许可 30 天内到期」这类提示自己做:

ts
import { getLicenseStatus } from '@virt-table-pro/license';

const status = getLicenseStatus();
if (status.valid && status.payload) {
  const daysLeft = Math.ceil((Date.parse(status.payload.exp) - Date.now()) / 86400000);
  if (daysLeft < 30) toast(`virt-table Pro 许可将在 ${daysLeft} 天后到期`);
} else {
  console.info('许可无效:', status.reason, status.message);
}

登录后才从服务端拿到密钥的场景,随时再设一次即可(会清掉缓存与告警记录):

ts
const { licenseKey } = await fetch('/api/me').then((r) => r.json());
setLicenseKey(licenseKey);
table.refreshLicenseBadge(); // 去掉已经挂上的角标

API ​

模块级函数 ​

函数签名说明
setLicenseKey(key: string | null | undefined) => void设密钥。可随时调,会清掉校验缓存与「已告警」记录
getLicenseKey() => string | null当前密钥(已 trim;空串归一成 null)
getLicenseStatus() => LicenseResult当前状态,含 payload。结果按 key 缓存
assertLicensed(product: string) => boolean检查某产品是否授权,未授权时 warn 一次并返回 false。不抛异常,纯逻辑,可跑在服务端
guardProduct(ctx: PluginContext, product: string) => booleanassertLicensed + 挂角标。付费插件在 setup 开头调它
clearLicenseBadge(rootEl: HTMLElement) => void移除某张表的角标
verifyLicense(key, opts?: { host?, now? }) => LicenseResult无状态校验,测试与服务端自检用
coversProduct(result: LicenseResult, product: string) => boolean某产品是否在这份结果的授权范围内

实例 API(装了 vtLicense 才有) ​

方法说明
getLicenseStatus()同上,透到实例上方便在组件里取
refreshLicenseBadge()换了新密钥后去掉角标(角标不会自己走)

密钥长什么样 ​

VTP1.<base64url(payload)>.<checksum>

payload 字段:

字段类型说明
substring客户标识(公司名或订单号)。只用于展示与排障
productsstring[] | '*'授权的产品名,与包名后半段一致(views / undo / formula / gantt…);'*' = 全部
domainsstring[]?授权域名,支持 *.example.com(同时覆盖裸域 example.com)。缺省或空数组 = 不限域名
expstring到期日 YYYY-MM-DD,当日末尾(UTC 23:59:59.999)前有效
seatsnumber?席位数。只展示不限制 —— 客户端无法可靠计数,写进来是为了合规时有据可依
iatstring签发日

未授权时到底发生什么 ​

功能完全正常 —— 不禁用、不限行数、不加水印到数据里
console每个未授权产品 warn 一次(不刷屏),带原因与联系方式
界面ctx.rootEl 右下角一枚 .vtp-license-badge,aria-hidden、不吃指针事件、不进 tab 序
多个未授权插件合并进同一枚角标的 title,不会摞三个

角标的 tooltip hover 不出来

它是 pointer-events: none(挡住滚动或单元格点击就成了 bug),代价是 title 的原生 tooltip 显示不了。所以同一份原因也会 console.warn 一份 —— 排障看 console,要自己做可 hover 的提示 就读 getLicenseStatus()。

排障对照表 ​

getLicenseStatus().reason 的取值:

reason含义怎么办
missing没设置密钥(或环境变量注入成了空串/空格)查 import.meta.env 有没有真的注入进来
malformed不是三段、base64 解不开、缺必需字段、到期日解析不了密钥被截断或被编辑器折行了
unsupported-version前缀不是 VTP1升级 @virt-table-pro/license 或换密钥
bad-checksum校验和不匹配(payload 被手改过)用原始密钥,别手动改到期日
expired已过期。payload 仍然给出来,能看到到期日与客户名续期
domain-mismatch当前域名不在授权范围内加域名,或确认不是在非白名单的预发域名上跑

开发域名(localhost / 127.0.0.1 / *.local / *.test …)一律放过,理由见

许可与授权。

角标样式 ​

css
/* 换个位置/配色,或者干脆自己做提示 */
.vtp-license-badge {
  right: auto;
  left: 8px;
  color: var(--vt-color-warning);
}

它读核心的设计令牌(--vt-color-warning / --vt-radius-full / --vt-font-size-sm …), 跟着主题走,不需要单独适配深色。

自己写付费插件时 ​

在 setup 的第一行调一次,产品名与包名后半段一致:

ts
import { guardProduct } from '@virt-table-pro/license';
import type { PluginContext, VirtTablePlugin } from '@virt-table/vanilla';

export function vtSomething(): VirtTablePlugin {
  return {
    name: 'vtSomething',
    setup(ctx: PluginContext) {
      guardProduct(ctx, 'something');
      // ↑ 返回值**不要用来 early return**。未授权时功能必须照跑。
      //   它的用途是做点无害的附加行为(比如导出文件名里带个 trial 标记)。
      return { api: {} };
    },
  };
}

相关 ​