Files
fdesk/app/admin/controller/InvItemController.php
T

137 lines
4.1 KiB
PHP
Raw Normal View History

2025-12-05 09:22:53 +00:00
<?php
namespace app\admin\controller;
use app\common\controller\BaseController;
2026-01-05 10:18:27 +08:00
use app\common\model\Corp;
use app\common\model\User;
2026-02-24 13:30:27 +08:00
use app\common\service\OssService;
2025-12-05 09:22:53 +00:00
use app\common\traits\crud\CrudControllerTrait;
use app\common\service\InvItemService;
2026-01-05 10:42:08 +08:00
use support\Request;
2025-12-05 09:22:53 +00:00
class InvItemController extends BaseController
{
use CrudControllerTrait;
protected $service;
public function __construct()
{
$this->service = new InvItemService();
}
2026-01-05 10:42:08 +08:00
public function preset(Request $request){
try {
$res = $this->service->getList();
$result = [];
foreach ($res as $value) {
$value['company_name'] = Corp::where('id', $value['company_id'])->value('name');
$value['manage_user_name'] = User::where('id', $value['manage_user_id'])->value('name');
$result[] = $value;
}
return $this->success($result, '获取物品列表');
} catch (\Throwable $e) {
return $this->error($e->getMessage());
}
}
2025-12-05 09:22:53 +00:00
/**
* 列表页数据格式化
*
* @param array $data
* @return array
*/
2026-02-24 11:28:12 +08:00
protected function indexFormat($data)
{
2026-01-05 10:18:27 +08:00
if (isset($data['created_at']))
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
if (isset($data['updated_at']))
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
if (isset($data['company_id']))
$data['company_name'] = Corp::where('id', $data['company_id'])->value('name');
if (isset($data['manage_user_id']))
$data['manage_user_name'] = User::where('id', $data['manage_user_id'])->value('name');
2026-02-24 16:54:39 +08:00
if (!empty($data['qr_code']))
2026-02-24 13:30:27 +08:00
$data['qr_code_pull'] = (new OssService)->generatePrivateUrl($data['qr_code']);;
2026-01-05 10:18:27 +08:00
return $data;
}
2026-02-24 11:28:12 +08:00
/**
* 生成物品二维码
*
* @param Request $request
* @return mixed
*/
public function generateQrCode(Request $request)
{
try {
$id = $request->post('id');
if (!$id) {
return $this->error('物品ID不能为空');
}
$item = \app\common\model\InvItem::find($id);
if (!$item) {
return $this->error('物品不存在');
}
// 获取应用访问地址
2026-02-24 16:15:49 +08:00
$appUrl = 'https://'.$request->host();
2026-02-24 11:28:12 +08:00
$qrCodeService = new \app\common\service\QrCodeService();
$content = $qrCodeService->generateItemQrCode($id, $appUrl);
$fileName = 'qr_item_' . $id . '_' . time() . '.png';
$result = $qrCodeService->generateAndUploadQrCode($id, $content, $fileName);
// 更新物品记录
$item->qr_code = $result['path'];
$item->qr_code_url = $result['url'];
$item->save();
return $this->success([
'qr_code_url' => $result['url'],
'content' => $content,
], '二维码生成成功');
} catch (\Throwable $e) {
return $this->error($e->getMessage());
}
}
/**
* 删除物品二维码
*
* @param Request $request
* @return mixed
*/
public function deleteQrCode(Request $request)
{
try {
$id = $request->post('id');
if (!$id) {
return $this->error('物品ID不能为空');
}
$item = \app\common\model\InvItem::find($id);
if (!$item) {
return $this->error('物品不存在');
}
// 删除 OSS 文件
if ($item->qr_code) {
$qrCodeService = new \app\common\service\QrCodeService();
$qrCodeService->deleteQrCode($item->qr_code);
}
// 清空物品记录中的二维码字段
2026-02-24 13:30:27 +08:00
$item->qr_code = '';
$item->qr_code_url = '';
2026-02-24 11:28:12 +08:00
$item->save();
return $this->success([], '二维码删除成功');
} catch (\Throwable $e) {
return $this->error($e->getMessage());
}
}
}