12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
- /*
- * This file is part of the Jiannei/lumen-api-starter.
- *
- * (c) Jiannei <longjian.huang@foxmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace App\Jobs;
- use App\Repositories\Models\User\User;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Facades\Storage;
- class ImportUserJob extends Job
- {
- public $timeout = 1200;
- private $path = '';
- private $disk = '';
- private $role = 0;
- /**
- * Create a new job instance.
- */
- public function __construct($disk, $path, $role = 0)
- {
- $this->disk = $disk;
- $this->path = $path;
- $this->role = $role;
- }
- /**
- * Execute the job.
- */
- public function handle()
- {
- $config = ['path' => ''];
- $excel = new \Vtiful\Kernel\Excel($config);
- $excel->openFile(Storage::disk($this->disk)->path($this->path))
- ->openSheet()
- ->setSkipRows(1);
- while (($row = $excel->nextRow()) !== NULL) {
- if ($row[1]) {
- if (!User::query()->where('username', $row[1])->exists()) {
- $data['username'] = trim($row[1]);
- $data['name'] = $row[2];
- $data['turename'] = $row[2];
- if (isset($row[3]) && $row[3]) {
- $data['mobile'] = $row[3];
- }
- if (isset($row[4])) {
- $sex = 0;
- switch ($row[4]) {
- case "男":
- case 'man':
- $sex = 1;
- break;
- case '女':
- case 'women':
- $sex = 2;
- break;
- }
- $data['sex'] = $sex;
- }
- if (isset($row[5])) {
- $data['class'] = $row[5];
- }
- if (isset($row[6])) {
- $data['password'] = Hash::make(trim($row[6]));
- } else {
- $data['password'] = Hash::make($data['username']);
- }
- $data['role_id'] = $this->role;
- User::query()->create($data);
- }
- }
- }
- return true;
- }
- }
|