Update.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. const { sanitizeHtml } = require("../../../lib/SanitizeHtml.js")
  6. function normalizeDateTime(value) {
  7. if (value === undefined || value === null || value === '') return null
  8. const n = Number(value)
  9. if (!Number.isNaN(n) && Number.isFinite(n)) {
  10. const d = new Date(n)
  11. if (!Number.isNaN(d.getTime())) {
  12. return d.toISOString().slice(0, 19).replace('T', ' ')
  13. }
  14. }
  15. return String(value)
  16. }
  17. class AdminUpdatePopup extends API {
  18. constructor() {
  19. super()
  20. this.setPath('/Admin/Popup')
  21. this.setMethod('PUT')
  22. }
  23. async onRequest(req, res) {
  24. let {
  25. uuid,
  26. session,
  27. id,
  28. title,
  29. content_html,
  30. priority,
  31. is_active,
  32. repeat_show,
  33. start_at,
  34. end_at
  35. } = req.body
  36. if ([uuid, session, id].some(v => v === '' || v === null || v === undefined))
  37. return res.json({ ...BaseStdResponse.MISSING_PARAMETER })
  38. if (!await AccessControl.checkSession(uuid, session))
  39. return res.status(401).json({ ...BaseStdResponse.ACCESS_DENIED })
  40. const permission = await AccessControl.getPermission(uuid)
  41. if (!permission.includes("admin") && !permission.includes("server"))
  42. return res.json({ ...BaseStdResponse.PERMISSION_DENIED })
  43. const sets = ['updated_at = ?']
  44. const params = [Date.now()]
  45. if (title !== undefined) {
  46. sets.push('title = ?')
  47. params.push(String(title).trim())
  48. }
  49. if (content_html !== undefined) {
  50. sets.push('content_html = ?')
  51. params.push(sanitizeHtml(content_html))
  52. }
  53. if (priority !== undefined) {
  54. sets.push('priority = ?')
  55. params.push(Number(priority) || 0)
  56. }
  57. if (is_active !== undefined) {
  58. sets.push('is_active = ?')
  59. params.push(Number(is_active) === 0 ? 0 : 1)
  60. }
  61. if (repeat_show !== undefined) {
  62. sets.push('repeat_show = ?')
  63. params.push(Number(repeat_show) === 1 ? 1 : 0)
  64. }
  65. if (start_at !== undefined) {
  66. sets.push('start_at = ?')
  67. params.push(normalizeDateTime(start_at))
  68. }
  69. if (end_at !== undefined) {
  70. sets.push('end_at = ?')
  71. params.push(normalizeDateTime(end_at))
  72. }
  73. if (sets.length === 1) {
  74. return res.json({ ...BaseStdResponse.ERR, msg: '缺少更新字段' })
  75. }
  76. const sql = `UPDATE site_popup SET ${sets.join(', ')} WHERE id = ?`
  77. params.push(id)
  78. const rows = await db.query(sql, params)
  79. if (!rows) return res.json({ ...BaseStdResponse.DATABASE_ERR })
  80. if (rows.affectedRows !== 1) return res.json({ ...BaseStdResponse.ERR, msg: '公告不存在或未修改' })
  81. return res.json({ ...BaseStdResponse.OK })
  82. }
  83. }
  84. module.exports.AdminUpdatePopup = AdminUpdatePopup