2025-10-29 17:29:17 +08:00
|
|
|
|
---
|
|
|
|
|
|
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
|
|
|
|
|
--- Created by admin.
|
|
|
|
|
|
--- DateTime: 2025/10/15 09:12
|
|
|
|
|
|
---
|
|
|
|
|
|
--local snowflake = require("util.snowflake")
|
|
|
|
|
|
local helpers = require("share.helpers")
|
|
|
|
|
|
--
|
|
|
|
|
|
--local workerId = 0 -- 假设当前机器的ID是1,范围在[0, 31]之间
|
|
|
|
|
|
--local datacenterId = 0 -- 数据中心ID,范围在[0, 31]之间
|
|
|
|
|
|
--local snow = snowflake.new(workerId, datacenterId)
|
|
|
|
|
|
--local id = snow:generateUniqueId()-- 生成ID
|
|
|
|
|
|
--ngx.say("Generated ID:"..snow.int64_to_string(id))
|
|
|
|
|
|
|
|
|
|
|
|
--max =a and b or c--a?b:c
|
|
|
|
|
|
|
|
|
|
|
|
local cjson = require("cjson.safe")
|
|
|
|
|
|
--[[
|
|
|
|
|
|
local uuid = require("resty.jit-uuid")
|
|
|
|
|
|
uuid.seed()
|
|
|
|
|
|
local val = uuid()
|
|
|
|
|
|
local uid = uuid.generate_v4() ---> v4 UUID
|
|
|
|
|
|
local uid1 = uuid.generate_v3() ---> v3 UUID (name-based with MD5)
|
|
|
|
|
|
local uid2 = uuid.generate_v5() ---> v5 UUID (name-based with SHA-1)
|
|
|
|
|
|
uuid.is_valid() ---> true/false (automatic JIT PCRE or Lua patterns)
|
|
|
|
|
|
--ngx.say(val.." "..uid)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local args = ngx.req.get_uri_args()
|
|
|
|
|
|
local pageNum = args["pagenum"] or 1
|
|
|
|
|
|
local pageSize = args["pagesize"] or 10
|
|
|
|
|
|
ngx.say("pageNum:", pageNum, " pageSize:", pageSize)
|
|
|
|
|
|
--]]
|
|
|
|
|
|
|
2025-10-30 11:30:42 +08:00
|
|
|
|
|
|
|
|
|
|
local jwttoken = require("validator.auth.auth")
|
|
|
|
|
|
--获取请求头中的令牌数据
|
|
|
|
|
|
local auth_header = ngx.var.http_Authorization
|
|
|
|
|
|
--调用令牌校验
|
|
|
|
|
|
local result = jwttoken.check(auth_header)
|
|
|
|
|
|
-- 输出结果
|
|
|
|
|
|
ngx.say(cjson.encode(result))
|
|
|
|
|
|
ngx.exit(result.code)
|
|
|
|
|
|
|
|
|
|
|
|
--[[
|
2025-10-29 17:29:17 +08:00
|
|
|
|
local jsonschema = require("jsonschema")
|
|
|
|
|
|
|
|
|
|
|
|
-- 定义一个JSON Schema
|
|
|
|
|
|
-- 定义一个JSON Schema
|
|
|
|
|
|
local schema = {
|
|
|
|
|
|
{type = "object", properties = {
|
|
|
|
|
|
{name = "username", type = "string"},
|
|
|
|
|
|
{name = "phone", type = "string"},
|
|
|
|
|
|
{name = "email", type = "string"},
|
|
|
|
|
|
{name = "idcard", type = "string"},
|
|
|
|
|
|
{name = "name", type = "string"},
|
|
|
|
|
|
{name = "office_phone", type = "string"},
|
|
|
|
|
|
{name = "telephone", type = "string"},
|
|
|
|
|
|
{name = "display_name", type = "string"},
|
|
|
|
|
|
}, required = {"username", "phone", "email", "idcard"}}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
-- 待验证的JSON数据
|
|
|
|
|
|
--local json_data = '{"name": "Alice", "age": 30}'
|
|
|
|
|
|
--local data, pos, err = cjson.decode(json_data)
|
|
|
|
|
|
--if err then
|
|
|
|
|
|
-- error("JSON decoding error: " .. err)
|
|
|
|
|
|
--end
|
|
|
|
|
|
--读取请求体的数据
|
|
|
|
|
|
ngx.req.read_body()
|
|
|
|
|
|
--获取请求数据
|
|
|
|
|
|
local body_data = ngx.req.get_body_data()
|
|
|
|
|
|
|
|
|
|
|
|
-- 验证数据是否符合schema
|
|
|
|
|
|
local ok, err = jsonschema:generate_validator(body_data, schema)
|
|
|
|
|
|
if not ok then
|
|
|
|
|
|
error("Validation failed: " .. err)
|
|
|
|
|
|
else
|
|
|
|
|
|
print("Validation succeeded!")
|
|
|
|
|
|
end
|
2025-10-30 11:30:42 +08:00
|
|
|
|
--]]
|
2025-10-29 17:29:17 +08:00
|
|
|
|
|
|
|
|
|
|
--[[
|
|
|
|
|
|
local jwt = require("resty.jwt")
|
|
|
|
|
|
|
|
|
|
|
|
local secret_key = "!@#$5412$#@!" -- 确保这个密钥足够安全并保密
|
|
|
|
|
|
|
|
|
|
|
|
--设置JWT的有效载荷
|
|
|
|
|
|
local obj = {
|
|
|
|
|
|
header = {typ="JWT", alg="HS256"},
|
|
|
|
|
|
payload = { -- 自定义数据
|
|
|
|
|
|
username = "admin",
|
|
|
|
|
|
role = "admin",
|
|
|
|
|
|
iss = "your_issuer", -- 签发者
|
|
|
|
|
|
sub = "1234567890", -- 主题
|
|
|
|
|
|
exp = os.time() + 3600, -- 过期时间(例如:当前时间+1小时)
|
|
|
|
|
|
iat = os.time() -- 签发时间
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
local jwt_token = jwt:sign(secret_key, obj)
|
|
|
|
|
|
ngx.say(jwt_token)
|
|
|
|
|
|
|
|
|
|
|
|
local jwt_obj = jwt:verify(secret_key, jwt_token)
|
|
|
|
|
|
local param = cjson.encode(jwt_obj)
|
|
|
|
|
|
ngx.say(param)
|
|
|
|
|
|
|
|
|
|
|
|
local ok, claims = jwt:verify(jwt_token) -- 使用之前生成的token
|
|
|
|
|
|
if ok then
|
|
|
|
|
|
print("Token is valid")
|
|
|
|
|
|
for k, v in pairs(claims) do
|
|
|
|
|
|
print(k, v)
|
|
|
|
|
|
end
|
|
|
|
|
|
else
|
|
|
|
|
|
print("Token is invalid:", claims) -- claims将包含错误信息
|
|
|
|
|
|
end
|
|
|
|
|
|
--]]
|
|
|
|
|
|
|
|
|
|
|
|
--[[
|
|
|
|
|
|
--创建新的JWT对象
|
|
|
|
|
|
--local jwt_obj = jwt:new()
|
|
|
|
|
|
--设置密钥
|
|
|
|
|
|
local secret_key = "!@#$5412$#@!" -- 确保这个密钥足够安全并保密
|
|
|
|
|
|
--jwt_obj:set_secret(secret_key)
|
|
|
|
|
|
|
|
|
|
|
|
--设置JWT的有效载荷
|
|
|
|
|
|
local payload = {
|
|
|
|
|
|
iss = "your_issuer", -- 签发者
|
|
|
|
|
|
exp = os.time() + 3600, -- 过期时间(例如:当前时间+1小时)
|
|
|
|
|
|
iat = os.time(), -- 签发时间
|
|
|
|
|
|
sub = "1234567890", -- 主题
|
|
|
|
|
|
data = { -- 自定义数据
|
|
|
|
|
|
username = "admin",
|
|
|
|
|
|
role = "admin"
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
-- 生成JWT token
|
|
|
|
|
|
local jwt_token, err = jwt:sign(secret_key, payload)
|
|
|
|
|
|
if err then
|
|
|
|
|
|
error("Failed to generate JWT token: " .. err)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
print("Generated JWT Token:", jwt_token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local ok, claims = jwt:verify(jwt_token) -- 使用之前生成的token
|
|
|
|
|
|
if ok then
|
|
|
|
|
|
print("Token is valid")
|
|
|
|
|
|
for k, v in pairs(claims) do
|
|
|
|
|
|
print(k, v)
|
|
|
|
|
|
end
|
|
|
|
|
|
else
|
|
|
|
|
|
print("Token is invalid:", claims) -- claims将包含错误信息
|
|
|
|
|
|
end
|
|
|
|
|
|
--]]
|
|
|
|
|
|
|
|
|
|
|
|
--[[
|
|
|
|
|
|
--引用使用的库文件
|
|
|
|
|
|
local Model = require("share.model")
|
|
|
|
|
|
--创建一个数据表相关的模型
|
|
|
|
|
|
local userModel = Model:new('sys_user')
|
|
|
|
|
|
|
|
|
|
|
|
--获取数据表中的记录数
|
|
|
|
|
|
local code, res = userModel:count()
|
|
|
|
|
|
ngx.say(res)
|
|
|
|
|
|
|
|
|
|
|
|
--查询表中id为1的数据记录
|
|
|
|
|
|
code, res = userModel:find("1")
|
|
|
|
|
|
if res ~= nil then
|
|
|
|
|
|
ngx.say(table.getn(res))
|
|
|
|
|
|
end
|
|
|
|
|
|
--查询表中的所有记录
|
|
|
|
|
|
code, res = userModel:all()
|
|
|
|
|
|
--显示查询到的数据记录
|
|
|
|
|
|
if code == 0 then
|
|
|
|
|
|
for _, row in ipairs(res) do
|
|
|
|
|
|
for key, value in pairs(row) do
|
|
|
|
|
|
ngx.say(key .. ":" .. tostring(value))
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--分页 获取数据表中的记录
|
|
|
|
|
|
local data = nil
|
|
|
|
|
|
code, data = userModel:paginate(1, 10)
|
|
|
|
|
|
local count = data.total
|
|
|
|
|
|
ngx.say("data total:", count)
|
|
|
|
|
|
for _, row in ipairs(data.data) do
|
|
|
|
|
|
ngx.say("begin show data:")
|
|
|
|
|
|
for key, value in pairs(row) do
|
|
|
|
|
|
ngx.say(key .. ":" .. tostring(value))
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
ngx.say("----begin where and query---")
|
|
|
|
|
|
-- 返回 users 表中 username 字段的值是 `cgreen` 的,`password` 字段的值是 `xxxxxx` 的多条数据,注意此处返回是 table 数组,`first()` 方法返回的是单条数据
|
|
|
|
|
|
code, res = userModel:where('name','=','zhangsan'):where('password','=','111111'):get()
|
|
|
|
|
|
--ngx.say(code)
|
|
|
|
|
|
--if res ~= nil then
|
|
|
|
|
|
-- for _, row in ipairs(res) do
|
|
|
|
|
|
-- for key, value in pairs(row) do
|
|
|
|
|
|
-- ngx.say(key .. ":" .. tostring(value))
|
|
|
|
|
|
-- end
|
|
|
|
|
|
-- end
|
|
|
|
|
|
--end
|
|
|
|
|
|
--]]
|
|
|
|
|
|
|
|
|
|
|
|
--[[
|
|
|
|
|
|
--ngx.say("----begin where or query---")
|
|
|
|
|
|
-- 返回 `name` 为 `xxx` 或者 `yyy` 的所有用户 table 数组
|
|
|
|
|
|
code, res = userModel:where('name','=','zhangsan'):orwhere('name','=','admin'):get()
|
|
|
|
|
|
--for _, row in ipairs(res) do
|
|
|
|
|
|
-- for key, value in pairs(row) do
|
|
|
|
|
|
-- ngx.say(key .. ":" .. tostring(value))
|
|
|
|
|
|
-- end
|
|
|
|
|
|
--end
|
|
|
|
|
|
|
|
|
|
|
|
--orderby(column, option)方法,第一个参数传入排序的列名,第二个参数默认为ASC 也可以传入 ASC 正序 或 DESC 倒序(不区分大小写),
|
|
|
|
|
|
code, res = userModel:orderby('created_time'):get()
|
|
|
|
|
|
--for _, row in ipairs(res) do
|
|
|
|
|
|
-- for key, value in pairs(row) do
|
|
|
|
|
|
-- ngx.say(key .. ":" .. tostring(value))
|
|
|
|
|
|
-- end
|
|
|
|
|
|
--end
|
|
|
|
|
|
|
|
|
|
|
|
-- 创建一个用户
|
|
|
|
|
|
code, res = userModel:create({
|
|
|
|
|
|
id='3',
|
|
|
|
|
|
password='22222',
|
|
|
|
|
|
name='lisi',
|
|
|
|
|
|
email='lisi@gmail.com',
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
-- 更新 id = 1 的 user 的 name 为 test, avatar 为 NULL
|
|
|
|
|
|
code, res = userModel:where('id', '=', '3'):update({
|
|
|
|
|
|
phone='666666',
|
|
|
|
|
|
email='zhangsan@qq.com'
|
|
|
|
|
|
})
|
|
|
|
|
|
--输出更新后影响的行总数
|
|
|
|
|
|
ngx.say("update affected_rows: ", res.affected_rows)
|
|
|
|
|
|
|
|
|
|
|
|
-- 删除 id = 1 的用户
|
|
|
|
|
|
code, res = userModel:where('id','=','3'):delete()
|
|
|
|
|
|
ngx.say("delete affected_rows: ", res.affected_rows)
|
|
|
|
|
|
--]]
|
|
|
|
|
|
|
|
|
|
|
|
--[[
|
|
|
|
|
|
--读取请求体的数据
|
|
|
|
|
|
ngx.req.read_body()
|
|
|
|
|
|
--获取请求数据
|
|
|
|
|
|
local body_data = ngx.req.get_body_data()
|
|
|
|
|
|
ngx.say(body_data)
|
|
|
|
|
|
|
|
|
|
|
|
local data = cjson.decode(body_data)
|
|
|
|
|
|
--键值为id产生uuid数据值,增加到json中
|
|
|
|
|
|
data.id = helpers.getUuid()
|
|
|
|
|
|
local ret = helpers.convert_json(data)
|
|
|
|
|
|
ngx.say(ret)
|
|
|
|
|
|
--]]
|
|
|
|
|
|
|
|
|
|
|
|
--local header = ngx.req.get_headers()
|
|
|
|
|
|
--for k,v in pairs(header) do
|
|
|
|
|
|
-- ngx.say("[header] name:", k, "value:", v)
|
|
|
|
|
|
--end
|
|
|
|
|
|
|
|
|
|
|
|
--local payloads = ngx.req.get_uri_args()
|
|
|
|
|
|
--for k,v in pairs(payloads) do
|
|
|
|
|
|
-- ngx.say("[params] name:", k, " value:", v)
|
|
|
|
|
|
--end
|
|
|
|
|
|
|
|
|
|
|
|
--去掉组装最后一位逗号(,)
|
|
|
|
|
|
--local newKeys = keys:sub(1, #keys -1)
|
|
|
|
|
|
--local newValues = values:sub(1, #values -1)
|
|
|
|
|
|
|
|
|
|
|
|
--[[
|
|
|
|
|
|
--读取请求体的数据
|
|
|
|
|
|
--ngx.req.read_body()
|
|
|
|
|
|
|
|
|
|
|
|
--获取请求数据
|
|
|
|
|
|
local body_data = ngx.req.get_body_data()
|
|
|
|
|
|
|
|
|
|
|
|
if not body_data then
|
|
|
|
|
|
ngx.say("read file error:", err)
|
|
|
|
|
|
return ngx.exit(400)
|
|
|
|
|
|
end
|
|
|
|
|
|
local len = #body_data
|
|
|
|
|
|
|
|
|
|
|
|
local file_name = ngx.req.get_body_file()
|
|
|
|
|
|
ngx.say("file length:", len)
|
|
|
|
|
|
|
|
|
|
|
|
ngx.req.read_body_in_buffer(ngx.var.request_body_file)
|
|
|
|
|
|
|
|
|
|
|
|
ngx.say(body_data)
|
|
|
|
|
|
--]]
|
|
|
|
|
|
|
|
|
|
|
|
--[[
|
|
|
|
|
|
local cjson = require("cjson.safe")
|
|
|
|
|
|
local file_path = "/home/frankly/work/test.dat"
|
|
|
|
|
|
local file_length = 1024 * 1024 * 400
|
|
|
|
|
|
local f, err = io.input(file_path, "r")
|
|
|
|
|
|
if not f then
|
|
|
|
|
|
ngx.say("read file error:"..err)
|
|
|
|
|
|
return
|
|
|
|
|
|
end
|
|
|
|
|
|
local content = f:read(file_length) --读取文件内容
|
|
|
|
|
|
f:close() --关闭文件
|
|
|
|
|
|
--ngx.say(content)
|
|
|
|
|
|
local res = {
|
|
|
|
|
|
key = "data",
|
|
|
|
|
|
value = content
|
|
|
|
|
|
}
|
|
|
|
|
|
--ngx.header["Length"] = #content
|
|
|
|
|
|
ngx.header["Content-Type"] = 'application/json; charset=UTF-8'
|
|
|
|
|
|
ngx.say(cjson.encode(res))
|
|
|
|
|
|
ngx.log(ngx.INFO, "send data success")
|
2025-10-27 17:17:58 +08:00
|
|
|
|
--]]
|