2025-10-16 21:02:10 +08:00
|
|
|
local cjson = require('cjson')
|
2025-10-29 17:29:17 +08:00
|
|
|
local conf = require('config')
|
|
|
|
|
local error_code = require('util.status')
|
2025-10-16 21:02:10 +08:00
|
|
|
|
|
|
|
|
local _M = {}
|
|
|
|
|
|
|
|
|
|
function _M:json(status, message, data, http_status)
|
|
|
|
|
-- you can modify this response struct as you favor
|
2025-11-12 09:58:02 +08:00
|
|
|
if status == 0 then status = ngx.HTTP_OK end
|
2025-10-16 21:02:10 +08:00
|
|
|
local msg = message
|
2025-11-12 09:58:02 +08:00
|
|
|
local response_status = http_status or ngx.HTTP_OK
|
2025-10-16 21:02:10 +08:00
|
|
|
if msg == nil or msg == '' then
|
2025-10-20 16:52:40 +08:00
|
|
|
--local locale = ngx.ctx.locale or conf.locale
|
|
|
|
|
--if error_code[locale] ~= nil then
|
|
|
|
|
--msg = error_code[locale][status]
|
|
|
|
|
--end
|
|
|
|
|
msg = error_code[status]
|
2025-10-16 21:02:10 +08:00
|
|
|
end
|
2025-11-12 09:58:02 +08:00
|
|
|
local response = {code = status, msg = msg, result = data,timestamp = ngx.time()}
|
2025-10-31 21:32:39 +08:00
|
|
|
if not response.code then
|
|
|
|
|
response.code = -1
|
2025-10-16 21:02:10 +08:00
|
|
|
response.message = 'not find status code'
|
|
|
|
|
end
|
|
|
|
|
return {
|
2025-10-31 21:32:39 +08:00
|
|
|
code = response_status,
|
2025-10-16 21:02:10 +08:00
|
|
|
headers = {content_type = 'application/json; charset=UTF-8'},
|
|
|
|
|
body = cjson.encode(response)
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2025-10-20 16:52:40 +08:00
|
|
|
function _M:json(status, data, http_status)
|
|
|
|
|
-- you can modify this response struct as you favor
|
2025-11-12 09:58:02 +08:00
|
|
|
if status == 0 then status = ngx.HTTP_OK end
|
2025-10-20 16:52:40 +08:00
|
|
|
local msg = ''
|
2025-11-12 09:58:02 +08:00
|
|
|
local response_status = http_status or ngx.HTTP_OK
|
2025-10-20 16:52:40 +08:00
|
|
|
msg = error_code[status]
|
|
|
|
|
|
2025-11-12 09:58:02 +08:00
|
|
|
local response = {code = status, msg = msg, result = data,timestamp = ngx.time()}
|
2025-10-31 21:32:39 +08:00
|
|
|
if not response.code then
|
|
|
|
|
response.code = -1
|
2025-10-20 16:52:40 +08:00
|
|
|
response.message = 'not find status code'
|
|
|
|
|
end
|
|
|
|
|
return {
|
2025-10-31 21:32:39 +08:00
|
|
|
code = response_status,
|
2025-10-20 16:52:40 +08:00
|
|
|
headers = {content_type = 'application/json; charset=UTF-8'},
|
|
|
|
|
body = cjson.encode(response)
|
|
|
|
|
}
|
|
|
|
|
end
|
2025-10-16 21:02:10 +08:00
|
|
|
|
|
|
|
|
function _M:raw(http_status, http_body)
|
|
|
|
|
return {
|
2025-10-31 21:32:39 +08:00
|
|
|
code = http_status,
|
2025-10-16 21:02:10 +08:00
|
|
|
headers = {},
|
2025-10-31 16:28:00 +08:00
|
|
|
body = http_body,
|
2025-11-04 09:33:40 +08:00
|
|
|
timestamp = ngx.time()
|
2025-10-16 21:02:10 +08:00
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function _M:error(http_status, http_headers, http_body)
|
|
|
|
|
return {
|
2025-10-31 21:32:39 +08:00
|
|
|
code = http_status,
|
2025-10-16 21:02:10 +08:00
|
|
|
headers = http_headers,
|
2025-10-31 16:28:00 +08:00
|
|
|
body = http_body,
|
|
|
|
|
timestamp = ngx.now()
|
2025-10-16 21:02:10 +08:00
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function _M:send(response)
|
2025-10-31 21:32:39 +08:00
|
|
|
ngx.status = response.code
|
2025-10-16 21:02:10 +08:00
|
|
|
if response.headers ~= nil then
|
|
|
|
|
for name, value in pairs(response.headers) do
|
|
|
|
|
ngx.header[name] = value
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if response.body ~= nil then
|
|
|
|
|
ngx.say(response.body)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return _M
|