ImportUserJob.php 2.9 KB

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