将传入的参数配置到路由中,通过传参进行处理,编写简单的例子,并获取数据内容

This commit is contained in:
wanglei 2025-11-07 10:33:32 +08:00
parent 477478510e
commit 36585af2fe
5 changed files with 67 additions and 20 deletions

View File

@ -14,32 +14,47 @@ local routes = {
{
paths = { "/api/system/users" },
methods = { "GET" },
params = { "system::users::list" },
filter_fun = function(vars)
ngx.ctx.perms = "system::users::list"
return true
end,
handler = systemUser.getSystemUsers,
},
{
paths = { "/api/system/users/:id" },
methods = { "GET" },
filter_fun = function(vars)
ngx.ctx.perms = "system::users::view"
return true
end,
handler = systemUser.getSystemUser,
params = { "system::users::view" }
},
{
paths = { "/api/system/users" },
methods = { "POST" },
filter_fun = function(vars)
ngx.ctx.perms = "system::users::add"
return true
end,
handler = systemUser.addSystemUser,
params = { "system::users::add" }
},
{
paths = { "/api/system/users/:id" },
methods = { "DELETE" },
filter_fun = function(vars)
ngx.ctx.perms = "system::users::delete"
return true
end,
handler = systemUser.deleteSystemUser,
params = { "system::users::delete" }
},
{
paths = { "/api/system/users/:id" },
methods = { "PUT" },
filter_fun = function(vars)
ngx.ctx.perms = "system::users::edit"
return true
end,
handler = systemUser.updateSystemUser,
params = { "system::users::edit" }
},
}

View File

@ -38,4 +38,4 @@ SYSTEM_CONFIG = {
POOL_SIZE = 100, -- postgresql pool size
TIMEOUT = 1000, -- postgresql timeout
}
}
}

View File

@ -37,6 +37,8 @@ local function handler()
local redis = require("resty.redis")
local red = redis:new()
--“admin-system::users::edit“ ”1“
-- 设置超时时间
red:set_timeout(SYSTEM_CONFIG.REDIS.TIMEOUT) -- 1秒

View File

@ -30,16 +30,13 @@ function _M.getSystemUsers(m)
local username = ngx.ctx.username
local role = ngx.ctx.role
ngx.log(ngx.INFO, "userid:"..userid.." username:"..username.." role:"..role)
local vars = m.params
print(vars)
--权限数据
local perms = ngx.ctx.perms
local method = m._method
local path = m._path
ngx.log(ngx.INFO, "path:"..path.." method:"..method)
--local metadata = m.matched.action
--ngx.log(ngx.INFO, "metadata value:"..metadata)
--判断当前接口用户和角色是否有权限
--获取页码和请求的数据量
--local args = ngx.req.get_uri_args()
local pageNum = ngx.var.pagenum or 1

View File

@ -18,22 +18,49 @@ local redis = require("share.redis")
--max =a and b or c--a?b:c
local radix = require("resty.radixtree")
--[[
local opts = {
--host = ngx.var.host,
method = ngx.var.request_method,
--remote_addr = ngx.var.remote_addr,
matched = {}
}
local vars = {
{"arg_name", "==", "json"},
{"arg_weight", ">", 10},
}
local rx = radix.new({
{
paths = {"/aa"},
methods = {"GET"},
filter_fun = function(vars)
return vars["perms"] == "json"
end,
}
})
-- try to match
ngx.say(rx:match("/aa", opts, opts.matched))
--]]
-- 路由处理函数注册表
local function user_handler(params)
print(params.name)
print(params.metadata.service)
local function user_handler(m)
print(m.name)
print(ngx.ctx.perms)
end
-- 创建路由规则
local routes = {
{
paths = {"/user/:name"},
metadata = {
service = "user-service",
timeout = 3000,
},
methods = {"GET"},
filter_fun = function(vars)
ngx.ctx.perms = "system::users::view"
return true
end,
handler = user_handler,
}
}
@ -53,6 +80,12 @@ local opts = {
-- 使用dispatch方法进行路由匹配
local ok = rx:dispatch("/user/123", opts, opts.matched)
--local result, err = rx:match("/user/123", opts, opts.matched)
--if result then
-- print(result)
-- --result.handler(opts, result.metadata)
--end
--[[
--获取用户相关的角色数据的数据
local function init_task()