web程序真是鸡鸣狗盗,零零碎碎。
学习类似SaaS的登录界面,补一大通web的基础知识。
http://127.0.0.1:8077和http://127.0.0.1:8077/admin
这是登录系统。登录账套和管理员登录。又不希望浏览器的get参数。
1、通过UniGUIServerModuleHTTPCommand的处理
procedure TUniServerModule.UniGUIServerModuleHTTPCommand(
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo;
var Handled: Boolean);
begin
{$REGION '判断是否管理员登录'}
// if SameText(ARequestInfo.URI, '/admin') then
// begin
// AResponseInfo.CustomHeaders.AddValue('Location', '/?loginmode=platform');
// AResponseInfo.ResponseNo := 302;
// Handled := True;
// end;
{$ENDREGION}
if SameText(ARequestInfo.URI, '/admin') then
begin
// 设置平台标记
AResponseInfo.CustomHeaders.AddValue('Set-Cookie', 'LoginMode=platform; Path=/');
AResponseInfo.CustomHeaders.AddValue('Location', '/');
AResponseInfo.ResponseNo := 302;
Handled := True;
end
else
begin
// 非 /admin,清除 Cookie
AResponseInfo.CustomHeaders.AddValue(
'Set-Cookie', 'LoginMode=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT'
);
end;
end;
2、UniGUIMainModuleBeforeLogin的处理。增加变量,用于在login中根据该参数识别处理
procedure TUniMainModule.UniGUIMainModuleBeforeLogin(Sender: TObject;
var Handled: Boolean);
begin
// FLoginMode := LowerCase(UniSession.QueryParams.Values['loginmode']);
var CookieVal: string;
var CookieRaw: string;
var CookieList: TStringList;
var I: Integer;
CookieRaw := UniSession.ARequest.RawHeaders.Values['Cookie']; // 这里才对
UniMainModule.FLoginMode := ''; // 默认
if CookieRaw <> '' then
begin
CookieList := TStringList.Create;
try
CookieList.Delimiter := ';';
CookieList.DelimitedText := CookieRaw;
for I := 0 to CookieList.Count - 1 do
begin
if Trim(CookieList.Names[I]) = 'LoginMode' then
begin
if LowerCase(Trim(CookieList.ValueFromIndex[I])) = 'platform' then
UniMainModule.FLoginMode := 'platform';
end;
end;
finally
CookieList.Free;
end;
end
end;
3、最后login中的处理:根据UniMainModule.FLoginMode判断处理
procedure TUniLoginForm1.UniLoginFormAfterShow(Sender: TObject);
var
LoginMode: string;
begin
UniSession.AddJS('setTimeout(function(){UniLoginForm1.undt_FG.setValue(MyFG)},500);');
if UniMainModule.FLoginMode='platform' then
begin
Caption := '平台管理员登录';
// 调整界面
end
else
begin
Caption := '账套用户登录';
// 默认界面
end;
end;
Randy。你这次踩的这个坑确实是很多做 Delphi 桌面开发转 Web 的人都会遇到的,特别是在 UniGUI 这种混合架构下:
明明走了 /,却被老 Cookie 干扰;
服务端没有主动清除,浏览器就一直保留;
又不像传统 JS 那样能随时 console.log。
你现在已经掌握了:
🚪 HTTP 请求流程
🍪 Cookie 设置与清除
🧠 基于路径的身份分流
🔁 302 重定向行为
这些都是做真正 Web SaaS 项目的基础能力,辛苦是值得的。