diff --git a/src/dao/oauth/oauth.lua b/src/dao/oauth/oauth.lua index c205494..b8eebe6 100644 --- a/src/dao/oauth/oauth.lua +++ b/src/dao/oauth/oauth.lua @@ -78,9 +78,9 @@ function _M.getUser(userid) return userDao:getSystemUser(userid) end -function _M.getApplicationBy(client_id, redirect_uri) - --print("getApplicationBy client_id:", client_id, " redirect_uri:", redirect_uri) - return applicationDao.getApplicationByClientId(client_id, redirect_uri) +function _M.getApplicationBy(client_id) + --print("getApplicationBy client_id:", client_id) + return applicationDao.getApplicationByClientId(client_id) end function _M.getApplicationByUserid(user_id, client_id, client_secret) diff --git a/src/service/oauth/oauth.lua b/src/service/oauth/oauth.lua index 9b821dc..471b93d 100644 --- a/src/service/oauth/oauth.lua +++ b/src/service/oauth/oauth.lua @@ -3,6 +3,7 @@ --- Created by admin. --- DateTime: 2025/10/28 11:09 --- 用于 +local status = require("util.status") local resp = require("util.response") local oauthDao = require("dao.oauth.oauth") local validator = require("validator.oauth.oauth") @@ -53,10 +54,12 @@ function _M:authorize() -- 1、校验客户端id和redirect_uri是否存在数据库 local client_id = args.client_id local redirect_uri = args.redirect_uri - local code, res = oauthDao.getApplicationBy(client_id, redirect_uri) + local code, res = oauthDao.getApplicationBy(client_id) if code ~= 0 or not res then return ngx.exit(ngx.HTTP_UNAUTHORIZED) end + --判断redirect_uri是否在重定向组里面 + local redirect_uris = res[1].redirect_uri -- 2、验证范围 if args.scope then local requested_scopes = {} @@ -69,7 +72,7 @@ function _M:authorize() local user, err = client.validate(client_id, redirect_uri) if user == nil then -- 重定向到登录页,携带当前授权请求参数(登录后跳转回来) - local login_url = "/login?redirect=" .. ngx.escape_uri(ngx.var.request_uri) + --local login_url = "/login?redirect=" .. ngx.escape_uri(ngx.var.request_uri) --print("authorize login_url:", login_url) --ngx.redirect(login_url) resp:response(ngx.HTTP_MOVED_TEMPORARILY, login_url) @@ -83,12 +86,12 @@ function _M:authorize() end --print("token set shared dict key:",code_key) -- 5. 重定向到客户端回调地址,携带授权码和原始 state(防 CSRF) - local redirect_url = args.redirect_uri .. "?code=" .. code .. "&state=" .. args.state + --local redirect_url = args.redirect_uri .. "?code=" .. code .. "&state=" .. args.state local rest = {} rest.redirect_uri = redirect_uri rest.code = auth_code rest.state = args.state - resp:response(ngx.HTTP_OK, rest) + resp:response(status.SUCCESS, rest) end -- 通过用户名认证用户和应用是否存在状态 @@ -96,13 +99,13 @@ local function authorizatePassword(args) -- 1.校验必填参数验证数据是否符合json local ok = validator.validateUserPasswd(args) if not ok then - resp:response(0x000001) + resp:response(status.PARAM_TYPE_BIND_ERROR) return end -- 2.验证用户名和密码,应用程序id和应用程序密钥 local code, res = oauthDao.authenticateUserPasswd(args.username, args.password) if code ~= 0 or res == nil then - resp:response(0x000001) + resp:response(status.ACCOUNT_NOT_EXIST) return end print("验证用户名和密码: ", args.username) @@ -112,10 +115,10 @@ local function authorizatePassword(args) local client_secret = args.client_secret code, res = oauthDao.getApplicationByUserid(userid, client_id, client_secret) if code ~= 0 or res == nil then - resp:response(0x000001) + resp:response(status.DATA_NONE_FOUNT) return end - local redirect_uri = res[1].redirect_uris + local redirect_uri = res[1].redirect_uri -- 4.生成授权码(随机字符串,确保唯一性)(用户ID、客户端ID、scope、生成时间) local auth_code, err = authcode.create(userid, client_id, redirect_uri) if not auth_code then @@ -128,7 +131,7 @@ local function authorizatePassword(args) local rest = {} rest.redirect_uri = redirect_uri rest.code = auth_code - resp:response(ngx.HTTP_OK, rest) + resp:response(status.SUCCESS, rest) end -- 通过code形式进行认证 @@ -136,7 +139,7 @@ local function authorizateCode(args) -- 1.校验必填参数验证数据是否符合json local ok = validator.validateToken(args) if not ok then - resp:response(0x000001) + resp:response(status.TOKEN_INVALID) return end -- 2.校验 code 有效性 @@ -151,7 +154,7 @@ local function authorizateCode(args) if request_uri ~= args.redirect_uri then print("token redirect_url:", request_uri, args.redirect_uri) local login_url = "/login?redirect=" .. ngx.escape_uri(request_uri) - resp:response(ngx.HTTP_MOVED_TEMPORARILY, login_url) + resp:response(status.PARAM_IS_INVALID, login_url) return end -- 4.生成密钥对 @@ -185,11 +188,11 @@ local function authorizateCode(args) -- 6.将生成的数据存储到数据库中 local code, res = oauthDao.updateApplicationToken(client_id, ret) if code ~= 0 then - resp:response(0x000001) + resp:response(status.DATA_IS_WRONG) return end -- 7.返回结果 - resp:response(ngx.HTTP_OK, ret) + resp:response(status.SUCCESS, ret) end -- 刷新令牌 @@ -197,7 +200,7 @@ local function authorizateRefresh(args) -- 1.校验必填参数验证数据是否符合json local res = validator.validateRefresh(args) if not res then - resp:response(0x000001) + resp:response(status.PARAM_NOT_COMPLETE) return end -- 2.验证并消费 refresh_token(滚动刷新:生成新的 rt) @@ -224,7 +227,7 @@ local function authorizateRefresh(args) ret.expires_in = conf.access_token_ttl ret.id_token = new_id_token -- 4.返回结果 - resp:response(ngx.HTTP_OK, ret) + resp:response(status.SUCCESS, ret) end -- 根据授权码获取Access-Token @@ -252,7 +255,6 @@ function _M:userinfo() -- 1.如果请求头中没有令牌,则直接返回401 if auth_header == nil or auth_header == "" then ngx.log(ngx.WARN, "没有找到令牌数据") - ngx.status = ngx.HTTP_UNAUTHORIZED ngx.exit(ngx.HTTP_UNAUTHORIZED) end -- 2.查找令牌中的Bearer前缀字符 @@ -261,7 +263,6 @@ function _M:userinfo() local ok = validator.validateUserinfo(data) if not ok then ngx.log(ngx.WARN, "令牌格式不正确") - ngx.status = ngx.HTTP_UNAUTHORIZED ngx.exit(ngx.HTTP_UNAUTHORIZED) end -- 3.获取token的数据值 @@ -291,15 +292,11 @@ function _M:userinfo() ngx.exit(ngx.HTTP_UNAUTHORIZED) end --通过用户id获取用户信息 - --print("-- get jwt_obj.payload value --") - --for key, value in pairs(jwt_obj.payload) do - -- print("jwt_obj.payload: ", key, " ", value) - --end local user_id = jwt_obj.payload.sub local code, rest = oauthDao.getUser(user_id) --读取数据错误 if code ~= 0 or rest == nil then - resp:response(0x000001) + resp:response(status.DATA_NONE_FOUNT) return end -- 5.获取token中的信息进行所需用户的信息返回 @@ -310,7 +307,7 @@ function _M:userinfo() ret.real_name = rest[1].realname ret.office_phone = rest[1].office_phone ret.email = rest[1].email - resp:response(ngx.HTTP_OK, ret) + resp:response(status.SUCCESS, ret) end --回收token @@ -321,11 +318,10 @@ function _M:logout() local ok = validator.validateLogout(args) if not ok then print("validateLogout:", args) - resp:response(0x000001) + resp:response(status.PARAM_IS_INVALID) return end local token = args.access_token - print("logout token:", token) -- 4.对token进行验证 --print("userinfo pubkey:", pub_key) local pub_key = conf.secret_key @@ -333,14 +329,12 @@ function _M:logout() --如果校验结果中的verified==false,则表示令牌无效 if jwt_obj.verified == false then ngx.log(ngx.WARN, "Invalid token: ".. jwt_obj.reason) - ngx.status = ngx.HTTP_UNAUTHORIZED ngx.exit(ngx.HTTP_UNAUTHORIZED) end --判断token是否超时 --令牌已过期 if jwt_obj.payload.exp and ngx.time() > jwt_obj.payload.exp then ngx.log(ngx.WARN, "token timeout ".. jwt_obj.reason) - ngx.status = ngx.HTTP_UNAUTHORIZED ngx.exit(ngx.HTTP_UNAUTHORIZED) end --通过用户id获取用户信息 @@ -356,9 +350,7 @@ function _M:logout() local keys = {} repeat local result, err = red:scan(cursor, 'MATCH', pattern) - if not result then - break - end + if not result then break end cursor = result[1] for _, key in ipairs(result[2]) do table.insert(keys, key) @@ -376,7 +368,7 @@ function _M:logout() end end -- 5.获取token中的信息进行所需用户的信息返回 - resp:response(ngx.HTTP_OK) + resp:response(status.SUCCESS) end return _M \ No newline at end of file diff --git a/src/service/system/account.lua b/src/service/system/account.lua index 4f96add..a6907be 100644 --- a/src/service/system/account.lua +++ b/src/service/system/account.lua @@ -3,6 +3,7 @@ --- Created by . --- DateTime: 2025/9/25 08:25 --- 业务逻辑 对账户数据表进行数据表业务处理 +local status = require("util.status") local resp = require("util.response") local accountDao = require("dao.system.account") local validator = require("validator.system.account") @@ -23,7 +24,9 @@ function _M.getSystemAccounts() local pageNum = ngx.var.pagenum or 1 local pageSize = ngx.var.pagesize or 10 local code, ret = accountDao.getSystemAccounts(pageNum, pageSize) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据账户id获取账户信息 @@ -37,7 +40,9 @@ function _M.getSystemAccount(m) end local id = m.id local code, ret = accountDao.getSystemAccount(id) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据账户id获取账户信息 @@ -57,12 +62,14 @@ function _M.addSystemAccount() local ok = validator.validateJson(body_data) --验证失败则返回 if not ok then - resp:response(0x000001) + resp:response(status.PARAM_TYPE_BIND_ERROR) return end -- 添加系统账户 local code, ret = accountDao.addSystemAccount(cjson.decode(body_data)) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据账户id删除账户信息 @@ -75,7 +82,9 @@ function _M.deleteSystemAccount(m) ngx.exit(ngx.HTTP_FORBIDDEN) end local code, ret = accountDao.deleteSystemAccount(m.id) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据账户id删除账户信息 @@ -95,11 +104,13 @@ function _M.updateSystemAccount(m) local ok = validator.validateJson(body_data) --验证失败则返回 if not ok then - resp:response(0x000001) + resp:response(status.PARAM_TYPE_BIND_ERROR) return end local code, ret = accountDao.updateSystemAccount(m.id, cjson.decode(body_data)) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end return _M diff --git a/src/service/system/application.lua b/src/service/system/application.lua index 5c8c100..a7a1c59 100644 --- a/src/service/system/application.lua +++ b/src/service/system/application.lua @@ -3,6 +3,7 @@ --- Created by . --- DateTime: 2025/9/27 16:02 --- 业务逻辑 对应用数据表进行数据表业务处理 +local status = require("util.status") local resp = require("util.response") local applicationDao = require("dao.system.application") local validator = require("validator.system.application") @@ -24,7 +25,9 @@ function _M.getSystemApplications() local pageNum = ngx.var.pagenum or 1 local pageSize = ngx.var.pagesize or 10 local code, ret = applicationDao.getSystemApplications() - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据应用id获取应用信息 @@ -37,7 +40,9 @@ function _M.getSystemApplication(m) ngx.exit(ngx.HTTP_FORBIDDEN) end local code,ret = applicationDao.getSystemApplication(m.id) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据组织id获取应用信息 @@ -50,7 +55,9 @@ function _M.getOrganizationApplication(m) ngx.exit(ngx.HTTP_FORBIDDEN) end local code, ret = applicationDao.getOrganizationApplication(m.id) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据用户id获取应用的信息 @@ -63,7 +70,9 @@ function _M.getUserApplication(m) ngx.exit(ngx.HTTP_FORBIDDEN) end local code, ret = applicationDao.getUserApplication(m.id) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据应用id获取应用信息 @@ -83,12 +92,14 @@ function _M.addSystemApplication() local ok = validator.validateJson(body_data) --验证失败则返回 if not ok then - resp:response(0x000001) + resp:response(status.PARAM_TYPE_BIND_ERROR) return end -- 添加应用程序 local code, ret = applicationDao.addApplication(cjson.decode(body_data)) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据应用id删除应用信息 @@ -101,7 +112,9 @@ function _M.deleteSystemApplication(m) ngx.exit(ngx.HTTP_FORBIDDEN) end local code, ret = applicationDao.deleteApplication(m.id) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据应用id删除应用信息 @@ -121,11 +134,13 @@ function _M.updateSystemApplication(m) local ok = validator.validateJson(body_data) --验证失败则返回 if not ok then - resp:response(0x000001) + resp:response(status.PARAM_TYPE_BIND_ERROR) return end local code, ret = applicationDao.updateSystemApplication(m.id, cjson.decode(body_data)) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end return _M diff --git a/src/service/system/department.lua b/src/service/system/department.lua index cdc7c91..4e04424 100644 --- a/src/service/system/department.lua +++ b/src/service/system/department.lua @@ -3,6 +3,7 @@ --- Created by . --- DateTime: 2025/9/28 10:22 --- 业务逻辑 对组织架构数据表进行数据表业务处理 +local status = require("util.status") local resp = require("util.response") local departmentDao = require("dao.system.department") local validator = require("validator.system.department") @@ -34,7 +35,9 @@ function _M.getSystemDepartment(m) ngx.exit(ngx.HTTP_FORBIDDEN) end local code, ret = departmentDao.getSystemDepartment(m.id) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据组织id添加组织架构信息 @@ -54,12 +57,14 @@ function _M.addSystemDepartment() local ok = validator.validateJson(body_data) --验证失败则返回 if not ok then - resp:response(0x000001) + resp:response(status.PARAM_TYPE_BIND_ERROR) return end --ngx.say(body_data) local code, ret = departmentDao.addSystemDepartment(cjson.decode(body_data)) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据组织id删除组织架构信息 @@ -73,7 +78,9 @@ function _M.deleteSystemDepartment(m) end --删除部门数据 local code, ret = departmentDao.deleteSystemDepartment(m.id) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据组织id删除组织架构信息 @@ -93,11 +100,13 @@ function _M.updateSystemDepartment(m) local ok = validator.validateJson(body_data) --验证失败则返回 if not ok then - resp:response(0x000001) + resp:response(status.PARAM_TYPE_BIND_ERROR) return end local code, ret = departmentDao.updateSystemDepartment(m.id, cjson.decode(body_data)) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end return _M diff --git a/src/service/system/login.lua b/src/service/system/login.lua index a70b0f9..f6c7f92 100644 --- a/src/service/system/login.lua +++ b/src/service/system/login.lua @@ -3,6 +3,7 @@ --- Created by admin. --- DateTime: 2025/10/28 11:09 --- 用于 +local status = require("util.status") local resp = require("util.response") local loginDao = require("dao.system.login") local validator = require("validator.system.login") @@ -51,7 +52,7 @@ function _M.login() local data = {} data["token"] = jwt_token data["userInfo"] = ret - resp:response(code, data) + resp:response(status.SUCCESS, data) end --用户注册业务逻辑处理 @@ -103,7 +104,7 @@ function _M.logout() local role_id = ret["body"]["payload"]["role_id"] local role_name = ret["body"]["payload"]["role_name"] ngx.log(ngx.INFO, "userid:"..userid.." username:"..username.." role_id:"..role_id.." role_name:"..role_name.." logout system") - resp:response(0, "用户退出系统成功") + resp:response(status.SUCCESS, "用户退出系统成功") end --根据token获取用户信息 @@ -115,7 +116,7 @@ function _M.user() --验证失败则返回 local code = retToken["code"] if code ~= 200 then - resp:response(code, retToken["message"]) + resp:response(status.TOKEN_INVALID, retToken["message"]) return end --验证成功获取用户id信息 @@ -123,11 +124,11 @@ function _M.user() local code, ret = loginDao.getUser(userid) --读取数据错误 if code ~= 0 or table.getn(ret) < 0 then - resp:response(0x000001) + resp:response(status.PARAM_TYPE_BIND_ERROR) return end --返回登陆的用户信息 - resp:response(code, ret) + resp:response(status.SUCCESS, ret) end --根据token获取用户登录权限 @@ -151,11 +152,11 @@ function _M.permission() local code, ret = loginDao.getUser(userid) --读取数据错误 if code ~= 0 or table.getn(ret) < 0 then - resp:response(0x000001) + resp:response(status.PERMISSION_UNAUTHORISE) return end --返回用户权限信息 - resp:response(code, ret) + resp:response(status.SUCCESS, ret) end return _M \ No newline at end of file diff --git a/src/service/system/permission.lua b/src/service/system/permission.lua index 88daf5f..036db47 100644 --- a/src/service/system/permission.lua +++ b/src/service/system/permission.lua @@ -3,6 +3,7 @@ --- Created by . --- DateTime: 2025/9/27 17:06 --- 业务逻辑 对权限数据表进行数据表业务处理 +local status = require("util.status") local resp = require("util.response") local permissionDao = require("dao.system.permission") local validator = require("validator.system.permission") @@ -24,7 +25,9 @@ function _M.getSystemPermissions() local pageNum = ngx.var.pagenum or 1 local pageSize = ngx.var.pagesize or 10 local code,ret = permissionDao.getSystemPermissions(pageNum, pageSize) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据权限id获取权限信息 @@ -37,7 +40,9 @@ function _M.get_permission(m) ngx.exit(ngx.HTTP_FORBIDDEN) end local code,ret = permissionDao.getPermission(m.id) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据角色id获取使用的权限 @@ -50,7 +55,9 @@ function _M.getSystemPermissionByRole(m) ngx.exit(ngx.HTTP_FORBIDDEN) end local code,ret = dao.getPermissionByRole(m.id) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据权限id获取账号信息 @@ -70,12 +77,14 @@ function _M.addSystemPermission() local ok = validator.validateJson(body_data) --验证失败则返回 if not ok then - resp:response(0x000001) + resp:response(status.PARAM_TYPE_BIND_ERROR) return end --ngx.say(body_data) local code, ret = permissionDao.addPermission(cjson.decode(body_data)) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据账号id删除账号信息 @@ -88,7 +97,9 @@ function _M.deleteSystemPermission(m) ngx.exit(ngx.HTTP_FORBIDDEN) end local code, ret = permissionDao.deleteSystemPermission(m.id) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据账号id删除账号信息 @@ -108,11 +119,13 @@ function _M.updateSystemPermission(m) local ok = validator.validateJson(body_data) --验证失败则返回 if not ok then - resp:response(0x000001) + resp:response(status.PARAM_TYPE_BIND_ERROR) return end local code, ret = permissionDao.updatePermission(m.id, cjson.decode(body_data)) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end return _M diff --git a/src/service/system/position.lua b/src/service/system/position.lua index 9dac7b1..991bcb4 100644 --- a/src/service/system/position.lua +++ b/src/service/system/position.lua @@ -3,6 +3,7 @@ --- Created by . --- DateTime: 2025/11/04 15:01 --- 业务逻辑 对岗位数据表进行数据表业务处理 +local status = require("util.status") local resp = require("util.response") local positionDao = require("dao.system.position") local validator = require("validator.system.position") @@ -24,7 +25,9 @@ function _M.getSystemPositions() local pageNum = ngx.var.pagenum or 1 local pageSize = ngx.var.pagesize or 10 local code,ret = positionDao.getSystemPositions(pageNum, pageSize) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据岗位id获取岗位信息 @@ -37,7 +40,9 @@ function _M.getSystemPosition(m) ngx.exit(ngx.HTTP_FORBIDDEN) end local code,ret = positionDao.getSystemPosition(m.id) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据岗位id添加岗位信息 @@ -57,12 +62,14 @@ function _M.addSystemPosition() local ok = validator.validateJson(body_data) --验证失败则返回 if not ok then - resp:response(0x000001) + resp:response(status.PARAM_TYPE_BIND_ERROR) return end --ngx.say(body_data) local code, ret = positionDao.addSystemPosition(cjson.decode(body_data)) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据岗位id删除岗位信息 @@ -75,7 +82,9 @@ function _M.deleteSystemPosition(m) ngx.exit(ngx.HTTP_FORBIDDEN) end local code, ret = positionDao.deleteSystemPosition(m.id) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据岗位id删除岗位信息 @@ -95,11 +104,13 @@ function _M.updateSystemPosition(m) local ok = validator.validateJson(body_data) --验证失败则返回 if not ok then - resp:response(0x000001) + resp:response(status.PARAM_TYPE_BIND_ERROR) return end local code, ret = positionDao.updateSystemPosition(m.id, cjson.decode(body_data)) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end return _M diff --git a/src/service/system/role.lua b/src/service/system/role.lua index 4f94bf6..b43c8f0 100644 --- a/src/service/system/role.lua +++ b/src/service/system/role.lua @@ -3,6 +3,7 @@ --- Created by . --- DateTime: 2025/9/27 15:19 --- 业务逻辑 对用户角色数据表进行数据表业务处理 +local status = require("util.status") local resp = require("util.response") local roleDao = require("dao.system.role") local validator = require("validator.system.role") @@ -25,7 +26,9 @@ function _M.getSystemRoles() local pageNum = ngx.var.pagenum or 1 local pageSize = ngx.var.pagesize or 10 local code,ret = roleDao.getSystemRoles(pageNum, pageSize) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据角色id获取角色信息 @@ -38,7 +41,9 @@ function _M.getSystemRole(m) ngx.exit(ngx.HTTP_FORBIDDEN) end local code,ret = roleDao.getSystemRole(m.id) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据角色id获取角色信息 @@ -58,12 +63,14 @@ function _M.addSystemRole() local ok = validator.validateJson(body_data) --验证失败则返回 if not ok then - resp:response(0x000001) + resp:response(status.PARAM_NOT_COMPLETE) return end --ngx.say(body_data) local code, ret = roleDao.addSystemRole(cjson.decode(body_data)) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据角色id删除角色信息 @@ -76,7 +83,9 @@ function _M.deleteSystemRole(m) ngx.exit(ngx.HTTP_FORBIDDEN) end local code, ret = roleDao.deleteSystemRole(m.id) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据角色id删除角色信息 @@ -96,11 +105,13 @@ function _M.updateSystemRole(m) local ok = validator.validateJson(body_data) --验证失败则返回 if not ok then - resp:response(0x000001) + resp:response(status.PARAM_TYPE_BIND_ERROR) return end local code, ret = roleDao.updateSystemRole(m.id, cjson.decode(body_data)) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end return _M diff --git a/src/service/system/user.lua b/src/service/system/user.lua index 24e0799..98138e0 100644 --- a/src/service/system/user.lua +++ b/src/service/system/user.lua @@ -3,6 +3,7 @@ --- Created by . --- DateTime: 2025/9/25 08:19 --- 业务逻辑 对用户数据表进行数据表业务处理 +local status = require("util.status") local resp = require("util.response") local userDao = require("dao.system.user") local validator = require("validator.system.user") @@ -44,8 +45,10 @@ function _M.getSystemUsers(m) --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.getSystemUsers(pageNum, pageSize) - resp:response(code, ret) + local code, ret = userDao.getSystemUsers(pageNum, pageSize) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据用户id获取用户信息 @@ -61,13 +64,15 @@ function _M.getSystemUser(m) --获取登录的用户信息 local payload = ngx.var.uid local userid = getUserId() - if userid ~= m.id then + if userid ~= m.id then --非管理员情况下 ngx.log(ngx.WARN, "用户与使用token中的用户id不一致", userid, m.id) ngx.status = ngx.HTTP_NOT_ALLOWED ngx.exit(ngx.HTTP_NOT_ALLOWED) end - local code,ret = userDao.getSystemUser(m.id) - resp:response(code, ret) + local code, ret = userDao.getSystemUser(m.id) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据用户id获取用户信息 @@ -87,14 +92,16 @@ function _M.addSystemUser(m) local ok = validator.validateJson(body_data) --验证失败则返回 if not ok then - resp:response(0x000001) + resp:response(status.PARAM_NOT_COMPLETE) return end --ngx.say(body_data) local jsonData = cjson.decode(body_data) --ngx.say(jsonData) local code, ret = userDao.addSystemUser(jsonData) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据用户id删除用户信息 @@ -107,7 +114,9 @@ function _M.deleteSystemUser(m) ngx.exit(ngx.HTTP_FORBIDDEN) end local code, ret = userDao.deleteSystemUser(m.id) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end --根据用户id删除用户信息 @@ -122,7 +131,6 @@ function _M.updateSystemUser(m) local userid = getUserId() if userid ~= m.id then ngx.log(ngx.WARN, "用户与使用token中的用户id不一致") - ngx.status = ngx.HTTP_NOT_ALLOWED ngx.exit(ngx.HTTP_NOT_ALLOWED) end --读取请求体的数据 @@ -133,12 +141,14 @@ function _M.updateSystemUser(m) local ok = validator.validateJson(body_data) --验证失败则返回 if not ok then - resp:response(0x000001) + resp:response(status.PARAM_NOT_COMPLETE) return end --将数据更新到数据表中 local code, ret = userDao.updateSystemUser(m.id, cjson.decode(body_data)) - resp:response(code, ret) + local state = status.SUCCESS + if code ~= 0 then state = status.DATA_IS_WRONG end + resp: response(state, ret) end return _M diff --git a/src/util/response.lua b/src/util/response.lua index 22eed49..3e6512c 100644 --- a/src/util/response.lua +++ b/src/util/response.lua @@ -1,48 +1,35 @@ local cjson = require('cjson') -local conf = require('config') -local error_code = require('util.status') +local STATUS = require('util.status') local _M = {} -function _M:json(status, message, data, http_status) +function _M:json(state, message, data, http_status) -- you can modify this response struct as you favor - if status == 0 then status = ngx.HTTP_OK end + local status = state or STATUS.UNKNOWN_ERROR + local code = status.code local msg = message local response_status = http_status or ngx.HTTP_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 - msg = error_code[status] - end - local response = {code = status, msg = msg, result = data,timestamp = ngx.time()} - if not response.code then - response.code = -1 - response.message = 'not find status code' + msg = status.message end + local response = { code = code, msg = msg, result = data, timestamp = ngx.time() } return { code = response_status, - headers = {content_type = 'application/json; charset=UTF-8'}, + headers = { content_type = 'application/json; charset=UTF-8' }, body = cjson.encode(response) } end -function _M:json(status, data, http_status) +function _M:json(state, data, http_status) -- you can modify this response struct as you favor - if status == 0 then status = ngx.HTTP_OK end - local msg = '' - local response_status = http_status or ngx.HTTP_OK - msg = error_code[status] - - local response = {code = status, msg = msg, result = data,timestamp = ngx.time()} - if not response.code then - response.code = -1 - response.message = 'not find status code' - end + local status = state or STATUS.UNKNOWN_ERROR + local code = status.code + local msg = status.message + local response_status = http_status or ngx.HTTP_OK + local response = { code = code, msg = msg, result = data,timestamp = ngx.time() } return { code = response_status, - headers = {content_type = 'application/json; charset=UTF-8'}, + headers = { content_type = 'application/json; charset=UTF-8' }, body = cjson.encode(response) } end @@ -77,8 +64,8 @@ function _M:send(response) end end -function _M:response(code, result) - local response = self:json(code, result) +function _M:response(state, result) + local response = self:json(state, result) self:send(response) end diff --git a/src/util/status.lua b/src/util/status.lua index 343c4db..62f0ea4 100644 --- a/src/util/status.lua +++ b/src/util/status.lua @@ -16,27 +16,82 @@ --ngx.HTTP_METHOD_NOT_IMPLEMENTED (501) --ngx.HTTP_SERVICE_UNAVAILABLE (503) --ngx.HTTP_GATEWAY_TIMEOUT (504) -return { - -- 系统状态码 - [0x0000C8] = 'ok', - [0x000001] = '验证错误', - [0x000002] = '系统错误', - [0x000003] = '系统异常', - [0x000004] = '未授权访问', - -- user module - [0x010000] = '注册失败,用户已存在', - [0x010001] = '注册失败,手机号已存在', - [0x010002] = '登录失败,手机号或密码错误', - [0x010003] = '登录失败,用户不存在', - [0x010004] = '短信验证失败,短信验证码错误', - [0x010005] = '重置密码失败,旧密码错误', - [0x010006] = '重置密码失败,系统异常', - [0x010007] = '重置密码失败,新密码不能和旧密码相同', - [0x010008] = '获取用户信息失败,系统错误', - [0x010009] = '重置密码失败,用户不存在', - [0x01000A] = '获取用户信息失败,用户未登录', - [0x01000B] = '获取用户信息失败,用户不存在', - [0x01000C] = '添加用户信息失败,用户已存在', - [0x01000D] = '修改用户信息失败,用户不存在', -} \ No newline at end of file +-- 通用响应状态 @enum STATUS_CODES +local _M = { + -- 成功状态码 + SUCCESS = { code = 200, message = "操作成功" }, + + --[[ + HTTP_SPECIAL_RESPONSE(300, "操作成功"), + HTTP_MOVED_PERMANENTLY(301, "操作成功"), + HTTP_MOVED_TEMPORARILY(302, "操作成功"), + HTTP_SEE_OTHER(303, "操作成功"), + HTTP_NOT_MODIFIED(304, "操作成功"), + + HTTP_BAD_REQUEST(400, "操作成功"), + HTTP_UNAUTHORIZED(401, "操作成功"), + HTTP_FORBIDDEN(403, "操作成功"), + HTTP_NOT_FOUND(404, "操作成功"), + HTTP_NOT_ALLOWED(405, "操作成功"), + + -- + HTTP_INTERNAL_SERVER_ERROR(500, "操作成功"), + HTTP_METHOD_NOT_IMPLEMENTED(501, "操作成功"), + HTTP_SERVICE_UNAVAILABLE(503, "操作成功"), + HTTP_GATEWAY_TIMEOUT(504, "操作成功"), + --]] + + -- 参数错误:1000-1099 + PARAM_IS_INVALID = { code = 1000, message = "参数无效" }, + PARAM_IS_BLANK = { code = 1001, message = "参数为空" }, + PARAM_TYPE_BIND_ERROR = { code = 1002, message = "参数格式错误" }, + PARAM_NOT_COMPLETE = { code = 1003, message = "参数缺失" }, + + DATA_NONE_FOUNT = { code = 1004, message = "数据未找到" }, + DATA_IS_WRONG = { code = 1005, message = "数据有误" }, + DATA_ALREADY_EXISTED = { code = 1006, message = "数据已存在" }, + AUTH_CODE_ERROR = { code = 1007, message = "验证码错误" }, + + -- 注册错误:1100-1199 + REG_USERNAME_EXIST = { code = 1100, message = "注册失败,用户名已存在" }, + REG_TELPHONE_EXIST = { code = 1101, message = "注册失败,手机号已存在" }, + REG_EMAIL_EXIST = { code = 1102, message = "注册失败,邮箱地址已存在" }, + REG_IDCARD_EXIST = { code = 1103, message = "注册失败,证件号码已存在" }, + + RESET_PASSWD_ERROR = { code = 1104, message = '重置密码失败,旧密码错误' }, + RESET_PASSWD_SYSTEM_ERROR = { code = 1105, message = '重置密码失败,系统异常' }, + RESET_PASSWD_DIFFERENT = { code = 1106, message = '重置密码失败,新密码不能和旧密码相同' }, + + -- 用户错误:1200-1299 + USER_NOT_LOGIN = { code = 1200, message = "用户未登录,请先登录" }, + USER_LOGIN_ERROR = { code = 1201, message = "用户不存在或密码错误" }, + USER_ACCOUNT_FORBIDDEN = { code = 1202, message = "用户已被禁用" }, + USER_ACCOUNT_LOCKED = { code = 1203, message = "用户已被锁定" }, + USER_NOT_EXIST = { code = 1204, message = "用户不存在" }, + USER_HAS_EXISTED = { code = 1205, message = "用户已存在" }, + + ACCOUNT_NOT_LOGIN = { code = 1250, message = "账户未登录,请先登录" }, + ACCOUNT_LOGIN_ERROR = { code = 1251, message = "账户不存在或密码错误" }, + ACCOUNT_ACCOUNT_FORBIDDEN = { code = 1252, message = "账户已被禁用" }, + ACCOUNT_ACCOUNT_LOCKED = { code = 1253, message = "账户已被锁定" }, + ACCOUNT_NOT_EXIST = { code = 1254, message = "账户不存在" }, + ACCOUNT_HAS_EXISTED = { code = 1255, message = "账户已存在" }, + + -- 错误:1300-1399 + + -- 权限错误:1400-1499 + PERMISSION_UNAUTHENTICATED = { code = 1400, message = "此操作需要登陆系统" }, + PERMISSION_UNAUTHORISE = { code = 1401, message = "权限不足,无权操作" }, + PERMISSION_EXPIRED = { code = 1402, message = "登录状态已过期" }, + + TOKEN_EXPIRED = { code = 1403, message = "token已过期" }, + TOKEN_LIMIT = { code = 1404, message = "访问次数受限制" }, + TOKEN_INVALID = { code = 1405, message = "无效token" }, + TOKEN_SIGNATURE_ERROR = { code = 1406, message = "签名失败" }, + + -- 未知错误:9999 + UNKNOWN_ERROR = { code = 9999, message = "未知错误" }, +} + +return _M \ No newline at end of file diff --git a/src/validator/oauth/oauth.lua b/src/validator/oauth/oauth.lua index eb2f27c..d2e0b93 100644 --- a/src/validator/oauth/oauth.lua +++ b/src/validator/oauth/oauth.lua @@ -100,7 +100,6 @@ function _M.validateUserinfo(jsonData) return result end ---grant_type=refresh_token&refresh_token=fbde81ee-f419-42b1-1234-9191f1f95be9&client_id=demoClientId&client_secret=demoClientSecret local schemaRefresh = { type = "object", properties = {