init.lua 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. },
  77. permissions_info = {
  78. },
  79. other_info = {
  80. },
  81. group_info = {
  82. }
  83. }
  84. s.resp.update_permissions_info = function(msg_body,fd)
  85. local permissions_info = {}
  86. skynet.error("update_permissions_info")
  87. function action(isOk,list)
  88. local error_json = '{"code": 10000, "msg": "更新权限失败成功"}'
  89. if isOk then
  90. if #list>0 then
  91. for i = 1, #list, 1 do
  92. local permissions_id = list[i].id
  93. permissions_info[permissions_id] = list[i]
  94. end
  95. config.permissions_info = permissions_info;
  96. end
  97. end
  98. if fd~=nil then
  99. s.resp.updateConfigDb()
  100. return tools.response(fd,200,error_json)
  101. end
  102. end
  103. local sql = string.format("select * from permissions_list_tab")
  104. local res = db:query(sql)
  105. if res and #res > 0 then
  106. local tab = {}
  107. if #res == 1 then
  108. tab[1] = tools.getDbResData(res)
  109. else
  110. tab = tools.getDbResData(res)
  111. end
  112. action(true,tab)
  113. skynet.error("Found data:")
  114. else
  115. action(false,nil)
  116. skynet.error("No data found.")
  117. end
  118. end
  119. s.resp.update_group_info = function(msg_body,fd)
  120. local group_info = {}
  121. function action(isOk,list)
  122. local error_json = '{"code": 10000, "msg": "更新权限失败成功"}'
  123. if isOk then
  124. if #list>0 then
  125. for i = 1, #list, 1 do
  126. local group_id = list[i].id
  127. group_info[group_id] = list[i]
  128. end
  129. config.group_info = group_info;
  130. end
  131. end
  132. if fd~=nil then
  133. s.resp.updateConfigDb()
  134. return tools.response(fd,200,error_json)
  135. end
  136. end
  137. local sql = string.format("select * from group_list_tab")
  138. local res = db:query(sql)
  139. if res and #res > 0 then
  140. local tab = {}
  141. if #res == 1 then
  142. tab[1] = tools.getDbResData(res)
  143. else
  144. tab = tools.getDbResData(res)
  145. end
  146. action(true,tab)
  147. skynet.error("Found data:")
  148. else
  149. action(false,nil)
  150. skynet.error("No data found.")
  151. end
  152. end
  153. s.resp.update_other_info = function()
  154. end
  155. s.resp.update_config_info = function(msg_body,fd)
  156. local config_info = {}
  157. skynet.error("update_config_info")
  158. function action(isOk,info)
  159. local error_json = '{"code": 10000, "msg": "更新权限失败成功"}'
  160. if isOk then
  161. config.config_info = info.info
  162. else
  163. config_info = cjson.encode(config.config_info)
  164. local sql = string.format("INSERT INTO classification_list_tab (info,self_id) VALUES ('%s',%d)",config_info,1)
  165. local res = db:query(sql)
  166. skynet.error(sql)
  167. end
  168. if fd~=nil then
  169. s.resp.updateConfigDb()
  170. return tools.response(fd,200,error_json)
  171. end
  172. end
  173. local sql = string.format("select info from classification_list_tab where self_id = 1")
  174. local res = db:query(sql)
  175. if res and #res > 0 then
  176. local tab = {}
  177. tab = tools.getDbResData(res)
  178. action(true,tab)
  179. skynet.error("Found data:")
  180. else
  181. action(false,nil)
  182. skynet.error("No data found.")
  183. end
  184. end
  185. s.resp.get_config_info = function(msg_body,fd)
  186. return tools.response(fd,200,cjson.encode({code=10000,msg = "成功!",data=config.config_info}))
  187. end
  188. s.resp.get_permissions_info = function(msg_body,fd)
  189. local permissions_info = {}
  190. if config.permissions_info~=nil and #config.permissions_info>0 then
  191. permissions_info = config.permissions_info
  192. end
  193. -- tools.dump(permissions_info)
  194. return tools.response(fd,200,cjson.encode({code=10000,msg = "成功!",data=permissions_info}))
  195. end
  196. s.resp.get_group_info = function(msg_body,fd)
  197. return tools.response(fd,200,cjson.encode({code=10000,msg = "成功!",data=config.group_info}))
  198. end
  199. s.resp.get_other_info = function(fd)
  200. return tools.response(fd,200,cjson.encode({code=10000,msg = "成功!",data=config.other_info}))
  201. end
  202. s.resp.on_recv = function (source, fd, msg_id, msg_body,user_data)
  203. skynet.error("接收一条客户端配置的接口 ",msg_id)
  204. local func = string.gsub(msg_id, '/config/', '')
  205. skynet.error("func",func)
  206. if s.resp[func] ~=nil then
  207. return s.resp[func](msg_body,fd)
  208. end
  209. return tools.response(fd,200,string.format("接口 %s 不存在",func))
  210. end
  211. s.init = function()
  212. db=mysql.connect({
  213. host=runconfig.db_tost,
  214. port=runconfig.db_port,
  215. database=runconfig.db_name,
  216. user="root",
  217. password=runconfig.db_pw,
  218. max_packet_size = 1024 * 1024,
  219. on_connect = nil
  220. })
  221. if not db then
  222. skynet.error("failed to connect")
  223. skynet.exit()
  224. else
  225. skynet.error(" config success to connect to mysql server")
  226. s.resp.initConfig()
  227. end
  228. end
  229. s.resp.updateConfigDb = function()
  230. -- skynet.fork(function()
  231. -- local config_info ="{}"
  232. -- if config.config_info~=nil and #config.config_info>0 then
  233. -- config_info = cjson.encode(config.config_info)
  234. -- end
  235. -- local permissions_info ="{}"
  236. -- if config.permissions_info~=nil and #config.permissions_info>0 then
  237. -- permissions_info = cjson.encode(config.permissions_info)
  238. -- end
  239. -- local other_info ="{}"
  240. -- if config.other_info~=nil and #config.other_info>0 then
  241. -- other_info = cjson.encode(config.other_info)
  242. -- end
  243. -- local group_info ="{}"
  244. -- if config.group_info~=nil and #config.group_info>0 then
  245. -- group_info = cjson.encode(config.group_info)
  246. -- end
  247. -- 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)
  248. -- db:query(sql)
  249. -- end)
  250. end
  251. s.resp.initConfig = function()
  252. -- local sql = string.format("select * from video_tools_config_tab where id = %d ",1)
  253. -- local res = db:query(sql)
  254. -- skynet.error('folder_res_list',sql)
  255. -- 判断是否找到数据
  256. -- if res and #res > 0 then
  257. -- local tab = {}
  258. -- tab = tools.getDbResData(res)
  259. -- config.config_info = cjson.decode( tab.config_info)
  260. -- config.group_info = cjson.decode( tab.group_info)
  261. -- config.permissions_info = cjson.decode( tab.permissions_info)
  262. -- config.other_info = cjson.decode( tab.other_info)
  263. -- else
  264. s.resp.update_config_info()
  265. s.resp.update_permissions_info()
  266. s.resp.update_group_info()
  267. s.resp.update_other_info()
  268. s.resp.updateConfigDb()
  269. -- end
  270. skynet.error("静态配置完成!")
  271. end
  272. s.start(...)