72 lines
1.6 KiB
PHP
72 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Exports;
|
|
|
|
use App\Models\Auth\User;
|
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
|
use Maatwebsite\Excel\Concerns\WithHeadings;
|
|
use Maatwebsite\Excel\Concerns\WithMapping;
|
|
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
|
|
|
class UserExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize
|
|
{
|
|
protected $userIds;
|
|
|
|
public function __construct(array $userIds = [])
|
|
{
|
|
$this->userIds = $userIds;
|
|
}
|
|
|
|
/**
|
|
* 获取数据集合
|
|
*/
|
|
public function collection()
|
|
{
|
|
$query = User::with(['department', 'roles']);
|
|
|
|
if (!empty($this->userIds)) {
|
|
$query->whereIn('id', $this->userIds);
|
|
}
|
|
|
|
return $query->get();
|
|
}
|
|
|
|
/**
|
|
* 设置表头
|
|
*/
|
|
public function headings(): array
|
|
{
|
|
return [
|
|
'ID',
|
|
'用户名',
|
|
'真实姓名',
|
|
'邮箱',
|
|
'手机号',
|
|
'部门',
|
|
'角色',
|
|
'状态',
|
|
'最后登录时间',
|
|
'创建时间',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 映射数据
|
|
*/
|
|
public function map($user): array
|
|
{
|
|
return [
|
|
$user->id,
|
|
$user->username,
|
|
$user->real_name,
|
|
$user->email,
|
|
$user->phone,
|
|
$user->department ? $user->department->name : '',
|
|
$user->roles->pluck('name')->implode(','),
|
|
$user->status == 1 ? '启用' : '禁用',
|
|
$user->last_login_at ? (string)$user->last_login_at : '',
|
|
$user->created_at ? (string)$user->created_at : '',
|
|
];
|
|
}
|
|
}
|