| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- const API = require("../../../lib/API.js")
- const db = require("../../../plugin/DataBase/db.js")
- const { BaseStdResponse } = require("../../../BaseStdResponse.js")
- const AccessControl = require("../../../lib/AccessControl.js")
- const SYSTEM_UNBIND_SOURCES = new Set(['admin_api', 'service_api', 'mcp_work_order'])
- function mapUserAuditItem(item) {
- const base = {
- student_num: item.student_num,
- created_at: item.created_at
- }
- if (item.action === 'platform_unbind' && SYSTEM_UNBIND_SOURCES.has(item.source)) {
- return {
- ...base,
- action: 'system_unbind',
- action_label: '系统解绑'
- }
- }
- const labels = {
- platform_bind: '绑定',
- platform_unbind: '解绑',
- bot_bind: '绑定',
- bot_unbind: '解绑'
- }
- return {
- ...base,
- action: item.action,
- action_label: labels[item.action] || item.action
- }
- }
- class MyBindAuditList extends API {
- constructor() {
- super()
- this.setPath('/Lepao/BindAudit/List')
- this.setMethod('GET')
- }
- async onRequest(req, res) {
- let { uuid, session, student_num, pagesize, current, queryTime } = req.query
- if ([uuid, session, pagesize, current].some(v => v === '' || v === null || v === undefined))
- return res.json({ ...BaseStdResponse.MISSING_PARAMETER })
- if (isNaN(pagesize) || Number(pagesize) <= 0 || isNaN(current) || Number(current) <= 0) {
- return res.json({ ...BaseStdResponse.ERR, msg: '参数错误' })
- }
- if (!await AccessControl.checkSession(uuid, session))
- return res.status(401).json({ ...BaseStdResponse.ACCESS_DENIED })
- const offset = (Number(current) - 1) * Number(pagesize)
- const where = ['lba.owner_uuid COLLATE utf8mb4_general_ci = (CONVERT(? USING utf8mb4) COLLATE utf8mb4_general_ci)']
- const params = [uuid]
- const countParams = [uuid]
- if (student_num) {
- where.push('lba.student_num COLLATE utf8mb4_general_ci LIKE (CONVERT(? USING utf8mb4) COLLATE utf8mb4_general_ci)')
- params.push(`%${student_num}%`)
- countParams.push(`%${student_num}%`)
- }
- if (Array.isArray(queryTime) && queryTime.length === 2) {
- where.push('lba.created_at >= ? AND lba.created_at < ?')
- params.push(queryTime[0], queryTime[1])
- countParams.push(queryTime[0], queryTime[1])
- }
- const whereSql = where.join(' AND ')
- const listSql = `
- SELECT
- lba.student_num,
- lba.action,
- lba.source,
- lba.created_at,
- la.name AS lepao_name,
- la.user_avatar AS lepao_avatar
- FROM lepao_bind_audit lba
- LEFT JOIN lepao_account la ON la.student_num = lba.student_num
- WHERE ${whereSql}
- ORDER BY lba.id DESC
- LIMIT ? OFFSET ?
- `
- const countSql = `SELECT COUNT(*) AS total FROM lepao_bind_audit lba WHERE ${whereSql}`
- params.push(String(pagesize), String(offset))
- const rows = await db.query(listSql, params)
- const countRows = await db.query(countSql, countParams)
- if (!rows || !countRows) return res.json({ ...BaseStdResponse.DATABASE_ERR })
- return res.json({
- ...BaseStdResponse.OK,
- data: rows.map(mapUserAuditItem),
- pagination: {
- current: Number(current),
- pagesize: Number(pagesize),
- total: countRows[0]?.total || 0
- }
- })
- }
- }
- module.exports.MyBindAuditList = MyBindAuditList
|