65 lines
1.8 KiB
Lua
65 lines
1.8 KiB
Lua
local utils = {}
|
|
local sys = require("sys")
|
|
function utils.bool_to_number(value)
|
|
return value and 1 or 0
|
|
end
|
|
|
|
function utils.is_empty(str)
|
|
return str == nil or str == ""
|
|
end
|
|
|
|
function utils.clear_table(table)
|
|
for i = 0, #table do
|
|
table[i] = nil
|
|
end
|
|
end
|
|
function utils.table_len(t)
|
|
local leng=0
|
|
if type(t) ~= "table" then
|
|
return leng
|
|
end
|
|
for k, v in pairs(t) do
|
|
leng=leng+1
|
|
end
|
|
return leng;
|
|
end
|
|
function utils.clear_fskv()
|
|
fskv.init()
|
|
fskv.clear()
|
|
end
|
|
-- 用于生成 http 请求的 id
|
|
local http_count = 0
|
|
|
|
--- 对 LuatOS-Air http.request 的封装
|
|
-- @param timeout (number) 超时时间
|
|
-- @param method (string) 请求方法
|
|
-- @param url (string) 请求地址
|
|
-- @param headers (table) 请求头
|
|
-- @param body (string) 请求体
|
|
-- @return (number, table, string) 状态码, 响应头, 响应体
|
|
function utils.fetch(timeout, method, url, headers, body)
|
|
|
|
local code,headers,body = http.request(method, url, headers,body).wait()
|
|
return code, headers, body
|
|
-- timeout = timeout or 1000 * 30
|
|
|
|
-- http_count = http_count + 1
|
|
-- local id = "http_c" .. http_count .. "_r" .. math.random(1000, 9999)
|
|
|
|
-- local function callback(res_result, res_prompt, res_headers, res_body)
|
|
-- sys.publish(id, {res_result, res_prompt, res_headers, res_body})
|
|
-- end
|
|
|
|
-- log.info("utils.fetch", "开始请求", "id:", id)
|
|
-- http.request(method, url, nil, headers, body, timeout, callback)
|
|
-- local result, data = sys.waitUntil(id, 1000 * 60)
|
|
-- log.info("utils.fetch", "请求结束", "id:", id)
|
|
|
|
-- if result == false then
|
|
-- log.warn("utils.fetch", "请求超时", "id:", id)
|
|
-- return 501, {}, "utils.fetch 请求超时"
|
|
-- end
|
|
|
|
-- return tonumber(data[2]) or -99, data[3] or {}, data[4] or ""
|
|
end
|
|
return utils |