webapi.lua 2.7 KB

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