user import
parent
394b091c3b
commit
7eac2e3f9d
Binary file not shown.
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace Peidikeji\User\Action;
|
||||
|
||||
use Dcat\Admin\Grid\Tools\AbstractTool;
|
||||
use Dcat\Admin\Widgets\Modal;
|
||||
use Peidikeji\User\Form\ImportForm;
|
||||
|
||||
class GridImportUser extends AbstractTool
|
||||
{
|
||||
protected $title = '导入';
|
||||
|
||||
protected function html()
|
||||
{
|
||||
return Modal::make()
|
||||
->lg()
|
||||
->body(ImportForm::make())
|
||||
->title($this->title)
|
||||
->button('<button type="button" class="btn btn-primary btn-outline"><i class="feather icon-upload"></i> '.$this->title.' </button>');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
namespace Peidikeji\User\Form;
|
||||
|
||||
use Dcat\Admin\Widgets\Form;
|
||||
use Dcat\EasyExcel\Excel;
|
||||
use Dcat\EasyExcel\Support\SheetCollection;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Peidikeji\User\Models\User;
|
||||
|
||||
class ImportForm extends Form
|
||||
{
|
||||
protected $buttons = ['reset' => 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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());
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue