task.controller.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
  2. import { TaskService } from '../services/task.service'
  3. import { UpdateTaskBody, ListTaskQuery, ListTaskItemQuery, SendMessageBody } from '../dto/task.dto'
  4. import { Task } from '../entities/task.entity'
  5. export class TaskController {
  6. private taskService: TaskService
  7. constructor(app: FastifyInstance) {
  8. this.taskService = new TaskService(app)
  9. }
  10. async create(request: FastifyRequest, reply: FastifyReply) {
  11. try {
  12. const userId = request.user.id
  13. const data = await request.file()
  14. if (!data) {
  15. return reply.code(400).send({ message: '请选择要上传的文件' })
  16. }
  17. const nameField = data.fields['name']
  18. const messageField = data.fields['message']
  19. const name =
  20. nameField && !Array.isArray(nameField) && 'value' in nameField ? (nameField.value as string) : undefined
  21. const message =
  22. messageField && !Array.isArray(messageField) && 'value' in messageField
  23. ? (messageField.value as string)
  24. : undefined
  25. if (!name || !message) {
  26. return reply.code(400).send({ message: '任务名称和消息内容不能为空' })
  27. }
  28. const buffer = await data.toBuffer()
  29. const task = await this.taskService.create({
  30. name,
  31. message,
  32. userId,
  33. buffer
  34. })
  35. return reply.code(201).send({
  36. task: {
  37. id: task.id,
  38. name: task.name,
  39. message: task.message
  40. }
  41. })
  42. } catch (error) {
  43. return reply.code(500).send(error)
  44. }
  45. }
  46. async getById(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) {
  47. try {
  48. const id = parseInt(request.params.id)
  49. if (isNaN(id)) {
  50. return reply.code(400).send({ message: '无效的任务ID' })
  51. }
  52. const task = await this.taskService.findById(id)
  53. return reply.send(task)
  54. } catch (error) {
  55. return reply.code(500).send({ error })
  56. }
  57. }
  58. async list(request: FastifyRequest<{ Querystring: ListTaskQuery }>, reply: FastifyReply) {
  59. try {
  60. const { page, size, userId } = request.query
  61. const result = await this.taskService.findAll(Number(page) || 0, Number(size) || 20, userId || request.user.id)
  62. return reply.send(result)
  63. } catch (error) {
  64. return reply.code(500).send({ error })
  65. }
  66. }
  67. async update(request: FastifyRequest<{ Body: UpdateTaskBody }>, reply: FastifyReply) {
  68. try {
  69. const { id, name, message, total, sent, successCount, startedAt } = request.body
  70. if (!id) {
  71. return reply.code(400).send({ message: '任务ID不能为空' })
  72. }
  73. const task = await this.taskService.findById(id)
  74. if (!task) {
  75. return reply.code(500).send({ message: '任务不存在' })
  76. }
  77. const updateData: Partial<Task> = {}
  78. if (name !== undefined) updateData.name = name
  79. if (message !== undefined) updateData.message = message
  80. if (total !== undefined) updateData.total = total
  81. if (sent !== undefined) updateData.sent = sent
  82. if (successCount !== undefined) updateData.successCount = successCount
  83. if (startedAt !== undefined) updateData.startedAt = startedAt
  84. await this.taskService.update(id, updateData)
  85. return reply.send({ message: '任务更新成功' })
  86. } catch (error) {
  87. return reply.code(500).send({ error })
  88. }
  89. }
  90. async delete(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) {
  91. try {
  92. const id = parseInt(request.params.id)
  93. if (isNaN(id)) {
  94. return reply.code(400).send({ message: '无效的任务ID' })
  95. }
  96. const task = await this.taskService.findById(id)
  97. if (!task) {
  98. return reply.code(500).send({ message: '任务不存在' })
  99. }
  100. await this.taskService.delete(id)
  101. return reply.send({ message: '任务删除成功' })
  102. } catch (error) {
  103. return reply.code(500).send(error)
  104. }
  105. }
  106. async listTaskItems(request: FastifyRequest<{ Querystring: ListTaskItemQuery }>, reply: FastifyReply) {
  107. try {
  108. const { page, size, taskId, status } = request.query
  109. const result = await this.taskService.findTaskItems(
  110. Number(page) || 0,
  111. Number(size) || 20,
  112. taskId ? Number(taskId) : undefined,
  113. status
  114. )
  115. return reply.send(result)
  116. } catch (error) {
  117. return reply.code(500).send({
  118. message: '查询任务项失败',
  119. error: error instanceof Error ? error.message : '未知错误'
  120. })
  121. }
  122. }
  123. async startTask(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) {
  124. try {
  125. const id = parseInt(request.params.id)
  126. if (isNaN(id)) {
  127. return reply.code(400).send({ message: '无效的任务ID' })
  128. }
  129. await this.taskService.startTask(id)
  130. return reply.send({ message: '任务启动成功' })
  131. } catch (error) {
  132. return reply.code(500).send({
  133. message: '启动任务失败',
  134. error: error instanceof Error ? error.message : '未知错误'
  135. })
  136. }
  137. }
  138. async testSendMessage(request: FastifyRequest<{ Body: SendMessageBody }>, reply: FastifyReply) {
  139. try {
  140. const { senderId, taskId, delay, count, session, dcId, authKey, sendToVerifyAccounts, verifyAccounts } =
  141. request.body
  142. if (!senderId || !taskId) {
  143. return reply.code(400).send({
  144. success: false,
  145. message: 'senderId 和 taskId 不能为空'
  146. })
  147. }
  148. const result = await this.taskService.testSendMessage(
  149. senderId,
  150. taskId,
  151. delay,
  152. count,
  153. session,
  154. dcId,
  155. authKey,
  156. sendToVerifyAccounts,
  157. verifyAccounts
  158. )
  159. return reply.code(200).send(result)
  160. } catch (error) {
  161. return reply.code(500).send({
  162. success: false,
  163. message: '测试发送消息时发生错误',
  164. error: error instanceof Error ? error.message : '未知错误'
  165. })
  166. }
  167. }
  168. }