115 lines
3.7 KiB
C++
115 lines
3.7 KiB
C++
#include <QCoreApplication>
|
||
#include <QTextStream>
|
||
#include <QDate>
|
||
#include <QString>
|
||
#include "AuthManager.h"
|
||
#include "AuthConfig.h"
|
||
|
||
#pragma execution_character_set("utf-8")
|
||
|
||
// 打印使用说明
|
||
void printUsage(const QString& programName)
|
||
{
|
||
QTextStream out(stdout);
|
||
out << QString::fromUtf8("授权码生成工具 - 自动获取本机机器码并保存授权") << "\n";
|
||
out << QString::fromUtf8("用法: ") << programName << " [过期日期]\n";
|
||
out << "\n";
|
||
out << QString::fromUtf8("参数说明:\n");
|
||
out << QString::fromUtf8(" 过期日期 - 可选,格式为 yyyyMMdd(如 20251231)\n");
|
||
out << QString::fromUtf8(" 不指定则为永久授权\n");
|
||
out << "\n";
|
||
out << QString::fromUtf8("示例:\n");
|
||
out << QString::fromUtf8(" %1\n").arg(programName);
|
||
out << QString::fromUtf8(" 生成永久授权码并保存\n");
|
||
out << QString::fromUtf8(" %1 20251231\n").arg(programName);
|
||
out << QString::fromUtf8(" 生成截止到 2025年12月31日 的授权码并保存\n");
|
||
out.flush();
|
||
}
|
||
|
||
int main(int argc, char *argv[])
|
||
{
|
||
QCoreApplication a(argc, argv);
|
||
QTextStream out(stdout);
|
||
QTextStream err(stderr);
|
||
|
||
// 检查是否请求帮助
|
||
if (argc >= 2) {
|
||
QString arg1 = QString::fromLocal8Bit(argv[1]);
|
||
if (arg1 == "-h" || arg1 == "--help" || arg1 == "/?") {
|
||
printUsage(argv[0]);
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
// 自动获取本机机器码
|
||
std::string machineCodeStd = AuthManager::GetMachineCode();
|
||
QString machineCode = QString::fromStdString(machineCodeStd);
|
||
|
||
if (machineCode.isEmpty()) {
|
||
err << QString::fromUtf8("错误: 无法获取本机机器码\n");
|
||
err.flush();
|
||
return 1;
|
||
}
|
||
|
||
QString expireDate;
|
||
|
||
// 解析过期日期参数
|
||
if (argc >= 2) {
|
||
expireDate = QString::fromLocal8Bit(argv[1]);
|
||
|
||
// 验证日期格式
|
||
if (expireDate.length() != 8) {
|
||
err << QString::fromUtf8("错误: 过期日期格式错误,应为 yyyyMMdd(如 20251231)\n");
|
||
err.flush();
|
||
return 1;
|
||
}
|
||
|
||
// 验证日期是否有效
|
||
QDate date = QDate::fromString(expireDate, "yyyyMMdd");
|
||
if (!date.isValid()) {
|
||
err << QString::fromUtf8("错误: 无效的日期: ") << expireDate << "\n";
|
||
err.flush();
|
||
return 1;
|
||
}
|
||
|
||
// 检查日期是否已过期
|
||
if (date < QDate::currentDate()) {
|
||
err << QString::fromUtf8("警告: 指定的日期已过期: ") << expireDate << "\n";
|
||
err.flush();
|
||
}
|
||
} else {
|
||
// 永久授权
|
||
expireDate = PERMANENT_LICENSE_DATE;
|
||
}
|
||
|
||
// 生成授权码
|
||
std::string licenseKey = AuthManager::GenerateLicenseKey(
|
||
machineCodeStd,
|
||
expireDate.toStdString()
|
||
);
|
||
|
||
// 保存授权信息
|
||
bool saveResult = AuthManager::SaveLicenseInfo(licenseKey, expireDate.toStdString());
|
||
|
||
// 输出结果
|
||
out << QString::fromUtf8("机器码: ") << machineCode << "\n";
|
||
if (expireDate == PERMANENT_LICENSE_DATE) {
|
||
out << QString::fromUtf8("授权类型: 永久授权\n");
|
||
} else {
|
||
QDate date = QDate::fromString(expireDate, "yyyyMMdd");
|
||
out << QString::fromUtf8("过期日期: ") << date.toString("yyyy-MM-dd") << "\n";
|
||
}
|
||
out << QString::fromUtf8("授权码: ") << QString::fromStdString(licenseKey) << "\n";
|
||
|
||
if (saveResult) {
|
||
out << QString::fromUtf8("状态: 授权信息已保存成功\n");
|
||
} else {
|
||
err << QString::fromUtf8("错误: 授权信息保存失败\n");
|
||
err.flush();
|
||
return 1;
|
||
}
|
||
|
||
out.flush();
|
||
return 0;
|
||
}
|