初始化项目
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports;
|
||||
|
||||
use App\Models\Auth\Department;
|
||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
use Maatwebsite\Excel\Concerns\WithMapping;
|
||||
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
||||
|
||||
class DepartmentExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize
|
||||
{
|
||||
protected $departmentIds;
|
||||
|
||||
public function __construct(array $departmentIds = [])
|
||||
{
|
||||
$this->departmentIds = $departmentIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据集合
|
||||
*/
|
||||
public function collection()
|
||||
{
|
||||
$query = Department::query();
|
||||
|
||||
if (!empty($this->departmentIds)) {
|
||||
$query->whereIn('id', $this->departmentIds);
|
||||
}
|
||||
|
||||
return $query->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置表头
|
||||
*/
|
||||
public function headings(): array
|
||||
{
|
||||
return [
|
||||
'ID',
|
||||
'部门名称',
|
||||
'上级部门',
|
||||
'负责人',
|
||||
'联系电话',
|
||||
'排序',
|
||||
'状态',
|
||||
'创建时间',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 映射数据
|
||||
*/
|
||||
public function map($department): array
|
||||
{
|
||||
return [
|
||||
$department->id,
|
||||
$department->name,
|
||||
$department->parent_id ? Department::find($department->parent_id)?->name : '',
|
||||
$department->leader,
|
||||
$department->phone,
|
||||
$department->sort,
|
||||
$department->status == 1 ? '启用' : '禁用',
|
||||
$department->created_at ? $department->created_at->toDateTimeString() : '',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
|
||||
class GenericExport implements FromCollection, WithHeadings
|
||||
{
|
||||
protected $data;
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据集合
|
||||
*/
|
||||
public function collection()
|
||||
{
|
||||
return collect($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置表头
|
||||
*/
|
||||
public function headings(): array
|
||||
{
|
||||
// 第一行作为表头
|
||||
return $this->data[0] ?? [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?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 ? $user->last_login_at->toDateTimeString() : '',
|
||||
$user->created_at ? $user->created_at->toDateTimeString() : '',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user