model = new InspectionApplication; $this->view->assign("auditStatusList", $this->model->getAuditStatusList()); } /** * 查看列表 */ public function index() { if ($this->request->isAjax()) { // 设置关联查询 $this->relationSearch = true; // 构建查询条件 list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model ->with(['user', 'supplier']) ->where($where) ->order($sort, $order) ->paginate($limit); foreach ($list as $item) { $item->append(['audit_status_text', 'apply_time_text', 'audit_time_text']); $item->user->visible(['id', 'username','avatar','nickname', 'mobile']); if ($item->supplier) { $item->supplier->visible(['id', 'name', 'contact', 'mobile']); } } $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } /** * 查看详情 */ public function detail($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $row->append(['audit_status_text', 'apply_time_text', 'audit_time_text','supplier_name']); $this->view->assign("row", $row); return $this->view->fetch(); } /** * 编辑申请 */ public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } // 只有审核中的申请才可以编辑 if ($row->audit_status != InspectionApplication::AUDIT_STATUS_PENDING) { $this->error('只有审核中的申请才可以编辑'); } if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { // 验证必填字段 if (empty($params['name']) || empty($params['phone']) || empty($params['address'])) { $this->error('姓名、手机号、详细地址不能为空'); } // 检查手机号是否被其他申请使用 if (InspectionApplication::isPhoneApplied($params['phone'], $ids)) { $this->error('该手机号已被其他用户申请'); } $params = $this->preExcludeFields($params); // 过滤不允许编辑的字段 $allowedFields = ['name', 'phone', 'province_adcode', 'city_adcode', 'district_adcode', 'address', 'id_card', 'id_card_front', 'id_card_back']; $params = array_intersect_key($params, array_flip($allowedFields)); if ($row->allowField($allowedFields)->save($params) !== false) { $this->success(); } else { $this->error(__('No rows were updated')); } } $this->error(__('Parameter %s can not be empty', '')); } $row->append(['audit_status_text', 'apply_time_text']); $this->view->assign("row", $row); return $this->view->fetch(); } /** * 审核申请 */ public function audit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } if ($row->audit_status != InspectionApplication::AUDIT_STATUS_PENDING) { $this->error('该申请已被审核,无法重复操作'); } if ($this->request->isPost()) { $auditStatus = $this->request->post('audit_status'); $rejectReason = $this->request->post('reject_reason', '', 'trim'); $supplierId = $this->request->post('supplier_id', 0, 'intval'); // 审核通过时必须选择供应商 if ($auditStatus == InspectionApplication::AUDIT_STATUS_PASSED && empty($supplierId)) { $this->error('审核通过时必须选择绑定的供应商'); } $result = InspectionService::auditApplication($ids, $auditStatus, $rejectReason, $supplierId); if ($result) { $this->success('审核成功'); } else { $this->error('审核失败'); } } $row->append(['audit_status_text', 'apply_time_text']); // 加载供应商信息 if ($row->supplier_id) { $row->load(['supplier']); } $this->view->assign("row", $row); $this->view->assign("auditStatusList", $this->model->getAuditStatusList()); return $this->view->fetch(); } /** * 批量审核通过 */ public function batchPass($ids = null) { if (!$this->request->isPost()) { $this->error(__("Invalid parameters")); } $ids = $ids ? $ids : $this->request->post("ids"); if (!$ids) { $this->error(__('Parameter %s can not be empty', 'ids')); } $pk = $this->model->getPk(); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $this->model->where($this->dataLimitField, 'in', $adminIds); } $list = $this->model->where($pk, 'in', $ids)->select(); $count = 0; $successIds = []; \think\Db::startTrans(); try { foreach ($list as $item) { if ($item->audit_status == InspectionApplication::AUDIT_STATUS_PENDING) { $item->audit_status = InspectionApplication::AUDIT_STATUS_PASSED; $item->audit_time = time(); $item->reject_reason = ''; $item->save(); $count++; $successIds[] = $item->id; } } \think\Db::commit(); } catch (\Exception $e) { \think\Db::rollback(); $this->error($e->getMessage()); } $this->success("成功审核通过{$count}个申请"); } /** * 批量审核驳回 */ public function batchReject($ids = null) { if (!$this->request->isPost()) { $this->error(__("Invalid parameters")); } $ids = $ids ? $ids : $this->request->post("ids"); $rejectReason = $this->request->post("reject_reason", '', 'trim'); if (!$ids) { $this->error(__('Parameter %s can not be empty', 'ids')); } if (empty($rejectReason)) { $this->error('驳回原因不能为空'); } $pk = $this->model->getPk(); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $this->model->where($this->dataLimitField, 'in', $adminIds); } $list = $this->model->where($pk, 'in', $ids)->select(); $count = 0; \think\Db::startTrans(); try { foreach ($list as $item) { if ($item->audit_status == InspectionApplication::AUDIT_STATUS_PENDING) { $item->audit_status = InspectionApplication::AUDIT_STATUS_REJECTED; $item->audit_time = time(); $item->reject_reason = $rejectReason; $item->save(); $count++; } } \think\Db::commit(); } catch (\Exception $e) { \think\Db::rollback(); $this->error($e->getMessage()); } $this->success("成功驳回{$count}个申请"); } /** * 删除 */ public function del($ids = "") { if (!$this->request->isPost()) { $this->error(__("Invalid parameters")); } $ids = $ids ? $ids : $this->request->post("ids"); if ($ids) { $pk = $this->model->getPk(); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $this->model->where($this->dataLimitField, 'in', $adminIds); } $list = $this->model->where($pk, 'in', $ids)->select(); $count = 0; \think\Db::startTrans(); try { foreach ($list as $k => $v) { $count += $v->delete(); } \think\Db::commit(); } catch (\PDOException $e) { \think\Db::rollback(); $this->error($e->getMessage()); } catch (\Exception $e) { \think\Db::rollback(); $this->error($e->getMessage()); } if ($count) { $this->success(); } else { $this->error(__('No rows were deleted')); } } $this->error(__('Parameter %s can not be empty', 'ids')); } }