GetReadList.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const API = require("../../../lib/API.js")
  2. const db = require("../../../plugin/DataBase/db.js")
  3. const AccessControl = require("../../../lib/AccessControl.js")
  4. const { BaseStdResponse } = require("../../../BaseStdResponse.js")
  5. class AdminPopupReadList extends API {
  6. constructor() {
  7. super()
  8. this.setPath('/Admin/Popup/ReadList')
  9. this.setMethod('GET')
  10. }
  11. async onRequest(req, res) {
  12. const { uuid, session, popup_id, keyword, pagesize, current } = req.query
  13. if ([uuid, session, popup_id, pagesize, current].some(v => v === '' || v === null || v === undefined))
  14. return res.json({ ...BaseStdResponse.MISSING_PARAMETER })
  15. if (isNaN(pagesize) || Number(pagesize) <= 0 || isNaN(current) || Number(current) <= 0)
  16. return res.json({ ...BaseStdResponse.ERR, msg: '参数错误' })
  17. if (!await AccessControl.checkSession(uuid, session))
  18. return res.status(401).json({ ...BaseStdResponse.ACCESS_DENIED })
  19. const permission = await AccessControl.getPermission(uuid)
  20. if (!permission.includes("admin") && !permission.includes("service") && !permission.includes("server"))
  21. return res.json({ ...BaseStdResponse.PERMISSION_DENIED })
  22. const offset = (Number(current) - 1) * Number(pagesize)
  23. const where = ['r.popup_id = ?']
  24. const params = [popup_id]
  25. const countParams = [popup_id]
  26. if (keyword) {
  27. where.push('(u.uuid COLLATE utf8mb4_general_ci LIKE CONVERT(? USING utf8mb4) COLLATE utf8mb4_general_ci OR u.username COLLATE utf8mb4_general_ci LIKE CONVERT(? USING utf8mb4) COLLATE utf8mb4_general_ci)')
  28. params.push(`%${keyword}%`, `%${keyword}%`)
  29. countParams.push(`%${keyword}%`, `%${keyword}%`)
  30. }
  31. const whereSql = where.join(' AND ')
  32. const listSql = `
  33. SELECT
  34. r.popup_id,
  35. r.user_uuid,
  36. r.read_at,
  37. u.username,
  38. u.avatar
  39. FROM site_popup_read r
  40. LEFT JOIN users u ON u.uuid COLLATE utf8mb4_general_ci = r.user_uuid COLLATE utf8mb4_general_ci
  41. WHERE ${whereSql}
  42. ORDER BY r.read_at DESC
  43. LIMIT ? OFFSET ?
  44. `
  45. const countSql = `SELECT COUNT(*) AS total FROM site_popup_read r LEFT JOIN users u ON u.uuid COLLATE utf8mb4_general_ci = r.user_uuid COLLATE utf8mb4_general_ci WHERE ${whereSql}`
  46. params.push(String(pagesize), String(offset))
  47. const rows = await db.query(listSql, params)
  48. const countRows = await db.query(countSql, countParams)
  49. if (!rows || !countRows) return res.json({ ...BaseStdResponse.DATABASE_ERR })
  50. return res.json({
  51. ...BaseStdResponse.OK,
  52. data: rows,
  53. pagination: {
  54. current: Number(current),
  55. pagesize: Number(pagesize),
  56. total: countRows[0]?.total || 0
  57. }
  58. })
  59. }
  60. }
  61. module.exports.AdminPopupReadList = AdminPopupReadList