request_config.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. local request = require("fastweb.request")
  2. local token_module = require("fwutils.token")
  3. local config = require("fwutils.config")
  4. local cjson = require("cjson")
  5. local utils = require("utils")
  6. local fwutils_init = require("fwutils.init")
  7. local M = {}
  8. M.__filepath = nil
  9. M.__method = nil
  10. M.__url_param = nil
  11. M.__action = nil
  12. M.__user_data = nil
  13. M.__role_id = nil
  14. M.__ext = nil
  15. M.init = function(website_config)
  16. M.__filepath = request.filepath()
  17. M.__method = request.method()
  18. M.__url_param = request.url_param()
  19. M.__action = nil
  20. M.__user_data = nil
  21. M.__role_id = nil
  22. M.__ext = utils.ext(M.__filepath)
  23. -- 修饰路径
  24. if M.__filepath == "/" then
  25. M.__filepath = website_config.default_filepath
  26. end
  27. -- 获取动作
  28. if M.__url_param ~= nil then
  29. M.__action = M.__url_param["action"]
  30. end
  31. -- 获取TOKEN
  32. local token = string.match(request.header("Cookie"),"token=(%w+)")
  33. if token ~= nil and token ~= "" then
  34. local result,user_data = token_module.get(token)
  35. -- print("TOKEN_DATA:",cjson.encode(user_data))
  36. if result and user_data ~= nil and user_data["role_id"] ~= nil and user_data["role_id"] > 0 then
  37. M.__user_data = user_data
  38. M.__role_id = user_data["role_id"]
  39. request.set("user_data",cjson.encode(user_data))
  40. else
  41. M.__role_id = fwutils_init.guest_role_id()
  42. end
  43. else
  44. -- 访客
  45. M.__role_id = fwutils_init.guest_role_id()
  46. end
  47. end
  48. M.role_id = function()
  49. return M.__role_id
  50. end
  51. M.user_data = function()
  52. return M.__user_data
  53. end
  54. M.action = function()
  55. return M.__action
  56. end
  57. M.filepath = function()
  58. return M.__filepath
  59. end
  60. M.ext = function()
  61. return M.__ext
  62. end
  63. return M