MyList.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const API = require("../../../lib/API.js")
  2. const db = require("../../../plugin/DataBase/db.js")
  3. const { BaseStdResponse } = require("../../../BaseStdResponse.js")
  4. const AccessControl = require("../../../lib/AccessControl.js")
  5. const SYSTEM_UNBIND_SOURCES = new Set(['admin_api', 'service_api', 'mcp_work_order'])
  6. function mapUserAuditItem(item) {
  7. const base = {
  8. student_num: item.student_num,
  9. created_at: item.created_at
  10. }
  11. if (item.action === 'platform_unbind' && SYSTEM_UNBIND_SOURCES.has(item.source)) {
  12. return {
  13. ...base,
  14. action: 'system_unbind',
  15. action_label: '系统解绑'
  16. }
  17. }
  18. const labels = {
  19. platform_bind: '绑定',
  20. platform_unbind: '解绑',
  21. bot_bind: '绑定',
  22. bot_unbind: '解绑'
  23. }
  24. return {
  25. ...base,
  26. action: item.action,
  27. action_label: labels[item.action] || item.action
  28. }
  29. }
  30. class MyBindAuditList extends API {
  31. constructor() {
  32. super()
  33. this.setPath('/Lepao/BindAudit/List')
  34. this.setMethod('GET')
  35. }
  36. async onRequest(req, res) {
  37. let { uuid, session, student_num, pagesize, current, queryTime } = req.query
  38. if ([uuid, session, pagesize, current].some(v => v === '' || v === null || v === undefined))
  39. return res.json({ ...BaseStdResponse.MISSING_PARAMETER })
  40. if (isNaN(pagesize) || Number(pagesize) <= 0 || isNaN(current) || Number(current) <= 0) {
  41. return res.json({ ...BaseStdResponse.ERR, msg: '参数错误' })
  42. }
  43. if (!await AccessControl.checkSession(uuid, session))
  44. return res.status(401).json({ ...BaseStdResponse.ACCESS_DENIED })
  45. const offset = (Number(current) - 1) * Number(pagesize)
  46. const where = ['lba.owner_uuid COLLATE utf8mb4_general_ci = (CONVERT(? USING utf8mb4) COLLATE utf8mb4_general_ci)']
  47. const params = [uuid]
  48. const countParams = [uuid]
  49. if (student_num) {
  50. where.push('lba.student_num COLLATE utf8mb4_general_ci LIKE (CONVERT(? USING utf8mb4) COLLATE utf8mb4_general_ci)')
  51. params.push(`%${student_num}%`)
  52. countParams.push(`%${student_num}%`)
  53. }
  54. if (Array.isArray(queryTime) && queryTime.length === 2) {
  55. where.push('lba.created_at >= ? AND lba.created_at < ?')
  56. params.push(queryTime[0], queryTime[1])
  57. countParams.push(queryTime[0], queryTime[1])
  58. }
  59. const whereSql = where.join(' AND ')
  60. const listSql = `
  61. SELECT
  62. lba.student_num,
  63. lba.action,
  64. lba.source,
  65. lba.created_at,
  66. la.name AS lepao_name,
  67. la.user_avatar AS lepao_avatar
  68. FROM lepao_bind_audit lba
  69. LEFT JOIN lepao_account la ON la.student_num = lba.student_num
  70. WHERE ${whereSql}
  71. ORDER BY lba.id DESC
  72. LIMIT ? OFFSET ?
  73. `
  74. const countSql = `SELECT COUNT(*) AS total FROM lepao_bind_audit lba WHERE ${whereSql}`
  75. params.push(String(pagesize), String(offset))
  76. const rows = await db.query(listSql, params)
  77. const countRows = await db.query(countSql, countParams)
  78. if (!rows || !countRows) return res.json({ ...BaseStdResponse.DATABASE_ERR })
  79. return res.json({
  80. ...BaseStdResponse.OK,
  81. data: rows.map(mapUserAuditItem),
  82. pagination: {
  83. current: Number(current),
  84. pagesize: Number(pagesize),
  85. total: countRows[0]?.total || 0
  86. }
  87. })
  88. }
  89. }
  90. module.exports.MyBindAuditList = MyBindAuditList