update license check
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
import type { LicenseInfo, LicenseCheckResult } from '@/types/license';
|
||||
import { LicenseStorage } from '@/utils/licenseStorage';
|
||||
import { LicenseStorage } from '@/utils/LicenseStorage';
|
||||
import { PermissionService } from '@/utils/permissions';
|
||||
import { Notification } from 'kintone-ui-component/lib/notification';
|
||||
import { createApp } from 'vue';
|
||||
import i18n from '@/i18n';
|
||||
import { MobileNotification } from 'kintone-ui-component/lib/mobile/notification';
|
||||
|
||||
export class LicenseService {
|
||||
// 常量定义
|
||||
private static readonly WARNING_DAYS_BEFORE_EXPIRY = 7;
|
||||
private static readonly TRIAL_DATE = 1;
|
||||
private static PLUGIN_ID: string = '';
|
||||
// ============ 基础工具函数 ============
|
||||
|
||||
@@ -45,24 +45,27 @@ export class LicenseService {
|
||||
return true;
|
||||
}
|
||||
|
||||
const today = new Date();
|
||||
// 检查存储是否过期(是否同一天)
|
||||
const fetchDate = new Date(license.fetchTime).toDateString();
|
||||
const todayStr = today.toDateString();
|
||||
if (fetchDate !== todayStr) {
|
||||
if (!this.isToday(license.fetchTime)) {
|
||||
// 不是同一天,已过期
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查试用是否到期
|
||||
const expiredTime = new Date(license.expiredTime);
|
||||
if (expiredTime < today) {
|
||||
if (expiredTime < new Date()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
static isToday(timestamp: number): boolean {
|
||||
const fetchDate = new Date(timestamp).toDateString();
|
||||
const todayStr = new Date().toDateString();
|
||||
return fetchDate == todayStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 许可证验证
|
||||
*/
|
||||
@@ -160,36 +163,36 @@ export class LicenseService {
|
||||
/**
|
||||
* 显示到期警告通知
|
||||
*/
|
||||
static showExpiryWarning(license: any, options?: {
|
||||
warningDialogTitle?: string;
|
||||
static async showExpiryWarning(license: any, options?: {
|
||||
warningDialogMessage?: string;
|
||||
}) {
|
||||
const remainingDays = this.getDaysRemaining(license.expiredTime);
|
||||
const defaultMessage = `您的插件许可证将在 ${remainingDays} 天后到期,请及时续期以避免服务中断。`;
|
||||
const msg = remainingDays > 0 ? ` ${remainingDays} 天后` : '今天'
|
||||
const defaultMessage = `您的插件许可证将在${msg}到期,请及时续期以避免服务中断。`;
|
||||
|
||||
// 使用自定义消息或默认消息
|
||||
const message = options?.warningDialogMessage || defaultMessage;
|
||||
|
||||
// 检查是否已设置不再提醒
|
||||
const dontShowKey = `license_notification_dont_show_again_${this.getPluginId()}`;
|
||||
const dontShowAgain = localStorage.getItem(dontShowKey) === 'true';
|
||||
if (dontShowAgain) return;
|
||||
const settings = LicenseStorage.getSettings(this.getPluginId());
|
||||
if (settings.suppressMsgTime && this.isToday(settings.suppressMsgTime)) {
|
||||
return
|
||||
}
|
||||
delete settings.suppressMsgTime
|
||||
LicenseStorage.saveSetting(settings, this.getPluginId());
|
||||
|
||||
// 使用KUC Notification
|
||||
const notification = new Notification({
|
||||
text: message,
|
||||
type: 'info',
|
||||
duration: -1 // 不自动关闭
|
||||
});
|
||||
|
||||
// 添加到页面
|
||||
const container = document.body;
|
||||
container.appendChild(notification);
|
||||
|
||||
// 监听关闭事件
|
||||
notification.addEventListener('close', () => {
|
||||
// 这里可以添加不再提醒的逻辑
|
||||
});
|
||||
if (await kintone.isMobilePage()) {
|
||||
const notification = new MobileNotification({
|
||||
text: message,
|
||||
});
|
||||
notification.open();
|
||||
} else {
|
||||
const notification = new Notification({
|
||||
text: message,
|
||||
type: 'info',
|
||||
});
|
||||
notification.open();
|
||||
}
|
||||
}
|
||||
|
||||
// ============ 主要入口函数 ============
|
||||
@@ -203,7 +206,6 @@ export class LicenseService {
|
||||
options?: {
|
||||
expiryDialogTitle?: string;
|
||||
expiryDialogMessage?: string;
|
||||
warningDialogTitle?: string;
|
||||
warningDialogMessage?: string;
|
||||
}
|
||||
) {
|
||||
@@ -233,13 +235,14 @@ export class LicenseService {
|
||||
}
|
||||
|
||||
// 许可证有效,如果快要到期,管理员可以看到警告
|
||||
if (isManager && licenseCheck.license && !licenseCheck.license.isPaid && this.isExpiringSoon(licenseCheck.license.expiredTime)) {
|
||||
if (isManager &&
|
||||
licenseCheck.license &&
|
||||
!licenseCheck.license.isPaid &&
|
||||
this.isExpiringSoon(licenseCheck.license.expiredTime)) {
|
||||
// 管理员可以看到过期弹框
|
||||
alert('即将过期')
|
||||
// this.showExpiryWarning(licenseCheck.license, {
|
||||
// warningDialogTitle: options?.warningDialogTitle,
|
||||
// warningDialogMessage: options?.warningDialogMessage
|
||||
// });
|
||||
this.showExpiryWarning(licenseCheck.license, {
|
||||
warningDialogMessage: options?.warningDialogMessage
|
||||
});
|
||||
}
|
||||
|
||||
// 许可证有效,可以加载插件功能
|
||||
@@ -275,7 +278,7 @@ export class LicenseService {
|
||||
*/
|
||||
private static mockCreateTrialLicense(): LicenseInfo {
|
||||
const expiryDate = new Date();
|
||||
expiryDate.setDate(expiryDate.getDate() + 30);
|
||||
expiryDate.setDate(expiryDate.getDate() + this.TRIAL_DATE);
|
||||
|
||||
return {
|
||||
expiredTime: expiryDate.getTime(),
|
||||
@@ -283,6 +286,7 @@ export class LicenseService {
|
||||
domain: this.getDomain(),
|
||||
pluginId: this.getPluginId(),
|
||||
fetchTime: new Date().getTime(),
|
||||
version: 1,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user