init.lua 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. local skynet = require "skynet"
  2. local s = require "service"
  3. local mysql = require "skynet.db.mysql"
  4. local runconfig = require("run_config")
  5. local cjson = require "cjson"
  6. local tools = require "tools"
  7. local db = nil
  8. local config = {
  9. config_info = {
  10. ["1"] = { --片头
  11. [2]={
  12. name = "横屏 16:9",
  13. is_ban = 0,
  14. classification_id = 2,
  15. describe = "",
  16. },
  17. [1]={
  18. name = "竖屏 9:16",
  19. is_ban = 0,
  20. classification_id = 1,
  21. describe = "",
  22. }
  23. },
  24. ["2"] = { --正片
  25. [2]={
  26. name = "横屏 16:9",
  27. is_ban = 0,
  28. classification_id = 2,
  29. describe = "",
  30. },
  31. [1]={
  32. name = "竖屏 9:16",
  33. is_ban = 0,
  34. classification_id = 1,
  35. describe = "",
  36. }
  37. },
  38. ["3"] = { --片尾
  39. [2]={
  40. name = "横屏 16:9",
  41. is_ban = 0,
  42. classification_id = 2,
  43. describe = "",
  44. },
  45. [1]={
  46. name = "竖屏 9:16",
  47. is_ban = 0,
  48. classification_id = 1,
  49. describe = "",
  50. }
  51. },
  52. ["4"] = { --bgm音频
  53. [1]={
  54. name = "全部",
  55. is_ban = 0,
  56. classification_id = 1,
  57. describe = "",
  58. }
  59. },
  60. ["5"] = { --片尾音频
  61. [1]={
  62. name = "全部",
  63. is_ban = 0,
  64. classification_id = 1,
  65. describe = "",
  66. }
  67. },
  68. ["6"] = { --图片
  69. [1]={
  70. name = "全部",
  71. is_ban = 0,
  72. classification_id = 1,
  73. describe = "",
  74. }
  75. },
  76. ["7"] = { --角标
  77. [1]={
  78. name = "全部",
  79. is_ban = 0,
  80. classification_id = 1,
  81. describe = "",
  82. }
  83. },
  84. ["8"] = { --底纹
  85. [1]={
  86. name = "全部",
  87. is_ban = 0,
  88. classification_id = 1,
  89. describe = "",
  90. }
  91. },
  92. },
  93. permissions_info = {
  94. },
  95. other_info = {
  96. },
  97. group_info = {
  98. }
  99. }
  100. s.resp.update_permissions_info = function(msg_body,fd)
  101. local permissions_info = {}
  102. skynet.error("update_permissions_info")
  103. function action(isOk,list)
  104. local error_json = '{"code": 10000, "msg": "更新权限失败成功"}'
  105. if isOk then
  106. if #list>0 then
  107. for i = 1, #list, 1 do
  108. local permissions_id = list[i].id
  109. permissions_info[permissions_id] = list[i]
  110. end
  111. config.permissions_info = permissions_info;
  112. end
  113. end
  114. if fd~=nil then
  115. s.resp.updateConfigDb()
  116. return tools.response(fd,200,error_json)
  117. end
  118. end
  119. local sql = string.format("select * from permissions_list_tab")
  120. local res = db:query(sql)
  121. if res and #res > 0 then
  122. local tab = {}
  123. if #res == 1 then
  124. tab[1] = tools.getDbResData(res)
  125. else
  126. tab = tools.getDbResData(res)
  127. end
  128. action(true,tab)
  129. skynet.error("Found data:")
  130. else
  131. action(false,nil)
  132. skynet.error("No data found.")
  133. end
  134. end
  135. s.resp.update_group_info = function(msg_body,fd)
  136. local group_info = {}
  137. function action(isOk,list)
  138. local error_json = '{"code": 10000, "msg": "更新权限失败成功"}'
  139. if isOk then
  140. if #list>0 then
  141. for i = 1, #list, 1 do
  142. local group_id = list[i].id
  143. group_info[group_id] = list[i]
  144. end
  145. config.group_info = group_info;
  146. end
  147. end
  148. if fd~=nil then
  149. s.resp.updateConfigDb()
  150. return tools.response(fd,200,error_json)
  151. end
  152. end
  153. local sql = string.format("select * from group_list_tab")
  154. local res = db:query(sql)
  155. if res and #res > 0 then
  156. local tab = {}
  157. if #res == 1 then
  158. tab[1] = tools.getDbResData(res)
  159. else
  160. tab = tools.getDbResData(res)
  161. end
  162. action(true,tab)
  163. skynet.error("Found data:")
  164. else
  165. action(false,nil)
  166. skynet.error("No data found.")
  167. end
  168. end
  169. s.resp.update_other_info = function()
  170. end
  171. s.resp.update_config_info = function(msg_body,fd)
  172. local config_info = {}
  173. skynet.error("update_config_info")
  174. function action(isOk,info)
  175. local error_json = '{"code": 10000, "msg": "更新权限失败"}'
  176. if isOk then
  177. config.config_info = info.info
  178. else
  179. config_info = cjson.encode(config.config_info)
  180. config.config_info = config_info
  181. local sql = string.format("INSERT INTO classification_list_tab (info,self_id) VALUES ('%s',%d)",config_info,1)
  182. local res = db:query(sql)
  183. skynet.error(sql)
  184. end
  185. if fd~=nil then
  186. s.resp.updateConfigDb()
  187. return tools.response(fd,200,error_json)
  188. end
  189. end
  190. local sql = string.format("select info from classification_list_tab where self_id = 1")
  191. local res = db:query(sql)
  192. if res and #res > 0 then
  193. local tab = {}
  194. tab = tools.getDbResData(res)
  195. action(true,tab)
  196. skynet.error("Found data:")
  197. else
  198. action(false,nil)
  199. skynet.error("No data found.")
  200. end
  201. end
  202. s.resp.get_config_info = function(msg_body,fd)
  203. -- skynet.error("config.config_info",config.config_info)
  204. return tools.response(fd,200,cjson.encode({code=10000,msg = "成功!",data=cjson.decode(config.config_info)}))
  205. end
  206. s.resp.get_permissions_info = function(msg_body,fd)
  207. -- local isOk,key = tools.checkData({"permissions_id"},msg_body)
  208. -- if not isOk then
  209. -- return tools.response(fd,200,cjson.encode({code=9001,msg = string.format("缺少字段: '%s'.", key)}))
  210. -- end
  211. local permissions_info = {}
  212. if config.permissions_info~=nil and #config.permissions_info>0 then
  213. permissions_info = config.permissions_info
  214. end
  215. -- tools.dump(permissions_info)
  216. return tools.response(fd,200,cjson.encode({code=10000,msg = "成功!",data=permissions_info}))
  217. end
  218. s.resp.get_group_info = function(msg_body,fd)
  219. return tools.response(fd,200,cjson.encode({code=10000,msg = "成功!",data=config.group_info}))
  220. end
  221. s.resp.get_other_info = function(fd)
  222. return tools.response(fd,200,cjson.encode({code=10000,msg = "成功!",data=config.other_info}))
  223. end
  224. s.resp.on_recv = function (source, fd, msg_id, msg_body,user_data)
  225. skynet.error("接收一条客户端配置的接口 ",msg_id)
  226. local func = string.gsub(msg_id, '/config/', '')
  227. skynet.error("func",func)
  228. if s.resp[func] ~=nil then
  229. return s.resp[func](msg_body,fd)
  230. end
  231. return tools.response(fd,200,string.format("接口 %s 不存在",func))
  232. end
  233. s.init = function()
  234. db=mysql.connect({
  235. host=runconfig.db_tost,
  236. port=runconfig.db_port,
  237. database=runconfig.db_name,
  238. user="root",
  239. password=runconfig.db_pw,
  240. max_packet_size = 1024 * 1024,
  241. on_connect = nil
  242. })
  243. if not db then
  244. skynet.error("failed to connect")
  245. skynet.exit()
  246. else
  247. skynet.error(" config success to connect to mysql server")
  248. s.resp.initConfig()
  249. end
  250. end
  251. s.resp.updateConfigDb = function()
  252. -- skynet.fork(function()
  253. -- local config_info ="{}"
  254. -- if config.config_info~=nil and #config.config_info>0 then
  255. -- config_info = cjson.encode(config.config_info)
  256. -- end
  257. -- local permissions_info ="{}"
  258. -- if config.permissions_info~=nil and #config.permissions_info>0 then
  259. -- permissions_info = cjson.encode(config.permissions_info)
  260. -- end
  261. -- local other_info ="{}"
  262. -- if config.other_info~=nil and #config.other_info>0 then
  263. -- other_info = cjson.encode(config.other_info)
  264. -- end
  265. -- local group_info ="{}"
  266. -- if config.group_info~=nil and #config.group_info>0 then
  267. -- group_info = cjson.encode(config.group_info)
  268. -- end
  269. -- 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)
  270. -- db:query(sql)
  271. -- end)
  272. end
  273. s.resp.initConfig = function()
  274. -- local sql = string.format("select * from video_tools_config_tab where id = %d ",1)
  275. -- local res = db:query(sql)
  276. -- skynet.error('folder_res_list',sql)
  277. -- 判断是否找到数据
  278. -- if res and #res > 0 then
  279. -- local tab = {}
  280. -- tab = tools.getDbResData(res)
  281. -- config.config_info = cjson.decode( tab.config_info)
  282. -- config.group_info = cjson.decode( tab.group_info)
  283. -- config.permissions_info = cjson.decode( tab.permissions_info)
  284. -- config.other_info = cjson.decode( tab.other_info)
  285. -- else
  286. s.resp.update_config_info()
  287. s.resp.update_permissions_info()
  288. s.resp.update_group_info()
  289. s.resp.update_other_info()
  290. s.resp.updateConfigDb()
  291. -- end
  292. skynet.error("静态配置完成!")
  293. end
  294. s.start(...)