a158 8 сар өмнө
parent
commit
1c22cc3315
1 өөрчлөгдсөн 54 нэмэгдсэн , 2 устгасан
  1. 54 2
      target/fwutils/token.lua

+ 54 - 2
target/fwutils/token.lua

@@ -3,6 +3,7 @@ local codec = require("fastweb.codec")
 local utils = require("utils")
 local cjson = require("cjson")
 local redis_pool = require("redis.pool")
+local mysql_pool = require("mysql.pool")
 local M = {}
 local function make_key(id,token)
     if type(id) == "number" then
@@ -10,6 +11,24 @@ local function make_key(id,token)
     end
     return config.server_id.."_token_"..id.."_"..token
 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)
     if token == nil or token == "" then
         return false,"token is required"
@@ -59,9 +78,27 @@ M.del_by_id = function(id)
         return false,"id is required"
     end
     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
         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
     return true
 end
@@ -72,10 +109,12 @@ M.del_by_token = function(token)
         return false,token_data_json
     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()
     conn:del(make_key(token_data_json["id"],token))
+    M.__delete_mysql_token(token,conn_mysql)
     return true
 end
-M.create = function(id,data)
+M.create = function(id,role_id,data)
     if id == nil or id <= 0 then
         return false,"id is required"
     end
@@ -87,6 +126,19 @@ M.create = function(id,data)
     if M.set(token,data) == false then
         return false,"create token failed"
     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
 end
 return M