123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- local sockethelper = require "http.sockethelper"
- local skynet = require "skynet"
- local httpd = require "http.httpd"
- local cjson = require "cjson"
- local crypt = require "client.crypt"
- local M = {
- }
- local headers = {
- ['Content-Type'] = 'application/x-www-form-urlencoded',
- ['Access-Control-Allow-Origin'] = '*', -- 这里写允许访问的域名就可以了,允许所有人访问的话就写*
- ['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept',
- ['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS',
- }
- M.read_request = function(id)
- -- limit request body size to 8192 (you can pass nil to unlimit)
- -- 一般的业务不需要处理大量上行数据,为了防止攻击,做了一个 8K 限制。这个限制可以去掉。
- -- local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id), 8192)
- local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id))
- return {code=code,url=url,method=method,header=header,body=body}
- end
- M.response = function(id, code,msg,...)
- local ok, err = httpd.write_response(sockethelper.writefunc(id),code, msg,...)
- if not ok then
- -- if err == sockethelper.socket_error , that means socket closed.
- skynet.error(string.format("fd = %d, %s", id, err))
- end
- end
- --检查工具返回的字段是否缺失
- M.checkData= function(origin,table)
- for _, field in pairs(origin) do
- if not table[field] then
- return false,field
- end
- end
- return true
- end
- M.dump = function(res, tab)
- tab = tab or 0
- if(tab == 0) then
- skynet.error("............dump...........")
- end
-
- if type(res) == "table" then
- skynet.error(string.rep("\t", tab).."{")
- for k,v in pairs(res) do
- if type(v) == "table" then
- skynet.error(k.."=")
- M.dump(v, tab + 1)
- else
- skynet.error(string.rep("\t", tab), k, "=", v, ",")
- end
- end
- skynet.error(string.rep("\t", tab).."}")
- else
- skynet.error(string.rep("\t", tab) , res)
- end
- end
- M.getDbResData = function(res)
- local tab = {}
- if #res >1 then
- local i = 1;
- for k, v in pairs(res) do
- -- skynet.error("Row " .. k)
- local tab_1 = {}
- for field, value in pairs(v) do
- tab_1[field] = value
- -- skynet.error(field .. ": " .. value)
- end
- tab[i] = tab_1;
- i=i+1;
- end
- else
- for k, v in pairs(res) do
- for field, value in pairs(v) do
- tab[field] = value
- end
- end
- end
- return tab
- end
- M.tokenEncode = function(user_data)
- return crypt.base64encode(user_data)
- end
- M.tokenDecode = function(base_str)
- return crypt.base64decode(base_str)
- end
- M.base64decode = function(base_str)
- return crypt.base64decode(base_str)
- end
- M.base64encode = function(base_str)
- return crypt.base64encode(base_str)
- end
- M.sort_table_by_keys = function(tbl)
- table.sort(tbl)
- return tbl
- -- local keys = {}
- -- -- 提取所有键
- -- for key in pairs(tbl) do
- -- table.insert(keys, key)
- -- end
-
- -- -- 使用table.sort对键进行字典序排序
- -- table.sort(keys, function(a, b)
- -- return a < b
- -- end)
-
- -- -- 创建一个新table,根据排序后的键来组织内容
- -- local sorted_tbl = {}
- -- for _, key in ipairs(keys) do
- -- sorted_tbl[key] = tbl[key]
- -- end
-
- -- return sorted_tbl
- end
- return M
|