diff --git a/assets/users.xlsx b/assets/users.xlsx new file mode 100644 index 0000000..77862ee Binary files /dev/null and b/assets/users.xlsx differ diff --git a/src/Action/GridImportUser.php b/src/Action/GridImportUser.php new file mode 100644 index 0000000..410af9c --- /dev/null +++ b/src/Action/GridImportUser.php @@ -0,0 +1,21 @@ +lg() + ->body(ImportForm::make()) + ->title($this->title) + ->button(''); + } +} diff --git a/src/Form/ImportForm.php b/src/Form/ImportForm.php new file mode 100644 index 0000000..e779a70 --- /dev/null +++ b/src/Form/ImportForm.php @@ -0,0 +1,77 @@ + false, 'submit' => true, 'back' => false]; + + public function handle(array $input) + { + $disk = Storage::disk('public'); + + Excel::import($disk->path($input['file']))->headings(false)->first()->chunk(500, fn(SheetCollection $collection) => $this->mapGoods($collection->toArray())); + + return $this->response()->success('导入成功')->refresh(); + } + + public function form() + { + $this->file('file')->autoUpload()->uniqueName()->move('user/import')->accept('xlsx,xls')->disk('public'); + } + + protected function mapGoods($rows) + { + array_shift($rows); + foreach ($rows as $row) { + $index = 0; + $username = data_get($row, $index); + $name = data_get($row, ++$index); + $avatar = data_get($row, ++$index); + $gender = data_get($row, ++$index); + $phone = data_get($row, ++$index); + $invitePhone = data_get($row, ++$index); + $createdAt = data_get($row, ++$index); + + if (!$username) { + throw new \Exception('用户名必填'); + } + if (!$phone) { + throw new \Exception('手机号必填'); + } + + $attributes = ['username' => $username]; + if ($name) { + $attributes['name'] = $name; + } + if ($avatar) { + $attributes['avatar'] = $avatar; + } + if ($gender) { + $attributes['gender'] = $gender; + } + if ($gender) { + $attributes['gender'] = $gender; + } + if ($createdAt) { + $attributes['created_at'] = $createdAt; + } + if ($invitePhone) { + $inviter = User::where('phone', $invitePhone)->first(); + if (!$inviter) { + throw new \Exception('邀请人不存在'); + } + $attributes['inviter_id'] = $inviter->id; + $attributes['inviter_path'] = $inviter->inviter_path . $inviter->id . '-'; + } + + User::updateOrCreate(['phone' => $phone], $attributes); + } + } +} diff --git a/src/Http/Admin/UserController.php b/src/Http/Admin/UserController.php index bf3da99..965157b 100644 --- a/src/Http/Admin/UserController.php +++ b/src/Http/Admin/UserController.php @@ -18,6 +18,7 @@ use Illuminate\Http\Request; use Illuminate\Validation\Rule; use Peidikeji\User\Models\User; use Illuminate\Support\Str; +use Peidikeji\User\Action\GridImportUser; use Peidikeji\User\Action\ShowBalance; use Peidikeji\User\Action\ShowPassword; @@ -86,6 +87,12 @@ class UserController extends AdminController } })->date()->width(3); }); + + $grid->tools(function (Grid\Tools $tools) use ($user) { + if ($user->can('dcat.admin.users.import')) { + $tools->append(new GridImportUser()); + } + }); }); } diff --git a/src/Models/User.php b/src/Models/User.php index 2388ea8..1652592 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -17,7 +17,7 @@ class User extends Authenticatable use HasDateTimeFormatter; use Filterable; - protected $fillable = ['username', 'password', 'avatar', 'balance', 'invite_code', 'inviter_id', 'inviter_path', 'name', 'gender', 'phone']; + protected $fillable = ['username', 'password', 'avatar', 'balance', 'invite_code', 'inviter_id', 'inviter_path', 'name', 'gender', 'phone', 'created_at']; protected static function booted() {