140 lines
4.2 KiB
PHP
140 lines
4.2 KiB
PHP
<?php
|
|
namespace app\admin\controller;
|
|
|
|
use app\common\controller\BaseController;
|
|
use app\common\model\Corp;
|
|
use app\common\model\User;
|
|
use app\common\service\OssService;
|
|
use app\common\traits\crud\CrudControllerTrait;
|
|
use app\common\service\InvItemService;
|
|
use support\Request;
|
|
|
|
class InvItemController extends BaseController
|
|
{
|
|
use CrudControllerTrait;
|
|
|
|
protected $service;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->service = new InvItemService();
|
|
}
|
|
|
|
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());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 列表页数据格式化
|
|
*
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
protected function indexFormat($data)
|
|
{
|
|
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');
|
|
if (!empty($data['qr_code']))
|
|
$data['qr_code_pull'] = (new OssService)->generatePrivateUrl($data['qr_code']);;
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 生成物品二维码
|
|
*
|
|
* @param Request $request
|
|
* @return mixed
|
|
*/
|
|
public function generateQrCode(Request $request)
|
|
{
|
|
try {
|
|
$id = $request->post('id');
|
|
$num = $request->post('num');
|
|
if (!$id) {
|
|
return $this->error('物品ID不能为空');
|
|
}
|
|
|
|
$item = \app\common\model\InvItem::find($id);
|
|
if (!$item) {
|
|
return $this->error('物品不存在');
|
|
}
|
|
|
|
// 获取应用访问地址
|
|
$appUrl = 'https://'.$request->host();
|
|
|
|
$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_num = !empty($num)?$num:0;
|
|
$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);
|
|
}
|
|
|
|
// 清空物品记录中的二维码字段
|
|
$item->qr_code = '';
|
|
//$item->qr_code_num = 0;
|
|
$item->qr_code_url = '';
|
|
$item->save();
|
|
|
|
return $this->success([], '二维码删除成功');
|
|
} catch (\Throwable $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
}
|