init.lua 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. --通知 start
  9. local response_db_new_folder = function(fd,is_ok)
  10. if not is_ok then
  11. return tools.response(fd,200,cjson.encode({code=10001,msg = "新建文件夹失败!"}))
  12. end
  13. return tools.response(fd,200,cjson.encode({code=10000,msg = "新建文件夹成功!"}))
  14. end
  15. --回应db获取资源文件夹列表
  16. local response_db_folder_list = function(fd,msg_body,isok,tab)
  17. if not isok then
  18. return tools.response(fd,200, '{"code": 10000, "msg": "获取资源文件夹列表失败!", "data": [] }')
  19. end
  20. local total_count = 0
  21. local list = tab
  22. list,total_count = tools.getPageData(msg_body.page,msg_body.count,list)
  23. if #list <= 0 or list == nil then
  24. return tools.response(fd,200,'{"code": 10000, "msg": "获取资源文件夹列表失败!", "data": [] }')
  25. -- return M.response(fd,200,cjson.encode({code=10000,msg = "获取资源文件夹列表成功!",data=folder_list,total_count=total_count}))
  26. end
  27. s.resp.folder_res_list_nums(list,fd,total_count)
  28. end
  29. --回应db获取指定文件夹内的资源列表
  30. local response_db_folder_res_list = function(fd,msg_body,isok,tab)
  31. if not isok then
  32. return tools.response(fd,200,'{"code": 10000, "msg": "获取文件夹资源失败", "data": [] }')
  33. end
  34. local list = tab
  35. local folder_list = {}
  36. local total_count = 0
  37. -- M.dump(list)
  38. -- skynet.error("list",list)
  39. -- skynet.error("msg_body",msg_body)
  40. -- M.dump(msg_body)
  41. list,total_count = tools.getPageData(msg_body.page,msg_body.count,list)
  42. for i = 1, #list, 1 do
  43. -- skynet.error("create_time")
  44. -- M.dump(list[i])
  45. table.insert(folder_list,i,{create_time=list[i].create_time,file_id=list[i].id,info=list[i].file_info,file_name=list[i].file_name})
  46. end
  47. if #list<=0 then
  48. return tools.response(fd,200,'{"code": 10000, "msg": "获取文件夹资源失败", "data": [] }')
  49. end
  50. return tools.response(fd,200,cjson.encode({code=10000,msg = "获取文件夹资源成功!",data=folder_list,total_count=total_count}))
  51. end
  52. local response_db_search_res = function(fd,msg_body,isok,tab)
  53. local error_json = '{"code": 10000, "msg": "搜索失败", "data": [] }'
  54. if not isok then
  55. return tools.response(fd,200,error_json)
  56. end
  57. local list = tab
  58. local file_list = {}
  59. local total_count = 0
  60. -- M.dump(list)
  61. -- skynet.error("list",list)
  62. -- skynet.error("msg_body",msg_body)
  63. -- tools.dump(msg_body)
  64. list ,total_count = tools.getPageData(msg_body.page,msg_body.count,list)
  65. for i = 1, #list, 1 do
  66. table.insert(file_list,i,{create_time=list[i].create_time,file_id=list[i].id,info=list[i].file_info,file_name=list[i].file_name})
  67. end
  68. if #list<=0 then
  69. return M.response(fd,200,error_json)
  70. end
  71. return tools.response(fd,200,cjson.encode({code=10000,msg = "搜索成功",data=file_list,total_count=total_count}))
  72. end
  73. local response_db_folder_list_nums = function(fd,list,count_list,total_count)
  74. local folder_list = {}
  75. for i = 1, #list, 1 do
  76. folder_list[i] = {file_nums=count_list[list[i].id],folder_name=list[i].folder_name,folder_id=list[i].id}
  77. end
  78. return tools.response(fd,200,cjson.encode({code=10000,msg = "获取资源文件夹列表成功!",data=folder_list,total_count=total_count}))
  79. end
  80. local response_db_tools_tools_create_folder = function(fd,isok,tab)
  81. if not isok then
  82. return tools.response(fd,200,cjson.encode({code=2,msg = "失败"}))
  83. end
  84. return tools.response(fd,200,cjson.encode({code=1,msg = "创建公共文件夹成功!",folder_id=tab[#tab].id}))
  85. end
  86. --通知 end
  87. local checkMsg = function(msg_body,user_data,orgin_body)
  88. local key = nil
  89. local user_id = nil
  90. if type(user_data) ~= "table" then
  91. return false,100,key,user_id
  92. end
  93. user_id = user_data.user_id
  94. if user_id==nil then
  95. return false,101,key,user_id
  96. end
  97. msg_body["user_id"] = user_id
  98. local isok ,key = tools.checkData(orgin_body,msg_body)
  99. if not isok then
  100. return false,102,key,user_id
  101. end
  102. return true,200,key,user_id
  103. end
  104. local response_error = function( isOk,err_code,key,fd,func_name)
  105. if err_code == 100 then
  106. tools.response(fd, 200, "token error!")
  107. elseif err_code == 101 then
  108. tools.response(fd,200,"user_id==nil "..func_name)
  109. elseif err_code == 102 then
  110. tools.response(fd,200,cjson.encode({code=9001,msg = string.format("缺少字段: %s.", key)}))
  111. end
  112. end
  113. --@@@@@@@@@@@@@@@@@@@@@@@@@@数据库 start
  114. --- 新建文件夹
  115. s.resp.db_public_new_folder = function(msg_body,fd)
  116. skynet.fork(function(_fd)
  117. local sql = string.format("INSERT INTO public_folder_list_tab (user_id,folder_name, folder_type,upload_user_id,classification_id) VALUES (%d, '%s',%d,%d,%d)",-1,msg_body.folder_name,msg_body.folder_type,msg_body.user_id,msg_body.classification_id)
  118. local res = db:query(sql)
  119. response_db_new_folder(_fd,true)
  120. end,fd)
  121. return true
  122. end
  123. --获取文件夹列表每个文件夹的文件数量
  124. s.resp.folder_res_list_nums = function(folder_list,fd,total_count)
  125. skynet.fork(function(_fd)
  126. local temp ={}
  127. for i = 1, #folder_list, 1 do
  128. local msg_body = folder_list[i]
  129. -- tools.dump(msg_body)
  130. local sql = string.format("select * from public_res_list_tab where user_id = %d AND folder_id = %d AND is_delete = 0",-1,msg_body.id)
  131. local res = db:query(sql)
  132. local count = 0
  133. if res and #res > 0 then
  134. count = #res
  135. skynet.error("Found data:")
  136. -- tools.dump(tab)
  137. -- return true,tab
  138. else
  139. skynet.error("No data found.",msg_body.id)
  140. end
  141. temp[msg_body.id] = count
  142. -- table.insert(temp,msg_body.id,count)
  143. end
  144. response_db_folder_list_nums(_fd,folder_list,temp,total_count)
  145. end,fd)
  146. end
  147. --- 获取资源文件夹列表
  148. s.resp.db_public_folder_list = function(msg_body,fd)
  149. skynet.fork(function(_fd)
  150. local sql = string.format("select * from public_folder_list_tab where user_id = %d AND folder_type = %d AND classification_id = %d",-1,msg_body.type,msg_body.classification_id)
  151. local res = db:query(sql)
  152. skynet.error('db_public_folder_list',sql)
  153. -- 判断是否找到数据
  154. if res and #res > 0 then
  155. local tab = {}
  156. if #res == 1 then
  157. tab[1] = tools.getDbResData(res)
  158. else
  159. tab = tools.getDbResData(res)
  160. end
  161. skynet.error("Found data:")
  162. -- tools.dump(tab)
  163. -- return true,tab
  164. response_db_folder_list(_fd,msg_body,true,tab)
  165. else
  166. skynet.error("No data found.")
  167. response_db_folder_list(_fd,msg_body,false,nil)
  168. -- return false,nil
  169. end
  170. end,fd)
  171. return true
  172. end
  173. --- 获取文件夹内所有资源
  174. s.resp.db_public_folder_res_list = function(msg_body,fd,call_back)
  175. skynet.fork(function(_fd)
  176. local sql_duration = ""
  177. -- if msg_body.duration~=nil then
  178. -- sql_duration = string.format(" AND duration >= %d",msg_body.duration)
  179. -- end
  180. local sql = string.format("select * from public_res_list_tab where user_id = %d AND folder_id = %d AND is_delete = 0"..sql_duration,-1,msg_body.folder_id)
  181. local res = db:query(sql)
  182. -- skynet.error('db_public_folder_res_list',sql)
  183. -- 判断是否找到数据
  184. if res and #res > 0 then
  185. local tab = {}
  186. if #res == 1 then
  187. tab[1] = tools.getDbResData(res)
  188. else
  189. tab = tools.getDbResData(res)
  190. end
  191. skynet.error("Found data:")
  192. if not call_back and _fd~=nil then
  193. response_db_folder_res_list(_fd,msg_body,true,tab)
  194. else
  195. if call_back ~=nil then
  196. call_back(tab)
  197. end
  198. end
  199. -- tools.dump(tab)
  200. -- return true,tab
  201. else
  202. skynet.error("No data found.",call_back)
  203. if call_back == nil and _fd~=nil then
  204. response_db_folder_res_list(_fd,msg_body,false,nil)
  205. else
  206. if call_back ~=nil then
  207. call_back({})
  208. end
  209. end
  210. -- return false,nil
  211. end
  212. end,fd)
  213. return true
  214. end
  215. --- 重命名资源
  216. s.resp.db_public_reset_res_name = function(msg_body,fd)
  217. local isOk = s.resp.get_res_file_by_id(msg_body.file_id)
  218. if not isOk then
  219. return false
  220. end
  221. local sql = string.format("UPDATE public_res_list_tab SET file_name ='%s' WHERE id = %d AND folder_id = %d AND is_delete = 0",msg_body.name,msg_body.file_id,msg_body.folder_id)
  222. db:query(sql)
  223. return true
  224. end
  225. --- 重命名文件夹
  226. s.resp.db_public_reset_folder_name = function(msg_body,fd)
  227. local isOk = s.resp.get_res_folder_by_id(msg_body.folder_id)
  228. if not isOk then
  229. return false
  230. end
  231. local sql = string.format("UPDATE public_folder_list_tab SET folder_name ='%s' WHERE id = %d ",msg_body.name,msg_body.folder_id)
  232. db:query(sql)
  233. return true
  234. end
  235. --根据所有文件id查到所有文件的url并删除
  236. local fork_get_file_list_url_and_delete = function(deleteString,call_back)
  237. local sql = string.format("SELECT file_info FROM public_res_list_tab WHERE id IN (%s)", deleteString)
  238. local res = db:query(sql)
  239. if res and #res > 0 then
  240. local tab = {}
  241. if #res == 1 then
  242. tab[1] = tools.getDbResData(res)
  243. else
  244. tab = tools.getDbResData(res)
  245. end
  246. skynet.error("Found data:fork_get_file_list_url_and_delete")
  247. local url_list = {}
  248. for i = 1, #tab, 1 do
  249. local file_info = cjson.decode(tab[i].file_info)
  250. table.insert(url_list,i,file_info.path)
  251. end
  252. skynet.send("tools_work","lua","delete_file",url_list)
  253. tools.dump(url_list)
  254. -- return true,tab
  255. else
  256. skynet.error("No data found.")
  257. -- return false,nil
  258. end
  259. if call_back~=nil then
  260. call_back()
  261. end
  262. end
  263. --执行删除文件操作
  264. local fork_delete_file = function(msg_body)
  265. -- tools.dump(msg_body)
  266. local listToDelete = {}
  267. for i = 1, #msg_body.file_id_list, 1 do
  268. table.insert(listToDelete,i,msg_body.file_id_list[i])
  269. end
  270. local deleteString = table.concat(listToDelete, ',')
  271. local sql = string.format("UPDATE public_res_list_tab SET is_delete = %d WHERE id IN (%s)",1,deleteString)
  272. db:query(sql)
  273. skynet.fork(fork_get_file_list_url_and_delete,deleteString,function()
  274. local delete_sql = string.format("DELETE FROM public_res_list_tab WHERE id IN (%s) AND user_id = %d", deleteString,-1)
  275. db:query(delete_sql)
  276. end)
  277. end
  278. --执行删除文件夹操作
  279. local fork_delete_folder = function(msg)
  280. for i = 1, #msg.folder_id_list, 1 do
  281. local isok = s.resp.folder_res_list({
  282. folder_id = msg.folder_id_list[i],
  283. user_id = -1},nil,nil,function(list)
  284. skynet.error("执行删除文件夹操作",#list)
  285. if #list>0 then
  286. local temp = {}
  287. for i = 1, #list, 1 do
  288. table.insert(temp,i,list[i].id)
  289. end
  290. skynet.fork(fork_delete_file,{file_id_list=temp,user_id=msg.user_id})
  291. end
  292. end)
  293. end
  294. end
  295. --- 文件夹删除
  296. s.resp.db_public_delete_folder = function(msg_body,fd)
  297. local listToDelete = {}
  298. for i = 1, #msg_body.folder_id_list, 1 do
  299. table.insert(listToDelete,i,msg_body.folder_id_list[i])
  300. end
  301. local deleteString = table.concat(listToDelete, ',')
  302. local sql = string.format("DELETE FROM public_folder_list_tab WHERE id IN (%s) AND user_id = %d", deleteString,-1)
  303. db:query(sql)
  304. skynet.fork(fork_delete_folder,msg_body)
  305. return true
  306. end
  307. --- 搜索文件
  308. s.resp.db_public_search_res = function(msg_body,fd)
  309. skynet.fork(function(_fd)
  310. local sql = string.format("SELECT * FROM public_res_list_tab WHERE file_type = %d AND file_name LIKE '%%%s%%' AND is_delete = 0 AND classification_id = %d",msg_body.type ,msg_body.search_content,msg_body.classification_id)
  311. skynet.error("sql",sql)
  312. local res = db:query(sql)
  313. if res and #res > 0 then
  314. local tab = {}
  315. if #res == 1 then
  316. tab[1] = tools.getDbResData(res)
  317. else
  318. tab = tools.getDbResData(res)
  319. end
  320. skynet.error("Found data:")
  321. -- tools.dump(tab)
  322. -- return true,tab
  323. response_db_search_res(_fd,msg_body,true,tab)
  324. else
  325. skynet.error("No data found.")
  326. -- return false,nil
  327. response_db_search_res(_fd,msg_body,false,nil)
  328. end
  329. end,fd)
  330. return true
  331. end
  332. --- 删除文件
  333. s.resp.db_public_delete_res = function(msg_body,fd)
  334. skynet.fork(fork_delete_file,msg_body)
  335. return true
  336. end
  337. --根据id获取文件夹
  338. s.resp.get_res_folder_by_id = function(folder_id)
  339. local sql = string.format("select * from public_folder_list_tab where id = %d ",folder_id)
  340. skynet.error("sql",sql)
  341. local res = db:query(sql)
  342. -- 判断是否找到数据
  343. if res and #res > 0 then
  344. local tab = tools.getDbResData(res)
  345. skynet.error("Found data:",tab)
  346. return true,tab
  347. else
  348. skynet.error("No data found.")
  349. return false,nil
  350. end
  351. end
  352. --根据id获取资源
  353. s.resp.get_res_file_by_id = function(file_id)
  354. local sql = string.format("select * from public_res_list_tab where id = %d AND is_delete = 0",file_id)
  355. skynet.error("sql",sql)
  356. local res = db:query(sql)
  357. -- 判断是否找到数据
  358. if res and #res > 0 then
  359. local tab = tools.getDbResData(res)
  360. skynet.error("Found data:",tab)
  361. return true,tab
  362. else
  363. skynet.error("No data found.")
  364. return false,nil
  365. end
  366. end
  367. s.resp.tools_create_folder = function(source,fd,msg_body)
  368. skynet.fork(function(_fd)
  369. local sql = string.format("INSERT INTO public_folder_list_tab (user_id,folder_name, folder_type,upload_user_id,classification_id) VALUES (%d, '%s',%d,%d,%d)",-1,msg_body.folder_name,msg_body.stype,msg_body.user_id,msg_body.classification_id)
  370. local res = db:query(sql)
  371. skynet.sleep(10)
  372. sql = string.format("select *from public_folder_list_tab where user_id = %d AND folder_name = '%s' AND folder_type = %d AND upload_user_id = %d AND classification_id = %d ",-1,msg_body.folder_name,msg_body.stype,msg_body.user_id,msg_body.classification_id)
  373. res = db:query(sql)
  374. if res and #res > 0 then
  375. local tab = {}
  376. if #res == 1 then
  377. tab[1] = tools.getDbResData(res)
  378. else
  379. tab = tools.getDbResData(res)
  380. end
  381. response_db_tools_tools_create_folder(_fd,true,tab)
  382. else
  383. response_db_tools_tools_create_folder(_fd,false,nil)
  384. skynet.error("No data found.")
  385. end
  386. end,fd)
  387. return true
  388. end
  389. s.resp.tools_add_public_res_info = function(source,fd,msg_body)
  390. skynet.fork(function()
  391. local temp = '{"width": 100, "height": 100, "duration": 123}'
  392. temp = cjson.encode(msg_body)
  393. local duration = 0
  394. if msg_body.duration~=nil then
  395. duration = tonumber(msg_body.duration)
  396. end
  397. local sql = string.format("INSERT INTO public_res_list_tab (user_id, folder_id, file_type, file_name,file_info,duration,classification_id,upload_user_id) VALUES (%d, %d, %d, '%s','%s',%f,%d,%d)",-1,msg_body.folder_id,msg_body.stype,msg_body.file_name,temp,duration,msg_body.classification_id,msg_body.user_id)
  398. skynet.error("add_res_file",sql)
  399. local res = db:query(sql)
  400. end)
  401. return true
  402. end
  403. s.resp.db_add_public_res_info = function(msg_body,fd)
  404. skynet.fork(function()
  405. local temp = '{"width": 100, "height": 100, "duration": 123}'
  406. temp = cjson.encode(msg_body)
  407. local duration = 0
  408. if msg_body.duration~=nil then
  409. duration = tonumber(msg_body.duration)
  410. end
  411. local sql = string.format("INSERT INTO public_res_list_tab (user_id, folder_id, file_type, file_name,file_info,duration,classification_id,upload_user_id) VALUES (%d, %d, %d, '%s','%s',%f,%d)",-1,msg_body.folder_id,msg_body.stype,msg_body.file_name,temp,duration,msg_body.classification_id,msg_body.user_id)
  412. -- skynet.error("add_res_file",sql)
  413. local res = db:query(sql)
  414. end)
  415. return true
  416. end
  417. local get_res_list_for_folder = function(user_id,folder_id,call_back)
  418. local sql = string.format("select * from public_res_list_tab where user_id = %d AND folder_id = %d AND is_delete = 0",-1,folder_id)
  419. local res = db:query(sql)
  420. skynet.error('folder_res_list',sql)
  421. -- 判断是否找到数据
  422. if res and #res > 0 then
  423. local tab = {}
  424. if #res == 1 then
  425. tab[1] = tools.getDbResData(res)
  426. else
  427. tab = tools.getDbResData(res)
  428. end
  429. skynet.error("Found data:")
  430. if call_back ~=nil then
  431. call_back(tab)
  432. end
  433. else
  434. if call_back ~=nil then
  435. call_back(nil)
  436. end
  437. end
  438. end
  439. s.resp.db_public_add_folder_list_to_private = function(msg_body,fd,call_back)
  440. local count = 0
  441. local check_all_finish = true
  442. for i = 1, #msg_body.folder_id_list, 1 do
  443. local public_folder_id = msg_body.folder_id_list[i]
  444. local isOk = s.resp.db_public_add_folder_to_private({
  445. public_folder_id = public_folder_id,
  446. user_id = msg_body.user_id},fd,function(isFinish)
  447. count = count+1;
  448. if isFinish == false then
  449. check_all_finish = false
  450. end
  451. if count>=#msg_body.folder_id_list then
  452. call_back(check_all_finish)
  453. end
  454. end)
  455. end
  456. end
  457. s.resp.db_public_add_folder_to_private = function(msg_body,fd,call_back)
  458. skynet.fork(function()
  459. --获取公共文件夹
  460. local isOk,public_folder_info = s.resp.get_res_folder_by_id(msg_body.public_folder_id)
  461. if not isOk then
  462. call_back(false)
  463. end
  464. --检测用户是否有此公共文件夹
  465. local self_folder_info = nil
  466. isOk,self_folder_info = skynet.call("dbmgr","lua","on_recv","check_have_folder",{
  467. folder_id = public_folder_info.id,
  468. user_id = msg_body.user_id}
  469. )
  470. if not isOk then
  471. --如果没有创建用户私人文件夹
  472. local sql = string.format("INSERT INTO folder_list_tab (user_id,folder_name, folder_type,classification_id,ref_id) VALUES (%d, '%s',%d,%d,%d)",msg_body.user_id,public_folder_info.folder_name,public_folder_info.folder_type,public_folder_info.classification_id,public_folder_info.id)
  473. local res = db:query(sql)
  474. --获取刚刚创建的私人文件夹
  475. sql = string.format("select *from folder_list_tab where user_id = %d AND folder_name = '%s' AND folder_type = %d AND classification_id = %d limit 1",msg_body.user_id,public_folder_info.folder_name,public_folder_info.folder_type,public_folder_info.classification_id)
  476. res = db:query(sql)
  477. skynet.error(sql)
  478. if res and #res > 0 then
  479. local tab = {}
  480. tab = tools.getDbResData(res)
  481. local user_id = tab.user_id
  482. local private_folder_id = tab.id
  483. local public_folder_id = msg_body.public_folder_id
  484. skynet.error("刚刚创建的文件夹",private_folder_id)
  485. --获取公共文件夹下的所有文件
  486. skynet.fork(get_res_list_for_folder,user_id,public_folder_id,function(list)
  487. --将所有文件也拷贝到私人
  488. skynet.error("获取公共文件夹下的所有文件")
  489. -- tools.dump(list)
  490. if list ~=nil then
  491. for i = 1, #list, 1 do
  492. local temp = {}
  493. temp = list[i].file_info
  494. sql = string.format("INSERT INTO res_list_tab (user_id, folder_id, file_type, file_name, file_info, duration, classification_id, ref_id) VALUES (%d, %d, %d, '%s','%s',%f,%d,%d)",user_id,private_folder_id,list[i].file_type,list[i].file_name,temp,list[i].duration,list[i].classification_id,list[i].id)
  495. db:query(sql)
  496. end
  497. end
  498. call_back(true)
  499. end)
  500. else
  501. call_back(false)
  502. skynet.error("No data found.")
  503. end
  504. else --如果用户已存此文件夹在
  505. skynet.fork(get_res_list_for_folder,msg_body.user_id, public_folder_info.id,function(list)
  506. --将所有文件也拷贝到私人
  507. -- skynet.error("将所有文件也拷贝到私人")
  508. -- tools.dump(list)
  509. if list ~=nil then
  510. for i = 1, #list, 1 do
  511. local file_id = list[i].id
  512. local self_file_info = nil
  513. isOk,self_file_info = skynet.call("dbmgr","lua","on_recv","check_have_file",{
  514. file_id = file_id,
  515. folder_id = public_folder_info.id,
  516. user_id = msg_body.user_id}
  517. )
  518. if not isOk then --如果用户没有此文件
  519. local temp = {}
  520. temp = list[i].file_info
  521. local sql = string.format("INSERT INTO res_list_tab (user_id, folder_id, file_type, file_name, file_info, duration, classification_id, ref_id) VALUES (%d, %d, %d, '%s','%s',%f,%d,%d)",self_folder_info.user_id,self_folder_info.id,list[i].file_type,list[i].file_name,temp,list[i].duration,list[i].classification_id,list[i].id)
  522. db:query(sql)
  523. end
  524. end
  525. end
  526. call_back(true)
  527. end)
  528. end
  529. end)
  530. return true
  531. end
  532. s.resp.db_public_add_file_list_to_private = function(msg_body,fd,call_back)
  533. local count = 0
  534. local check_all_finish = true
  535. for i = 1, #msg_body.file_id_list, 1 do
  536. local public_file_id = msg_body.file_id_list[i]
  537. local isOk = s.resp.db_public_add_file_to_private({
  538. file_id = public_file_id,
  539. user_id = msg_body.user_id},fd,function(isFinish)
  540. count = count+1;
  541. if isFinish == false then
  542. check_all_finish = false
  543. end
  544. skynet.error("count",count)
  545. skynet.error("file_id_list",#msg_body.file_id_list)
  546. if count>=#msg_body.file_id_list then
  547. call_back(check_all_finish)
  548. end
  549. end)
  550. end
  551. return true
  552. end
  553. s.resp.db_public_add_file_to_private = function(msg_body,fd,call_back)
  554. local isOk,public_file_info = s.resp.get_res_file_by_id(msg_body.file_id)
  555. if not isOk then
  556. return false
  557. end
  558. local self_file_info = nil
  559. local file_id = public_file_info.id
  560. isOk,self_file_info = skynet.call("dbmgr","lua","on_recv","check_have_file",{
  561. file_id = file_id,
  562. user_id = msg_body.user_id}
  563. )
  564. --检测是否有此文件夹
  565. --如果没有创建用户私人文件夹
  566. -- local sql = string.format("INSERT INTO folder_list_tab (user_id,folder_name, folder_type,classification_id,ref_id) VALUES (%d, '%s',%d,%d,%d)",msg_body.user_id,public_folder_info.folder_name,public_folder_info.folder_type,public_folder_info.classification_id,public_folder_info.id)
  567. -- local res = db:query(sql)
  568. -- --获取刚刚创建的私人文件夹
  569. -- sql = string.format("select *from folder_list_tab where user_id = %d AND folder_name = '%s' AND folder_type = %d AND classification_id = %d limit 1",msg_body.user_id,public_folder_info.folder_name,public_folder_info.folder_type,public_folder_info.classification_id)
  570. -- res = db:query(sql)
  571. if not isOk then
  572. --检测用户是否有此公共文件夹
  573. local self_folder_info = nil
  574. isOk,self_folder_info = skynet.call("dbmgr","lua","on_recv","check_have_folder",{
  575. folder_id = public_file_info.folder_id,
  576. user_id = msg_body.user_id}
  577. )
  578. if not isOk then
  579. local isOk,public_folder_info = s.resp.get_res_folder_by_id(public_file_info.folder_id)
  580. if not isOk then
  581. call_back(false)
  582. end
  583. --如果没有创建用户私人文件夹
  584. local sql = string.format("INSERT INTO folder_list_tab (user_id,folder_name, folder_type,classification_id,ref_id) VALUES (%d, '%s',%d,%d,%d)",msg_body.user_id,public_folder_info.folder_name,public_folder_info.folder_type,public_folder_info.classification_id,public_folder_info.id)
  585. local res = db:query(sql)
  586. --获取刚刚创建的私人文件夹
  587. sql = string.format("select *from folder_list_tab where user_id = %d AND folder_name = '%s' AND folder_type = %d AND classification_id = %d limit 1",msg_body.user_id,public_folder_info.folder_name,public_folder_info.folder_type,public_folder_info.classification_id)
  588. res = db:query(sql)
  589. if res and #res > 0 then
  590. local tab = {}
  591. tab = tools.getDbResData(res)
  592. local private_folder_id = tab.id
  593. skynet.error("添加一个文件到用户")
  594. local temp = {}
  595. temp = public_file_info.file_info
  596. local sql = string.format("INSERT INTO res_list_tab (user_id, folder_id, file_type, file_name,file_info,duration,classification_id,ref_id) VALUES (%d, %d, %d, '%s','%s',%f,%d,%d)",msg_body.user_id,private_folder_id,public_file_info.file_type,public_file_info.file_name,temp,public_file_info.duration,public_file_info.classification_id,public_file_info.id)
  597. skynet.error(sql)
  598. db:query(sql)
  599. end
  600. else
  601. local private_folder_id = self_folder_info.id
  602. local temp = {}
  603. temp = public_file_info.file_info
  604. local sql = string.format("INSERT INTO res_list_tab (user_id, folder_id, file_type, file_name,file_info,duration,classification_id,ref_id) VALUES (%d, %d, %d, '%s','%s',%f,%d,%d)",msg_body.user_id,private_folder_id,public_file_info.file_type,public_file_info.file_name,temp,public_file_info.duration,public_file_info.classification_id,public_file_info.id)
  605. skynet.error("用户已有此文件夹,直接添加")
  606. db:query(sql)
  607. end
  608. end
  609. call_back(true)
  610. return true
  611. end
  612. --@@@@@@@@@@@@@@@@@@@@@@@@@@数据库 end
  613. -----------------------------------------
  614. -----------------------------------------
  615. -----------------------------------------
  616. -----------------------------------------
  617. -----------------------------------------
  618. -----------------------------------------
  619. -----------------------------------------
  620. -----------------------------------------
  621. -----------------------------------------
  622. -----------------------------------------
  623. -----------------------------------------
  624. --==========================逻辑 start
  625. --- 搜索文件
  626. s.resp.search_res = function(msg_body,fd,user_data)
  627. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  628. {"search_content",
  629. "type",
  630. "page",
  631. "count",
  632. "classification_id"})
  633. if isOk then
  634. s.resp.db_public_search_res({
  635. type = msg_body.type,
  636. search_content = msg_body.search_content,
  637. page = msg_body.page,
  638. count = msg_body.count,
  639. classification_id = msg_body.classification_id,
  640. user_id = user_id},fd)
  641. else
  642. response_error(isOk,err_code,key,fd,"search_res")
  643. end
  644. end
  645. --- 文件夹删除
  646. s.resp.delete_folder = function(msg_body,fd,user_data)
  647. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  648. {"folder_id_list"})
  649. skynet.error("公共文件夹删除")
  650. if isOk then
  651. isOk = s.resp.db_public_delete_folder({
  652. folder_id_list = msg_body.folder_id_list,
  653. user_id = user_id},fd)
  654. if not isOk then
  655. return tools.response(fd,200,cjson.encode({code=10000,msg = "删除失败!",data={}}))
  656. end
  657. isOk = skynet.call("dbmgr","lua","on_recv","recv_ref_folder_list_del_info",{folder_id_list= msg_body.folder_id_list})
  658. return tools.response(fd,200,cjson.encode({code=10000,msg = "删除成功!",data={}}))
  659. else
  660. skynet.error("err_code:",err_code)
  661. response_error(isOk,err_code,key,fd,"delete_folder")
  662. end
  663. end
  664. --- 删除文件
  665. s.resp.delete_res = function(msg_body,fd,user_data)
  666. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  667. {"file_id_list"})
  668. if isOk then
  669. isOk = s.resp.db_public_delete_res({
  670. file_id_list = msg_body.file_id_list,
  671. user_id = user_id},fd)
  672. if not isOk then
  673. return tools.response(fd,200,cjson.encode({code=10000,msg = "删除失败!",data={}}))
  674. end
  675. isOk = skynet.call("dbmgr","lua","on_recv","recv_ref_file_list_del_info",{file_id_list= msg_body.file_id_list})
  676. return tools.response(fd,200,cjson.encode({code=10000,msg = "删除成功!",data={}}))
  677. else
  678. response_error(isOk,err_code,key,fd,"delete_res")
  679. end
  680. end
  681. --- 新建文件夹
  682. s.resp.new_folder = function(msg_body,fd,user_data)
  683. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  684. {"category_name","type","classification_id"})
  685. if isOk then
  686. isOk = s.resp.db_public_new_folder({
  687. user_id=user_id,
  688. folder_name=msg_body.category_name,
  689. folder_type=msg_body.type,
  690. classification_id=msg_body.classification_id},fd)
  691. return
  692. else
  693. response_error(isOk,err_code,key,fd,"new_folder")
  694. end
  695. end
  696. --- 获取资源文件夹列表
  697. s.resp.folder_list = function(msg_body,fd,user_data)
  698. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  699. {"type","page","count","classification_id"})
  700. if isOk then
  701. isOk = s.resp.db_public_folder_list({user_id = user_id,
  702. type=msg_body.type,
  703. page=msg_body.page,
  704. count = msg_body.count,
  705. classification_id = msg_body.classification_id },fd)
  706. return
  707. else
  708. response_error(isOk,err_code,key,fd,"folder_list")
  709. end
  710. end
  711. --- 获取文件夹内所有资源
  712. s.resp.folder_res_list = function(msg_body,fd,user_data,call_back)
  713. if call_back~=nil then
  714. local duration = nil
  715. if msg_body.duration ~=nil then
  716. duration = msg_body.duration
  717. end
  718. s.resp.db_public_folder_res_list({
  719. folder_id = msg_body.folder_id,
  720. user_id = msg_body.user_id,
  721. page=msg_body.page,
  722. count = msg_body.count,
  723. classification_id = msg_body.classification_id,
  724. duration = duration},fd,call_back)
  725. return
  726. end
  727. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  728. {"folder_id","page","count"})
  729. if isOk then
  730. local duration = nil
  731. if msg_body.duration ~=nil then
  732. duration = msg_body.duration
  733. end
  734. isOk = s.resp.db_public_folder_res_list({
  735. folder_id = msg_body.folder_id,
  736. user_id = user_id,
  737. page=msg_body.page,
  738. count = msg_body.count,
  739. classification_id = msg_body.classification_id,
  740. duration = duration},fd)
  741. return
  742. else
  743. response_error(isOk,err_code,key,fd,"folder_res_list")
  744. end
  745. end
  746. --- 重命名资源
  747. s.resp.reset_res_name = function(msg_body,fd,user_data)
  748. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  749. {"file_id","name","file_id"})
  750. if isOk then
  751. isOk = s.resp.db_public_reset_res_name({
  752. folder_id = msg_body.folder_id,
  753. file_id = msg_body.file_id,
  754. name = msg_body.name,
  755. user_id = user_id},fd)
  756. if not isOk then
  757. return tools.response(fd,200,cjson.encode({code=10001,msg = "--重命名资源失败"}))
  758. end
  759. isOk = skynet.call("dbmgr","lua","on_recv","recv_ref_file_modify_info",{file_id= msg_body.file_id,name=msg_body.name})
  760. return tools.response(fd,200,cjson.encode({code=10000,msg = "重命名资源成功"}))
  761. else
  762. response_error(isOk,err_code,key,fd,"reset_res_name")
  763. end
  764. end
  765. --- 重命名文件夹
  766. s.resp.reset_folder_name = function(msg_body,fd,user_data)
  767. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  768. {"folder_id","name"})
  769. if isOk then
  770. isOk = s.resp.db_public_reset_folder_name({
  771. folder_id = msg_body.folder_id,
  772. name = msg_body.name,
  773. user_id = user_id},fd)
  774. if not isOk then
  775. return tools.response(fd,200,cjson.encode({code=10001,msg = "--重命名文件夹失败"}))
  776. end
  777. isOk = skynet.call("dbmgr","lua","on_recv","recv_ref_folder_modify_info",{folder_id= msg_body.folder_id,name=msg_body.name})
  778. return tools.response(fd,200,cjson.encode({code=10000,msg = "重命名文件夹成功"}))
  779. else
  780. response_error(isOk,err_code,key,fd,"reset_folder_name")
  781. end
  782. end
  783. s.resp.add_public_res_info = function(msg_body,fd,user_data)
  784. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  785. {"classification_id","stype","folder_id", "height", "width","duration", "user_id", "ratio", "category_name","file_name","size","surl","path"})
  786. if isOk then
  787. isOk = s.resp.db_add_public_res_info(msg_body,fd)
  788. if not isOk then
  789. return tools.response(fd,200,cjson.encode({code=10001,msg = "--添加文件失败"}))
  790. end
  791. return tools.response(fd,200,cjson.encode({code=10000,msg = "添加文件成功"}))
  792. else
  793. response_error(isOk,err_code,key,fd,"add_public_res_info")
  794. end
  795. end
  796. --添加公共文件列表到个人
  797. s.resp.add_file_list_to_private = function(msg_body,fd,user_data)
  798. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  799. {"file_id_list"})
  800. if isOk then
  801. isOk = s.resp.db_public_add_file_list_to_private({
  802. file_id_list = msg_body.file_id_list,
  803. user_id = user_id},fd,function(isFinish)
  804. if isFinish == true then
  805. return tools.response(fd,200,cjson.encode({code=10000,msg = "--添加公共文件到个人成功!"}))
  806. else
  807. return tools.response(fd,200,cjson.encode({code=10001,msg = "--添加公共文件到个人失败!"}))
  808. end
  809. end)
  810. return
  811. else
  812. response_error(isOk,err_code,key,fd,"add_file_to_private")
  813. end
  814. end
  815. --添加公共文件到个人
  816. s.resp.add_file_to_private = function(msg_body,fd,user_data)
  817. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  818. {"file_id"})
  819. if isOk then
  820. isOk = s.resp.db_public_add_file_to_private({
  821. file_id = msg_body.file_id,
  822. user_id = user_id},fd)
  823. if isOk == true then
  824. return tools.response(fd,200,cjson.encode({code=10001,msg = "--添加公共文件到个人失败!"}))
  825. else
  826. return tools.response(fd,200,cjson.encode({code=10000,msg = "--添加公共文件到个人成功!"}))
  827. end
  828. else
  829. response_error(isOk,err_code,key,fd,"add_file_to_private")
  830. end
  831. end
  832. --添加公共文件夹列表到个人
  833. s.resp.add_folder_list_to_private = function(msg_body,fd,user_data)
  834. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  835. {"folder_id_list"})
  836. if isOk then
  837. isOk = s.resp.db_public_add_folder_list_to_private({
  838. folder_id_list = msg_body.folder_id_list,
  839. user_id = user_id},fd,function(isFinish)
  840. if isFinish then
  841. return tools.response(fd,200,cjson.encode({code=10000,msg = "--添加公共文件夹到个人成功!"}))
  842. else
  843. return tools.response(fd,200,cjson.encode({code=10001,msg = "--添加公共文件夹到个人失败!"}))
  844. end
  845. end)
  846. return
  847. else
  848. response_error(isOk,err_code,key,fd,"add_folder_to_private")
  849. end
  850. end
  851. --添加公共文件夹到个人
  852. s.resp.add_folder_to_private = function(msg_body,fd,user_data)
  853. local isOk,err_code,key,user_id = checkMsg(msg_body,user_data,
  854. {"folder_id"})
  855. if isOk then
  856. isOk = s.resp.db_public_add_folder_to_private({
  857. public_folder_id = msg_body.folder_id,
  858. user_id = user_id},fd,function(isFinish)
  859. if isFinish == true then
  860. return tools.response(fd,200,cjson.encode({code=10001,msg = "--添加公共文件夹到个人成功!"}))
  861. else
  862. return tools.response(fd,200,cjson.encode({code=10001,msg = "--添加公共文件夹到个人失败!"}))
  863. end
  864. end)
  865. return
  866. else
  867. response_error(isOk,err_code,key,fd,"add_folder_to_private")
  868. end
  869. end
  870. --==========================逻辑 end
  871. -----------------------------------------
  872. -----------------------------------------
  873. -----------------------------------------
  874. -----------------------------------------
  875. -----------------------------------------
  876. -----------------------------------------
  877. -----------------------------------------
  878. -----------------------------------------
  879. -----------------------------------------
  880. -----------------------------------------
  881. -----------------------------------------
  882. s.resp.on_recv = function (source, fd, msg_id, msg_body,user_data)
  883. skynet.error("接收一条客户端public消息 ",msg_id,user_data)
  884. tools.dump(msg_body)
  885. local func = string.gsub(msg_id, '/public/', '')
  886. local isKick = skynet.call("agentmgr","lua","account_is_other_user_login",user_data.user_id,user_data.index)
  887. if not isKick then
  888. else
  889. skynet.error("用户被挤掉",user_data.user_id)
  890. return tools.response(fd,200,cjson.encode({code=9999,msg = "您的账号被其他用户登录!"}))
  891. end
  892. if s.resp[func] ~=nil then
  893. msg_body = cjson.decode(msg_body)
  894. return s.resp[func](msg_body,fd,user_data)
  895. end
  896. return tools.response(fd,200,string.format("接口 %s 不存在",func))
  897. end
  898. s.init = function()
  899. db=mysql.connect({
  900. host=runconfig.db_tost,
  901. port=runconfig.db_port,
  902. database=runconfig.db_name,
  903. user="root",
  904. password=runconfig.db_pw,
  905. max_packet_size = 1024 * 1024,
  906. on_connect = nil
  907. })
  908. if not db then
  909. skynet.error("failed to connect")
  910. skynet.exit()
  911. else
  912. skynet.error(" config success to connect to mysql server")
  913. skynet.error(" 初始化公共服务成功!")
  914. end
  915. end
  916. s.start(...)