117 lines
2.7 KiB
Lua
117 lines
2.7 KiB
Lua
cjson = require("cjson")
|
|
fw = require("fastweb")
|
|
request = require("fastweb.request")
|
|
response = require("fastweb.response")
|
|
utils = require("utils")
|
|
mysql = require("mysql.pool")
|
|
fwutils_config = require("fwutils.config")
|
|
-- 执行
|
|
function exec(map)
|
|
local action = param("action")
|
|
if action == nil or action == "" then
|
|
fw.throw_string("action is required")
|
|
return
|
|
end
|
|
if map[action] == nil then
|
|
fw.throw_string("action `"..action.."()` not found")
|
|
return
|
|
end
|
|
map[action]()
|
|
end
|
|
-- 标准回复
|
|
function reply(code,msg,data)
|
|
local body = cjson.encode({
|
|
code = code,
|
|
msg = msg,
|
|
data = data
|
|
})
|
|
response.header("Content-Type","application/json")
|
|
response.send(body)
|
|
end
|
|
-- 回复JSON
|
|
function reply_json(data)
|
|
response.header("Content-Type","application/json")
|
|
response.send(cjson.encode(data))
|
|
end
|
|
|
|
-- 快速回复-错误
|
|
function error(msg)
|
|
reply(201,msg)
|
|
end
|
|
-- 快速回复-成功
|
|
function succ(data)
|
|
reply(200,"",data)
|
|
end
|
|
-- 取请求参数(无参抛异常)
|
|
function param_trw(name)
|
|
return request.param(name,true)
|
|
end
|
|
-- 取请求参数
|
|
function param(name)
|
|
return request.param(name,false)
|
|
end
|
|
|
|
|
|
function pint(name)
|
|
local result = utils.tointeger(param_trw(name))
|
|
if result == nil then
|
|
fw.throw_string("Parameters require integers,name:"..name..",value:"..param_trw(name))
|
|
else
|
|
return result
|
|
end
|
|
end
|
|
function pnum(name)
|
|
local result = tonumber(param_trw(name))
|
|
if result == nil then
|
|
fw.throw_string("Parameters require number,name:"..name..",value:"..param_trw(name))
|
|
else
|
|
return result
|
|
end
|
|
end
|
|
function pstr(name)
|
|
return param_trw(name)
|
|
end
|
|
function pjson()
|
|
return cjson.decode(request.body())
|
|
end
|
|
-- 查询模型
|
|
function query_model_ex(conn,table,field,start,length,callback_gen,callback_data,callback_count,distinct)
|
|
local sel_data = conn:select()
|
|
local sel_count = conn:select()
|
|
|
|
sel_data:table(table)
|
|
sel_data:field(field)
|
|
if start ~= nil and length ~= nil then
|
|
sel_data:limit(start,length)
|
|
end
|
|
|
|
sel_count:table(table)
|
|
|
|
|
|
if distinct == nil then
|
|
sel_count:field({"COUNT(1)"})
|
|
else
|
|
sel_count:field(distinct)
|
|
end
|
|
|
|
if callback_gen ~= nil then
|
|
callback_gen(sel_data)
|
|
callback_gen(sel_count)
|
|
end
|
|
if callback_data ~= nil then
|
|
callback_data(sel_data)
|
|
end
|
|
if callback_count ~= nil then
|
|
callback_count(sel_count)
|
|
end
|
|
|
|
|
|
local result_count = sel_count:query()
|
|
result_count:next()
|
|
|
|
local data = {
|
|
count = result_count:get(1),
|
|
data = sel_data:query():table()
|
|
}
|
|
return data
|
|
end |