SendCount.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 [insertResult] = await conn.execute(
  53. `INSERT INTO lepao_send_count_request
  54. (sender_uuid, receiver_user_id, count, status, created_at)
  55. VALUES (?, ?, ?, 'pending', NOW())`,
  56. [uuid, targetRows[0].id, count]
  57. )
  58. if (!insertResult || insertResult.affectedRows !== 1) {
  59. await conn.rollback()
  60. return res.json({ ...BaseStdResponse.ERR, msg: "提交赠送审核失败,请稍后再试!" })
  61. }
  62. await conn.commit()
  63. return res.json({ ...BaseStdResponse.OK, msg: "已提交审核,审核通过后接收方将到账" })
  64. } catch (err) {
  65. try { await conn.rollback() } catch (_) { }
  66. this.logger.error(`赠送乐跑次数失败!${err.message || "未知错误"}`)
  67. return res.json({
  68. ...BaseStdResponse.ERR,
  69. msg: `赠送次数失败,请稍后再试!`
  70. })
  71. }
  72. }
  73. }
  74. module.exports.SendCount = SendCount