ImportUserJob.php 2.9 KB

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