local skynet = require "skynet" local s = require "service" local tools = require "tools" local cjson = require "cjson" local mysql = require "skynet.db.mysql" local runconfig = require("run_config") local back_response = require "back_response" local db = nil --数据库 --数据库end --接口 s.resp.admin_login = function(msg_body,fd) local isOk,key = tools.checkData({"account","password"},msg_body) if not isOk then return tools.response(fd,200,cjson.encode({code=9001,msg = string.format("缺少字段: '%s'.", key)})) end skynet.error("msg_body") if msg_body.account == "zhuoyueweizhong2024" and msg_body.password == "zywz123" then return tools.response(fd,200,cjson.encode({code=10000,msg = "成功!",data={user_name="卓越微众"}})) else return tools.response(fd,200,cjson.encode({code=10001,msg = "登陆失败,密码错误!"})) end tools.dump(msg_body) end --添加用户 s.resp.add_user = function(msg_body,fd) local isOk,key = tools.checkData({"account","password","user_name","group_id","permit_id"},msg_body) if not isOk then return tools.response(fd,200,cjson.encode({code=9001,msg = string.format("缺少字段: '%s'.", key)})) end local user_data = nil isOk,user_data = skynet.call("dbmgr","lua","on_recv","select_user_by_account",msg_body.account) if isOk then return tools.response(fd,200,string.format("用户 %s 已存在",msg_body.account)) end isOk = skynet.call("dbmgr","lua","on_recv","add_new_user",msg_body) if not isOk then return tools.response(fd,200,"创建失败!") end return tools.response(fd,200,cjson.encode({code=10000,msg = "创建成功!"})) end --修改用户 s.resp.modify_user = function(msg_body,fd) local isOk,key = tools.checkData({"user_id","modify_password","modify_permissions_id","modify_group_id","is_ban","user_name"},msg_body) if not isOk then return tools.response(fd,200,cjson.encode({code=9001,msg = string.format("缺少字段: '%s'.", key)})) end function action(isOk) if isOk then return tools.response(fd,200,cjson.encode({code=10000,msg = "修改用户成功!"})) end return tools.response(fd,200,cjson.encode({code=10001,msg = "修改用户失败!"})) end local sql = string.format("UPDATE users SET password ='%s' ,group_id = %d ,permit_id = %d , is_ban = %d , name = '%s' WHERE id = %d ",msg_body.modify_password,msg_body.modify_group_id,msg_body.modify_permissions_id,msg_body.is_ban,msg_body.user_name,msg_body.user_id) db:query(sql) skynet.error(sql) action(true) end --获取用户列表 s.resp.get_user_list = function(msg_body,fd) local isOk,key = tools.checkData({"page","count"},msg_body) if not isOk then return tools.response(fd,200,cjson.encode({code=9001,msg = string.format("缺少字段: '%s'.", key)})) end function action(isOk,list) local error_json = '{"code": 10000, "msg": "获取用户列表失败", "data": [] }' if isOk then local _list = {} local total_count = 0 list ,total_count = tools.getPageData(msg_body.page,msg_body.count,list) if #list<=0 then return tools.response(fd,200,error_json) end return tools.response(fd,200,cjson.encode({code=10000,msg = "获取用户列表成功",data=list})) end return tools.response(fd,200,error_json) end local sql = string.format("select * from users") local res = db:query(sql) if res and #res > 0 then local tab = {} if #res == 1 then tab[1] = tools.getDbResData(res) else tab = tools.getDbResData(res) end action(true,tab) skynet.error("Found data:") else action(false,nil) skynet.error("No data found.") end end --添加权限 s.resp.add_permissions = function(msg_body,fd) local isOk,key = tools.checkData({"permissions_name","permissions_describe","permissions_json"},msg_body) if not isOk then return tools.response(fd,200,cjson.encode({code=9001,msg = string.format("缺少字段: '%s'.", key)})) end function action(isOk) if isOk then return tools.response(fd,200,cjson.encode({code=10000,msg = "添加权限成功!"})) end return tools.response(fd,200,cjson.encode({code=10001,msg = "添加权限失败!"})) end local permissions_json = msg_body.permissions_json local sql = string.format("INSERT INTO permissions_list_tab (permissions_name, permissions_describe, info) VALUES ('%s','%s','%s')",msg_body.permissions_name,msg_body.permissions_describe,permissions_json) local res = db:query(sql) skynet.error(sql) action(true) end --修改权限 s.resp.modify_permissions = function(msg_body,fd) local isOk,key = tools.checkData({"permissions_id","modify_permissions_name","modify_permissions_describe","modify_permissions_json"},msg_body) if not isOk then return tools.response(fd,200,cjson.encode({code=9001,msg = string.format("缺少字段: '%s'.", key)})) end function action(isOk) if isOk then return tools.response(fd,200,cjson.encode({code=10000,msg = "修改权限权限成功!"})) end return tools.response(fd,200,cjson.encode({code=10001,msg = "修改权限权限失败!"})) end local permissions_json = msg_body.modify_permissions_json -- skynet.error("json",msg_body.modify_permissions_json) local sql = string.format("UPDATE permissions_list_tab SET permissions_name ='%s' ,permissions_describe ='%s' , info ='%s' WHERE id = %d ",msg_body.modify_permissions_name,msg_body.modify_permissions_describe,permissions_json,msg_body.permissions_id) db:query(sql) skynet.error(sql) action(true) end --获取权限列表 s.resp.get_permissions_list = function(msg_body,fd) local isOk,key = tools.checkData({"page","count"},msg_body) if not isOk then return tools.response(fd,200,cjson.encode({code=9001,msg = string.format("缺少字段: '%s'.", key)})) end function action(isOk,list) local error_json = '{"code": 10000, "msg": "获取权限列表失败", "data": [] }' if isOk then local _list = {} local total_count = 0 list ,total_count = tools.getPageData(msg_body.page,msg_body.count,list) if #list<=0 then return tools.response(fd,200,error_json) end return tools.response(fd,200,cjson.encode({code=10000,msg = "获取权限列表成功",data=list,total_count=total_count})) end return tools.response(fd,200,error_json) end local sql = string.format("select * from permissions_list_tab") local res = db:query(sql) if res and #res > 0 then local tab = {} if #res == 1 then tab[1] = tools.getDbResData(res) else tab = tools.getDbResData(res) end action(true,tab) skynet.error("Found data:") else action(false,nil) skynet.error("No data found.") end end --添加部门 s.resp.add_group = function(msg_body,fd) local isOk,key = tools.checkData({"group_name","group_describe","info"},msg_body) if not isOk then return tools.response(fd,200,cjson.encode({code=9001,msg = string.format("缺少字段: '%s'.", key)})) end function action(isOk) if isOk then return tools.response(fd,200,cjson.encode({code=10000,msg = "添加部门成功!"})) end return tools.response(fd,200,cjson.encode({code=10001,msg = "添加部门失败!"})) end local info = "{}" local sql = string.format("INSERT INTO group_list_tab (group_name, group_describe, info) VALUES ('%s','%s','%s')",msg_body.group_name,msg_body.group_describe,info) local res = db:query(sql) skynet.error(sql) action(true) end --修改部门 s.resp.modify_group = function(msg_body,fd) local isOk,key = tools.checkData({"group_id","modify_name","modify_describe","info"},msg_body) if not isOk then return tools.response(fd,200,cjson.encode({code=9001,msg = string.format("缺少字段: '%s'.", key)})) end function action(isOk) if isOk then return tools.response(fd,200,cjson.encode({code=10000,msg = "修改权限权限成功!"})) end return tools.response(fd,200,cjson.encode({code=10001,msg = "修改权限权限失败!"})) end local info = "{}" local sql = string.format("UPDATE group_list_tab SET group_name ='%s' ,group_describe ='%s' , info ='%s' WHERE id = %d ",msg_body.modify_name,msg_body.modify_describe,info,msg_body.group_id) db:query(sql) action(true) end --获取部门列表 s.resp.get_group_list = function(msg_body,fd) local isOk,key = tools.checkData({"page","count"},msg_body) if not isOk then return tools.response(fd,200,cjson.encode({code=9001,msg = string.format("缺少字段: '%s'.", key)})) end function action(isOk,list) local error_json = '{"code": 10000, "msg": "获取部门列表失败", "data": [] }' if isOk then local _list = {} local total_count = 0 list ,total_count = tools.getPageData(msg_body.page,msg_body.count,list) if #list<=0 then return tools.response(fd,200,error_json) end return tools.response(fd,200,cjson.encode({code=10000,msg = "获取部门列表成功",data=list})) end return tools.response(fd,200,error_json) end local sql = string.format("select * from group_list_tab") local res = db:query(sql) if res and #res > 0 then local tab = {} if #res == 1 then tab[1] = tools.getDbResData(res) else tab = tools.getDbResData(res) end action(true,tab) skynet.error("Found data:") else action(false,nil) skynet.error("No data found.") end end --修改分类 s.resp.modify_classification = function(msg_body,fd) local isOk,key = tools.checkData({"info"},msg_body) if not isOk then return tools.response(fd,200,cjson.encode({code=9001,msg = string.format("缺少字段: '%s'.", key)})) end function action(isOk) if isOk then return tools.response(fd,200,cjson.encode({code=10000,msg = "修改权限权限成功!"})) end return tools.response(fd,200,cjson.encode({code=10001,msg = "修改权限权限失败!"})) end local info = msg_body.info local sql = string.format("UPDATE classification_list_tab SET info ='%s' WHERE self_id = 1 ",info) -- skynet.error("sql",sql) db:query(sql) action(true) end --接口end s.resp.on_recv = function (source, fd, msg_id, msg_body) skynet.error("接收一后台消息 ",msg_id) -- tools.dump(msg_body) local func = string.gsub(msg_id, '/back/', '') if s.resp[func] ~=nil then msg_body = cjson.decode(msg_body) return s.resp[func](msg_body,fd) end return tools.response(fd,200,string.format("接口 %s 不存在",func)) end s.init = function() db=mysql.connect({ host=runconfig.db_tost, port=runconfig.db_port, database=runconfig.db_name, user="root", password=runconfig.db_pw, max_packet_size = 1024 * 1024, on_connect = nil }) if not db then skynet.error("failed to connect") skynet.exit() else skynet.error(" config success to connect to mysql server") skynet.error(" 初始化后台服务成功!") end end s.start(...)