// +---------------------------------------------------------------------- declare(strict_types=1); namespace addon\haonav\controller\backend; use think\facade\Db; use addon\haonav\model\Apply as ApplyModel; use addon\haonav\model\Links as LinksModel; use addon\haonav\model\Category as CategoryModel; /** * 友链/广告 申请审核 * 通过友链申请时可指定分类并一键写入 Links(默认待审核状态可直接上架)。 */ class Apply extends HaonavBackend { protected $noNeedVerify = ['*']; protected function initialize() { $this->model = new ApplyModel(); } public function index() { if ($this->request->isAjax()) { $type = $this->request->param('type', ''); $status = $this->request->param('status', ''); $page = $this->request->param('page/d', 1); $limit = $this->request->param('limit/d', 20); $data = $this->model ->when($type !== '', fn($q, $v) => $q->where('type', $type)) ->when($status !== '', fn($q, $v) => $q->where('status', $status)) ->order('status', 'asc')->order('id', 'desc') ->paginate(['page' => $page, 'list_rows' => $limit]); $this->result->setCount($data->total()); $this->result->success($data->items()); } return $this->view->fetch('apply/index'); } /** * 表单辅助数据:分类列表(通过友链时选分类) */ public function create() { if ($this->request->isAjax()) { $cates = CategoryModel::where('status', 1) ->order('sort', 'desc') ->field('id,title') ->select() ->toArray(); $this->result->success($cates); } } /** * 通过申请 * 友链(type=1):可带 cid(分类)与 online(1=直接上架 0=进待审核),自动写入 Links 并回填 link_id * 广告(type=2):仅标记通过,商务细节线下沟通后在「广告管理」建广告 */ public function approve() { if (! ($this->request->isAjax() && $this->request->isPost())) { return $this->result->error('请求方式错误'); } $id = (int)$this->request->param('id'); $reply = (string)$this->request->param('reply', ''); $model = $this->model->find($id); if (! $model) { return $this->result->error('数据不存在'); } if ((int)$model->status === ApplyModel::STATUS_APPROVED) { return $this->result->error('该申请已通过,请勿重复操作'); } Db::startTrans(); try { $linkId = 0; if ((int)$model->type === ApplyModel::TYPE_LINK) { $cid = (int)$this->request->param('cid/d', 0); $online = (int)$this->request->param('online/d', 1); if ($cid <= 0) { throw new \think\exception\ValidateException('请选择收录分类'); } // 已有同 URL 链接则复用,避免重复收录 $exists = LinksModel::where('url', $model->url)->find(); if ($exists) { $linkId = (int)$exists->id; } else { $link = new LinksModel(); $link->cid = $cid; $link->title = $model->title; $link->url = $model->url; $link->description = (string)$model->description; $link->status = $online ? 1 : 2; // 1上架 2待审核 $link->save(); $linkId = (int)$link->id; } } $model->status = ApplyModel::STATUS_APPROVED; $model->reply = mb_substr($reply, 0, 255); $model->link_id = $linkId; $model->save(); Db::commit(); $this->result->success([], '已通过' . ($linkId ? ',并已收录(links#' . $linkId . ')' : '')); } catch (\Exception $e) { Db::rollback(); $this->result->error('操作失败: ' . $e->getMessage()); } } /** * 拒绝申请(可填审核备注) */ public function reject() { if (! ($this->request->isAjax() && $this->request->isPost())) { return $this->result->error('请求方式错误'); } $id = (int)$this->request->param('id'); $reply = (string)$this->request->param('reply', ''); $model = $this->model->find($id); if (! $model) { return $this->result->error('数据不存在'); } $model->status = ApplyModel::STATUS_REJECTED; $model->reply = mb_substr($reply, 0, 255); $model->save(); $this->result->success([], '已拒绝'); } public function delete() { $ids = (string)$this->request->param('ids', ''); $arr = array_values(array_filter(array_map('intval', explode(',', $ids)))); if (empty($arr)) { return $this->result->error('请选择数据'); } $this->model->whereIn('id', $arr)->delete(); $this->result->success([], '已删除'); } }