From d5a73a21db1da923243956c187864aa930422bda Mon Sep 17 00:00:00 2001 From: wanglei <34475144@qq.com> Date: Thu, 16 Oct 2025 21:02:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=EF=BC=8C=E4=BF=AE=E6=94=B9=E8=BF=94=E5=9B=9E=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/system/user.lua | 7 +++-- src/config/config.lua | 5 +++ src/config/status.lua | 58 +++++++++++++++++++++++++++++++++++ src/service/system/user.lua | 17 ++++++----- src/util/response.lua | 61 +++++++++++++++++++++++++++++++++++++ 5 files changed, 139 insertions(+), 9 deletions(-) create mode 100644 src/config/status.lua create mode 100644 src/util/response.lua diff --git a/src/api/system/user.lua b/src/api/system/user.lua index 8212174..dadde25 100644 --- a/src/api/system/user.lua +++ b/src/api/system/user.lua @@ -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 \ No newline at end of file diff --git a/src/config/config.lua b/src/config/config.lua index 83210f6..3a03121 100644 --- a/src/config/config.lua +++ b/src/config/config.lua @@ -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 diff --git a/src/config/status.lua b/src/config/status.lua new file mode 100644 index 0000000..df091e6 --- /dev/null +++ b/src/config/status.lua @@ -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 error,user 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', + } +} diff --git a/src/service/system/user.lua b/src/service/system/user.lua index ac03838..a7f9806 100644 --- a/src/service/system/user.lua +++ b/src/service/system/user.lua @@ -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 diff --git a/src/util/response.lua b/src/util/response.lua new file mode 100644 index 0000000..089b92b --- /dev/null +++ b/src/util/response.lua @@ -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