Files
verify/shell/duilib/shell9_v3.cpp
T

618 lines
26 KiB
C++

// shell9.cpp —— DuiLib 版 Windows 验证壳(多套结构不同的皮肤)
//
// 与平台验证方式严格对齐(/api/v1/app/<appKey>/login):
// POST {username, password, device_id, device_name, device_type, instance_id}
// 即「账号(或卡密)+ 密码 + 设备绑定」,不是只填一个卡密框。
//
// 对外接口不变:
// int VerifyMain(void *oep, const char *cfgjson);
// int VerifyShellVersion(void); // 9
// const char *VerifyShellThemes(void); // 主题清单
#include <windows.h>
#include <winhttp.h>
#include <shlobj.h>
#include <shellapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include "UIlib.h"
#include "Utils/WinImplBase.h"
#pragma comment(lib, "winhttp.lib")
using namespace DuiLib;
/* ============================== cfg ============================== */
static char g_cfg[4096];
static char g_api[512];
static char g_name[128] = "\xE6\xBC\x94\xE7\xA4\xBA\xE7\xA8\x8B\xE5\xBA\x8F"; // 演示程序
static char g_theme[32] = "dark";
static char g_device[64] = "";
static char g_instance[64] = "";
static int g_deviceAuto = 1;
static int json_str(const char *src, const char *key, char *out, int cap) {
char pat[64]; snprintf(pat, sizeof(pat), "\"%s\"", key);
const char *p = strstr(src, pat);
if (!p) return 0;
p = strchr(p + strlen(pat), ':');
if (!p) return 0;
while (*p && *p != '"') p++;
if (!*p) return 0;
p++;
int i = 0;
while (*p && *p != '"' && i < cap - 1) out[i++] = *p++;
out[i] = 0;
return 1;
}
static int json_int(const char *src, const char *key, int def) {
char pat[64]; snprintf(pat, sizeof(pat), "\"%s\"", key);
const char *p = strstr(src, pat);
if (!p) return def;
p = strchr(p + strlen(pat), ':');
if (!p) return def;
return atoi(p + 1);
}
static void make_machine_id(char *out, int cap);
static void load_cfg(const char *json);
/* ============================ 工具 ============================ */
static std::wstring U8(const char *s) {
if (!s) return L"";
int n = ::MultiByteToWideChar(CP_UTF8, 0, s, -1, NULL, 0);
std::wstring w(n > 0 ? n - 1 : 0, L'\0');
if (n > 1) ::MultiByteToWideChar(CP_UTF8, 0, s, -1, &w[0], n);
return w;
}
static std::string W2U8(const std::wstring &w) {
if (w.empty()) return std::string();
int n = ::WideCharToMultiByte(CP_UTF8, 0, w.c_str(), -1, NULL, 0, NULL, NULL);
std::string s(n > 0 ? n - 1 : 0, '\0');
if (n > 1) ::WideCharToMultiByte(CP_UTF8, 0, w.c_str(), -1, &s[0], n, NULL, NULL);
return s;
}
// MachineGuid + 系统盘卷序列号 → 稳定机器码(重装系统才变)
static void make_machine_id(char *out, int cap) {
wchar_t guid[128] = {0};
HKEY hk = NULL;
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Cryptography",
0, KEY_READ | KEY_WOW64_64KEY, &hk) == ERROR_SUCCESS) {
DWORD sz = sizeof(guid) - sizeof(wchar_t), type = 0;
RegQueryValueExW(hk, L"MachineGuid", NULL, &type, (LPBYTE)guid, &sz);
RegCloseKey(hk);
}
DWORD serial = 0;
GetVolumeInformationW(L"C:\\", NULL, 0, &serial, NULL, NULL, NULL, 0);
unsigned int h1 = 2166136261u, h2 = 3735928559u;
for (const wchar_t *p = guid; *p; p++) {
h1 = (h1 ^ (unsigned int)(*p & 0xFF)) * 16777619u;
h2 = (h2 ^ (unsigned int)(*p >> 8)) * 16777619u;
}
h1 = (h1 ^ serial) * 16777619u;
h2 = (h2 ^ (serial >> 16)) * 16777619u;
snprintf(out, cap, "%04X-%04X-%04X", h1 & 0xFFFF, (h1 >> 16) & 0xFFFF, h2 & 0xFFFF);
}
static std::wstring AccountIniPath() {
wchar_t app[MAX_PATH] = {0};
::SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, app);
std::wstring dir = std::wstring(app) + L"\\vfy";
::CreateDirectoryW(dir.c_str(), NULL);
return dir + L"\\account.ini";
}
static void SaveAccount(const std::wstring &user, bool remember) {
std::wstring p = AccountIniPath();
::WritePrivateProfileStringW(L"login", L"remember", remember ? L"1" : L"0", p.c_str());
if (remember) ::WritePrivateProfileStringW(L"login", L"user", user.c_str(), p.c_str());
}
static std::wstring LoadAccount(bool &remember) {
std::wstring p = AccountIniPath();
wchar_t buf[256] = {0};
::GetPrivateProfileStringW(L"login", L"remember", L"0", buf, 256, p.c_str());
remember = (buf[0] == L'1');
wchar_t user[256] = {0};
::GetPrivateProfileStringW(L"login", L"user", L"", user, 256, p.c_str());
return remember ? std::wstring(user) : std::wstring();
}
/* --------------------------- 皮肤 ---------------------------
* 7 套在「结构」上彼此不同,字段集统一按平台验证方式:
* 账号(账号或卡密)/ 密码 / 记住账号 / 设备信息 / 登录 / 状态行 / 注册·卡密充值·忘记密码
* 注意:老 DuiLib 的 Edit 是「真 EDIT 子窗口」,必须用 nativebkcolor 指定它自己的底色,
* 否则聚焦时会出现刺眼的白条(这是它构造里写死 0xFFFFFFFF 导致的)。
*/
#include "skins_v3.h"
static const SkinDef *skin_for(const char *theme) {
for (int i = 0; kSkins[i].name; i++)
if (!strcmp(theme, kSkins[i].name)) return &kSkins[i];
return &kSkins[0];
}
/* ---------------------------- 接口请求 ---------------------------- */
// 平台接口:POST /api/v1/app/<appKey>/login
// {"username","password","device_id","device_name","device_type","instance_id"}
static int api_login(const char *user, const char *pass, char *msg, int cap) {
msg[0] = 0;
if (!g_api[0]) { snprintf(msg, cap, "配置缺少验证地址"); return 0; }
const char *rest = g_api;
if (!strncmp(g_api, "https://", 8)) rest = g_api + 8;
else if (!strncmp(g_api, "http://", 7)) rest = g_api + 7;
bool https = (rest != g_api && g_api[4] == 's');
char host[256] = {0}, path[512] = "/";
const char *slash = strchr(rest, '/');
if (slash) {
int hl = (int)(slash - rest);
if (hl > 255) hl = 255;
memcpy(host, rest, hl);
snprintf(path, sizeof(path), "%s", slash);
} else {
snprintf(host, sizeof(host), "%s", rest);
}
unsigned short port = https ? 443 : 80;
char *colon = strrchr(host, ':');
if (colon) { *colon = 0; port = (unsigned short)atoi(colon + 1); }
char cname[256] = {0}; DWORD cn = sizeof(cname);
::GetComputerNameA(cname, &cn);
char body[2048];
snprintf(body, sizeof(body),
"{\"username\":\"%s\",\"password\":\"%s\",\"device_id\":\"%s\","
"\"device_name\":\"%s\",\"device_type\":\"windows\",\"instance_id\":\"%s\"}",
user, pass, g_device, cname, g_instance);
std::wstring wHost = U8(host), wPath = U8(path);
HINTERNET hS = WinHttpOpen(L"vfy/9.0", WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0);
if (!hS) { snprintf(msg, cap, "网络初始化失败"); return 0; }
HINTERNET hC = WinHttpConnect(hS, wHost.c_str(), port, 0);
HINTERNET hR = hC ? WinHttpOpenRequest(hC, L"POST", wPath.c_str(), NULL, NULL, NULL, 0) : NULL;
int ok = 0;
if (hR) {
WinHttpSendRequest(hR, L"Content-Type: application/json\r\n", (DWORD)-1,
(LPVOID)body, (DWORD)strlen(body), (DWORD)strlen(body), 0);
WinHttpReceiveResponse(hR, NULL);
char resp[4096] = {0};
DWORD cl = 0, total = 0;
while (WinHttpReadData(hR, resp + total, sizeof(resp) - 1 - total, &cl) && cl > 0) {
total += cl;
if (total >= sizeof(resp) - 1) break;
}
resp[total] = 0;
if (strstr(resp, "\"code\":0") || strstr(resp, "\"code\":200") || strstr(resp, "\"success\":true")) ok = 1;
if (!json_str(resp, "message", msg, cap) || !msg[0]) {
if (strstr(resp, "DEVICE_LIMIT")) snprintf(msg, cap, "设备数量已达上限,请先在平台下线一台设备");
else if (strstr(resp, "IP_LIMIT")) snprintf(msg, cap, "IP 绑定已达上限,请解绑后再试");
else if (ok) snprintf(msg, cap, "登录成功");
else snprintf(msg, cap, "账号或密码错误");
}
} else {
snprintf(msg, cap, "无法连接验证服务器");
}
if (hR) WinHttpCloseHandle(hR);
if (hC) WinHttpCloseHandle(hC);
WinHttpCloseHandle(hS);
return ok;
}
// 注册:POST /api/v1/app/<appKey>/register
static int api_register(const char *user, const char *email, const char *code,
const char *pass, char *msg, int cap) {
msg[0] = 0;
if (!g_api[0]) { snprintf(msg, cap, "配置缺少验证地址"); return 0; }
std::string reg = g_api;
size_t pos = reg.rfind("/login");
if (pos != std::string::npos) reg = reg.substr(0, pos) + "/register";
const char *rest = reg.c_str();
bool https = false;
if (!strncmp(rest, "https://", 8)) { rest += 8; https = true; }
else if (!strncmp(rest, "http://", 7)) rest += 7;
char host[256] = {0}, path[512] = "/";
const char *slash = strchr(rest, '/');
if (slash) { int hl = (int)(slash - rest); if (hl > 255) hl = 255; memcpy(host, rest, hl); snprintf(path, sizeof(path), "%s", slash); }
else snprintf(host, sizeof(host), "%s", rest);
unsigned short port = https ? 443 : 80;
char *colon = strrchr(host, ':');
if (colon) { *colon = 0; port = (unsigned short)atoi(colon + 1); }
char cname[256] = {0}; DWORD cn = sizeof(cname);
::GetComputerNameA(cname, &cn);
char body[2048];
snprintf(body, sizeof(body),
"{\"username\":\"%s\",\"email\":\"%s\",\"email_code\":\"%s\",\"password\":\"%s\","
"\"device_id\":\"%s\",\"device_name\":\"%s\",\"device_type\":\"windows\",\"instance_id\":\"%s\"}",
user, email, code, pass, g_device, cname, g_instance);
std::wstring wHost = U8(host), wPath = U8(path);
HINTERNET hS = WinHttpOpen(L"vfy/9.0", WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0);
if (!hS) { snprintf(msg, cap, "网络初始化失败"); return 0; }
HINTERNET hC = WinHttpConnect(hS, wHost.c_str(), port, 0);
HINTERNET hR = hC ? WinHttpOpenRequest(hC, L"POST", wPath.c_str(), NULL, NULL, NULL, 0) : NULL;
int ok = 0;
if (hR) {
WinHttpSendRequest(hR, L"Content-Type: application/json\r\n", (DWORD)-1,
(LPVOID)body, (DWORD)strlen(body), (DWORD)strlen(body), 0);
WinHttpReceiveResponse(hR, NULL);
char resp[4096] = {0}; DWORD cl = 0, total = 0;
while (WinHttpReadData(hR, resp + total, sizeof(resp) - 1 - total, &cl) && cl > 0) {
total += cl; if (total >= sizeof(resp) - 1) break;
}
resp[total] = 0;
if (strstr(resp, "\"code\":0") || strstr(resp, "\"code\":200") || strstr(resp, "\"success\":true")) ok = 1;
if (!json_str(resp, "message", msg, cap) || !msg[0])
snprintf(msg, cap, ok ? "注册成功,正在登录…" : "注册失败,请检查邮箱验证码");
} else {
snprintf(msg, cap, "无法连接验证服务器");
}
if (hR) WinHttpCloseHandle(hR);
if (hC) WinHttpCloseHandle(hC);
WinHttpCloseHandle(hS);
return ok;
}
// 发送邮箱验证码:POST /api/v1/app/<appKey>/send-email-code
static int api_send_code(const char *email, char *msg, int cap) {
msg[0] = 0;
std::string u = g_api;
size_t pos = u.rfind("/login");
if (pos != std::string::npos) u = u.substr(0, pos) + "/send-email-code";
const char *rest = u.c_str();
bool https = false;
if (!strncmp(rest, "https://", 8)) { rest += 8; https = true; }
else if (!strncmp(rest, "http://", 7)) rest += 7;
char host[256] = {0}, path[512] = "/";
const char *slash = strchr(rest, '/');
if (slash) { int hl = (int)(slash - rest); if (hl > 255) hl = 255; memcpy(host, rest, hl); snprintf(path, sizeof(path), "%s", slash); }
else snprintf(host, sizeof(host), "%s", rest);
unsigned short port = https ? 443 : 80;
char *colon = strrchr(host, ':');
if (colon) { *colon = 0; port = (unsigned short)atoi(colon + 1); }
char body[512];
snprintf(body, sizeof(body), "{\"email\":\"%s\"}", email);
std::wstring wHost = U8(host), wPath = U8(path);
HINTERNET hS = WinHttpOpen(L"vfy/9.0", WINHTTP_ACCESS_TYPE_NO_PROXY, NULL, NULL, 0);
if (!hS) { snprintf(msg, cap, "网络初始化失败"); return 0; }
HINTERNET hC = WinHttpConnect(hS, wHost.c_str(), port, 0);
HINTERNET hR = hC ? WinHttpOpenRequest(hC, L"POST", wPath.c_str(), NULL, NULL, NULL, 0) : NULL;
int ok = 0;
if (hR) {
WinHttpSendRequest(hR, L"Content-Type: application/json\r\n", (DWORD)-1,
(LPVOID)body, (DWORD)strlen(body), (DWORD)strlen(body), 0);
WinHttpReceiveResponse(hR, NULL);
char resp[2048] = {0}; DWORD cl = 0;
WinHttpReadData(hR, resp, sizeof(resp) - 1, &cl);
resp[cl < sizeof(resp) ? cl : sizeof(resp) - 1] = 0;
if (strstr(resp, "\"code\":0") || strstr(resp, "\"code\":200") || strstr(resp, "\"success\":true")) ok = 1;
if (!json_str(resp, "message", msg, cap) || !msg[0])
snprintf(msg, cap, ok ? "验证码已发送" : "发送失败");
} else {
snprintf(msg, cap, "无法连接验证服务器");
}
if (hR) WinHttpCloseHandle(hR);
if (hC) WinHttpCloseHandle(hC);
WinHttpCloseHandle(hS);
return ok;
}
/* ---------------------------- 窗口 ---------------------------- */
typedef void (*PFN_OEP)(void);
static PFN_OEP g_oep = NULL;
static int g_logged_in = 0;
// 从 cfg 的 api 里抽出站点根地址(去掉 /api/v1/... 部分)
static std::wstring SiteRoot() {
std::string a(g_api);
size_t p = a.find("/api/");
if (p != std::string::npos) a = a.substr(0, p);
return U8(a.c_str());
}
static bool g_remember = false;
static int g_tab = 0; // 0=登录 1=注册
class CVerifyWnd : public CWindowWnd, public INotifyUI {
public:
explicit CVerifyWnd(const char *skin) : m_skin(skin) {}
LPCTSTR GetWindowClassName() const override { return _T("VfyDuiWnd"); }
UINT GetClassStyle() const override { return CS_DBLCLKS; }
void Notify(TNotifyUI &msg) override {
if (msg.sType == _T("click")) {
CControlUI *p = msg.pSender;
LPCTSTR nm = p ? p->GetName().GetData() : NULL;
if (!nm) return;
if (!_tcsicmp(nm, _T("btnClose")) || !_tcsicmp(nm, _T("btnQuit"))) { ::PostQuitMessage(0); return; }
if (!_tcsicmp(nm, _T("btnLogin"))) { DoLogin(); return; }
if (!_tcsicmp(nm, _T("tabLogin"))) { SwitchTab(0); return; }
if (!_tcsicmp(nm, _T("tabRegister"))) { SwitchTab(1); return; }
if (!_tcsicmp(nm, _T("btnRegister"))) { DoRegister(); return; }
if (!_tcsicmp(nm, _T("btnSendCode"))) { DoSendCode(); return; }
if (!_tcsicmp(nm, _T("btnLinkReg")) || !_tcsicmp(nm, _T("btnLinkCard")) ||
!_tcsicmp(nm, _T("btnLinkForgot"))) {
std::wstring url = SiteRoot();
// 平台只有 /login 与 /register 是公开页:注册直开,其余引导到登录页
url += _tcsicmp(nm, _T("btnLinkReg")) == 0 ? L"/register" : L"/login";
::ShellExecuteW(NULL, L"open", url.c_str(), NULL, NULL, SW_SHOWNORMAL);
return;
}
if (!_tcsicmp(nm, _T("btnRemember"))) {
g_remember = !g_remember;
UpdateRememberButton();
return;
}
} else if (msg.sType == _T("return")) {
DoLogin();
} else if (msg.sType == _T("setfocus") || msg.sType == _T("killfocus")) {
CControlUI *p = msg.pSender;
if (!p) return;
LPCTSTR nm = p->GetName().GetData();
if (!nm || (_tcsicmp(nm, _T("editUser")) && _tcsicmp(nm, _T("editPass")))) return;
bool focus = !_tcsicmp(msg.sType.GetData(), _T("setfocus"));
if (CEditUI *e = static_cast<CEditUI *>(p)) {
DWORD bg = e->GetBkColor();
// 聚焦时把原生 EDIT 窗口底色提亮一档(纯 XML 画不出描边,用亮度做反馈)
if (focus) {
BYTE r = GetRValue(bg) + 10 > 255 ? 255 : GetRValue(bg) + 10;
BYTE g = GetGValue(bg) + 10 > 255 ? 255 : GetGValue(bg) + 10;
BYTE b = GetBValue(bg) + 14 > 255 ? 255 : GetBValue(bg) + 14;
e->SetNativeEditBkColor(RGB(r, g, b));
} else {
e->SetNativeEditBkColor(bg);
}
e->Invalidate();
}
}
}
// ---------------- 登录 / 注册 切换 ----------------
void SwitchTab(int tab) {
g_tab = tab;
if (CControlUI *p = m_pm.FindControl(_T("panelLogin"))) p->SetVisible(tab == 0);
if (CControlUI *p = m_pm.FindControl(_T("panelRegister"))) p->SetVisible(tab == 1);
UpdateTabStyle();
}
// 每套皮肤的 tab 表达方式不同:下划线 / 段按钮 / 纯文字 / 标题栏内
void UpdateTabStyle() {
CButtonUI *bL = static_cast<CButtonUI *>(m_pm.FindControl(_T("tabLogin")));
CButtonUI *bR = static_cast<CButtonUI *>(m_pm.FindControl(_T("tabRegister")));
if (!bL || !bR) return;
bool login = (g_tab == 0);
std::string th(g_theme);
if (th == "light") {
bL->SetBkColor(login ? 0xFF1B6FD6 : 0xFFEDF0F5);
bR->SetBkColor(login ? 0xFFEDF0F5 : 0xFF1B6FD6);
bL->SetTextColor(login ? 0xFFFFFFFF : 0xFF5B6270);
bR->SetTextColor(login ? 0xFF5B6270 : 0xFFFFFFFF);
} else if (th == "industrial") {
bL->SetBkColor(login ? 0xFF1A5A8C : 0x00000000);
bR->SetBkColor(login ? 0x00000000 : 0xFF1A5A8C);
bL->SetTextColor(login ? 0xFFFFFFFF : 0xFF9FC8E6);
bR->SetTextColor(login ? 0xFF9FC8E6 : 0xFFFFFFFF);
} else if (th == "compact") {
bL->SetBkColor(login ? 0xFF2E9E6B : 0xFF1B232B);
bR->SetBkColor(login ? 0xFF1B232B : 0xFF2E9E6B);
bL->SetTextColor(login ? 0xFFFFFFFF : 0xFF9FB3C8);
bR->SetTextColor(login ? 0xFF9FB3C8 : 0xFFFFFFFF);
} else if (th == "card") {
bL->SetBkColor(login ? 0xFFFFFFFF : 0xFFE8E6FF);
bR->SetBkColor(login ? 0xFFE8E6FF : 0xFFFFFFFF);
bL->SetTextColor(login ? 0xFF3B2BD6 : 0xFF5B56A8);
bR->SetTextColor(login ? 0xFF5B56A8 : 0xFF3B2BD6);
} else { // dark / split / terminal:只有文字颜色 + 下划线控件
DWORD act = (th == "terminal") ? 0xFF7FE39B : (th == "split" ? 0xFF4CC2FF : 0xFF4EA3E8);
DWORD idle = (th == "terminal") ? 0xFF4E7A5C : (th == "split" ? 0xFF7A8794 : 0xFF85858F);
bL->SetTextColor(login ? act : idle);
bR->SetTextColor(login ? idle : act);
if (CControlUI *line = m_pm.FindControl(_T("tabRegLine")))
line->SetBkColor(login ? 0x00000000 : act);
}
bL->Invalidate(); bR->Invalidate();
}
void DoRegister() {
CEditUI *u = static_cast<CEditUI *>(m_pm.FindControl(_T("regUser")));
CEditUI *e = static_cast<CEditUI *>(m_pm.FindControl(_T("regEmail")));
CEditUI *c = static_cast<CEditUI *>(m_pm.FindControl(_T("regCode")));
CEditUI *p1 = static_cast<CEditUI *>(m_pm.FindControl(_T("regPass")));
CEditUI *p2 = static_cast<CEditUI *>(m_pm.FindControl(_T("regPass2")));
CLabelUI *tip = static_cast<CLabelUI *>(m_pm.FindControl(_T("regTip")));
if (!u || !e || !c || !p1 || !p2 || !tip) return;
std::wstring user = u->GetText().GetData(), email = e->GetText().GetData();
std::wstring code = c->GetText().GetData(), pass = p1->GetText().GetData();
std::wstring pass2 = p2->GetText().GetData();
if (user.empty()) { tip->SetText(L"请输入用户名"); return; }
if (email.empty()) { tip->SetText(L"请输入邮箱"); return; }
if (code.empty()) { tip->SetText(L"请输入邮箱验证码"); return; }
if (pass.size() < 6) { tip->SetText(L"密码至少 6 位"); return; }
if (pass != pass2) { tip->SetText(L"两次输入的密码不一致"); return; }
tip->SetText(L"正在提交注册…");
char msg[1024] = {0};
int ok = api_register(W2U8(user).c_str(), W2U8(email).c_str(), W2U8(code).c_str(),
W2U8(pass).c_str(), msg, sizeof(msg));
if (!ok) { tip->SetText(U8(msg).c_str()); return; }
// 注册成功直接登录
if (CEditUI *lu = static_cast<CEditUI *>(m_pm.FindControl(_T("editUser")))) lu->SetText(user.c_str());
if (CEditUI *lp = static_cast<CEditUI *>(m_pm.FindControl(_T("editPass")))) lp->SetText(pass.c_str());
SwitchTab(0);
DoLogin();
}
void DoSendCode() {
CEditUI *e = static_cast<CEditUI *>(m_pm.FindControl(_T("regEmail")));
CLabelUI *tip = static_cast<CLabelUI *>(m_pm.FindControl(_T("regTip")));
if (!e || !tip) return;
std::wstring email = e->GetText().GetData();
if (email.empty()) { tip->SetText(L"请先填邮箱"); return; }
char msg[512] = {0};
api_send_code(W2U8(email).c_str(), msg, sizeof(msg));
tip->SetText(U8(msg).c_str());
}
// 自绘勾选框:选中时方框填充主色(老 DuiLib 没有勾选图片资源,只能用色块表达)
void UpdateRememberButton() {
CLabelUI *box = static_cast<CLabelUI *>(m_pm.FindControl(_T("chkBox")));
if (!box) return;
box->SetBkColor(g_remember ? 0xFF2C7BE5 : 0x00000000);
box->SetBorderColor(g_remember ? 0xFF2C7BE5 : 0xFF5A5A66);
box->Invalidate();
}
void DoLogin() {
CEditUI *pUser = static_cast<CEditUI *>(m_pm.FindControl(_T("editUser")));
CEditUI *pPass = static_cast<CEditUI *>(m_pm.FindControl(_T("editPass")));
CLabelUI *pTip = static_cast<CLabelUI *>(m_pm.FindControl(_T("lblTip")));
CButtonUI *pBtn = static_cast<CButtonUI *>(m_pm.FindControl(_T("btnLogin")));
if (!pUser || !pPass || !pTip) return;
std::wstring user = pUser->GetText().GetData();
std::wstring pass = pPass->GetText().GetData();
if (user.empty()) { pTip->SetText(L"请输入账号或卡密"); return; }
if (pass.empty()) { pTip->SetText(L"请输入密码"); return; }
SaveAccount(user, g_remember);
if (pBtn) { pBtn->SetText(L"验证中…"); pBtn->SetEnabled(false); }
pTip->SetText(L"正在连接验证服务器…");
char msg[1024] = {0};
int ok = api_login(W2U8(user).c_str(), W2U8(pass).c_str(), msg, sizeof(msg));
if (ok) {
g_logged_in = 1;
pTip->SetText(L"验证成功,正在启动程序…");
::SetTimer(m_hWnd, 1, 600, NULL);
} else {
pTip->SetText(U8(msg).c_str());
if (pBtn) { pBtn->SetText(L"登 录"); pBtn->SetEnabled(true); }
}
}
LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) override {
if (uMsg == WM_CREATE) {
m_pm.Init(m_hWnd);
CDialogBuilder builder;
std::wstring xml = U8(m_skin);
CControlUI *pRoot = builder.Create(xml.c_str(), (UINT)0, NULL, &m_pm);
if (!pRoot) { ::MessageBoxW(m_hWnd, L"界面加载失败", L"验证", MB_ICONERROR); return 0; }
m_pm.AttachDialog(pRoot);
m_pm.AddNotifier(this);
if (CLabelUI *p = static_cast<CLabelUI *>(m_pm.FindControl(_T("lblApp"))))
p->SetText(U8(g_name).c_str());
std::wstring devText = L"设备 ";
devText += U8(g_device);
if (CLabelUI *p = static_cast<CLabelUI *>(m_pm.FindControl(_T("lblDevice"))))
p->SetText(devText.c_str());
bool remember = false;
std::wstring saved = LoadAccount(remember);
g_remember = remember;
UpdateRememberButton();
SwitchTab(0);
if (CEditUI *pUser = static_cast<CEditUI *>(m_pm.FindControl(_T("editUser")))) {
if (!saved.empty()) pUser->SetText(saved.c_str());
pUser->SetFocus();
}
return 0;
}
if (uMsg == WM_TIMER && wParam == 1) {
::KillTimer(m_hWnd, 1);
::ShowWindow(m_hWnd, SW_HIDE);
if (g_oep) g_oep();
::PostQuitMessage(0);
return 0;
}
if (uMsg == WM_DESTROY) { ::PostQuitMessage(0); return 0; }
LRESULT lr = 0;
if (m_pm.MessageHandler(uMsg, wParam, lParam, lr)) return lr;
return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
}
CPaintManagerUI m_pm;
const char *m_skin;
};
static int RunShell(void *oep) {
g_oep = (PFN_OEP)oep;
const SkinDef *sk = skin_for(g_theme);
CPaintManagerUI::SetInstance(::GetModuleHandle(NULL));
::CoInitialize(NULL);
CVerifyWnd wnd(sk->xml);
UINT style = WS_POPUP | WS_CLIPCHILDREN;
UINT exstyle = WS_EX_TOPMOST | WS_EX_APPWINDOW;
wnd.Create(NULL, L"软件授权验证", style, exstyle, 0, 0, sk->w, sk->h);
wnd.CenterWindow();
::ShowWindow(wnd, SW_SHOW);
CPaintManagerUI::MessageLoop();
::CoUninitialize();
return g_logged_in ? 0 : 6;
}
/* ---------------------------- 对外接口 ---------------------------- */
static void load_cfg(const char *json) {
snprintf(g_cfg, sizeof(g_cfg), "%s", json ? json : "");
json_str(g_cfg, "api", g_api, sizeof(g_api));
json_str(g_cfg, "name", g_name, sizeof(g_name));
g_deviceAuto = json_int(g_cfg, "device_auto", 1);
char mid[64] = {0};
make_machine_id(mid, sizeof(mid));
snprintf(g_instance, sizeof(g_instance), "%s", mid);
if (!json_str(g_cfg, "device", g_device, sizeof(g_device)) || !g_device[0]) {
if (g_deviceAuto) snprintf(g_device, sizeof(g_device), "%s", mid);
else snprintf(g_device, sizeof(g_device), "%s", "UNKNOWN-DEVICE");
}
if (!json_str(g_cfg, "theme", g_theme, sizeof(g_theme))) snprintf(g_theme, sizeof(g_theme), "dark");
}
#ifdef SHELL_EXE
int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR lpCmdLine, int) {
const char *cfg = (lpCmdLine && lpCmdLine[0]) ? lpCmdLine : getenv("VFY_CFG");
if (!cfg || !cfg[0]) cfg = "{\"theme\":\"dark\"}";
load_cfg(cfg);
return RunShell(NULL);
}
#else
extern "C" __declspec(dllexport) int VerifyMain(void *oep, const char *cfgjson) {
load_cfg(cfgjson);
return RunShell(oep);
}
extern "C" __declspec(dllexport) int VerifyShellVersion(void) { return 9; }
extern "C" __declspec(dllexport) const char *VerifyShellThemes(void) {
return "dark,light,split,card,industrial,terminal,compact";
}
BOOL WINAPI DllMain(HINSTANCE h, DWORD reason, LPVOID) {
if (reason == DLL_PROCESS_ATTACH) DisableThreadLibraryCalls(h);
return TRUE;
}
#endif