增加配置文件,修改返回接口数据

This commit is contained in:
wanglei 2025-10-16 21:02:10 +08:00
parent f3c602f90e
commit d5a73a21db
5 changed files with 139 additions and 9 deletions

View File

@ -7,16 +7,19 @@
local _M = {}
local dao = require("service.system.user")
local resp = require("util.response")
--获取所有用户信息
function _M.get_allusers()
dao.getAllUser()
local res = dao.getAllUser()
response:json(0, 'index args', res)
end
--根据用户id获取用户信息
function _M.get_user(m)
local id = m.id
dao.getUser(id)
local res = dao.getUser(id)
response:json(0, 'index args', res)
end
return _M

View File

@ -7,6 +7,11 @@
return {
APP_ENV = "dev", -- dev/prod
locale = 'en',
fallback_locale = 'zh',
time_zone = "+8:00", -- UTC + 8
-- 配置redis数据库连接
REDIS = {
HOST = "127.0.0.1", -- redis host

58
src/config/status.lua Normal file
View File

@ -0,0 +1,58 @@
return {
zh = {
-- 系统状态码
[0x000000] = 'ok',
[0x000001] = '验证错误',
[0x000002] = '数据不存在',
[0x000003] = '密码错误',
[0x000004] = '未授权访问',
[0x000005] = '系统错误,数据库错误',
[0x000006] = '请求太频繁,请稍后访问',
[0x000007] = '系统错误,系统数据异常',
[0x000008] = '系统错误,共享内存错误',
[0x000009] = '系统错误,发起 Http 请求错误',
[0x00000A] = '系统错误, Cookie 错误',
[0x00000B] = '系统错误,定时器错误',
[0x00000C] = '系统异常,用户未登录',
-- user module
[0x010001] = '注册失败,手机号已存在',
[0x010002] = '登录失败,手机号或密码错误',
[0x010003] = '登录失败,用户不存在',
[0x010004] = '短信验证失败,短信验证码错误',
[0x010005] = '重置密码失败,旧密码错误',
[0x010006] = '重置密码失败,系统异常',
[0x010007] = '重置密码失败,新密码不能和旧密码相同',
[0x010008] = '获取用户信息失败,系统错误',
[0x010009] = '重置密码失败,用户不存在',
[0x01000A] = '获取用户信息失败,用户未登录',
[0x01000B] = '获取用户信息失败,用户不存在',
},
en = {
-- system code
[0x000000] = 'ok',
[0x000001] = 'validate error',
[0x000002] = 'data not found',
[0x000003] = 'password error',
[0x000004] = 'no authorization',
[0x000005] = 'database error',
[0x000006] = 'request frequency please be gentle',
[0x000007] = 'system error,data cache error',
[0x000008] = 'shared memory error',
[0x000009] = 'http request err',
[0x00000A] = 'system error, cookie error',
[0x00000B] = 'system error, timer error',
[0x00000C] = 'system erroruser not authenticat',
-- user module
[0x010001] = 'phone number already exits',
[0x010002] = 'phone no or password error',
[0x010003] = 'user not exits',
[0x010004] = 'SMS verification failed, SMS code error',
[0x010005] = 'fail to reset password, old password error',
[0x010006] = 'fail to reset password, unknow error',
[0x010007] = 'fail to reset password, new password cannot equal to old password',
[0x010008] = 'fail to get user info, system error',
}
}

View File

@ -38,14 +38,15 @@ function _M.getAllUser()
ngx.exit(ngx.HTTP_NOT_FOUND)
end
--整理数据库结果返回值
for _, row in ipairs(res) do
for key, value in pairs(row) do
ngx.say(key .. ":" .. tostring(value))
end
end
--for _, row in ipairs(res) do
-- for key, value in pairs(row) do
-- ngx.say(key .. ":" .. tostring(value))
-- end
--end
--关闭数据库
conn:disconnect()
return cjson.encode(res)
end
--根据用户id获取用户信息
@ -69,8 +70,10 @@ function _M.getUser(id)
-- ngx.say(key .. ":" .. tostring(value))
-- end
--end
ngx.say(cjson.encode(res))
-- ngx.say(cjson.encode(res))
--关闭数据库
conn:disconnect()
return cjson.encode(res)
end
return _M

61
src/util/response.lua Normal file
View File

@ -0,0 +1,61 @@
local cjson = require('cjson')
local conf = require('config.config')
local error_code = require('config.status')
local ngx = ngx
local _M = {}
function _M:json(status, message, data, http_status)
-- you can modify this response struct as you favor
local msg = message
local response_status = http_status or ngx.OK
if msg == nil or msg == '' then
local locale = ngx.ctx.locale or conf.locale
if error_code[locale] ~= nil then
msg = error_code[locale][status]
end
end
local response = {status=status, msg=msg, data=data}
if not response.status then
response.status = -1
response.message = 'not find status code'
end
return {
status = response_status,
headers = {content_type = 'application/json; charset=UTF-8'},
body = cjson.encode(response)
}
end
function _M:raw(http_status, http_body)
return {
status = http_status,
headers = {},
body = http_body
}
end
function _M:error(http_status, http_headers, http_body)
return {
status = http_status,
headers = http_headers,
body = http_body
}
end
function _M:send(response)
ngx.status = response.status
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