ImportUserJob.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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;
  11. use App\Repositories\Models\User\User;
  12. use Illuminate\Support\Facades\Hash;
  13. use Illuminate\Support\Facades\Storage;
  14. class ImportUserJob extends Job
  15. {
  16. public $timeout = 1200;
  17. private $path = '';
  18. private $disk = '';
  19. private $role = 0;
  20. /**
  21. * Create a new job instance.
  22. */
  23. public function __construct($disk, $path, $role = 0)
  24. {
  25. $this->disk = $disk;
  26. $this->path = $path;
  27. $this->role = $role;
  28. }
  29. /**
  30. * Execute the job.
  31. */
  32. public function handle()
  33. {
  34. $config = ['path' => ''];
  35. $excel = new \Vtiful\Kernel\Excel($config);
  36. $excel->openFile(Storage::disk($this->disk)->path($this->path))
  37. ->openSheet()
  38. ->setSkipRows(1);
  39. while (($row = $excel->nextRow()) !== NULL) {
  40. if ($row[1]) {
  41. if (!User::query()->where('username', $row[1])->exists()) {
  42. $data['username'] = trim($row[1]);
  43. $data['name'] = $row[2];
  44. $data['turename'] = $row[2];
  45. if (isset($row[3]) && $row[3]) {
  46. $data['mobile'] = $row[3];
  47. }
  48. if (isset($row[4])) {
  49. $sex = 0;
  50. switch ($row[4]) {
  51. case "男":
  52. case 'man':
  53. $sex = 1;
  54. break;
  55. case '女':
  56. case 'women':
  57. $sex = 2;
  58. break;
  59. }
  60. $data['sex'] = $sex;
  61. }
  62. if (isset($row[5])) {
  63. $data['class'] = $row[5];
  64. }
  65. if (isset($row[6])) {
  66. $data['password'] = Hash::make(trim($row[6]));
  67. } else {
  68. $data['password'] = Hash::make($data['username']);
  69. }
  70. $data['role_id'] = $this->role;
  71. User::query()->create($data);
  72. }
  73. }
  74. }
  75. return true;
  76. }
  77. }