diff --git a/src/api/api.lua b/src/api/api.lua index 3b7f251..4cefdc9 100644 --- a/src/api/api.lua +++ b/src/api/api.lua @@ -9,17 +9,35 @@ local userApi = require("api.system.user") --定义相关路由,前端接口url地址 local routes = { { - paths = { "/api/user" }, - metadata = { "metadata user" }, - methods = { "GET", "POST" }, + paths = { "/api/get-users" }, + metadata = { "metadata get-users" }, + methods = { "GET" }, handler = userApi.get_allusers, }, { - paths = { "/api/user/:id" }, - metadata = { "metadata /api/user/id" }, - methods = { "GET", "PUT", "DELETE" }, + paths = { "/api/get-user/:id" }, + metadata = { "metadata /api/get-user/id" }, + methods = { "GET" }, handler = userApi.get_user, }, + { + paths = { "/api/delete-user/:id" }, + metadata = { "metadata /api/delete-user/id" }, + methods = { "DELETE" }, + handler = userApi.delete_user, + }, + { + paths = { "/api/add-user" }, + metadata = { "metadata /api/add-user" }, + methods = { "POST" }, + handler = userApi.add_user, + }, + { + paths = { "/api/update-user/:id" }, + metadata = { "metadata /api/update-user/id" }, + methods = { "PUT" }, + handler = userApi.update_user, + }, } -- 初始化路由 @@ -33,7 +51,6 @@ end local uri = ngx.var.uri local opts = { method = ngx.var.request_method, - --vars = ngx.var, matched = {} } diff --git a/src/api/system/user.lua b/src/api/system/user.lua index 5b30334..a22b39e 100644 --- a/src/api/system/user.lua +++ b/src/api/system/user.lua @@ -9,18 +9,67 @@ local _M = {} local dao = require("service.system.user") local resp = require("util.response") +--验证请求头是否正确 +local function checkReqHeader() + local headers = ngx.req.get_headers() + if headers["content-type"] ~= "application/json" then + local result = resp:json(0x000001) + resp:send(result) + return false + end + return true +end + --获取所有用户信息 function _M.get_allusers() - local ret = dao.getAllUser() - local result = resp:json(0, 'ok', ret) + local code,ret = dao.getAllUser() + local result = resp:json(code, ret) resp:send(result) end --根据用户id获取用户信息 function _M.get_user(m) local id = m.id - local ret = dao.getUser(id) - local result = resp:json(0, 'ok', ret) + local code,ret = dao.getUser(id) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据用户id获取用户信息 +function _M.add_user() + --获取请求头并进行校验 + if checkReqHeader() == false then + return + end + --读取请求体的数据 + ngx.req.read_body() + --获取请求数据 + local body_data = ngx.req.get_body_data() + --判断请求体数据是否为空 + if body_data == nil then + local result = resp:json(0x000001) + resp:send(result) + return + end + --ngx.say(body_data) + local code, ret = dao.addUser(body_data) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据用户id删除用户信息 +function _M.delete_user(m) + local id = m.id + local code, ret = dao.delete_user(id) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据用户id删除用户信息 +function _M.update_user(m) + local id = m.id + local code, ret = dao.update_user(id) + local result = resp:json(code, ret) resp:send(result) end diff --git a/src/config/status.lua b/src/config/status.lua index df091e6..9395b63 100644 --- a/src/config/status.lua +++ b/src/config/status.lua @@ -1,58 +1,21 @@ return { - zh = { - -- 系统状态码 - [0x000000] = 'ok', - [0x000001] = '验证错误', - [0x000002] = '数据不存在', - [0x000003] = '密码错误', - [0x000004] = '未授权访问', - [0x000005] = '系统错误,数据库错误', - [0x000006] = '请求太频繁,请稍后访问', - [0x000007] = '系统错误,系统数据异常', - [0x000008] = '系统错误,共享内存错误', - [0x000009] = '系统错误,发起 Http 请求错误', - [0x00000A] = '系统错误, Cookie 错误', - [0x00000B] = '系统错误,定时器错误', - [0x00000C] = '系统异常,用户未登录', + -- 系统状态码 + [0x000000] = 'ok', + [0x000001] = '验证错误', + [0x000002] = '系统错误', + [0x000003] = '系统异常', + [0x000004] = '未授权访问', - -- 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', - } + -- user module + [0x010001] = '注册失败,手机号已存在', + [0x010002] = '登录失败,手机号或密码错误', + [0x010003] = '登录失败,用户不存在', + [0x010004] = '短信验证失败,短信验证码错误', + [0x010005] = '重置密码失败,旧密码错误', + [0x010006] = '重置密码失败,系统异常', + [0x010007] = '重置密码失败,新密码不能和旧密码相同', + [0x010008] = '获取用户信息失败,系统错误', + [0x010009] = '重置密码失败,用户不存在', + [0x01000A] = '获取用户信息失败,用户未登录', + [0x01000B] = '获取用户信息失败,用户不存在', } diff --git a/src/service/system/user.lua b/src/service/system/user.lua index 11e4957..632c80f 100644 --- a/src/service/system/user.lua +++ b/src/service/system/user.lua @@ -3,23 +3,25 @@ --- Created by . --- DateTime: 2025/9/25 08:19 --- 业务逻辑 ---local cjson = require('cjson') +local cjson = require('cjson') local pgmoon = require('pgmoon') local dbconf = require("config.database") +local status = require("config.status") local _M = {} local function get_con(cfg) + local code = 0 -- 创建一个新的连接 local conn = pgmoon.new(cfg); ---- 连接到数据库 local ok, err = conn:connect() if not ok then error("Connection failed: " .. err) - ngx.exit(ngx.HTTP_NOT_FOUND) + code = 0x000002 end --ngx.say("Connection success") - return conn + return code,conn end -- 查询数据表中的所有用户信息 @@ -27,15 +29,14 @@ function _M.getAllUser() --组装sql语句 local sql = "select * from \"T_Users\"" --获取数据库连接 - local conn = get_con(dbconf.postgres) + local code,conn = get_con(dbconf.postgres) --设置数据库的编码格式 --conn:exec("SET NAMES UTF8") --执行数据库操作 local res = conn:query(sql) if not res then error("Query failed: " .. err) - --ngx.say(err) - ngx.exit(ngx.HTTP_NOT_FOUND) + code = 0x000003 end --整理数据库结果返回值 --for _, row in ipairs(res) do @@ -45,7 +46,7 @@ function _M.getAllUser() --end --关闭数据库 conn:disconnect() - return res + return code,res end --根据用户id获取用户信息 @@ -53,26 +54,60 @@ function _M.getUser(id) --组装sql语句 local sql = "select * from \"T_Users\" where id="..id --获取数据库连接 - local conn = get_con(dbconf.postgres) + local code,conn = get_con(dbconf.postgres) --设置数据库的编码格式 --conn:exec("SET NAMES UTF8") --执行数据库操作 local res = conn:query(sql) if not res then error("Query failed: " .. err) - --ngx.say(err) - ngx.exit(ngx.HTTP_NOT_FOUND) + code = 0x000003 end - --整理数据库结果返回值 - --for _, row in ipairs(res) do - -- for key, value in pairs(row) do - -- ngx.say(key .. ":" .. tostring(value)) - -- end - --end - -- ngx.say(cjson.encode(res)) --关闭数据库 conn:disconnect() - - return res + return code,res end + +--增加用户信息到数据表 +function _M.addUser(jsonData) + --ngx.say(jsonData) + local success, result = pcall(function() + return cjson.decode(jsonData) + end) + local res = nil + if success == false then + return 0x000001,res + end + --组装sql语句 + local sql = "select * from \"T_Users\"" + --获取数据库连接 + local code,conn = get_con(dbconf.postgres) + --执行数据库操作 + res = conn:query(sql) + if not res then + error("Query failed: " .. err) + return 0x000003,res + end + --关闭数据库 + conn:disconnect() + return code,res +end + +--增加用户信息到数据表 +function _M.delete_user(id) + --组装sql语句 + local sql = "delete from \"T_Users\" where id="..id + --获取数据库连接 + local code,conn = get_con(dbconf.postgres) + --执行数据库操作 + local res = conn:query(sql) + if not res then + error("exec sql failed: " .. err) + code = 0x000003 + end + --关闭数据库 + conn:disconnect() + return code,res +end + return _M diff --git a/src/util/response.lua b/src/util/response.lua index 089b92b..4e82bda 100644 --- a/src/util/response.lua +++ b/src/util/response.lua @@ -10,10 +10,11 @@ function _M:json(status, message, data, http_status) 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 + --local locale = ngx.ctx.locale or conf.locale + --if error_code[locale] ~= nil then + --msg = error_code[locale][status] + --end + msg = error_code[status] end local response = {status=status, msg=msg, data=data} if not response.status then @@ -27,6 +28,23 @@ function _M:json(status, message, data, http_status) } end +function _M:json(status, data, http_status) + -- you can modify this response struct as you favor + local msg = '' + local response_status = http_status or ngx.OK + msg = error_code[status] + + 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 {