review_user.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. --审核员用户
  2. local M = {}
  3. local mysqldbx = require "mysqldbx"
  4. local tools = require "tools"
  5. local skynet = require "skynet"
  6. local mysql = require "skynet.db.mysql"
  7. local config = require "run_config"
  8. local mysqldtaskbx = {}
  9. local db
  10. function M.Register(msg_body)
  11. local isok ,key = tools.checkData({"name","account","password","Disable","permissions"},msg_body)
  12. if not isok then
  13. return false,string.format("缺少字段: %s.", key)
  14. end
  15. local sql = string.format("SELECT * FROM review_user WHERE account = '%s' and password = '%s' ", msg_body.account, msg_body.password)
  16. local isok,res;
  17. res = mysqldtaskbx.Singleton().query(sql)
  18. if #res > 0 then
  19. return false ,"用户已存在"
  20. end
  21. sql = string.format("insert into review_user ( name, account, password, Disable,permissions) values ('%s','%s','%s',%d,%d)",msg_body.name,msg_body.account,msg_body.password,msg_body.Disable,msg_body.permissions)
  22. res = mysqldtaskbx.Singleton().query(sql)
  23. tools.dump(res)
  24. return true, {}
  25. end
  26. function M.Modify(msg_body)
  27. local isok ,key = tools.checkData({"account","id","name","password","Disable","permissions"},msg_body)
  28. if not isok then
  29. return false,string.format("缺少字段: %s.", key)
  30. end
  31. local sql = string.format("SELECT * FROM review_user WHERE id = %d ", msg_body.id)
  32. local isok,res;
  33. res = mysqldtaskbx.Singleton().query(sql)
  34. if #res < 0 then
  35. return false ,"用户不存在"
  36. end
  37. local sql = string.format("UPDATE review_user set account = '%s', name = '%s' , password = '%s' , Disable = %d , permissions = %d WHERE id = %d ", msg_body.account, msg_body.name,msg_body.password,msg_body.Disable,msg_body.permissions,msg_body.id)
  38. local isok,res;
  39. res = mysqldtaskbx.Singleton().query(sql)
  40. return true, {}
  41. end
  42. function M.Login(msg_body)
  43. local isok ,key = tools.checkData({"account","password"},msg_body)
  44. if not isok then
  45. return false,string.format("缺少字段: %s.", key)
  46. end
  47. local sql = string.format("SELECT * FROM review_user WHERE account = '%s' and password = '%s' ", msg_body.account, msg_body.password)
  48. local isok,res;
  49. res = mysqldtaskbx.Singleton().query(sql)
  50. if #res <= 0 then
  51. return false ,"账号或密码错误!"
  52. end
  53. return true, res
  54. end
  55. function M.Search(msg_body)
  56. local isok ,key = tools.checkData({"page_size","page_number"},msg_body)
  57. if not isok then
  58. return false,string.format("缺少字段: %s.", key)
  59. end
  60. local page_size = msg_body.page_size
  61. local page_number = msg_body.page_number
  62. local offset = (page_number - 1) * page_size
  63. local sql = "SELECT * FROM review_user WHERE 1=1 "..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  64. local list = mysqldtaskbx.Singleton().query(sql)
  65. sql = "SELECT COUNT(*) AS total FROM review_user "
  66. local total = mysqldtaskbx.Singleton().query(sql)
  67. return true,list,total[1].total
  68. end
  69. function mysqldtaskbx.start()
  70. local function on_connect(db)
  71. db:query("set charset utf8mb4");
  72. end
  73. local conf = {
  74. ip = 'n8-mr-dsq824ev2pqxbr6rhw.rwlb.rds.aliyuncs.com',
  75. port = 3306,
  76. user = "zdroot",
  77. password = "p0LTZh&CjMl2023",
  78. db = "ml_oceanengine"
  79. }
  80. db = mysql.connect{
  81. host=conf.ip,
  82. port=conf.port,
  83. database=conf.db,
  84. user=conf.user,
  85. password=conf.password,
  86. charset="utf8mb4",
  87. max_packet_size = 1024 * 1024,
  88. on_connect = on_connect
  89. }
  90. if not db then
  91. skynet.error("mysql connect fail")
  92. end
  93. end
  94. function mysqldtaskbx.Singleton()
  95. if db == nil then
  96. mysqldtaskbx.start()
  97. end
  98. return mysqldtaskbx
  99. end
  100. function mysqldtaskbx.query(sql)
  101. return db:query(sql)
  102. end
  103. return M