webapi.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. cjson = require("cjson")
  2. fw = require("fastweb")
  3. request = require("fastweb.request")
  4. response = require("fastweb.response")
  5. utils = require("utils")
  6. fwutils_config = require("fwutils.config")
  7. -- 执行
  8. function exec(map)
  9. local action = param("action")
  10. if action == nil or action == "" then
  11. fw.throw_string("action is required")
  12. return
  13. end
  14. if map[action] == nil then
  15. fw.throw_string("action `"..action.."()` not found")
  16. return
  17. end
  18. map[action]()
  19. end
  20. -- 标准回复
  21. function reply(code,msg,data)
  22. local body = cjson.encode({
  23. code = code,
  24. msg = msg,
  25. data = data
  26. })
  27. response.header("Content-Type","application/json")
  28. response.send(body)
  29. end
  30. -- 回复JSON
  31. function reply_json(data)
  32. response.header("Content-Type","application/json")
  33. response.send(cjson.encode(data))
  34. end
  35. -- 快速回复-错误
  36. function error(msg)
  37. reply(201,msg)
  38. end
  39. -- 快速回复-成功
  40. function succ(data)
  41. reply(200,"",data)
  42. end
  43. -- 取请求参数(无参抛异常)
  44. function param_trw(name)
  45. return request.param(name,true)
  46. end
  47. -- 取请求参数
  48. function param(name)
  49. return request.param(name,false)
  50. end
  51. function pint(name)
  52. local result = utils.tointeger(param_trw(name))
  53. if result == nil then
  54. fw.throw_string("Parameters require integers,name:"..name..",value:"..param_trw(name))
  55. else
  56. return result
  57. end
  58. end
  59. function pnum(name)
  60. local result = tonumber(param_trw(name))
  61. if result == nil then
  62. fw.throw_string("Parameters require number,name:"..name..",value:"..param_trw(name))
  63. else
  64. return result
  65. end
  66. end
  67. function pstr(name)
  68. return param_trw(name)
  69. end
  70. function pjson()
  71. return cjson.decode(request.body())
  72. end
  73. -- 查询模型
  74. function query_model_ex(conn,table,field,start,length,callback_gen,callback_data,callback_count,distinct)
  75. local sel_data = conn:select()
  76. local sel_count = conn:select()
  77. sel_data:table(table)
  78. sel_data:field(field)
  79. if start ~= nil and length ~= nil then
  80. sel_data:limit(start,length)
  81. end
  82. sel_count:table(table)
  83. if distinct == nil then
  84. sel_count:field({"COUNT(1)"})
  85. else
  86. sel_count:field(distinct)
  87. end
  88. if callback_gen ~= nil then
  89. callback_gen(sel_data)
  90. callback_gen(sel_count)
  91. end
  92. if callback_data ~= nil then
  93. callback_data(sel_data)
  94. end
  95. if callback_count ~= nil then
  96. callback_count(sel_count)
  97. end
  98. local result_count = sel_count:query()
  99. result_count:next()
  100. local data = {
  101. count = result_count:get(1),
  102. data = sel_data:query():table()
  103. }
  104. return data
  105. end