SendAccountMessageJob.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Jobs\Base;
  11. use App\Jobs\Job;
  12. use Carbon\Carbon;
  13. use Illuminate\Support\Facades\Hash;
  14. use Illuminate\Support\Facades\Log;
  15. use Illuminate\Support\Str;
  16. class SendAccountMessageJob extends Job
  17. {
  18. private $admin;
  19. /**
  20. * Create a new job instance.
  21. */
  22. public function __construct($admin)
  23. {
  24. $this->admin = $admin;
  25. }
  26. /**
  27. * 确定任务应该超时的时间
  28. *
  29. * @return \DateTime
  30. */
  31. public function retryUntil()
  32. {
  33. return Carbon::now()->addHours(8);
  34. }
  35. /**
  36. * Execute the job.
  37. */
  38. public function handle()
  39. {
  40. $admin = $this->admin;
  41. try {
  42. $password = rand(10000000, 99999999);
  43. $admin->password = Hash::make($password);
  44. $admin->save();
  45. $this->sendSMSMsg($admin['mobile'], [
  46. 'username' => $admin['username'],
  47. 'name' => $admin['name'],
  48. 'password' => $password,
  49. ]);
  50. } catch (\Exception $exception) {
  51. Log::error($exception);
  52. }
  53. return true;
  54. }
  55. private function sendSMSMsg($mobile, $data = [])
  56. {
  57. if (!$mobile) return false;
  58. app('easy_sms')->send($mobile, [
  59. 'template' => config('sms.template.new_admin_account'),
  60. 'data' => [
  61. 'username' => $data['username'],
  62. // 'name' => $data['name'],
  63. 'password' => $data['password'],
  64. ]
  65. ]);
  66. }
  67. }