tools.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. local sockethelper = require "http.sockethelper"
  2. local skynet = require "skynet"
  3. local httpd = require "http.httpd"
  4. local cjson = require "cjson"
  5. local crypt = require "client.crypt"
  6. local M = {
  7. }
  8. local headers = {
  9. ['Content-Type'] = 'application/json',
  10. ['Access-Control-Allow-Origin'] = '*', -- 这里写允许访问的域名就可以了,允许所有人访问的话就写*
  11. ['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept',
  12. ['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS',
  13. }
  14. M.read_request = function(id)
  15. -- limit request body size to 8192 (you can pass nil to unlimit)
  16. -- 一般的业务不需要处理大量上行数据,为了防止攻击,做了一个 8K 限制。这个限制可以去掉。
  17. -- local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id), 8192)
  18. local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id))
  19. return {code=code,url=url,method=method,header=header,body=body}
  20. end
  21. M.response = function(id, ...)
  22. local ok, err = httpd.write_response(sockethelper.writefunc(id), ...)
  23. if not ok then
  24. -- if err == sockethelper.socket_error , that means socket closed.
  25. skynet.error(string.format("fd = %d, %s", id, err))
  26. end
  27. end
  28. --检查工具返回的字段是否缺失
  29. M.checkData= function(origin,table)
  30. for _, field in pairs(origin) do
  31. if not table[field] then
  32. return false,field
  33. end
  34. end
  35. return true
  36. end
  37. M.dump = function(res, tab)
  38. tab = tab or 0
  39. if(tab == 0) then
  40. skynet.error("............dump...........")
  41. end
  42. if type(res) == "table" then
  43. skynet.error(string.rep("\t", tab).."{")
  44. for k,v in pairs(res) do
  45. if type(v) == "table" then
  46. skynet.error(k.."=")
  47. M.dump(v, tab + 1)
  48. else
  49. skynet.error(string.rep("\t", tab), k, "=", v, ",")
  50. end
  51. end
  52. skynet.error(string.rep("\t", tab).."}")
  53. else
  54. skynet.error(string.rep("\t", tab) , res)
  55. end
  56. end
  57. M.getDbResData = function(res)
  58. local tab = {}
  59. if #res >1 then
  60. local i = 1;
  61. for k, v in pairs(res) do
  62. -- skynet.error("Row " .. k)
  63. local tab_1 = {}
  64. for field, value in pairs(v) do
  65. tab_1[field] = value
  66. -- skynet.error(field .. ": " .. value)
  67. end
  68. tab[i] = tab_1;
  69. i=i+1;
  70. end
  71. else
  72. for k, v in pairs(res) do
  73. for field, value in pairs(v) do
  74. tab[field] = value
  75. end
  76. end
  77. end
  78. return tab
  79. end
  80. M.tokenEncode = function(user_data)
  81. return crypt.base64encode(user_data)
  82. end
  83. M.tokenDecode = function(base_str)
  84. return crypt.base64decode(base_str)
  85. end
  86. M.base64decode = function(base_str)
  87. return crypt.base64decode(base_str)
  88. end
  89. M.base64encode = function(base_str)
  90. return crypt.base64encode(base_str)
  91. end
  92. M.sort_table_by_keys = function(tbl)
  93. table.sort(tbl)
  94. return tbl
  95. -- local keys = {}
  96. -- -- 提取所有键
  97. -- for key in pairs(tbl) do
  98. -- table.insert(keys, key)
  99. -- end
  100. -- -- 使用table.sort对键进行字典序排序
  101. -- table.sort(keys, function(a, b)
  102. -- return a < b
  103. -- end)
  104. -- -- 创建一个新table,根据排序后的键来组织内容
  105. -- local sorted_tbl = {}
  106. -- for _, key in ipairs(keys) do
  107. -- sorted_tbl[key] = tbl[key]
  108. -- end
  109. -- return sorted_tbl
  110. end
  111. return M