SendCount.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. const EmailTemplate = require("../../plugin/Email/emailTemplate")
  6. const { insertLedgerRecord } = require("../../lib/Lepao/CountLedger")
  7. class SendCount extends API {
  8. constructor() {
  9. super()
  10. this.setPath("/Goods/SendCount")
  11. this.setMethod("POST")
  12. }
  13. async onRequest(req, res) {
  14. let { uuid, session, username, count } = req.body
  15. username = typeof username === "string" ? username.trim() : username
  16. count = Number(count)
  17. if ([uuid, session, username, count].some(v => v == null || v === "" || Number.isNaN(count)))
  18. return res.json({ ...BaseStdResponse.MISSING_PARAMETER })
  19. if (!Number.isInteger(count) || count < 1 || count > 9999)
  20. return res.json({ ...BaseStdResponse.ERR, msg: "超出赠送的次数范围,请重新选择赠送次数" })
  21. if (!(await AccessControl.checkSession(uuid, session)))
  22. return res.status(401).json({ ...BaseStdResponse.ACCESS_DENIED })
  23. const conn = await db.connect() // 这里直接拿 connection
  24. try {
  25. await conn.beginTransaction()
  26. const [senderRows] = await conn.execute(
  27. "SELECT id, username, lepao_count, COALESCE(send_count_auto_approve, 0) AS send_count_auto_approve FROM users WHERE uuid = ?",
  28. [uuid]
  29. )
  30. if (!senderRows || senderRows.length !== 1) {
  31. await conn.rollback()
  32. return res.json({ ...BaseStdResponse.MISSING_FILE, msg: "获取用户信息失败!" })
  33. }
  34. const [targetRows] = await conn.execute(
  35. "SELECT id, uuid FROM users WHERE username = ?",
  36. [username]
  37. )
  38. if (!targetRows || targetRows.length !== 1) {
  39. await conn.rollback()
  40. return res.json({ ...BaseStdResponse.ERR, msg: "未找到接收用户,请检查用户名是否正确!" })
  41. }
  42. if (targetRows[0].uuid === uuid) {
  43. await conn.rollback()
  44. return res.json({ ...BaseStdResponse.ERR, msg: "不能给自己赠送次数!" })
  45. }
  46. const [decResult] = await conn.execute(
  47. "UPDATE users SET lepao_count = lepao_count - ? WHERE uuid = ? AND lepao_count >= ?",
  48. [count, uuid, count]
  49. )
  50. if (decResult.affectedRows !== 1) {
  51. await conn.rollback()
  52. return res.json({ ...BaseStdResponse.ERR, msg: "剩余乐跑次数不足,请购买后再赠送!" })
  53. }
  54. const senderLepaoBefore = Number(senderRows[0].lepao_count || 0)
  55. const autoApprove = Number(senderRows[0].send_count_auto_approve) === 1
  56. if (autoApprove) {
  57. const [recvRows] = await conn.execute(
  58. "SELECT uuid, lepao_count FROM users WHERE id = ? FOR UPDATE",
  59. [targetRows[0].id]
  60. )
  61. if (!recvRows || recvRows.length !== 1) {
  62. await conn.rollback()
  63. return res.json({ ...BaseStdResponse.ERR, msg: "未找到接收用户,请检查用户名是否正确!" })
  64. }
  65. const receiverUuid = recvRows[0].uuid
  66. const beforeRecv = Number(recvRows[0].lepao_count || 0)
  67. const [incResult] = await conn.execute(
  68. "UPDATE users SET lepao_count = lepao_count + ? WHERE id = ?",
  69. [count, targetRows[0].id]
  70. )
  71. if (!incResult || incResult.affectedRows !== 1) {
  72. await conn.rollback()
  73. return res.json({ ...BaseStdResponse.ERR, msg: "接收方入账失败,请稍后再试!" })
  74. }
  75. const [insertResult] = await conn.execute(
  76. `INSERT INTO lepao_send_count_request
  77. (sender_uuid, receiver_user_id, count, status, created_at, reviewed_at, reviewer_uuid)
  78. VALUES (?, ?, ?, 'approved', NOW(), NOW(), NULL)`,
  79. [uuid, targetRows[0].id, count]
  80. )
  81. if (!insertResult || insertResult.affectedRows !== 1) {
  82. await conn.rollback()
  83. return res.json({ ...BaseStdResponse.ERR, msg: "记录赠送失败,请稍后再试!" })
  84. }
  85. const requestId = insertResult.insertId
  86. await insertLedgerRecord({
  87. executor: conn,
  88. userUuid: uuid,
  89. delta: -count,
  90. balanceBefore: senderLepaoBefore,
  91. balanceAfter: senderLepaoBefore - count,
  92. bizType: "gift_send_lock",
  93. bizId: `send_request:${requestId}`,
  94. remark: `向${username}赠送${count}次`
  95. })
  96. await insertLedgerRecord({
  97. executor: conn,
  98. userUuid: receiverUuid,
  99. delta: count,
  100. balanceBefore: beforeRecv,
  101. balanceAfter: beforeRecv + count,
  102. bizType: "gift_receive",
  103. bizId: `send_request:${requestId}`,
  104. operatorUuid: null,
  105. remark: `${senderRows[0].username}赠送${count}次`
  106. })
  107. await conn.commit()
  108. const reviewTime = new Date().getTime()
  109. Promise.resolve().then(async () => {
  110. try {
  111. const infoSql = `
  112. SELECT ru.email AS receiver_email, ru.username AS receiver_username
  113. FROM users ru
  114. WHERE ru.id = ?
  115. `
  116. const infoRows = await db.query(infoSql, [targetRows[0].id])
  117. if (!infoRows || infoRows.length !== 1 || !infoRows[0].receiver_email) {
  118. this.logger.warn(`[SendCountNotify][auto][requestId=${requestId}] 接收人邮箱为空,跳过通知`)
  119. return
  120. }
  121. await EmailTemplate.sendCountRequestApproved(infoRows[0].receiver_email, {
  122. requestId,
  123. senderUsername: senderRows[0].username,
  124. count,
  125. reviewTime
  126. })
  127. } catch (mailErr) {
  128. this.logger.error(`[SendCountNotify][auto][requestId=${requestId}] 接收人通知发送失败:${mailErr.message || "未知错误"}`)
  129. }
  130. })
  131. return res.json({ ...BaseStdResponse.OK, msg: "赠送成功,对方已到账" })
  132. }
  133. const [insertResult] = await conn.execute(
  134. `INSERT INTO lepao_send_count_request
  135. (sender_uuid, receiver_user_id, count, status, created_at)
  136. VALUES (?, ?, ?, 'pending', NOW())`,
  137. [uuid, targetRows[0].id, count]
  138. )
  139. if (!insertResult || insertResult.affectedRows !== 1) {
  140. await conn.rollback()
  141. return res.json({ ...BaseStdResponse.ERR, msg: "提交赠送审核失败,请稍后再试!" })
  142. }
  143. const requestId = insertResult.insertId
  144. await insertLedgerRecord({
  145. executor: conn,
  146. userUuid: uuid,
  147. delta: -count,
  148. balanceBefore: senderLepaoBefore,
  149. balanceAfter: senderLepaoBefore - count,
  150. bizType: "gift_send_lock",
  151. bizId: `send_request:${requestId}`,
  152. remark: `向${username}赠送${count}次`
  153. })
  154. await conn.commit()
  155. const createTime = new Date().getTime()
  156. // 非阻塞通知管理员,不影响主业务流程
  157. Promise.resolve().then(async () => {
  158. try {
  159. const adminSql = `
  160. SELECT email
  161. FROM users
  162. WHERE email IS NOT NULL
  163. AND email <> ''
  164. AND (JSON_CONTAINS(permission, '"admin"') OR JSON_CONTAINS(permission, '"service"'))
  165. `
  166. const adminRows = await db.query(adminSql)
  167. if (!adminRows || adminRows.length === 0) {
  168. this.logger.warn(`[SendCountNotify][submit][requestId=${requestId}] 未找到可通知的管理员邮箱`)
  169. return
  170. }
  171. const emails = [...new Set(adminRows.map(row => row.email).filter(Boolean))]
  172. for (const email of emails) {
  173. await EmailTemplate.sendCountRequestNotifyAdmins(email, {
  174. requestId,
  175. senderUsername: senderRows[0].username,
  176. receiverUsername: username,
  177. count,
  178. createTime
  179. })
  180. }
  181. } catch (mailErr) {
  182. this.logger.error(`[SendCountNotify][submit][requestId=${requestId}] 管理员通知发送失败:${mailErr.message || "未知错误"}`)
  183. }
  184. })
  185. return res.json({ ...BaseStdResponse.OK, msg: "已提交审核,审核通过后接收方将到账" })
  186. } catch (err) {
  187. try { await conn.rollback() } catch (_) { }
  188. this.logger.error(`赠送乐跑次数失败!${err.message || "未知错误"}`)
  189. return res.json({
  190. ...BaseStdResponse.ERR,
  191. msg: `赠送次数失败,请稍后再试!`
  192. })
  193. } finally {
  194. if (conn?.connection && typeof conn.connection.release === 'function' && typeof conn?.release === 'function') {
  195. conn.release()
  196. }
  197. }
  198. }
  199. }
  200. module.exports.SendCount = SendCount