2
0

EmailCertificationController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Resources\InviteResource;
  4. use App\Mail\EmailCertif;
  5. use App\Models\Invite;
  6. use App\Models\UserInfo;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\Response;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Mail;
  12. use Illuminate\Support\Str;
  13. class EmailCertificationController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return Response
  19. */
  20. public function index()
  21. {
  22. //
  23. }
  24. /**
  25. * Store a newly created resource in storage.
  26. *
  27. * @return Response
  28. */
  29. public function store(Request $request)
  30. {
  31. // 查询是否重复
  32. if (UserInfo::where('email', $request->input('email'))->exists()) {
  33. return $this->error('email.exists', 'err.email.exists', 200);
  34. }
  35. $sender = config('mint.admin.root_uuid');
  36. $uuid = Str::uuid();
  37. $invite = Invite::firstOrNew(
  38. ['email' => $request->input('email')],
  39. ['id' => $uuid]
  40. );
  41. $invite->user_uid = $sender;
  42. $invite->status = 'invited';
  43. $invite->save();
  44. try {
  45. Mail::to($request->input('email'))
  46. ->send(new EmailCertif(
  47. $invite->id,
  48. $request->input('subject', 'sign up wikipali'),
  49. $request->input('lang'),
  50. ));
  51. } catch (\Exception $e) {
  52. Log::error('send email fail', [
  53. 'message' => $e->getMessage(),
  54. 'trace' => $e->getTraceAsString(),
  55. ]);
  56. return $this->error('send email fail', $e->getMessage(), 200);
  57. }
  58. return $this->ok(new InviteResource($invite));
  59. }
  60. /**
  61. * Display the specified resource.
  62. *
  63. * @return Response
  64. */
  65. public function show(string $id)
  66. {
  67. //
  68. $code = Cache::get('/email/certification/'.$id);
  69. if (empty($code)) {
  70. return $this->error('Certification is avalide', 200, 200);
  71. }
  72. return $this->ok($code);
  73. }
  74. /**
  75. * Update the specified resource in storage.
  76. *
  77. * @param int $id
  78. * @return Response
  79. */
  80. public function update(Request $request, $id)
  81. {
  82. //
  83. }
  84. /**
  85. * Remove the specified resource from storage.
  86. *
  87. * @param int $id
  88. * @return Response
  89. */
  90. public function destroy($id)
  91. {
  92. //
  93. }
  94. }