// logger.fatal(request);
if request.Method<>'POST' then
begin
    print('{"code": 400, "msg": "只允许使用POST方法提交请求。"}');
    exit;
end; 
var login_ip := request.RemoteIP;
// 处理代理场景：如果使用了反向代理，可从X-Real-IP或X-Forwarded-For获取真实IP
// 注意：生产环境需验证代理的可靠性，避免伪造IP
if request.Headers['X-Real-IP']<>'' then
    login_ip := request.Headers['X-Real-IP']
else if request.Headers['X-Forwarded-For']<>'' then
    // X-Forwarded-For格式：客户端IP, 代理1IP, 代理2IP...
    login_ip := string(request.Headers['X-Forwarded-For']).split(',')[0];
include('/v1/user/jwt');
var token := json.parse(jwt_token(request.Headers['Authorization']));
if not token.verify then
begin
    print('{"code": 401, "msg": "授权信息无效，'+token.msg+'"}');
    exit;
end;
var data := json.NewObject;
try
    data := json.parse(request.Params);
except
    begin
        print('{"code": 400, "msg": "请求体不是有效的JSON格式"}');
        exit;
    end;
end;
// 验证必要参数
if not regex.ismatch(data.pageIndex,'^[1-9]\d*$')
or not regex.ismatch(data.pageSize,'^[1-9]\d*$') then
begin
    print('{"code": 400, "msg": "参数错误！"}');
    exit;
end;
// 扩展参数，可选
var c:='';
var name:=data.name;
if trim(name)<>'' then
begin
    if not regex.ismatch(name,'.{1,200}') then
    begin
        print('{"code": 400, "msg": "名称长度不能超过200字符！"}');
        exit;
    end;
    c+=' and t1.fname like '+QuotedStr('%'+name+'%');
end;
var code:=data.code;
if trim(code)<>'' then
begin
    if not regex.ismatch(code,'\w{1,200}') then
    begin
        print('{"code": 400, "msg": "代码长度不能超过200字符！"}');
        exit;
    end;
    c+=' and t1.fcode like '+QuotedStr('%'+code+'%');
end;
var coding:=data.coding;
if trim(coding)<>'' then
begin
    if not regex.ismatch(coding,'\w{1,200}') then
    begin
        print('{"code": 400, "msg": "编码长度不能超过200字符！"}');
        exit;
    end;
    c+=' and t1.fcoding like '+QuotedStr('%'+coding+'%');
end;
// 配置数据库连接
include('/conn.cfg');
var conn:='mydb';
var formatted_pwd := pwd_fmt(token.msg.UserPwd);
var sql := 'SELECT * FROM tEmployee WHERE FClose=0 AND FUserName=:p1 AND FUserPwd=:p2';
var query := json.parse(db.query(conn,sql,token.msg.UserName,formatted_pwd));
if query.code<>200 then
begin
    print(format('{"code": 400, "msg": "%s"}',[query.msg]));
    exit;
end;
if query.data.length()<>1 then
begin
    print('{"code": 403, "msg": "用户名、密码错误，或已被禁用！"}');
    logger.warn(format('用户验证拒绝：IP[%s]用户名[%s]密码[%s]',[login_ip,token.msg.UserName,token.msg.UserPwd]));
    exit;
end;
// 控制数据权限
if query.data[0].FLevel<>2 then
begin
    print('{"code": 403, "msg": "权限不足！"}');
    exit;
end; 
if c.trim<>'' then
    c:=' WHERE t1.FID<>0'+c;
sql:=#'SELECT COUNT(*) AS total
    FROM tDictionary t1'+c;
query := json.parse(db.query(conn,sql));
var total:=0;
if query.data.length()=1 then
    total:=query.data[0].total;
sql:=#'SELECT t1.FID as fid,
    t1.FName AS fname,
    t1.FCode as fcode,
    t1.FCoding as fcoding,
    t1.FNote as fnote
    FROM tDictionary t1'+c
    +' ORDER BY t1.fcode DESC LIMIT '+IntToStr(data.pageSize)
    +' OFFSET '+IntToStr((data.pageIndex - 1) * data.pageSize);
query := json.parse(db.query(conn,sql));
if query.code<>200 then
begin
    print(format('{"code": 400, "msg": "%s"}',[query.msg]));
    exit;
end;
var j:=json.parse(#'{
    "code": 200,
    "msg": "success",
    "data": [],
    "pageIndex": 1,
    "pageSize": 1,
    "total": 1
}');
j.pageIndex:=data.pageIndex;
j.pageSize:=data.pageSize;
j.total:=total;
j.data:=query.data;
for var i := 0 to j.data.length-1 do
begin
    if j.data[i].fclose=0 then
        j.data[i].fclose:=false
    else
        j.data[i].fclose:=true;
    // ISO8601 DateTime 2025-10-22T10:10:10 其实不需要处理，前端会处理
    j.data[i].fdate:=formatdatetime('yyyy-MM-dd hh:nn:ss',ISO8601ToDateTime(j.data[i].fdate));
end;
print(j);