local skynet = require "skynet" local s = require "service" local mysql = require "skynet.db.mysql" local runconfig = require("run_config") local cjson = require "cjson" local tools = require "tools" local db = nil local config = { config_info = { ["1"] = { --片头 [2]={ name = "横屏 16:9", is_ban = 0, classification_id = 2, describe = "", }, [1]={ name = "竖屏 9:16", is_ban = 0, classification_id = 1, describe = "", } }, ["2"] = { --正片 [2]={ name = "横屏 16:9", is_ban = 0, classification_id = 2, describe = "", }, [1]={ name = "竖屏 9:16", is_ban = 0, classification_id = 1, describe = "", } }, ["3"] = { --片尾 [2]={ name = "横屏 16:9", is_ban = 0, classification_id = 2, describe = "", }, [1]={ name = "竖屏 9:16", is_ban = 0, classification_id = 1, describe = "", } }, ["4"] = { --bgm音频 [1]={ name = "全部", is_ban = 0, classification_id = 1, describe = "", } }, ["5"] = { --片尾音频 [1]={ name = "全部", is_ban = 0, classification_id = 1, describe = "", } }, ["6"] = { --图片 [1]={ name = "全部", is_ban = 0, classification_id = 1, describe = "", } }, }, permissions_info = { }, other_info = { }, group_info = { } } s.resp.update_permissions_info = function(msg_body,fd) local permissions_info = {} skynet.error("update_permissions_info") function action(isOk,list) local error_json = '{"code": 10000, "msg": "更新权限失败成功"}' if isOk then if #list>0 then for i = 1, #list, 1 do local permissions_id = list[i].id permissions_info[permissions_id] = list[i] end config.permissions_info = permissions_info; end end if fd~=nil then s.resp.updateConfigDb() return tools.response(fd,200,error_json) end 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.update_group_info = function(msg_body,fd) local group_info = {} function action(isOk,list) local error_json = '{"code": 10000, "msg": "更新权限失败成功"}' if isOk then if #list>0 then for i = 1, #list, 1 do local group_id = list[i].id group_info[group_id] = list[i] end config.group_info = group_info; end end if fd~=nil then s.resp.updateConfigDb() return tools.response(fd,200,error_json) end 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.update_other_info = function() end s.resp.update_config_info = function(msg_body,fd) local config_info = {} skynet.error("update_config_info") function action(isOk,info) local error_json = '{"code": 10000, "msg": "更新权限失败成功"}' if isOk then config.config_info = info.info else config_info = cjson.encode(config.config_info) local sql = string.format("INSERT INTO classification_list_tab (info,self_id) VALUES ('%s',%d)",config_info,1) local res = db:query(sql) skynet.error(sql) end if fd~=nil then s.resp.updateConfigDb() return tools.response(fd,200,error_json) end end local sql = string.format("select info from classification_list_tab where self_id = 1") local res = db:query(sql) if res and #res > 0 then local tab = {} tab = tools.getDbResData(res) action(true,tab) skynet.error("Found data:") else action(false,nil) skynet.error("No data found.") end end s.resp.get_config_info = function(msg_body,fd) return tools.response(fd,200,cjson.encode({code=10000,msg = "成功!",data=config.config_info})) end s.resp.get_permissions_info = function(msg_body,fd) local permissions_info = {} if config.permissions_info~=nil and #config.permissions_info>0 then permissions_info = config.permissions_info end -- tools.dump(permissions_info) return tools.response(fd,200,cjson.encode({code=10000,msg = "成功!",data=permissions_info})) end s.resp.get_group_info = function(msg_body,fd) return tools.response(fd,200,cjson.encode({code=10000,msg = "成功!",data=config.group_info})) end s.resp.get_other_info = function(fd) return tools.response(fd,200,cjson.encode({code=10000,msg = "成功!",data=config.other_info})) end s.resp.on_recv = function (source, fd, msg_id, msg_body,user_data) skynet.error("接收一条客户端配置的接口 ",msg_id) local func = string.gsub(msg_id, '/config/', '') skynet.error("func",func) if s.resp[func] ~=nil then 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") s.resp.initConfig() end end s.resp.updateConfigDb = function() -- skynet.fork(function() -- local config_info ="{}" -- if config.config_info~=nil and #config.config_info>0 then -- config_info = cjson.encode(config.config_info) -- end -- local permissions_info ="{}" -- if config.permissions_info~=nil and #config.permissions_info>0 then -- permissions_info = cjson.encode(config.permissions_info) -- end -- local other_info ="{}" -- if config.other_info~=nil and #config.other_info>0 then -- other_info = cjson.encode(config.other_info) -- end -- local group_info ="{}" -- if config.group_info~=nil and #config.group_info>0 then -- group_info = cjson.encode(config.group_info) -- end -- local sql = string.format("UPDATE video_tools_config_tab SET config_info = '%s' , permissions_info = '%s' , other_info = '%s' , group_info = '%s' WHERE id =%d",config_info,permissions_info,other_info,group_info,1) -- db:query(sql) -- end) end s.resp.initConfig = function() -- local sql = string.format("select * from video_tools_config_tab where id = %d ",1) -- local res = db:query(sql) -- skynet.error('folder_res_list',sql) -- 判断是否找到数据 -- if res and #res > 0 then -- local tab = {} -- tab = tools.getDbResData(res) -- config.config_info = cjson.decode( tab.config_info) -- config.group_info = cjson.decode( tab.group_info) -- config.permissions_info = cjson.decode( tab.permissions_info) -- config.other_info = cjson.decode( tab.other_info) -- else s.resp.update_config_info() s.resp.update_permissions_info() s.resp.update_group_info() s.resp.update_other_info() s.resp.updateConfigDb() -- end skynet.error("静态配置完成!") end s.start(...)