import {
|
query
|
} from '../common/sqlite'
|
import {
|
ActivationConfig
|
} from '../config/activation'
|
|
// 格式化日期为 YYYY-MM-DD
|
function formatDate(date) {
|
const year = date.getFullYear()
|
const month = (date.getMonth() + 1).toString().padStart(2, '0')
|
const day = date.getDate().toString().padStart(2, '0')
|
return `${year}-${month}-${day}`
|
}
|
|
// 计算剩余天数
|
function calculateDaysLeft(expiryDate) {
|
const now = new Date()
|
return Math.ceil((new Date(expiryDate) - now) / (1000 * 60 * 60 * 24))
|
}
|
|
export default {
|
|
async checkDataCount() {
|
try {
|
if (ActivationConfig.isDebugVersion) {
|
//#ifdef APP-PLUS
|
const orderCounts = await query('select count(*) AS orderCount from orders');
|
const orderCount = orderCounts[0].orderCount;
|
|
const detailCounts = await query('select count(*) AS detailCount from scan_records');
|
const detailCount = detailCounts[0].detailCount;
|
|
if (orderCount > 20 || detailCount > 2000) {
|
return {
|
expStatus: true,
|
message: '试用版已到使用期,请联系管理员激活'
|
}
|
}
|
//#endif
|
}
|
// 临时使用,后续再改激活检测
|
return {
|
expStatus: false
|
}
|
|
} catch (e) {
|
console.log(e);
|
return {
|
expStatus: true,
|
message: e
|
}
|
}
|
},
|
|
// 使用激活码激活系统
|
activateWithCode(activationCode) {
|
// 验证激活码格式
|
if (!activationCode || typeof activationCode !== 'string' || activationCode.length < 10) {
|
return {
|
success: false,
|
message: '激活码格式不正确'
|
}
|
}
|
|
// 在实际应用中,这里应该发送请求到服务器验证激活码
|
// 这里简化处理,只检查激活码前缀
|
if (activationCode.startsWith('SMTPDA-')) {
|
const now = new Date()
|
// 假设激活码有效期为1年
|
const expiryDate = new Date()
|
expiryDate.setFullYear(now.getFullYear() + 1)
|
|
const activationInfo = {
|
activatedDate: formatDate(now),
|
expiryDate: formatDate(expiryDate),
|
daysLeft: calculateDaysLeft(formatDate(expiryDate)),
|
isTrial: false,
|
isDebug: false,
|
isValid: true
|
}
|
|
// 保存到本地存储
|
uni.setStorageSync(ActivationConfig.storageKeys.activationInfo, JSON.stringify(activationInfo))
|
|
return {
|
success: true,
|
message: '系统已成功激活',
|
activationInfo
|
}
|
} else {
|
return {
|
success: false,
|
message: '无效的激活码'
|
}
|
}
|
},
|
|
// 激活调试版本
|
activateDebugVersion() {
|
const now = new Date()
|
const activationInfo = {
|
activatedDate: formatDate(now),
|
expiryDate: ActivationConfig.debugExpiryDate,
|
daysLeft: calculateDaysLeft(ActivationConfig.debugExpiryDate),
|
isTrial: false,
|
isDebug: true
|
}
|
|
// 保存到本地存储
|
uni.setStorageSync(ActivationConfig.storageKeys.debugActivationInfo, JSON.stringify(activationInfo))
|
|
return activationInfo
|
},
|
|
// 激活试用版本
|
activateTrialVersion() {
|
const now = new Date()
|
const expiryDate = new Date()
|
expiryDate.setDate(now.getDate() + ActivationConfig.trialDays)
|
|
const activationInfo = {
|
activatedDate: formatDate(now),
|
expiryDate: formatDate(expiryDate),
|
daysLeft: ActivationConfig.trialDays,
|
isTrial: true,
|
isDebug: false
|
}
|
|
// 保存到本地存储
|
uni.setStorageSync(ActivationConfig.storageKeys.activationInfo, JSON.stringify(activationInfo))
|
|
return activationInfo
|
},
|
|
// 检查激活状态
|
checkActivationStatus() {
|
|
// 从本地存储获取激活状态
|
const isActivated = uni.getStorageSync(ActivationConfig.storageKeys.isActivated)
|
if (isActivated) {
|
// 如果已激活,直接返回激活信息
|
const activationInfo = uni.getStorageSync(ActivationConfig.storageKeys.activationInfo)
|
if (activationInfo) {
|
try {
|
const info = JSON.parse(activationInfo)
|
return {
|
...info,
|
daysLeft: calculateDaysLeft(info.expiryDate),
|
isValid: true
|
}
|
} catch (e) {
|
console.error('激活信息解析失败:', e)
|
}
|
}
|
}
|
|
// 检查是否为调试版本
|
const isDebugVersion = ActivationConfig.isDebugVersion
|
|
if (isDebugVersion) {
|
// 调试版本逻辑
|
const debugActivationInfo = uni.getStorageSync(ActivationConfig.storageKeys.debugActivationInfo)
|
|
if (debugActivationInfo) {
|
try {
|
const info = JSON.parse(debugActivationInfo)
|
const now = new Date()
|
const expiryDate = new Date(info.expiryDate)
|
|
if (expiryDate > now) {
|
uni.setStorageSync(ActivationConfig.storageKeys.isActivated, true)
|
// 调试版本激活有效
|
return {
|
...info,
|
daysLeft: calculateDaysLeft(info.expiryDate),
|
isValid: true
|
}
|
} else {
|
return {
|
success: false,
|
daysLeft: calculateDaysLeft(info.expiryDate),
|
message: '调试版本已过期'
|
}
|
}
|
} catch (e) {
|
console.error('调试激活信息解析失败:', e)
|
}
|
}
|
|
// 如果没有有效的调试激活信息,重新激活
|
return this.activateDebugVersion()
|
} else {
|
// 非调试版本逻辑
|
const activationInfo = uni.getStorageSync(ActivationConfig.storageKeys.activationInfo)
|
|
if (activationInfo) {
|
try {
|
const info = JSON.parse(activationInfo)
|
const now = new Date()
|
const expiryDate = new Date(info.expiryDate)
|
|
if (expiryDate > now) {
|
// 试用版本激活有效
|
return {
|
...info,
|
daysLeft: calculateDaysLeft(info.expiryDate),
|
isValid: true
|
}
|
}
|
} catch (e) {
|
console.error('激活信息解析失败:', e)
|
}
|
}
|
|
// 如果没有有效的激活信息,激活试用版本
|
return this.activateTrialVersion()
|
}
|
},
|
|
// 生成设备运行码
|
generateRunningCode(deviceId) {
|
// 获取当前时间戳
|
const timestamp = Math.floor(Date.now() / 1000)
|
|
// 生成随机数
|
const randomStr = Math.random().toString(36).substr(2, 8).toUpperCase()
|
|
// 格式化设备ID(只保留字母数字)
|
const formattedDeviceId = (deviceId || '').replace(/[^a-zA-Z0-9]/g, '')
|
|
// 生成运行码
|
const prefix = ActivationConfig.isDebugVersion ? 'SMTPDA-DEBUG' : 'SMTPDA'
|
return `${prefix}-${timestamp}-${formattedDeviceId}-${randomStr}`
|
}
|
}
|