You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
264 lines
8.0 KiB
264 lines
8.0 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Exceptions\CustomException;
|
|
use App\Exports\AdminVipListExport;
|
|
use App\Exports\AdminVipListImportTemplateExport;
|
|
use App\Imports\AdminVipListImport;
|
|
use App\Models\AdminUsers;
|
|
use App\Models\AdminVipList;
|
|
use App\Services\AdminVipListService;
|
|
use App\Services\ApiResponseService;
|
|
use Exception;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
|
|
class VipListController extends BaseController
|
|
{
|
|
/**
|
|
* @var ApiResponseService
|
|
*/
|
|
protected ApiResponseService $responseService;
|
|
|
|
/**
|
|
* @var AdminVipListService
|
|
*/
|
|
protected AdminVipListService $vipListService;
|
|
|
|
/**
|
|
* @param ApiResponseService $responseService
|
|
* @param AdminVipListService $vipListService
|
|
*/
|
|
public function __construct(
|
|
ApiResponseService $responseService,
|
|
AdminVipListService $vipListService
|
|
) {
|
|
$this->responseService = $responseService;
|
|
$this->vipListService = $vipListService;
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
*/
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$query = AdminVipList::query();
|
|
|
|
if ($request->has('license')) {
|
|
$license = $request->input('license');
|
|
$query->where('license', 'like', "%{$license}%");
|
|
}
|
|
// 分页
|
|
$page = $request->input('page', 1);
|
|
$perPage = $request->input('per_page', 10);
|
|
|
|
$total = $query->count();
|
|
$items = $query->latest()->forPage($page, $perPage)->get()->each(
|
|
function ($item) {
|
|
$item['username'] = AdminUsers::getUsername(
|
|
$item['user_id']
|
|
);
|
|
return $item;
|
|
}
|
|
);
|
|
|
|
return $this->responseService->success([
|
|
'items' => $items,
|
|
'total' => $total,
|
|
'page' => $page,
|
|
'per_page' => $perPage,
|
|
'last_page' => ceil($total / $perPage),
|
|
]);
|
|
} catch (Exception $e) {
|
|
$m_prefix = __('exception.get_user_info_list_failed');
|
|
return $this->responseService->systemError(
|
|
$m_prefix . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @param string $id
|
|
* @return JsonResponse
|
|
* @throws CustomException
|
|
* @throws ValidationException
|
|
*/
|
|
public function update(Request $request, string $id): JsonResponse
|
|
{
|
|
try {
|
|
$this->saveValidator($request->all(), $id);
|
|
$this->vipListService->updateModel($request->all(), $id);
|
|
return $this->responseService->success(
|
|
null,
|
|
__('admin.update_succeeded')
|
|
);
|
|
} catch (ValidationException|CustomException $e) {
|
|
throw $e;
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.admin_vip_list.update_failed') . ':'
|
|
. $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @param int $id
|
|
* @return void
|
|
* @throws ValidationException
|
|
*/
|
|
protected function saveValidator(array $data, int $id = 0): void
|
|
{
|
|
$rules = [
|
|
'license' => 'required|max:20'
|
|
];
|
|
$messages = [
|
|
'license.required' => __('validation.admin_list_vip.l_empty'),
|
|
'license.max' => __('validation.admin_list_vip.l_max')
|
|
];
|
|
if ($id) {
|
|
$this->validateId($id, AdminVipList::class);
|
|
}
|
|
$validator = Validator::make($data, $rules, $messages);
|
|
|
|
if ($validator->fails()) {
|
|
throw new ValidationException($validator);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param string $id
|
|
* @return JsonResponse
|
|
* @throws CustomException
|
|
* @throws ValidationException
|
|
*/
|
|
public function destroy(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$this->validateId($id, AdminVipList::class);
|
|
$this->vipListService->deleteModel($id);
|
|
return $this->responseService->success(
|
|
null,
|
|
__('admin.delete_succeeded')
|
|
);
|
|
} catch (ValidationException|CustomException $e) {
|
|
throw $e;
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.admin_vip_list.destroy_failed') . ':'
|
|
. $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
* @throws ValidationException
|
|
*/
|
|
public function import(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
// 1. 验证上传的文件
|
|
$request->validate([
|
|
'file' => 'required|mimes:xlsx,xls,csv|max:2048' // 限制文件类型和大小
|
|
]);
|
|
$validator = Validator::make($request->file('file'), [
|
|
'file' => 'required|mimes:xlsx,xls,csv|max:2048'
|
|
], [
|
|
'file.required' => __('validation.admin_list_vip.file_empty'),
|
|
'file.mimes' => __('validation.admin_list_vip.file_mimes'),
|
|
'file.max' => __('validation.admin_list_vip.file_max'),
|
|
]);
|
|
if ($validator->fails()) {
|
|
throw new ValidationException($validator);
|
|
}
|
|
|
|
// 2. 获取上传的文件
|
|
$file = $request->file('file');
|
|
|
|
// 3. 正确的做法:先存储文件,再获取绝对路径进行导入
|
|
// 将文件存储到 storage/app/imports 目录下
|
|
$path = $file->store('imports');
|
|
|
|
// 4. 执行导入(使用存储后的绝对路径)
|
|
// storage_path('app') 获取 storage/app 的绝对路径
|
|
Excel::import(
|
|
new AdminVipListImport(),
|
|
storage_path('app/' . $path)
|
|
);
|
|
|
|
// 5. (可选)导入完成后删除临时文件
|
|
Storage::delete($path);
|
|
|
|
return $this->responseService->systemError(
|
|
__('controller.import.success')
|
|
);
|
|
} catch (ValidationException $e) {
|
|
throw $e;
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.admin_vip_list.import_failed') . ':'
|
|
. $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
* @throws CustomException
|
|
* @throws ValidationException
|
|
*/
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$this->saveValidator($request->all());
|
|
$this->vipListService->createModel($request->all());
|
|
|
|
return $this->responseService->success(
|
|
null,
|
|
__('admin.save_succeeded')
|
|
);
|
|
} catch (ValidationException|CustomException $e) {
|
|
throw $e;
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.admin_vip_list.create_failed') . ':'
|
|
. $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return BinaryFileResponse
|
|
*/
|
|
public function importTemplate(): BinaryFileResponse
|
|
{
|
|
return Excel::download(
|
|
new AdminVipListImportTemplateExport(),
|
|
__('exports.vip_list.import_template') . '.xlsx'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return BinaryFileResponse
|
|
*/
|
|
public function export(): BinaryFileResponse
|
|
{
|
|
return Excel::download(
|
|
new AdminVipListExport(),
|
|
__('exports.vip_list.list') . '.xlsx'
|
|
);
|
|
}
|
|
}
|
|
|