From 795a60d78d98ed0ddf49981228837ef9ae59a8bd Mon Sep 17 00:00:00 2001 From: wanglei <34475144@qqcom> Date: Wed, 29 Oct 2025 17:29:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E6=95=B4=E4=BD=93=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E4=BF=AE=E6=94=B9=EF=BC=8C=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=9A=82=E6=97=B6=E4=B8=8D=E4=BD=BF=E7=94=A8=E7=9A=84lua?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/nginx.conf | 14 +- conf/system/account.conf | 4 +- conf/system/test.conf | 8 - conf/system/user.conf | 4 +- src/api/api.lua | 265 ---------- src/api/system/account.lua | 117 ++--- src/api/system/application.lua | 131 ++--- src/api/system/auth.lua | 49 ++ src/api/system/controller.lua | 61 --- src/api/system/organization.lua | 115 ++--- src/api/system/permission.lua | 123 ++--- src/api/system/role.lua | 115 ++--- src/api/system/user.lua | 125 ++--- src/{config => }/config.lua | 0 src/config/database.lua | 2 +- src/{model => dao}/account.lua | 2 +- src/{model => dao}/application.lua | 2 +- src/{model => dao}/organization.lua | 2 +- src/{model => dao}/permission.lua | 2 +- src/{model => dao}/role.lua | 2 +- src/dao/user.lua | 94 ++++ src/model/user.lua | 12 - src/routes/route_updater.lua | 2 +- src/service/system/account.lua | 70 ++- src/service/system/application.lua | 87 +++- .../system/{controller.lua => auth.lua} | 53 +- src/service/system/organization.lua | 71 ++- src/service/system/permission.lua | 79 ++- src/service/system/role.lua | 71 ++- src/service/system/user.lua | 138 +++--- src/{util => share}/database.lua | 8 + src/{util => share}/helpers.lua | 6 +- src/share/initial/loading_config.lua | 2 +- src/{util => share}/model.lua | 49 +- src/{util => share}/snowflake.lua | 0 src/test/test.lua | 468 +++++++++++------- src/util/cookie.lua | 213 -------- src/util/response.lua | 4 +- src/{config => util}/status.lua | 3 +- src/util/validator.lua | 33 -- 40 files changed, 1299 insertions(+), 1307 deletions(-) delete mode 100644 conf/system/test.conf delete mode 100644 src/api/api.lua create mode 100644 src/api/system/auth.lua delete mode 100644 src/api/system/controller.lua rename src/{config => }/config.lua (100%) rename src/{model => dao}/account.lua (87%) rename src/{model => dao}/application.lua (87%) rename src/{model => dao}/organization.lua (88%) rename src/{model => dao}/permission.lua (87%) rename src/{model => dao}/role.lua (87%) create mode 100644 src/dao/user.lua delete mode 100644 src/model/user.lua rename src/service/system/{controller.lua => auth.lua} (63%) rename src/{util => share}/database.lua (95%) rename src/{util => share}/helpers.lua (97%) rename src/{util => share}/model.lua (90%) rename src/{util => share}/snowflake.lua (100%) delete mode 100644 src/util/cookie.lua rename src/{config => util}/status.lua (99%) delete mode 100644 src/util/validator.lua diff --git a/conf/nginx.conf b/conf/nginx.conf index fd9ceb7..4d4ff95 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -30,8 +30,20 @@ http { access_log off; } + include 'system/account.conf'; + include 'system/application.conf'; + include 'system/organization.conf'; + include 'system/permission.conf'; + include 'system/role.conf'; include 'system/user.conf'; - include 'system/test.conf'; + #测试接口文件 + location /testSQL { + content_by_lua_file '/home/frankly/work/AuthPlatform/src/test/testPostgres.lua'; + } + + location /cjson { + content_by_lua_file '/home/frankly/work/AuthPlatform/src/test/test.lua'; + } } } \ No newline at end of file diff --git a/conf/system/account.conf b/conf/system/account.conf index 837330c..6292344 100644 --- a/conf/system/account.conf +++ b/conf/system/account.conf @@ -1,4 +1,4 @@ #API接口文件 -location /api { - content_by_lua_file '/home/frankly/work/AuthPlatform/src/api/api.lua'; +location /api/system/accounts { + content_by_lua_file '/home/frankly/work/AuthPlatform/src/api/system/account.lua'; } \ No newline at end of file diff --git a/conf/system/test.conf b/conf/system/test.conf deleted file mode 100644 index 70ba431..0000000 --- a/conf/system/test.conf +++ /dev/null @@ -1,8 +0,0 @@ -#测试接口文件 -location /testSQL { - content_by_lua_file '/home/frankly/work/AuthPlatform/src/test/testPostgres.lua'; -} - -location /cjson { - content_by_lua_file '/home/frankly/work/AuthPlatform/src/test/test.lua'; -} \ No newline at end of file diff --git a/conf/system/user.conf b/conf/system/user.conf index 837330c..9549a3a 100644 --- a/conf/system/user.conf +++ b/conf/system/user.conf @@ -1,4 +1,4 @@ #API接口文件 -location /api { - content_by_lua_file '/home/frankly/work/AuthPlatform/src/api/api.lua'; +location /api/system/users { + content_by_lua_file '/home/frankly/work/AuthPlatform/src/api/system/user.lua'; } \ No newline at end of file diff --git a/src/api/api.lua b/src/api/api.lua deleted file mode 100644 index 8da5491..0000000 --- a/src/api/api.lua +++ /dev/null @@ -1,265 +0,0 @@ ---- ---- Generated by EmmyLua(https://github.com/EmmyLua) ---- Created by admin. ---- DateTime: 2025/9/24 15:29 ---- ---解析url路由过滤库 -local radix = require("resty.radixtree") ---数据表业务处理 -local userApi = require("api.system.user") -local accountApi = require("api.system.account") -local roleApi = require("api.system.role") -local permissionApi = require("api.system.permission") -local applicationApi = require("api.system.application") -local organizationApi = require("api.system.organization") ---业务接口控制 -local controllerApi = require("api.system.controller") - ---定义相关路由,前端接口url地址 -local routes = { - -------------------------------------------- - -----------数据表操作相关路由配置-------------- - -------------------------------------------- - --用户相关路由接口 - { - paths = { "/api/get-user" }, - metadata = { "metadata get-user" }, - methods = { "GET" }, - handler = userApi.get_alluser, - }, - { - 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, - }, - --账户相关路由接口 - { - paths = { "/api/get-account" }, - metadata = { "metadata get-account" }, - methods = { "GET" }, - handler = accountApi.get_allaccount, - }, - { - paths = { "/api/get-account/:id" }, - metadata = { "metadata /api/get-account/id" }, - methods = { "GET" }, - handler = accountApi.get_account, - }, - { - paths = { "/api/delete-account/:id" }, - metadata = { "metadata /api/delete-account/id" }, - methods = { "DELETE" }, - handler = accountApi.delete_account, - }, - { - paths = { "/api/add-account" }, - metadata = { "metadata /api/add-account" }, - methods = { "POST" }, - handler = accountApi.add_account, - }, - { - paths = { "/api/update-account/:id" }, - metadata = { "metadata /api/update-account/id" }, - methods = { "PUT" }, - handler = accountApi.update_account, - }, - --权限相关路由接口 - { - paths = { "/api/get-permission" }, - metadata = { "metadata get-permission" }, - methods = { "GET" }, - handler = permissionApi.get_allpermission, - }, - { - paths = { "/api/get-permission/:id" }, - metadata = { "metadata /api/get-permission/id" }, - methods = { "GET" }, - handler = permissionApi.get_permission, - }, - { - paths = { "/api/delete-permission/:id" }, - metadata = { "metadata /api/delete-permission/id" }, - methods = { "DELETE" }, - handler = permissionApi.delete_permission, - }, - { - paths = { "/api/add-permission" }, - metadata = { "metadata /api/add-permission" }, - methods = { "POST" }, - handler = permissionApi.add_permission, - }, - { - paths = { "/api/update-permission/:id" }, - metadata = { "metadata /api/update-permission/id" }, - methods = { "PUT" }, - handler = permissionApi.update_permission, - }, - { - paths = { "/api/get-permission-by-role/:id" }, - metadata = { "metadata /api/get-permission-by-role/id" }, - methods = { "PUT" }, - handler = permissionApi.get_permission_by_role, - }, - --角色相关路由接口 - { - paths = { "/api/get-role" }, - metadata = { "metadata get-role" }, - methods = { "GET" }, - handler = roleApi.get_allrole, - }, - { - paths = { "/api/get-role/:id" }, - metadata = { "metadata /api/get-role/id" }, - methods = { "GET" }, - handler = roleApi.get_role, - }, - { - paths = { "/api/delete-role/:id" }, - metadata = { "metadata /api/delete-role/id" }, - methods = { "DELETE" }, - handler = roleApi.delete_role, - }, - { - paths = { "/api/add-role" }, - metadata = { "metadata /api/add-role" }, - methods = { "POST" }, - handler = roleApi.add_role, - }, - { - paths = { "/api/update-role/:id" }, - metadata = { "metadata /api/update-role/id" }, - methods = { "PUT" }, - handler = roleApi.update_role, - }, - --应用相关路由接口 - { - paths = { "/api/get-application" }, - metadata = { "metadata get-application" }, - methods = { "GET" }, - handler = applicationApi.get_allapplication, - }, - { - paths = { "/api/get-application/:id" }, - metadata = { "metadata /api/get-application/id" }, - methods = { "GET" }, - handler = applicationApi.get_application, - }, - { - paths = { "/api/get-organization-application/:id" }, - metadata = { "metadata /api/get-organization-application/id" }, - methods = { "GET" }, - handler = applicationApi.get_organization_application, - }, - { - paths = { "/api/get-user-application/:id" }, - metadata = { "metadata /api/get-user-application/id" }, - methods = { "GET" }, - handler = applicationApi.get_user_application, - }, - { - paths = { "/api/delete-application/:id" }, - metadata = { "metadata /api/delete-application/id" }, - methods = { "DELETE" }, - handler = applicationApi.delete_application, - }, - { - paths = { "/api/add-application" }, - metadata = { "metadata /api/add-application" }, - methods = { "POST" }, - handler = applicationApi.add_application, - }, - { - paths = { "/api/update-application/:id" }, - metadata = { "metadata /api/update-application/id" }, - methods = { "PUT" }, - handler = applicationApi.update_application, - }, - --组织架构相关路由接口 - { - paths = { "/api/get-organization" }, - metadata = { "metadata get-organization" }, - methods = { "GET" }, - handler = organizationApi.get_allorganization, - }, - { - paths = { "/api/get-organization/:id" }, - metadata = { "metadata /api/get-organization/id" }, - methods = { "GET" }, - handler = organizationApi.get_organization, - }, - { - paths = { "/api/delete-organization/:id" }, - metadata = { "metadata /api/delete-organization/id" }, - methods = { "DELETE" }, - handler = organizationApi.delete_organization, - }, - { - paths = { "/api/add-organization" }, - metadata = { "metadata /api/add-organization" }, - methods = { "POST" }, - handler = organizationApi.add_organization, - }, - { - paths = { "/api/update-organization/:id" }, - metadata = { "metadata /api/update-organization/id" }, - methods = { "PUT" }, - handler = organizationApi.update_organization, - }, - -------------------------------------------- - -------------用户登录相关路由配置-------------- - -------------------------------------------- - --用户登录路由接口 - { - paths = { "/api/login" }, - metadata = { "metadata /api/login" }, - methods = { "POST" }, - handler = controllerApi.login, - }, - --用户退出路由接口 - { - paths = { "/api/logout/:id" }, - metadata = { "metadata /api/logout/id" }, - methods = { "POST" }, - handler = controllerApi.logout, - }, -} - --- 初始化路由 -local rx, err = radix.new(routes) -if not rx then - ngx.say("Not Found") - ngx.exit(ngx.HTTP_NOT_FOUND) -end - ---获取访问的uri地址 -local uri = ngx.var.uri -local opts = { - method = ngx.var.request_method, - matched = {} -} - --- 进行路由匹配和相关函数调用 -local ok = rx:dispatch(uri, opts, opts.matched) -if not ok then - ngx.say("Not Found") - ngx.exit(ngx.HTTP_NOT_FOUND) -end diff --git a/src/api/system/account.lua b/src/api/system/account.lua index 2e591fd..e4dad4b 100644 --- a/src/api/system/account.lua +++ b/src/api/system/account.lua @@ -4,75 +4,58 @@ --- DateTime: 2025/9/25 08:19 --- -local _M = {} +--解析url路由过滤库 +local radix = require("resty.radixtree") +--数据表业务处理 +local systemAccount = require("service.system.account") -local dao = require("service.system.account") -local resp = require("util.response") -local validator = require("util.validator") +--定义相关路由,前端接口url地址 +local routes = { + --用户相关路由接口 + { + paths = { "/api/system/accounts" }, + methods = { "GET" }, + handler = systemAccount.getSystemAccounts, + }, + { + paths = { "/api/system/accounts/:id" }, + methods = { "GET" }, + handler = systemAccount.getSystemAccount, + }, + { + paths = { "/api/system/accounts" }, + methods = { "POST" }, + handler = systemAccount.addSystemAccount, + }, + { + paths = { "/api/system/accounts/:id" }, + methods = { "DELETE" }, + handler = systemAccount.deleteSystemAccount, + }, + { + paths = { "/api/system/accounts/:id" }, + methods = { "PUT" }, + handler = systemAccount.updateSystemAccount, + }, +} ---获取所有账户信息 -function _M.get_allaccount() - local code,ret = dao.getAllAccount() - local result = resp:json(code, ret) - resp:send(result) +-- 初始化路由 +local rx, err = radix.new(routes) +if not rx then + ngx.say("Not Found") + ngx.exit(ngx.HTTP_NOT_FOUND) end ---根据账户id获取账户信息 -function _M.get_account(m) - local id = m.id - local code,ret = dao.getAccount(id) - local result = resp:json(code, ret) - resp:send(result) -end +--获取访问的uri地址 +local uri = ngx.var.uri +local opts = { + method = ngx.var.request_method, + matched = {} +} ---根据账户id获取账户信息 -function _M.add_account() - --获取请求头并进行校验 - if validator.checkReqHeader() == false then - local result = resp:json(0x000001) - resp:send(result) - 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.addAccount(body_data) - local result = resp:json(code, ret) - resp:send(result) -end - ---根据账户id删除账户信息 -function _M.delete_account(m) - local id = m.id - local code, ret = dao.deleteAccount(id) - local result = resp:json(code, ret) - resp:send(result) -end - ---根据账户id删除账户信息 -function _M.update_account(m) - local id = m.id - --读取请求体的数据 - 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 - local code, ret = dao.updateAccount(id, body_data) - local result = resp:json(code, ret) - resp:send(result) -end - -return _M \ No newline at end of file +-- 进行路由匹配和相关函数调用 +local ok = rx:dispatch(uri, opts, opts.matched) +if not ok then + ngx.say("Not Found") + ngx.exit(ngx.HTTP_NOT_FOUND) +end \ No newline at end of file diff --git a/src/api/system/application.lua b/src/api/system/application.lua index 84324eb..a8dcf6e 100644 --- a/src/api/system/application.lua +++ b/src/api/system/application.lua @@ -4,91 +4,58 @@ --- DateTime: 2025/9/25 08:19 --- -local _M = {} +--解析url路由过滤库 +local radix = require("resty.radixtree") +--数据表业务处理 +local systemApplication = require("service.system.application") -local dao = require("service.system.application") -local resp = require("util.response") -local validator = require("util.validator") +--定义相关路由,前端接口url地址 +local routes = { + --用户相关路由接口 + { + paths = { "/api/system/applications" }, + methods = { "GET" }, + handler = systemApplication.getSystemApplications, + }, + { + paths = { "/api/system/applications/:id" }, + methods = { "GET" }, + handler = systemApplication.getSystemApplication, + }, + { + paths = { "/api/system/applications" }, + methods = { "POST" }, + handler = systemApplication.addSystemApplication, + }, + { + paths = { "/api/system/applications/:id" }, + methods = { "DELETE" }, + handler = systemApplication.deleteSystemApplication, + }, + { + paths = { "/api/system/applications/:id" }, + methods = { "PUT" }, + handler = systemApplication.updateSystemApplication, + }, +} ---获取所有应用程序信息 -function _M.get_allapplication() - local code,ret = dao.getAllApplication() - local result = resp:json(code, ret) - resp:send(result) +-- 初始化路由 +local rx, err = radix.new(routes) +if not rx then + ngx.say("Not Found") + ngx.exit(ngx.HTTP_NOT_FOUND) end ---根据应用id获取应用信息 -function _M.get_application(m) - local id = m.id - local code,ret = dao.getApplication(id) - local result = resp:json(code, ret) - resp:send(result) -end +--获取访问的uri地址 +local uri = ngx.var.uri +local opts = { + method = ngx.var.request_method, + matched = {} +} ---根据组织id获取权限的信息 -function _M.get_organization_application(m) - local id = m.id - local code,ret = dao.getOrganizationApplication(id) - local result = resp:json(code, ret) - resp:send(result) +-- 进行路由匹配和相关函数调用 +local ok = rx:dispatch(uri, opts, opts.matched) +if not ok then + ngx.say("Not Found") + ngx.exit(ngx.HTTP_NOT_FOUND) end - ---根据用户id获取权限的信息 -function _M.get_user_application(m) - local id = m.id - local code,ret = dao.getUserApplication(id) - local result = resp:json(code, ret) - resp:send(result) -end - ---根据应用id获取应用信息 -function _M.add_application() - --获取请求头并进行校验 - if validator.checkReqHeader() == false then - local result = resp:json(0x000001) - resp:send(result) - 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.addApplication(body_data) - local result = resp:json(code, ret) - resp:send(result) -end - ---根据应用id删除应用信息 -function _M.delete_application(m) - local id = m.id - local code, ret = dao.deleteApplication(id) - local result = resp:json(code, ret) - resp:send(result) -end - ---根据应用id删除应用信息 -function _M.update_application(m) - local id = m.id - --读取请求体的数据 - 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 - local code, ret = dao.updateApplication(id, body_data) - local result = resp:json(code, ret) - resp:send(result) -end - -return _M \ No newline at end of file diff --git a/src/api/system/auth.lua b/src/api/system/auth.lua new file mode 100644 index 0000000..2c70f83 --- /dev/null +++ b/src/api/system/auth.lua @@ -0,0 +1,49 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by admin. +--- DateTime: 2025/10/28 11:09 +--- +--解析url路由过滤库 +local radix = require("resty.radixtree") +--数据表业务处理 +local systemAuth = require("service.system.auth") + +--定义相关路由,前端接口url地址 +local routes = { + -------------------------------------------- + -------------用户登录相关路由配置-------------- + -------------------------------------------- + --用户登录路由接口 + { + paths = { "/api/login" }, + methods = { "POST" }, + handler = systemAuth.login, + }, + --用户退出路由接口 + { + paths = { "/api/logout/:id" }, + methods = { "POST" }, + handler = systemAuth.logout, + }, +} + +-- 初始化路由 +local rx, err = radix.new(routes) +if not rx then + ngx.say("Not Found") + ngx.exit(ngx.HTTP_NOT_FOUND) +end + +--获取访问的uri地址 +local uri = ngx.var.uri +local opts = { + method = ngx.var.request_method, + matched = {} +} + +-- 进行路由匹配和相关函数调用 +local ok = rx:dispatch(uri, opts, opts.matched) +if not ok then + ngx.say("Not Found") + ngx.exit(ngx.HTTP_NOT_FOUND) +end diff --git a/src/api/system/controller.lua b/src/api/system/controller.lua deleted file mode 100644 index c78d302..0000000 --- a/src/api/system/controller.lua +++ /dev/null @@ -1,61 +0,0 @@ ---- ---- Generated by EmmyLua(https://github.com/EmmyLua) ---- Created by admin. ---- DateTime: 2025/10/28 11:09 ---- -local validator = require("util.validator") -local helpers = require("util.helpers") -local resp = require("util.response") -local dao = require("service.system.controller") - -local _M = {} - ---用户登录业务逻辑处理 -function _M.login() - --获取请求头并进行校验 - if validator.checkReqHeader() == false then - local result = resp:json(0x000001) - resp:send(result) - 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.login(body_data) - local result = resp:json(code, ret) - resp:send(result) -end - ---用户登出业务逻辑处理 -function _M.logout() - --获取请求头并进行校验 - if validator.checkReqHeader() == false then - local result = resp:json(0x000001) - resp:send(result) - 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.logout(body_data) - local result = resp:json(code, ret) - resp:send(result) -end - -return _M \ No newline at end of file diff --git a/src/api/system/organization.lua b/src/api/system/organization.lua index 4680c7e..de11203 100644 --- a/src/api/system/organization.lua +++ b/src/api/system/organization.lua @@ -4,75 +4,58 @@ --- DateTime: 2025/10/28 10:05 --- -local _M = {} +--解析url路由过滤库 +local radix = require("resty.radixtree") +--数据表业务处理 +local systemOrganization = require("service.system.organization") -local dao = require("service.system.organization") -local resp = require("util.response") -local validator = require("util.validator") +--定义相关路由,前端接口url地址 +local routes = { + --用户相关路由接口 + { + paths = { "/api/system/organizations" }, + methods = { "GET" }, + handler = systemOrganization.getSystemOrganization, + }, + { + paths = { "/api/system/organizations/:id" }, + methods = { "GET" }, + handler = systemOrganization.getSystemOrganization, + }, + { + paths = { "/api/system/organizations" }, + methods = { "POST" }, + handler = systemOrganization.addSystemOrganization, + }, + { + paths = { "/api/system/organizations/:id" }, + methods = { "DELETE" }, + handler = systemOrganization.deleteSystemOrganization, + }, + { + paths = { "/api/system/organizations/:id" }, + methods = { "PUT" }, + handler = systemOrganization.updateSystemOrganization, + }, +} ---获取所有组织架构信息 -function _M.get_allorganization() - local code,ret = dao.getAllOrganization() - local result = resp:json(code, ret) - resp:send(result) +-- 初始化路由 +local rx, err = radix.new(routes) +if not rx then + ngx.say("Not Found") + ngx.exit(ngx.HTTP_NOT_FOUND) end ---根据组织id获取组织架构信息 -function _M.get_organization(m) - local id = m.id - local code,ret = dao.getOrganization(id) - local result = resp:json(code, ret) - resp:send(result) -end +--获取访问的uri地址 +local uri = ngx.var.uri +local opts = { + method = ngx.var.request_method, + matched = {} +} ---根据组织id添加组织架构信息 -function _M.add_organization() - --获取请求头并进行校验 - if validator.checkReqHeader() == false then - local result = resp:json(0x000001) - resp:send(result) - 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.addOrganization(body_data) - local result = resp:json(code, ret) - resp:send(result) +-- 进行路由匹配和相关函数调用 +local ok = rx:dispatch(uri, opts, opts.matched) +if not ok then + ngx.say("Not Found") + ngx.exit(ngx.HTTP_NOT_FOUND) end - ---根据组织id删除组织架构信息 -function _M.delete_organization(m) - local id = m.id - local code, ret = dao.deleteOrganization(id) - local result = resp:json(code, ret) - resp:send(result) -end - ---根据组织id删除组织架构信息 -function _M.update_organization(m) - local id = m.id - --读取请求体的数据 - 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 - local code, ret = dao.updateOrganization(id, body_data) - local result = resp:json(code, ret) - resp:send(result) -end - -return _M \ No newline at end of file diff --git a/src/api/system/permission.lua b/src/api/system/permission.lua index 87d46fb..e2e7810 100644 --- a/src/api/system/permission.lua +++ b/src/api/system/permission.lua @@ -4,83 +4,58 @@ --- DateTime: 2025/9/25 08:19 --- -local _M = {} +--解析url路由过滤库 +local radix = require("resty.radixtree") +--数据表业务处理 +local systemPermission = require("service.system.permission") -local dao = require("service.system.permission") -local resp = require("util.response") -local validator = require("util.validator") +--定义相关路由,前端接口url地址 +local routes = { + --用户相关路由接口 + { + paths = { "/api/system/permissions" }, + methods = { "GET" }, + handler = systemPermission.getSystemPermissions, + }, + { + paths = { "/api/system/permissions/:id" }, + methods = { "GET" }, + handler = systemPermission.getSystemPermission, + }, + { + paths = { "/api/system/permissions" }, + methods = { "POST" }, + handler = systemPermission.addSystemPermission, + }, + { + paths = { "/api/system/permissions/:id" }, + methods = { "DELETE" }, + handler = systemPermission.deleteSystemPermission, + }, + { + paths = { "/api/system/permission/:id" }, + methods = { "PUT" }, + handler = systemPermission.updateSystemPermission, + }, +} ---获取所有权限信息 -function _M.get_allpermission() - local code,ret = dao.getAllPermission() - local result = resp:json(code, ret) - resp:send(result) +-- 初始化路由 +local rx, err = radix.new(routes) +if not rx then + ngx.say("Not Found") + ngx.exit(ngx.HTTP_NOT_FOUND) end ---根据权限id获取权限信息 -function _M.get_permission(m) - local id = m.id - local code,ret = dao.getPermission(id) - local result = resp:json(code, ret) - resp:send(result) -end +--获取访问的uri地址 +local uri = ngx.var.uri +local opts = { + method = ngx.var.request_method, + matched = {} +} ---根据角色id获取使用的权限 -function _M.get_permission_by_role(m) - local id = m.id - local code,ret = dao.getPermissionByRole(id) - local result = resp:json(code, ret) - resp:send(result) +-- 进行路由匹配和相关函数调用 +local ok = rx:dispatch(uri, opts, opts.matched) +if not ok then + ngx.say("Not Found") + ngx.exit(ngx.HTTP_NOT_FOUND) end - ---根据账号id获取账号信息 -function _M.add_permission() - --获取请求头并进行校验 - if validator.checkReqHeader() == false then - local result = resp:json(0x000001) - resp:send(result) - 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.addPermission(body_data) - local result = resp:json(code, ret) - resp:send(result) -end - ---根据账号id删除账号信息 -function _M.delete_permission(m) - local id = m.id - local code, ret = dao.deletePermission(id) - local result = resp:json(code, ret) - resp:send(result) -end - ---根据账号id删除账号信息 -function _M.update_permission(m) - local id = m.id - --读取请求体的数据 - 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 - local code, ret = dao.updatePermission(id, body_data) - local result = resp:json(code, ret) - resp:send(result) -end - -return _M \ No newline at end of file diff --git a/src/api/system/role.lua b/src/api/system/role.lua index aef901c..957654a 100644 --- a/src/api/system/role.lua +++ b/src/api/system/role.lua @@ -4,75 +4,58 @@ --- DateTime: 2025/9/25 08:19 --- -local _M = {} +--解析url路由过滤库 +local radix = require("resty.radixtree") +--数据表业务处理 +local systemRole = require("service.system.role") -local dao = require("service.system.role") -local resp = require("util.response") -local validator = require("util.validator") +--定义相关路由,前端接口url地址 +local routes = { + --用户相关路由接口 + { + paths = { "/api/system/roles" }, + methods = { "GET" }, + handler = systemRole.getSystemRoles, + }, + { + paths = { "/api/system/roles/:id" }, + methods = { "GET" }, + handler = systemRole.getSystemRole, + }, + { + paths = { "/api/system/roles" }, + methods = { "POST" }, + handler = systemRole.addSystemRole, + }, + { + paths = { "/api/system/roles/:id" }, + methods = { "DELETE" }, + handler = systemRole.deleteSystemRole, + }, + { + paths = { "/api/system/roles/:id" }, + methods = { "PUT" }, + handler = systemRole.updateSystemRole, + }, +} ---获取所有角色信息 -function _M.get_allrole() - local code,ret = dao.getAllRole() - local result = resp:json(code, ret) - resp:send(result) +-- 初始化路由 +local rx, err = radix.new(routes) +if not rx then + ngx.say("Not Found") + ngx.exit(ngx.HTTP_NOT_FOUND) end ---根据角色id获取角色信息 -function _M.get_role(m) - local id = m.id - local code,ret = dao.getRole(id) - local result = resp:json(code, ret) - resp:send(result) -end +--获取访问的uri地址 +local uri = ngx.var.uri +local opts = { + method = ngx.var.request_method, + matched = {} +} ---根据角色id获取角色信息 -function _M.add_role() - --获取请求头并进行校验 - if validator.checkReqHeader() == false then - local result = resp:json(0x000001) - resp:send(result) - 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.addRole(body_data) - local result = resp:json(code, ret) - resp:send(result) +-- 进行路由匹配和相关函数调用 +local ok = rx:dispatch(uri, opts, opts.matched) +if not ok then + ngx.say("Not Found") + ngx.exit(ngx.HTTP_NOT_FOUND) end - ---根据角色id删除角色信息 -function _M.delete_role(m) - local id = m.id - local code, ret = dao.deleteRole(id) - local result = resp:json(code, ret) - resp:send(result) -end - ---根据角色id删除角色信息 -function _M.update_role(m) - local id = m.id - --读取请求体的数据 - 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 - local code, ret = dao.updateRole(id, body_data) - local result = resp:json(code, ret) - resp:send(result) -end - -return _M \ No newline at end of file diff --git a/src/api/system/user.lua b/src/api/system/user.lua index df1ed06..8ef7015 100644 --- a/src/api/system/user.lua +++ b/src/api/system/user.lua @@ -3,85 +3,58 @@ --- Created by admin. --- DateTime: 2025/9/25 08:19 --- +--解析url路由过滤库 +local radix = require("resty.radixtree") +--数据表业务处理 +local systemUser = require("service.system.user") -local _M = {} +--定义相关路由,前端接口url地址 +local routes = { + --用户相关路由接口 + { + paths = { "/api/system/users" }, + methods = { "GET" }, + handler = systemUser.getSystemUsers, + }, + { + paths = { "/api/system/users/:id" }, + methods = { "GET" }, + handler = systemUser.getSystemUser, + }, + { + paths = { "/api/system/users" }, + methods = { "POST" }, + handler = systemUser.addSystemUser, + }, + { + paths = { "/api/system/users/:id" }, + methods = { "DELETE" }, + handler = systemUser.deleteSystemUser, + }, + { + paths = { "/api/system/user/:id" }, + methods = { "PUT" }, + handler = systemUser.updateSystemUser, + }, +} -local dao = require("service.system.user") -local resp = require("util.response") -local validator = require("util.validator") - ---获取所有用户信息 -function _M.get_alluser() - --获取页码和请求的数据量 - local page = 0 - local pagesize = 0 - local payloads = ngx.req.get_uri_args() - for k,v in pairs(payloads) do - if k == "page" then page = v end - if k == "pagesize" then pagesize = v end - end - --ngx.say("page:", page, " pagesize:", pagesize) - local code,ret = dao.getAllUser() - local result = resp:json(code, ret) - resp:send(result) +-- 初始化路由 +local rx, err = radix.new(routes) +if not rx then + ngx.say("Not Found") + ngx.exit(ngx.HTTP_NOT_FOUND) end ---根据用户id获取用户信息 -function _M.get_user(m) - local id = m.id - local code,ret = dao.getUser(id) - local result = resp:json(code, ret) - resp:send(result) -end +--获取访问的uri地址 +local uri = ngx.var.uri +local opts = { + method = ngx.var.request_method, + matched = {} +} ---根据用户id获取用户信息 -function _M.add_user() - --获取请求头并进行校验 - if validator.checkReqHeader() == false then - local result = resp:json(0x000001) - resp:send(result) - 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) +-- 进行路由匹配和相关函数调用 +local ok = rx:dispatch(uri, opts, opts.matched) +if not ok then + ngx.say("Not Found") + ngx.exit(ngx.HTTP_NOT_FOUND) end - ---根据用户id删除用户信息 -function _M.delete_user(m) - local id = m.id - local code, ret = dao.deleteUser(id) - local result = resp:json(code, ret) - resp:send(result) -end - ---根据用户id删除用户信息 -function _M.update_user(m) - local id = m.id - --读取请求体的数据 - 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 - local code, ret = dao.updateUser(id, body_data) - local result = resp:json(code, ret) - resp:send(result) -end - -return _M \ No newline at end of file diff --git a/src/config/config.lua b/src/config.lua similarity index 100% rename from src/config/config.lua rename to src/config.lua diff --git a/src/config/database.lua b/src/config/database.lua index 691cc49..5288618 100644 --- a/src/config/database.lua +++ b/src/config/database.lua @@ -1,4 +1,4 @@ -local env = require('config.config') +local env = require('config') return { redis_prefix = 'Auth:', diff --git a/src/model/account.lua b/src/dao/account.lua similarity index 87% rename from src/model/account.lua rename to src/dao/account.lua index 71b033e..5bc11a0 100644 --- a/src/model/account.lua +++ b/src/dao/account.lua @@ -5,7 +5,7 @@ --- 数据表模型文件 --引用使用的库文件 -local Model = require("util.model") +local Model = require("share.model") --创建一个数据表相关的模型 local Account = Model:new('tbl_account') diff --git a/src/model/application.lua b/src/dao/application.lua similarity index 87% rename from src/model/application.lua rename to src/dao/application.lua index 3bc1414..a1b5c72 100644 --- a/src/model/application.lua +++ b/src/dao/application.lua @@ -5,7 +5,7 @@ --- 数据表模型文件 --引用使用的库文件 -local Model = require("util.model") +local Model = require("share.model") --创建一个数据表相关的模型 local Application = Model:new('tbl_application') diff --git a/src/model/organization.lua b/src/dao/organization.lua similarity index 88% rename from src/model/organization.lua rename to src/dao/organization.lua index 324ea70..14af9af 100644 --- a/src/model/organization.lua +++ b/src/dao/organization.lua @@ -5,7 +5,7 @@ --- 数据表模型文件 --引用使用的库文件 -local Model = require("util.model") +local Model = require("share.model") --创建一个数据表相关的模型 local Organization = Model:new('tbl_organization') diff --git a/src/model/permission.lua b/src/dao/permission.lua similarity index 87% rename from src/model/permission.lua rename to src/dao/permission.lua index 0d96ade..9a8c471 100644 --- a/src/model/permission.lua +++ b/src/dao/permission.lua @@ -5,7 +5,7 @@ --- 数据表模型文件 --引用使用的库文件 -local Model = require("util.model") +local Model = require("share.model") --创建一个数据表相关的模型 local Permission = Model:new('tbl_permission') diff --git a/src/model/role.lua b/src/dao/role.lua similarity index 87% rename from src/model/role.lua rename to src/dao/role.lua index 194899d..eac1493 100644 --- a/src/model/role.lua +++ b/src/dao/role.lua @@ -5,7 +5,7 @@ --- 数据表模型文件 --引用使用的库文件 -local Model = require("util.model") +local Model = require("share.model") --创建一个数据表相关的模型 local Role = Model:new('tbl_role') diff --git a/src/dao/user.lua b/src/dao/user.lua new file mode 100644 index 0000000..53bcfc6 --- /dev/null +++ b/src/dao/user.lua @@ -0,0 +1,94 @@ +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by admin. +--- DateTime: 2025/10/25 16:36 +--- 数据表模型文件 + +local helpers = require("share.helpers") +--引用使用的库文件 +local model = require("share.model") +--创建一个数据表相关的模型 +local userModel = model:new('sys_user') + +local _M = {} + +--判断用户是否存在 +local function isExistUser(id) + --根据用户id进行验证用户是否存在 + local code, res = userModel:find(id) + if code ~= 0 then + return false + end + local num = 0 + if res ~= nil then + num = table.getn(res) + end + --用户不存在返回错误 + if num <= 0 then + return false + end + return true +end + +-- 查询数据表中的所有用户信息 +function _M.getAllUser(pageNum, pageSize) + return userModel:paginate(pageNum, pageSize) +end + +--根据用户id获取用户信息 +function _M.getUser(id) + return userModel:find(id) +end + +--增加用户信息到数据表 +function _M.addUser(jsonData) + --解析json中的键和数据值 + local userName = jsonData['username'] + local phone = jsonData['phone'] + local email = jsonData['email'] + + --根据用户、手机号、邮箱进行验证用户是否存在 + local code, res = userModel:where("username", "=", userName):where("phone", "=", phone):where("email", "=", email):get() + if code ~= 0 then + return 0x000001,res + end + local num = 0 + if res ~= nil then + num = table.getn(res) + end + --用户存在时返回用户已经存在 + if num > 0 then + return 0x01000C,nil + end + + --键值为id产生uuid数据值,增加到json中 + jsonData.id = helpers.getUuid() + local ret = helpers.convert_json(jsonData) + -- 创建一个用户 + return userModel:create('{'..ret..'}') +end + +--删除用户信息到数据表 +function _M:deleteUser(id) + --根据用户id进行验证用户是否存在 + local ok = isExistUser(id) + --用户不存在则返回 + if ok == false then + return 0x000001,nil + end + return userModel:delete(id) +end + +--更新用户信息到数据表 +function _M:updateUser(id, jsonData) + --根据用户id进行验证用户是否存在 + local ok = isExistUser(id) + --用户不存在则返回 + if ok == false then + return 0x000001,nil + end + --对数据内容进行更新 + return userModel:where('id', '=', id):update(jsonData) +end + +return _M \ No newline at end of file diff --git a/src/model/user.lua b/src/model/user.lua deleted file mode 100644 index 3a7c622..0000000 --- a/src/model/user.lua +++ /dev/null @@ -1,12 +0,0 @@ ---- ---- Generated by EmmyLua(https://github.com/EmmyLua) ---- Created by admin. ---- DateTime: 2025/10/25 16:36 ---- 数据表模型文件 - ---引用使用的库文件 -local Model = require("util.model") - ---创建一个数据表相关的模型 -local User = Model:new('tbl_user') -return User \ No newline at end of file diff --git a/src/routes/route_updater.lua b/src/routes/route_updater.lua index 0c37356..9703369 100644 --- a/src/routes/route_updater.lua +++ b/src/routes/route_updater.lua @@ -4,7 +4,7 @@ --- DateTime: 2025/9/24 18:14 --- -local cjson = require("cjson") +local cjson = require("cjson.safe") local dict = ngx.shared.routes_cache ngx.req.read_body() local body = ngx.req.get_body_data() diff --git a/src/service/system/account.lua b/src/service/system/account.lua index a6edcd6..1a971a2 100644 --- a/src/service/system/account.lua +++ b/src/service/system/account.lua @@ -4,11 +4,79 @@ --- DateTime: 2025/9/25 08:25 --- 业务逻辑 对账户数据表进行数据表业务处理 local validator = require("util.validator") -local helpers = require("util.helpers") +local helpers = require("share.helpers") local account = require("model.account") local _M = {} +local dao = require("service.system.account") +local resp = require("util.response") + +--获取所有账户信息 +function _M.get_allaccount() + local code,ret = dao.getAllAccount() + local result = resp:json(code, ret) + resp:send(result) +end + +--根据账户id获取账户信息 +function _M.get_account(m) + local id = m.id + local code,ret = dao.getAccount(id) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据账户id获取账户信息 +function _M.add_account() + --获取请求头并进行校验 + if validator.checkReqHeader() == false then + local result = resp:json(0x000001) + resp:send(result) + 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.addAccount(body_data) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据账户id删除账户信息 +function _M.delete_account(m) + local id = m.id + local code, ret = dao.deleteAccount(id) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据账户id删除账户信息 +function _M.update_account(m) + local id = m.id + --读取请求体的数据 + 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 + local code, ret = dao.updateAccount(id, body_data) + local result = resp:json(code, ret) + resp:send(result) +end + -- 查询数据表中的所有账户信息 function _M.getAllAccount() return account:all() diff --git a/src/service/system/application.lua b/src/service/system/application.lua index b1c1242..970f86e 100644 --- a/src/service/system/application.lua +++ b/src/service/system/application.lua @@ -4,11 +4,96 @@ --- DateTime: 2025/9/27 16:02 --- 业务逻辑 对应用数据表进行数据表业务处理 local validator = require("util.validator") -local helpers = require("util.helpers") +local helpers = require("share.helpers") local application = require("model.application") local _M = {} +local dao = require("service.system.application") +local resp = require("util.response") +local validator = require("util.validator") + +--获取所有应用程序信息 +function _M.get_allapplication() + local code,ret = dao.getAllApplication() + local result = resp:json(code, ret) + resp:send(result) +end + +--根据应用id获取应用信息 +function _M.get_application(m) + local id = m.id + local code,ret = dao.getApplication(id) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据组织id获取权限的信息 +function _M.get_organization_application(m) + local id = m.id + local code,ret = dao.getOrganizationApplication(id) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据用户id获取权限的信息 +function _M.get_user_application(m) + local id = m.id + local code,ret = dao.getUserApplication(id) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据应用id获取应用信息 +function _M.add_application() + --获取请求头并进行校验 + if validator.checkReqHeader() == false then + local result = resp:json(0x000001) + resp:send(result) + 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.addApplication(body_data) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据应用id删除应用信息 +function _M.delete_application(m) + local id = m.id + local code, ret = dao.deleteApplication(id) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据应用id删除应用信息 +function _M.update_application(m) + local id = m.id + --读取请求体的数据 + 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 + local code, ret = dao.updateApplication(id, body_data) + local result = resp:json(code, ret) + resp:send(result) +end + -- 查询数据表中的所有应用信息 function _M.getAllApplication() return application:all() diff --git a/src/service/system/controller.lua b/src/service/system/auth.lua similarity index 63% rename from src/service/system/controller.lua rename to src/service/system/auth.lua index ba40151..ff6eabc 100644 --- a/src/service/system/controller.lua +++ b/src/service/system/auth.lua @@ -3,13 +3,62 @@ --- Created by admin. --- DateTime: 2025/10/28 11:09 --- -local validator = require("util.validator") -local helpers = require("util.helpers") +local helpers = require("share.helpers") local resp = require("util.response") local user = require("model.user") local _M = {} +local dao = require("service.system.auth") + +--用户登录业务逻辑处理 +function _M.login() + --获取请求头并进行校验 + if validator.checkReqHeader() == false then + local result = resp:json(0x000001) + resp:send(result) + 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.login(body_data) + local result = resp:json(code, ret) + resp:send(result) +end + +--用户登出业务逻辑处理 +function _M.logout() + --获取请求头并进行校验 + if validator.checkReqHeader() == false then + local result = resp:json(0x000001) + resp:send(result) + 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.logout(body_data) + local result = resp:json(code, ret) + resp:send(result) +end + --认证用户返回用户数据信息 local function authenticate(name, passwd) --验证用户名是否为空 diff --git a/src/service/system/organization.lua b/src/service/system/organization.lua index e36f8c9..d08b2b8 100644 --- a/src/service/system/organization.lua +++ b/src/service/system/organization.lua @@ -4,11 +4,80 @@ --- DateTime: 2025/9/28 10:22 --- 业务逻辑 对组织架构数据表进行数据表业务处理 local validator = require("util.validator") -local helpers = require("util.helpers") +local helpers = require("share.helpers") local organization = require("model.organization") local _M = {} +local dao = require("service.system.organization") +local resp = require("util.response") +local validator = require("util.validator") + +--获取所有组织架构信息 +function _M.get_allorganization() + local code,ret = dao.getAllOrganization() + local result = resp:json(code, ret) + resp:send(result) +end + +--根据组织id获取组织架构信息 +function _M.get_organization(m) + local id = m.id + local code,ret = dao.getOrganization(id) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据组织id添加组织架构信息 +function _M.add_organization() + --获取请求头并进行校验 + if validator.checkReqHeader() == false then + local result = resp:json(0x000001) + resp:send(result) + 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.addOrganization(body_data) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据组织id删除组织架构信息 +function _M.delete_organization(m) + local id = m.id + local code, ret = dao.deleteOrganization(id) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据组织id删除组织架构信息 +function _M.update_organization(m) + local id = m.id + --读取请求体的数据 + 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 + local code, ret = dao.updateOrganization(id, body_data) + local result = resp:json(code, ret) + resp:send(result) +end + -- 查询数据表中的所有组织架构信息 function _M.getAllOrganization() return organization:all() diff --git a/src/service/system/permission.lua b/src/service/system/permission.lua index e26d270..9504b88 100644 --- a/src/service/system/permission.lua +++ b/src/service/system/permission.lua @@ -4,11 +4,88 @@ --- DateTime: 2025/9/27 17:06 --- 业务逻辑 对权限数据表进行数据表业务处理 local validator = require("util.validator") -local helpers = require("util.helpers") +local helpers = require("share.helpers") local permission = require("model.permission") local _M = {} +local dao = require("service.system.permission") +local resp = require("util.response") +local validator = require("util.validator") + +--获取所有权限信息 +function _M.get_allpermission() + local code,ret = dao.getAllPermission() + local result = resp:json(code, ret) + resp:send(result) +end + +--根据权限id获取权限信息 +function _M.get_permission(m) + local id = m.id + local code,ret = dao.getPermission(id) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据角色id获取使用的权限 +function _M.get_permission_by_role(m) + local id = m.id + local code,ret = dao.getPermissionByRole(id) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据账号id获取账号信息 +function _M.add_permission() + --获取请求头并进行校验 + if validator.checkReqHeader() == false then + local result = resp:json(0x000001) + resp:send(result) + 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.addPermission(body_data) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据账号id删除账号信息 +function _M.delete_permission(m) + local id = m.id + local code, ret = dao.deletePermission(id) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据账号id删除账号信息 +function _M.update_permission(m) + local id = m.id + --读取请求体的数据 + 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 + local code, ret = dao.updatePermission(id, body_data) + local result = resp:json(code, ret) + resp:send(result) +end + -- 查询数据表中的所有权限信息 function _M.getAllPermission() return permission:all() diff --git a/src/service/system/role.lua b/src/service/system/role.lua index 280620f..3e7c2bb 100644 --- a/src/service/system/role.lua +++ b/src/service/system/role.lua @@ -4,11 +4,80 @@ --- DateTime: 2025/9/27 15:19 --- 业务逻辑 对用户角色数据表进行数据表业务处理 local validator = require("util.validator") -local helpers = require("util.helpers") +local helpers = require("share.helpers") local role = require("model.role") local _M = {} +local dao = require("service.system.role") +local resp = require("util.response") +local validator = require("util.validator") + +--获取所有角色信息 +function _M.get_allrole() + local code,ret = dao.getAllRole() + local result = resp:json(code, ret) + resp:send(result) +end + +--根据角色id获取角色信息 +function _M.get_role(m) + local id = m.id + local code,ret = dao.getRole(id) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据角色id获取角色信息 +function _M.add_role() + --获取请求头并进行校验 + if validator.checkReqHeader() == false then + local result = resp:json(0x000001) + resp:send(result) + 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.addRole(body_data) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据角色id删除角色信息 +function _M.delete_role(m) + local id = m.id + local code, ret = dao.deleteRole(id) + local result = resp:json(code, ret) + resp:send(result) +end + +--根据角色id删除角色信息 +function _M.update_role(m) + local id = m.id + --读取请求体的数据 + 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 + local code, ret = dao.updateRole(id, body_data) + local result = resp:json(code, ret) + resp:send(result) +end + -- 查询数据表中的所有角色信息 function _M.getAllRole() return role:all() diff --git a/src/service/system/user.lua b/src/service/system/user.lua index f40608e..fa796d8 100644 --- a/src/service/system/user.lua +++ b/src/service/system/user.lua @@ -3,89 +3,89 @@ --- Created by . --- DateTime: 2025/9/25 08:19 --- 业务逻辑 对用户数据表进行数据表业务处理 -local validator = require("util.validator") -local helpers = require("util.helpers") -local user = require("model.user") +local jsonschema = require("jsonschema") +local resp = require("util.response") +local userDao = require("dao.user") local _M = {} --- 查询数据表中的所有用户信息 -function _M.getAllUser() - return user:all() +-- 定义一个JSON Schema +local schema = { + {type = "object", properties = { + {name = "username", type = "string"}, + {name = "phone", type = "string"}, + {name = "email", type = "string"}, + {name = "idcard", type = "string"}, + {name = "name", type = "string"}, + {name = "office_phone", type = "string"}, + {name = "telephone", type = "string"}, + {name = "display_name", type = "string"}, + }, required = {"username", "phone", "email", "idcard"}} +} + +--获取所有用户信息 +function _M.getSystemUsers() + --获取页码和请求的数据量 + --local args = ngx.req.get_uri_args() + local pageNum = ngx.var.pagenum or 1 + local pageSize = ngx.var.pagesize or 10 + local code,ret = userDao.getAllUser(pageNum, pageSize) + local result = resp:json(code, ret) + resp:send(result) end --根据用户id获取用户信息 -function _M.getUser(id) - return user:find(id) +function _M.getSystemUser(m) + local code,ret = userDao.getUser(m.id) + local result = resp:json(code, ret) + resp:send(result) end ---增加用户信息到数据表 -function _M.addUser(jsonData) - --验证数据的正确性,错误时返回 - local success, result = validator.checkJson(jsonData) - if success == false then - return 0x000001,result +--根据用户id获取用户信息 +function _M.addSystemUser() + --读取请求体的数据 + ngx.req.read_body() + --获取请求数据 + local body_data = ngx.req.get_body_data() + -- 验证数据是否符合schema + local ok, err = jsonschema:generate_validator(body_data, schema) + --验证失败则返回 + if not ok then + local result = resp:json(0x000001) + resp:send(result) + return end - --解析json中的键和数据值 - local name, phone, email - for key, value in pairs(result) do - if key == "username" then name = value end - if key == "phone" then phone = value end - if key == "email" then email = value end - end - - --根据用户、手机号、邮箱进行验证用户是否存在 - local code, res = user:where("name", "=", name):where("phone", "=", phone):where("email", "=", phone):get() - if code ~= 0 then - return 0x000001,res - end - local num = 0 - for _, row in ipairs(res) do - for key, value in pairs(row) do - num = num + 1 - end - end - --用户存在时返回用户已经存在 - if num > 0 then - return 0x01000C,nil - end - - --键值为id产生uuid数据值,增加到json中 - result.id = helpers.getUuid() - local ret = helpers.convert_json(result) - -- 创建一个用户 - return user:create('{'..ret..'}') + --ngx.say(body_data) + local code, ret = userDao.addUser(body_data) + local result = resp:json(code, ret) + resp:send(result) end ---删除用户信息到数据表 -function _M.deleteUser(id) - return user:delete(id) +--根据用户id删除用户信息 +function _M.deleteSystemUser(m) + local code, ret = userDao.deleteUser(m.id) + local result = resp:json(code, ret) + resp:send(result) end ---更新用户信息到数据表 -function _M.updateUser(id, jsonData) - --根据用户id进行验证用户是否存在 - local code, res = user:find(id) - if code ~= 0 then - return 0x000001,res +--根据用户id删除用户信息 +function _M.updateSystemUser(m) + --读取请求体的数据 + ngx.req.read_body() + --获取请求数据 + local body_data = ngx.req.get_body_data() + -- 验证数据是否符合schema + local ok, err = jsonschema:generate_validator(body_data, schema) + --验证失败则返回 + if not ok then + local result = resp:json(0x000001) + resp:send(result) + return end - local num = 0 - for _, row in ipairs(res) do - for key, value in pairs(row) do - num = num + 1 - end - end - --用户不存在返回错误 - if num <= 0 then - return 0x01000C,nil - end - --验证数据的正确性,错误时返回 - local success, result = validator.checkJson(jsonData) - if success == false then - return 0x000001,result - end - --对数据内容进行更新 - return user:where('id', '=', id):update(jsonData) + --将数据更新到数据表中 + local code, ret = userDao.updateUser(m.id, body_data) + local result = resp:json(code, ret) + resp:send(result) end return _M diff --git a/src/util/database.lua b/src/share/database.lua similarity index 95% rename from src/util/database.lua rename to src/share/database.lua index 2e12cb3..f52e0e2 100644 --- a/src/util/database.lua +++ b/src/share/database.lua @@ -13,6 +13,9 @@ local mt = { __index = _M } --]] function _M:get_connection() local code = 0 + -- modify by wanglei : 修改添加支持postgresql连接 + + if ngx.ctx[self.db_type] then -- if write before read, make sure write read connection the same if ngx.ctx[WRITE] then @@ -77,6 +80,11 @@ function _M.db_query(self, sql) return 0, res end +--[[ + 构造函数 + @param 数据相关的配置 + @return +--]] function _M.new(self, opts) return setmetatable({ host = opts.host or '127.0.0.1', diff --git a/src/util/helpers.lua b/src/share/helpers.lua similarity index 97% rename from src/util/helpers.lua rename to src/share/helpers.lua index 5d5c970..a03ab0a 100644 --- a/src/util/helpers.lua +++ b/src/share/helpers.lua @@ -3,8 +3,8 @@ --- Created by admin. --- DateTime: 2025/10/24 11:36 --- -local snowflake = require("util.snowflake") -local cjson = require("cjson") +local snowflake = require("share.snowflake") +local cjson = require("cjson.safe") local _M = {} @@ -127,7 +127,7 @@ local function get_cookie(key) end local function get_local_time() - local config = require("config.config") + local config = require("config") local time_zone = ngx.re.match(config.time_zone, "[0-9]+") if time_zone == nil then local err = "not set time zone or format error, time zone should look like `+8:00` current is: " .. config.time_zone diff --git a/src/share/initial/loading_config.lua b/src/share/initial/loading_config.lua index b66d7d7..b0f9c7f 100644 --- a/src/share/initial/loading_config.lua +++ b/src/share/initial/loading_config.lua @@ -18,7 +18,7 @@ ngx.DEBUG ]] -- 加载cjson -cjson = require("cjson"); +cjson = require("cjson.safe"); -- 加载string string = require("string"); diff --git a/src/util/model.lua b/src/share/model.lua similarity index 90% rename from src/util/model.lua rename to src/share/model.lua index 48d952a..213b8d0 100644 --- a/src/util/model.lua +++ b/src/share/model.lua @@ -1,7 +1,7 @@ -local Database = require('util.database') +local Database = require('share.database') local dbconf = require("config.database") -local helpers = require('util.helpers') +local helpers = require('share.helpers') local implode = helpers.implode local unique = helpers.unique local table_remove = helpers.table_remove @@ -121,21 +121,23 @@ end -- end function _M:find(id,column) - if self.query_sql ~= nil then - ngx.log(ngx.ERR, 'cannot use find() with other query sql') - return 1, nil - end - column = column or 'id' - id = transform_value(id) + if self.query_sql ~= nil then + ngx.log(ngx.ERR, 'cannot use find() with other query sql') + return 1, nil + end + column = column or 'id' + id = transform_value(id) ngx.log(ngx.INFO, 'table name :' .. self.table) - local sql = 'select * from \"'..self.table..'\" where '..column..'='..id..' limit 1' + local sql = 'select * from \"'..self.table..'\" where '..column..'='..id..' limit 1' ngx.log(ngx.INFO, 'query sql:', sql) - local code, res = self:query(sql, READ) - if table.getn(res) > 0 then - ngx.log(ngx.INFO, 'query record count:', table.getn(res)) - res = self:make_relations(res) - return code, res - end + local code, res = self:query(sql, READ) + if code == 0 then + if table.getn(res) > 0 then + ngx.log(ngx.INFO, 'query record count:', table.getn(res)) + res = self:make_relations(res) + return code, res + end + end return code, nil end @@ -194,8 +196,10 @@ function _M:count() sql = 'select count(*) from \"'..self.table.."\"" end local code, res = self:query(sql, READ) - if table.getn(res) > 0 then - return code, tonumber(res[1]['count']) + if code == 0 then + if table.getn(res) > 0 then + return code, tonumber(res[1]['count']) + end end return code, nil end @@ -221,19 +225,20 @@ function _M:get(num) end --根据数据模型中的 -function _M:paginate(page_num, page_size) - page_num = page_num or 1 - page_size = page_size or 10 +function _M:paginate(pageNum, pageSize) + ngx.log(ngx.INFO, "pageNum:", tostring(pageNum), " pageSize:", tostring(pageSize)) + pageNum = pageNum or 1 + pageSize = pageSize or 10 local sql, count_sql, total, code, res local data={ data = {}, total = 0 } if not self.query_sql then - sql = 'select * from \"'..self.table..'\" limit '..page_size..' offset '..(page_num - 1) * page_size + sql = 'select * from \"'..self.table..'\" limit '..tostring(pageSize)..' offset '..tostring((pageNum - 1) * pageSize) count_sql = 'select count(*) from \"'..self.table..'\"' else - sql = 'select * from \"'..self.table .. '\" '..self.query_sql .. ' limit '..page_size..' offset '..(page_num - 1) * page_size + sql = 'select * from \"'..self.table .. '\" '..self.query_sql .. ' limit '..tostring(pageSize)..' offset '..tostring((pageNum - 1) * pageSize) count_sql = 'select count(*) from \"'..self.table..'\" '..self.query_sql end code, total = self:query(count_sql, READ) diff --git a/src/util/snowflake.lua b/src/share/snowflake.lua similarity index 100% rename from src/util/snowflake.lua rename to src/share/snowflake.lua diff --git a/src/test/test.lua b/src/test/test.lua index 3b83b52..eea4d2e 100644 --- a/src/test/test.lua +++ b/src/test/test.lua @@ -1,167 +1,303 @@ ---- ---- Generated by EmmyLua(https://github.com/EmmyLua) ---- Created by admin. ---- DateTime: 2025/10/15 09:12 ---- ---local snowflake = require("util.snowflake") -local helpers = require("util.helpers") --- ---local workerId = 0 -- 假设当前机器的ID是1,范围在[0, 31]之间 ---local datacenterId = 0 -- 数据中心ID,范围在[0, 31]之间 ---local snow = snowflake.new(workerId, datacenterId) ---local id = snow:generateUniqueId()-- 生成ID ---ngx.say("Generated ID:"..snow.int64_to_string(id)) - ---max =a and b or c--a?b:c - -local cjson = require("cjson") -local User = require("model.user") - ---获取数据表中的记录数 -local code, res = User:count() ---ngx.say(res) - ---查询表中id为1的数据记录 -code, res = User:find("1") - ---查询表中的所有记录 -code, res = User:all() ---显示查询到的数据记录 -for _, row in ipairs(res) do - for key, value in pairs(row) do - ngx.say(key .. ":" .. tostring(value)) - end -end - -ngx.say("----begin where and query---") --- 返回 users 表中 username 字段的值是 `cgreen` 的,`password` 字段的值是 `xxxxxx` 的多条数据,注意此处返回是 table 数组,`first()` 方法返回的是单条数据 -code, res = User:where('name','=','zhangsan'):where('password','=','111111'):get() -ngx.say(code) -if res ~= nil then - for _, row in ipairs(res) do - for key, value in pairs(row) do - ngx.say(key .. ":" .. tostring(value)) - end - end -end ---[[ ---ngx.say("----begin where or query---") --- 返回 `name` 为 `xxx` 或者 `yyy` 的所有用户 table 数组 -code, res = User:where('name','=','zhangsan'):orwhere('name','=','admin'):get() ---for _, row in ipairs(res) do --- for key, value in pairs(row) do --- ngx.say(key .. ":" .. tostring(value)) --- end ---end - ---orderby(column, option)方法,第一个参数传入排序的列名,第二个参数默认为ASC 也可以传入 ASC 正序 或 DESC 倒序(不区分大小写), -code, res = User:orderby('created_time'):get() ---for _, row in ipairs(res) do --- for key, value in pairs(row) do --- ngx.say(key .. ":" .. tostring(value)) --- end ---end - --- 创建一个用户 -code, res = User:create({ - id='3', - password='22222', - name='lisi', - email='lisi@gmail.com', -}) - --- 更新 id = 1 的 user 的 name 为 test, avatar 为 NULL -code, res = User:where('id', '=', '3'):update({ - phone='666666', - email='zhangsan@qq.com' -}) ---输出更新后影响的行总数 -ngx.say("update affected_rows: ", res.affected_rows) - --- 删除 id = 1 的用户 -code, res = User:where('id','=','3'):delete() -ngx.say("delete affected_rows: ", res.affected_rows) - ---分页 获取数据表中的记录 -local data = nil -code, data = User:paginate(1) -local count = data.total -ngx.say("data total:", count) -for _, row in ipairs(data.data) do - ngx.say("begin show data:") - for key, value in pairs(row) do - ngx.say(key .. ":" .. tostring(value)) - end -end ---]] - ---[[ ---读取请求体的数据 -ngx.req.read_body() ---获取请求数据 -local body_data = ngx.req.get_body_data() ---ngx.say(body_data) - -local data = cjson.decode(body_data) ---键值为id产生uuid数据值,增加到json中 -data.id = helpers.getUuid() -local ret = helpers.convert_json(data) -ngx.say(ret) ---]] - ---local header = ngx.req.get_headers() ---for k,v in pairs(header) do --- ngx.say("[header] name:", k, "value:", v) ---end - ---local payloads = ngx.req.get_uri_args() ---for k,v in pairs(payloads) do --- ngx.say("[params] name:", k, " value:", v) ---end - ---去掉组装最后一位逗号(,) ---local newKeys = keys:sub(1, #keys -1) ---local newValues = values:sub(1, #values -1) - ---[[ ---读取请求体的数据 ---ngx.req.read_body() - ---获取请求数据 -local body_data = ngx.req.get_body_data() - -if not body_data then - ngx.say("read file error:", err) - return ngx.exit(400) -end -local len = #body_data - -local file_name = ngx.req.get_body_file() -ngx.say("file length:", len) - -ngx.req.read_body_in_buffer(ngx.var.request_body_file) - -ngx.say(body_data) ---]] - ---[[ -local cjson = require("cjson") -local file_path = "/home/frankly/work/test.dat" -local file_length = 1024 * 1024 * 400 -local f, err = io.input(file_path, "r") -if not f then - ngx.say("read file error:"..err) - return -end -local content = f:read(file_length) --读取文件内容 -f:close() --关闭文件 ---ngx.say(content) -local res = { - key = "data", - value = content -} ---ngx.header["Length"] = #content -ngx.header["Content-Type"] = 'application/json; charset=UTF-8' -ngx.say(cjson.encode(res)) -ngx.log(ngx.INFO, "send data success") +--- +--- Generated by EmmyLua(https://github.com/EmmyLua) +--- Created by admin. +--- DateTime: 2025/10/15 09:12 +--- +--local snowflake = require("util.snowflake") +local helpers = require("share.helpers") +-- +--local workerId = 0 -- 假设当前机器的ID是1,范围在[0, 31]之间 +--local datacenterId = 0 -- 数据中心ID,范围在[0, 31]之间 +--local snow = snowflake.new(workerId, datacenterId) +--local id = snow:generateUniqueId()-- 生成ID +--ngx.say("Generated ID:"..snow.int64_to_string(id)) + +--max =a and b or c--a?b:c + +local cjson = require("cjson.safe") +--[[ +local uuid = require("resty.jit-uuid") +uuid.seed() +local val = uuid() +local uid = uuid.generate_v4() ---> v4 UUID +local uid1 = uuid.generate_v3() ---> v3 UUID (name-based with MD5) +local uid2 = uuid.generate_v5() ---> v5 UUID (name-based with SHA-1) +uuid.is_valid() ---> true/false (automatic JIT PCRE or Lua patterns) +--ngx.say(val.." "..uid) + + +local args = ngx.req.get_uri_args() +local pageNum = args["pagenum"] or 1 +local pageSize = args["pagesize"] or 10 +ngx.say("pageNum:", pageNum, " pageSize:", pageSize) +--]] + +local jsonschema = require("jsonschema") + +-- 定义一个JSON Schema +-- 定义一个JSON Schema +local schema = { + {type = "object", properties = { + {name = "username", type = "string"}, + {name = "phone", type = "string"}, + {name = "email", type = "string"}, + {name = "idcard", type = "string"}, + {name = "name", type = "string"}, + {name = "office_phone", type = "string"}, + {name = "telephone", type = "string"}, + {name = "display_name", type = "string"}, + }, required = {"username", "phone", "email", "idcard"}} +} + +-- 待验证的JSON数据 +--local json_data = '{"name": "Alice", "age": 30}' +--local data, pos, err = cjson.decode(json_data) +--if err then +-- error("JSON decoding error: " .. err) +--end +--读取请求体的数据 +ngx.req.read_body() +--获取请求数据 +local body_data = ngx.req.get_body_data() + +-- 验证数据是否符合schema +local ok, err = jsonschema:generate_validator(body_data, schema) +if not ok then + error("Validation failed: " .. err) +else + print("Validation succeeded!") +end + +--[[ +local jwt = require("resty.jwt") + +local secret_key = "!@#$5412$#@!" -- 确保这个密钥足够安全并保密 + +--设置JWT的有效载荷 +local obj = { + header = {typ="JWT", alg="HS256"}, + payload = { -- 自定义数据 + username = "admin", + role = "admin", + iss = "your_issuer", -- 签发者 + sub = "1234567890", -- 主题 + exp = os.time() + 3600, -- 过期时间(例如:当前时间+1小时) + iat = os.time() -- 签发时间 + } +} +local jwt_token = jwt:sign(secret_key, obj) +ngx.say(jwt_token) + +local jwt_obj = jwt:verify(secret_key, jwt_token) +local param = cjson.encode(jwt_obj) +ngx.say(param) + +local ok, claims = jwt:verify(jwt_token) -- 使用之前生成的token +if ok then + print("Token is valid") + for k, v in pairs(claims) do + print(k, v) + end +else + print("Token is invalid:", claims) -- claims将包含错误信息 +end +--]] + +--[[ +--创建新的JWT对象 +--local jwt_obj = jwt:new() +--设置密钥 +local secret_key = "!@#$5412$#@!" -- 确保这个密钥足够安全并保密 +--jwt_obj:set_secret(secret_key) + +--设置JWT的有效载荷 +local payload = { + iss = "your_issuer", -- 签发者 + exp = os.time() + 3600, -- 过期时间(例如:当前时间+1小时) + iat = os.time(), -- 签发时间 + sub = "1234567890", -- 主题 + data = { -- 自定义数据 + username = "admin", + role = "admin" + } +} +-- 生成JWT token +local jwt_token, err = jwt:sign(secret_key, payload) +if err then + error("Failed to generate JWT token: " .. err) +end + +print("Generated JWT Token:", jwt_token) + + +local ok, claims = jwt:verify(jwt_token) -- 使用之前生成的token +if ok then + print("Token is valid") + for k, v in pairs(claims) do + print(k, v) + end +else + print("Token is invalid:", claims) -- claims将包含错误信息 +end +--]] + +--[[ +--引用使用的库文件 +local Model = require("share.model") +--创建一个数据表相关的模型 +local userModel = Model:new('sys_user') + +--获取数据表中的记录数 +local code, res = userModel:count() +ngx.say(res) + +--查询表中id为1的数据记录 +code, res = userModel:find("1") +if res ~= nil then + ngx.say(table.getn(res)) +end +--查询表中的所有记录 +code, res = userModel:all() +--显示查询到的数据记录 +if code == 0 then + for _, row in ipairs(res) do + for key, value in pairs(row) do + ngx.say(key .. ":" .. tostring(value)) + end + end +end + +--分页 获取数据表中的记录 +local data = nil +code, data = userModel:paginate(1, 10) +local count = data.total +ngx.say("data total:", count) +for _, row in ipairs(data.data) do + ngx.say("begin show data:") + for key, value in pairs(row) do + ngx.say(key .. ":" .. tostring(value)) + end +end + +ngx.say("----begin where and query---") +-- 返回 users 表中 username 字段的值是 `cgreen` 的,`password` 字段的值是 `xxxxxx` 的多条数据,注意此处返回是 table 数组,`first()` 方法返回的是单条数据 +code, res = userModel:where('name','=','zhangsan'):where('password','=','111111'):get() +--ngx.say(code) +--if res ~= nil then +-- for _, row in ipairs(res) do +-- for key, value in pairs(row) do +-- ngx.say(key .. ":" .. tostring(value)) +-- end +-- end +--end +--]] + +--[[ +--ngx.say("----begin where or query---") +-- 返回 `name` 为 `xxx` 或者 `yyy` 的所有用户 table 数组 +code, res = userModel:where('name','=','zhangsan'):orwhere('name','=','admin'):get() +--for _, row in ipairs(res) do +-- for key, value in pairs(row) do +-- ngx.say(key .. ":" .. tostring(value)) +-- end +--end + +--orderby(column, option)方法,第一个参数传入排序的列名,第二个参数默认为ASC 也可以传入 ASC 正序 或 DESC 倒序(不区分大小写), +code, res = userModel:orderby('created_time'):get() +--for _, row in ipairs(res) do +-- for key, value in pairs(row) do +-- ngx.say(key .. ":" .. tostring(value)) +-- end +--end + +-- 创建一个用户 +code, res = userModel:create({ + id='3', + password='22222', + name='lisi', + email='lisi@gmail.com', +}) + +-- 更新 id = 1 的 user 的 name 为 test, avatar 为 NULL +code, res = userModel:where('id', '=', '3'):update({ + phone='666666', + email='zhangsan@qq.com' +}) +--输出更新后影响的行总数 +ngx.say("update affected_rows: ", res.affected_rows) + +-- 删除 id = 1 的用户 +code, res = userModel:where('id','=','3'):delete() +ngx.say("delete affected_rows: ", res.affected_rows) +--]] + +--[[ +--读取请求体的数据 +ngx.req.read_body() +--获取请求数据 +local body_data = ngx.req.get_body_data() +ngx.say(body_data) + +local data = cjson.decode(body_data) +--键值为id产生uuid数据值,增加到json中 +data.id = helpers.getUuid() +local ret = helpers.convert_json(data) +ngx.say(ret) +--]] + +--local header = ngx.req.get_headers() +--for k,v in pairs(header) do +-- ngx.say("[header] name:", k, "value:", v) +--end + +--local payloads = ngx.req.get_uri_args() +--for k,v in pairs(payloads) do +-- ngx.say("[params] name:", k, " value:", v) +--end + +--去掉组装最后一位逗号(,) +--local newKeys = keys:sub(1, #keys -1) +--local newValues = values:sub(1, #values -1) + +--[[ +--读取请求体的数据 +--ngx.req.read_body() + +--获取请求数据 +local body_data = ngx.req.get_body_data() + +if not body_data then + ngx.say("read file error:", err) + return ngx.exit(400) +end +local len = #body_data + +local file_name = ngx.req.get_body_file() +ngx.say("file length:", len) + +ngx.req.read_body_in_buffer(ngx.var.request_body_file) + +ngx.say(body_data) +--]] + +--[[ +local cjson = require("cjson.safe") +local file_path = "/home/frankly/work/test.dat" +local file_length = 1024 * 1024 * 400 +local f, err = io.input(file_path, "r") +if not f then + ngx.say("read file error:"..err) + return +end +local content = f:read(file_length) --读取文件内容 +f:close() --关闭文件 +--ngx.say(content) +local res = { + key = "data", + value = content +} +--ngx.header["Length"] = #content +ngx.header["Content-Type"] = 'application/json; charset=UTF-8' +ngx.say(cjson.encode(res)) +ngx.log(ngx.INFO, "send data success") --]] \ No newline at end of file diff --git a/src/util/cookie.lua b/src/util/cookie.lua deleted file mode 100644 index f4c81ab..0000000 --- a/src/util/cookie.lua +++ /dev/null @@ -1,213 +0,0 @@ --- Copyright (C) 2013-2016 Jiale Zhi (calio), CloudFlare Inc. --- See RFC6265 http://tools.ietf.org/search/rfc6265 --- require "luacov" - -local type = type -local byte = string.byte -local sub = string.sub -local format = string.format -local log = ngx.log -local ERR = ngx.ERR -local WARN = ngx.WARN -local ngx_header = ngx.header - -local EQUAL = byte("=") -local SEMICOLON = byte(";") -local SPACE = byte(" ") -local HTAB = byte("\t") - --- table.new(narr, nrec) -local ok, new_tab = pcall(require, "table.new") -if not ok then - new_tab = function () return {} end -end - -local ok, clear_tab = pcall(require, "table.clear") -if not ok then - clear_tab = function(tab) for k, _ in pairs(tab) do tab[k] = nil end end -end - -local _M = new_tab(0, 2) - -_M._VERSION = '0.01' - - -local function get_cookie_table(text_cookie) - if type(text_cookie) ~= "string" then - log(ERR, format("expect text_cookie to be \"string\" but found %s", - type(text_cookie))) - return {} - end - - local EXPECT_KEY = 1 - local EXPECT_VALUE = 2 - local EXPECT_SP = 3 - - local n = 0 - local len = #text_cookie - - for i=1, len do - if byte(text_cookie, i) == SEMICOLON then - n = n + 1 - end - end - - local cookie_table = new_tab(0, n + 1) - - local state = EXPECT_SP - local i = 1 - local j = 1 - local key, value - - while j <= len do - if state == EXPECT_KEY then - if byte(text_cookie, j) == EQUAL then - key = sub(text_cookie, i, j - 1) - state = EXPECT_VALUE - i = j + 1 - end - elseif state == EXPECT_VALUE then - if byte(text_cookie, j) == SEMICOLON - or byte(text_cookie, j) == SPACE - or byte(text_cookie, j) == HTAB - then - value = sub(text_cookie, i, j - 1) - cookie_table[key] = value - - key, value = nil, nil - state = EXPECT_SP - i = j + 1 - end - elseif state == EXPECT_SP then - if byte(text_cookie, j) ~= SPACE - and byte(text_cookie, j) ~= HTAB - then - state = EXPECT_KEY - i = j - j = j - 1 - end - end - j = j + 1 - end - - if key ~= nil and value == nil then - cookie_table[key] = sub(text_cookie, i) - end - - return cookie_table -end - -function _M.new(self) - local _cookie = ngx.var.http_cookie - --if not _cookie then - --return nil, "no cookie found in current request" - --end - return setmetatable({ _cookie = _cookie, set_cookie_table = new_tab(4, 0) }, - { __index = self }) -end - -function _M.get(self, key) - if not self._cookie then - return nil, "no cookie found in the current request" - end - if self.cookie_table == nil then - self.cookie_table = get_cookie_table(self._cookie) - end - - return self.cookie_table[key] -end - -function _M.get_all(self) - if not self._cookie then - return nil, "no cookie found in the current request" - end - - if self.cookie_table == nil then - self.cookie_table = get_cookie_table(self._cookie) - end - - return self.cookie_table -end - -function _M.get_cookie_size(self) - if not self._cookie then - return 0 - end - - return string.len(self._cookie) -end - -local function bake(cookie) - if not cookie.key or not cookie.value then - return nil, 'missing cookie field "key" or "value"' - end - - if cookie["max-age"] then - cookie.max_age = cookie["max-age"] - end - - if (cookie.samesite) then - local samesite = cookie.samesite - - -- if we don't have a valid-looking attribute, ignore the attribute - if (samesite ~= "Strict" and samesite ~= "Lax" and samesite ~= "None") then - log(WARN, "SameSite value must be 'Strict', 'Lax' or 'None'") - cookie.samesite = nil - end - end - - local str = cookie.key .. "=" .. cookie.value - .. (cookie.expires and "; Expires=" .. cookie.expires or "") - .. (cookie.max_age and "; Max-Age=" .. cookie.max_age or "") - .. (cookie.domain and "; Domain=" .. cookie.domain or "") - .. (cookie.path and "; Path=" .. cookie.path or "") - .. (cookie.secure and "; Secure" or "") - .. (cookie.httponly and "; HttpOnly" or "") - .. (cookie.samesite and "; SameSite=" .. cookie.samesite or "") - .. (cookie.extension and "; " .. cookie.extension or "") - return str -end - -function _M.set(self, cookie) - local cookie_str, err = bake(cookie) - if not cookie_str then - return nil, err - end - - local set_cookie = ngx_header['Set-Cookie'] - local set_cookie_type = type(set_cookie) - local t = self.set_cookie_table - clear_tab(t) - - if set_cookie_type == "string" then - -- only one cookie has been setted - if set_cookie ~= cookie_str then - t[1] = set_cookie - t[2] = cookie_str - ngx_header['Set-Cookie'] = t - end - elseif set_cookie_type == "table" then - -- more than one cookies has been setted - local size = #set_cookie - - -- we can not set cookie like ngx.header['Set-Cookie'][3] = val - -- so create a new table, copy all the values, and then set it back - for i=1, size do - t[i] = ngx_header['Set-Cookie'][i] - if t[i] == cookie_str then - -- new cookie is duplicated - return true - end - end - t[size + 1] = cookie_str - ngx_header['Set-Cookie'] = t - else - -- no cookie has been setted - ngx_header['Set-Cookie'] = cookie_str - end - return true -end - -_M.get_cookie_string = bake - -return _M diff --git a/src/util/response.lua b/src/util/response.lua index 31afa94..f19248b 100644 --- a/src/util/response.lua +++ b/src/util/response.lua @@ -1,6 +1,6 @@ local cjson = require('cjson') -local conf = require('config.config') -local error_code = require('config.status') +local conf = require('config') +local error_code = require('util.status') local ngx = ngx local _M = {} diff --git a/src/config/status.lua b/src/util/status.lua similarity index 99% rename from src/config/status.lua rename to src/util/status.lua index ebc1ca4..3b28c75 100644 --- a/src/config/status.lua +++ b/src/util/status.lua @@ -16,7 +16,6 @@ --ngx.HTTP_METHOD_NOT_IMPLEMENTED (501) --ngx.HTTP_SERVICE_UNAVAILABLE (503) --ngx.HTTP_GATEWAY_TIMEOUT (504) - return { -- 系统状态码 [0x000000] = 'ok', @@ -39,4 +38,4 @@ return { [0x01000A] = '获取用户信息失败,用户未登录', [0x01000B] = '获取用户信息失败,用户不存在', [0x01000C] = '修改用户信息失败,用户不存在', -} +} \ No newline at end of file diff --git a/src/util/validator.lua b/src/util/validator.lua deleted file mode 100644 index d8fea21..0000000 --- a/src/util/validator.lua +++ /dev/null @@ -1,33 +0,0 @@ ---- ---- Generated by EmmyLua(https://github.com/EmmyLua) ---- Created by admin. ---- DateTime: 2025/10/24 11:01 ---- 提供公共需要的验证接口等功能 - -local cjson = require('cjson') - -local _M = {} - ---验证请求头是否正确 -function _M.checkReqHeader() - local headers = ngx.req.get_headers() - if headers["content-type"] ~= "application/json" then - return false - end - return true -end - ---校验json数据的正确性,并返回json解析后的数据 -function _M.checkJson(jsonData) - --ngx.say(jsonData) - local success, result = pcall(function() - return cjson.decode(jsonData) - end) - if success == true then - return true, result - end - local res = nil - return false,res -end - -return _M \ No newline at end of file