193 lines
6.5 KiB
PHP
193 lines
6.5 KiB
PHP
<?php
|
|
namespace app\mobile\controller;
|
|
|
|
use app\common\controller\BaseController;
|
|
use app\common\model\Corp;
|
|
use app\common\model\InvItem;
|
|
use app\common\model\InvRequisitionApprove;
|
|
use app\common\model\InvRequisitionApproveAttachment;
|
|
use app\common\model\InvRequisitionAttachment;
|
|
use app\common\model\InvRequisitionItem;
|
|
use app\common\service\OssService;
|
|
use app\common\traits\crud\CrudControllerTrait;
|
|
use app\common\service\InvRequisitionService;
|
|
use support\Request;
|
|
|
|
class InvRequisitionController extends BaseController
|
|
{
|
|
use CrudControllerTrait;
|
|
|
|
protected $service;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->service = new InvRequisitionService();
|
|
}
|
|
|
|
/**
|
|
* 提交物品申请
|
|
*
|
|
* @param Request $request
|
|
* @return mixed
|
|
*/
|
|
public function apply(Request $request)
|
|
{
|
|
try {
|
|
// 验证申请数据
|
|
$data = $this->validate($this->service->getValidator(), $request->post(), 'apply');
|
|
|
|
// 处理申请逻辑
|
|
$result = $this->service->serviceStore($request, $data, 'apply');
|
|
|
|
return $this->success($result, '申请提交成功');
|
|
} catch (\Throwable $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取列表
|
|
*
|
|
* @param Request $request 请求对象
|
|
* @return mixed 返回分页数据及总数
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
try {
|
|
$param = $request->all();
|
|
$applicant_id = $request->get('applicant_id');
|
|
$raw = $this->service->serviceList($request);
|
|
$uid = $request->user->uid;
|
|
$data = $this->formatindex($raw,$applicant_id,$uid,$param);
|
|
// $data = $this->transformData($raw, method_exists($this, 'indexFormat')
|
|
// ? [$this, 'indexFormat']
|
|
// : null);
|
|
return $this->success($data);
|
|
} catch (\Throwable $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function formatindex($p,$applicant_id,$uid,$param){
|
|
$records = $p->getCollection()
|
|
->map(fn($item) => $this->indexFormat($item, $applicant_id,$uid))
|
|
->toArray();
|
|
|
|
if(isset($param['phase']) && $param['phase'] == 'pending'){
|
|
foreach ($records as $key => $record) {
|
|
if($record['action']==1 || $record['action']==2)
|
|
unset($records[$key]);
|
|
}
|
|
$records = array_values($records);
|
|
}
|
|
|
|
return [
|
|
'records' => $records,
|
|
'total' => $p->total(),
|
|
'current' => $p->currentPage(),
|
|
'size' => $p->perPage(),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 列表页数据格式化
|
|
*
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
protected function indexFormat($data, $applicant_id, $uid)
|
|
{
|
|
if(isset($data['id']) && is_object($data)){
|
|
$data = $data->toArray();
|
|
}
|
|
if (isset($data['created_at'])){
|
|
$data['created_at'] = is_int($data['created_at'])?$data['created_at']:strtotime($data['created_at']);
|
|
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
|
}
|
|
if (isset($data['updated_at'])){
|
|
$data['updated_at'] = is_int($data['updated_at'])?$data['updated_at']:strtotime($data['updated_at']);
|
|
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
|
}
|
|
if (isset($data['id'])){
|
|
$data['item'] = InvRequisitionItem::where('requisition_id', $data['id'])->get();
|
|
}
|
|
|
|
if($applicant_id != 'self' && isset($data['id'])){ //不是查询自己数据时查询是否已审核
|
|
$approve = InvRequisitionApprove::where('requisition_id', $data['id'])
|
|
->where('approver_id', $uid)
|
|
->first();
|
|
$data['action'] = $approve['action']??3;//查询人审核状态:0待审核 1通过 2驳回 3非审核人
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 详情页数据格式化
|
|
*
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
protected function showFormat($data)
|
|
{
|
|
$oss = new OssService();
|
|
if (isset($data['id'])){
|
|
$data['approve'] = InvRequisitionApprove::where('requisition_id', $data['id'])->get()->toArray();
|
|
foreach ($data['approve'] as $key => $item){
|
|
$attachment = InvRequisitionApproveAttachment::where('approve_id', $item['id'])->get();
|
|
foreach ($attachment as $k => $v){
|
|
$attachment[$k] = $v->toArray();
|
|
$attachment[$k]['file_path_pull'] = $oss->generatePrivateUrl($v['file_path']);
|
|
}
|
|
$data['approve'][$key]['attachment'] = $attachment;
|
|
}
|
|
$approve_id = !empty($data['approve'])?array_pop($data['approve'])['id']:null;
|
|
$data['approve_id'] = $approve_id;
|
|
}
|
|
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['id'])){
|
|
$data['item'] = InvRequisitionItem::where('requisition_id', $data['id'])->get();
|
|
$data['attachment'] = InvRequisitionAttachment::where('requisition_id', $data['id'])->get();
|
|
foreach ($data['attachment'] as $key => $value) {
|
|
$data['attachment'][$key]['file_path_pull'] = $oss->generatePrivateUrl($value['file_path']);
|
|
}
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 扫码领取物品
|
|
*
|
|
* @param Request $request
|
|
* @return mixed
|
|
*/
|
|
public function qrClaim(Request $request)
|
|
{
|
|
try {
|
|
$data = $this->service->qrClaim($request, $request->post());
|
|
return $this->success($data, '领取成功');
|
|
} catch (\Throwable $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 我的扫码/代领记录
|
|
*
|
|
* @param Request $request
|
|
* @return mixed
|
|
*/
|
|
public function myQrClaims(Request $request)
|
|
{
|
|
try {
|
|
// 只查询当前用户的扫码记录,通过设置 applicant_id=self 实现
|
|
$request->setGet('applicant_id','self');
|
|
$data = $this->service->qrClaimsList($request);
|
|
return $this->success($data);
|
|
} catch (\Throwable $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
} |