SendCount.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const API = require("../../lib/API")
  2. const db = require("../../plugin/DataBase/db")
  3. const AccessControl = require("../../lib/AccessControl")
  4. const { BaseStdResponse } = require("../../BaseStdResponse")
  5. class SendCount extends API {
  6. constructor() {
  7. super()
  8. this.setPath("/Goods/SendCount")
  9. this.setMethod("POST")
  10. }
  11. async onRequest(req, res) {
  12. let { uuid, session, username, count } = req.body
  13. username = typeof username === "string" ? username.trim() : username
  14. count = Number(count)
  15. if ([uuid, session, username, count].some(v => v == null || v === "" || Number.isNaN(count)))
  16. return res.json({ ...BaseStdResponse.MISSING_PARAMETER })
  17. if (!Number.isInteger(count) || count < 1 || count > 9999)
  18. return res.json({ ...BaseStdResponse.ERR, msg: "超出赠送的次数范围,请重新选择赠送次数" })
  19. if (!(await AccessControl.checkSession(uuid, session)))
  20. return res.status(401).json({ ...BaseStdResponse.ACCESS_DENIED })
  21. const conn = await db.connect() // 这里直接拿 connection
  22. try {
  23. await conn.beginTransaction()
  24. const [senderRows] = await conn.execute(
  25. "SELECT id, username, lepao_count FROM users WHERE uuid = ?",
  26. [uuid]
  27. )
  28. if (!senderRows || senderRows.length !== 1) {
  29. await conn.rollback()
  30. return res.json({ ...BaseStdResponse.MISSING_FILE, msg: "获取用户信息失败!" })
  31. }
  32. const [targetRows] = await conn.execute(
  33. "SELECT id, uuid FROM users WHERE username = ?",
  34. [username]
  35. )
  36. if (!targetRows || targetRows.length !== 1) {
  37. await conn.rollback()
  38. return res.json({ ...BaseStdResponse.ERR, msg: "未找到接收用户,请检查用户名是否正确!" })
  39. }
  40. if (targetRows[0].uuid === uuid) {
  41. await conn.rollback()
  42. return res.json({ ...BaseStdResponse.ERR, msg: "不能给自己赠送次数!" })
  43. }
  44. const [decResult] = await conn.execute(
  45. "UPDATE users SET lepao_count = lepao_count - ? WHERE uuid = ? AND lepao_count >= ?",
  46. [count, uuid, count]
  47. )
  48. if (decResult.affectedRows !== 1) {
  49. await conn.rollback()
  50. return res.json({ ...BaseStdResponse.ERR, msg: "剩余乐跑次数不足,请购买后再赠送!" })
  51. }
  52. const [incResult] = await conn.execute(
  53. "UPDATE users SET lepao_count = lepao_count + ? WHERE id = ?",
  54. [count, targetRows[0].id]
  55. )
  56. if (incResult.affectedRows !== 1) {
  57. await conn.rollback()
  58. return res.json({ ...BaseStdResponse.ERR, msg: "赠送次数失败,请稍后再试!" })
  59. }
  60. await conn.commit()
  61. return res.json({ ...BaseStdResponse.OK, msg: "赠送成功" })
  62. } catch (err) {
  63. try { await conn.rollback() } catch (_) { }
  64. this.logger.error(`赠送乐跑次数失败!${err.message || "未知错误"}`)
  65. return res.json({
  66. ...BaseStdResponse.ERR,
  67. msg: `赠送次数失败,请稍后再试!`
  68. })
  69. }
  70. }
  71. }
  72. module.exports.SendCount = SendCount