增加TOKEN的MYSQL表

This commit is contained in:
a158
2026-01-14 22:09:31 +08:00
parent 1ad806f8c8
commit 1c22cc3315

View File

@@ -3,6 +3,7 @@ local codec = require("fastweb.codec")
local utils = require("utils") local utils = require("utils")
local cjson = require("cjson") local cjson = require("cjson")
local redis_pool = require("redis.pool") local redis_pool = require("redis.pool")
local mysql_pool = require("mysql.pool")
local M = {} local M = {}
local function make_key(id,token) local function make_key(id,token)
if type(id) == "number" then if type(id) == "number" then
@@ -10,6 +11,24 @@ local function make_key(id,token)
end end
return config.server_id.."_token_"..id.."_"..token return config.server_id.."_token_"..id.."_"..token
end end
M.__delete_mysql_token = function(token,conn)
local update = conn:update()
update:table("fw_token")
update:set("delete_time=NOW()")
update:where_str("`fw_token`.`token`","=",token)
update:where_expression("AND `fw_token`.`delete_time` IS NULL")
local d = update:exec()
return d == 1
end
M.__list_mysql_token = function(role_id,id,conn)
local select = conn:select()
select:table("fw_token")
select:where_i32("`fw_token`.`role_id`","=",role_id)
select:where_i32("`fw_token`.`custom_id`","=",id)
select:where_expression("AND `fw_token`.`delete_time` IS NULL")
local d = select:query():table()
return d
end
M.__parse_token = function(token) M.__parse_token = function(token)
if token == nil or token == "" then if token == nil or token == "" then
return false,"token is required" return false,"token is required"
@@ -59,9 +78,27 @@ M.del_by_id = function(id)
return false,"id is required" return false,"id is required"
end end
local conn = redis_pool.new(_G[config.db.redis_pool_name]):get() local conn = redis_pool.new(_G[config.db.redis_pool_name]):get()
local keys = conn:keys(config.other.SERVER_ID.."_token_"..string.format("%d",id).."_*") local conn_mysql = mysql_pool.new(_G[config.db.mysql_pool_name]):get()
local keys = conn:keys(config.server_id.."_token_"..string.format("%d",id).."_*")
for _,key in ipairs(keys) do for _,key in ipairs(keys) do
conn:del(key) conn:del(key)
M.__delete_mysql_token(key,conn_mysql)
end
return true
end
M.del_by_id_ex = function(server_id,id)
if server_id == nil or server_id == "" then
return false,"server_id is required"
end
if id == nil or id <= 0 then
return false,"id is required"
end
local conn = redis_pool.new(_G[config.db.redis_pool_name]):get()
local conn_mysql = mysql_pool.new(_G[config.db.mysql_pool_name]):get()
local keys = conn:keys(server_id.."_token_"..string.format("%d",id).."_*")
for _,key in ipairs(keys) do
conn:del(key)
M.__delete_mysql_token(key,conn_mysql)
end end
return true return true
end end
@@ -72,10 +109,12 @@ M.del_by_token = function(token)
return false,token_data_json return false,token_data_json
end end
local conn = redis_pool.new(_G[config.db.redis_pool_name]):get() local conn = redis_pool.new(_G[config.db.redis_pool_name]):get()
local conn_mysql = mysql_pool.new(_G[config.db.mysql_pool_name]):get()
conn:del(make_key(token_data_json["id"],token)) conn:del(make_key(token_data_json["id"],token))
M.__delete_mysql_token(token,conn_mysql)
return true return true
end end
M.create = function(id,data) M.create = function(id,role_id,data)
if id == nil or id <= 0 then if id == nil or id <= 0 then
return false,"id is required" return false,"id is required"
end end
@@ -87,6 +126,19 @@ M.create = function(id,data)
if M.set(token,data) == false then if M.set(token,data) == false then
return false,"create token failed" return false,"create token failed"
end end
-- 保存TOKEN到MYSQL
local conn = mysql_pool.new(_G[config.db.mysql_pool_name]):get()
local ppst = conn:setsql("INSERT INTO fw_token (role_id,server_id,custom_id,token,create_time) VALUES (?,?,?,?,NOW())")
ppst:set_i32(1,role_id)
ppst:set_str(2,config.server_id)
ppst:set_i32(3,id)
ppst:set_str(4,token)
ppst:update()
return true,token return true,token
end end
return M return M