model = new ApplyModel(); } /** * 查看 */ public function index() { if (!$this->request->isAjax()) { return $this->view->fetch(); } if ($this->request->request('keyField')) { return $this->selectpage(); } [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $list = $this->model ->with(['user', 'identity', 'province', 'city', 'district', 'admin']) ->where($where) ->order($sort, $order) ->paginate($limit); $result = ['total' => $list->total(), 'rows' => $list->items()]; return json($result); } /** * 详情 */ public function detail($ids = null) { $row = $this->model::with(['user', 'identity', 'province', 'city', 'district', 'admin']) ->where('id', $ids) ->find(); if (!$row) { $this->error(__('No Results were found')); } $this->view->assign('row', $row); return $this->view->fetch(); } /** * 审核通过 */ public function approve($ids = null) { if (!$this->request->isPost()) { $this->error(__('Invalid parameters')); } $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } if ($row->status != ApplyModel::STATUS_PENDING) { $this->error('该申请已经处理过了'); } Db::transaction(function () use ($row) { $service = new AgentApplyService(); $service->approveApply($row, $this->auth->id); }); $this->success('审核通过成功'); } /** * 审核拒绝 */ public function reject($ids = null) { if (!$this->request->isPost()) { $this->error(__('Invalid parameters')); } $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } if ($row->status != ApplyModel::STATUS_PENDING) { $this->error('该申请已经处理过了'); } $reason = $this->request->post('reason', ''); if (empty($reason)) { $this->error('请填写拒绝原因'); } $service = new AgentApplyService(); $service->rejectApply($row, $reason, $this->auth->id); $this->success('审核拒绝成功'); } /** * 删除 */ public function del($ids = null) { if (!$this->request->isPost()) { $this->error(__('Invalid parameters')); } if (!$ids) { $this->error(__('Parameter %s can not be empty', 'ids')); } $ids = explode(',', $ids); $deleteCount = 0; Db::transaction(function () use ($ids, &$deleteCount) { foreach ($ids as $id) { $row = $this->model->get($id); if ($row) { // 只允许删除待审核和已拒绝的申请 //if (in_array($row->status, [ApplyModel::STATUS_PENDING, ApplyModel::STATUS_REJECTED])) { $row->delete(); $deleteCount++; //} } } }); if ($deleteCount > 0) { $this->success("成功删除 {$deleteCount} 条申请记录"); } else { $this->error('没有可删除的记录(只能删除待审核和已拒绝的申请)'); } } /** * 批量审核通过 */ public function multi($ids = null) { if (!$this->request->isPost()) { $this->error(__('Invalid parameters')); } $action = $this->request->post('action'); if ($action == 'approve') { // 批量审核通过 if (!$ids) { $this->error(__('Parameter %s can not be empty', 'ids')); } $ids = explode(',', $ids); $service = new AgentApplyService(); Db::transaction(function () use ($ids, $service) { foreach ($ids as $id) { $row = $this->model->get($id); if ($row && $row->status == ApplyModel::STATUS_PENDING) { $service->approveApply($row, $this->auth->id); } } }); $this->success('批量审核成功'); } elseif ($action == 'del') { // 批量删除 return $this->del($ids); } else { return parent::multi($ids); } } }