// +---------------------------------------------------------------------- declare (strict_types = 1); namespace addon\blog\controller\member; use addon\blog\model\BlogArticle as ArticleModel; use addon\blog\model\BlogCategory as CategoryModel; use addon\blog\model\BlogTag as TagModel; use think\facade\Request; use think\facade\View; use think\Validate; use ywxapp\controller\MemberBase; /** * 会员中心文章管理(多用户模式:文章由会员在用户中心发布/管理) * 继承 AddonMember,处于 route/app.php 的 member 路由组中,强制会员登录。 * 文章作者固定为当前登录会员(uid = $this->auth->info->id)。 */ class Article extends MemberBase { protected $noNeedLogin = []; protected $noNeedVerify = ['*']; // 当前登录会员 id(文章作者);会员主键为 uid,需从模型取 uid protected function uid() { return $this->auth->model->uid; } /** * 我的文章列表(ajax 请求返回 layui table 所需 JSON,普通请求渲染视图) */ public function index() { $status = Request::param('status', 'all'); $keyword = Request::param('keyword', ''); $page = Request::param('page', 1); $limit = Request::param('limit', 15); if (Request::isAjax()) { $query = ArticleModel::with(['category', 'tags'])->where('uid', $this->uid()); if ($status !== 'all') { $query->where('status', $status); } if ($keyword) { $query->where('title|content', 'like', "%{$keyword}%"); } $paginator = $query->order('create_at', 'desc') ->paginate(['list_rows' => $limit, 'page' => $page]); $rows = []; foreach ($paginator->items() as $a) { $rows[] = [ 'id' => $a->id, 'title' => $a->title, 'is_top' => $a->is_top, 'category' => $a->category->name ?? '-', 'status' => $a->status, 'view_count' => (int) $a->view_count, 'comment_count' => (int) $a->comment_count, 'like_count' => (int) $a->like_count, 'favorite_count' => (int) $a->favorite_count, 'share_count' => (int) $a->share_count, 'create_at' => $a->create_at, 'create_text' => (function ($t) { if ($t instanceof \DateTime) return $t->format('Y-m-d H:i'); if (is_numeric($t)) return date('Y-m-d H:i', (int) $t); $ts = strtotime((string) $t); return $ts !== false ? date('Y-m-d H:i', $ts) : (string) $t; })($a->create_at), 'detail_url' => (string) url('index/article/detail', ['id' => $a->id]), 'edit_url' => (string) url('blog/member.article/edit', ['id' => $a->id]), ]; } return json([ 'code' => 0, 'msg' => '', 'count' => $paginator->total(), 'data' => $rows, ]); } return View::fetch('article/index', [ 'status' => $status, 'keyword' => $keyword, ]); } /** * 写文章页面 */ public function create() { $categories = CategoryModel::getAvailable($this->uid()); $tags = TagModel::select(); return View::fetch('article/create', [ 'categories' => $categories, 'tags' => $tags, ]); } /** * 保存文章(作者为当前会员,不可指定) */ public function save() { $data = Request::post(); $data['uid'] = $this->uid(); // 多用户:作者固定为当前会员 $data['lat'] = (float) Request::post('lat', 0); $data['lng'] = (float) Request::post('lng', 0); unset($data['user_id'], $data['tags']); $validate = new Validate([ 'title|标题' => 'require|max:255', 'content|内容' => 'require', 'cid|分类' => 'require|number', ]); if (! $validate->check($data)) { return json(['code' => 0, 'msg' => $validate->getError()]); } $tagIds = $this->parseTags(Request::post('tags')); // 内容审核:开启 need_audit 时进入审核流(机审 + 人工复核) $needAudit = (int) config('blog.need_audit'); $auditStatus = 2; // 默认通过(2=通过) $auditReason = ''; if ($needAudit) { $ai = new \addon\wxchat\service\AiAuditService(); $res = $ai->audit(($data['title'] ?? '') . "\n" . ($data['content'] ?? '')); if ($res['code'] === 0 && !empty($res['data']['blocked'])) { $auditStatus = 3; // 机审命中违规:直接驳回 $auditReason = '机审不通过:' . ($res['data']['reason'] ?? '内容违规'); } elseif ($res['code'] === 0) { $auditStatus = 1; // 机审通过:进入待审,等待人工复核 } // AI 服务不可用:维持待审,转人工队列 } $data['status'] = ($auditStatus === 2) ? 1 : 0; // 仅通过态对外可见 $data['audit_status'] = $auditStatus; \addon\blog\library\BlogSchema::ensure(); $article = ArticleModel::create($data); $article->tags()->sync($tagIds); \addon\blog\service\BlogSearchService::sync($article->id); if ($auditStatus === 3) { return json(['code' => 0, 'msg' => $auditReason, 'url' => '/blog/member/article']); } if ($needAudit && $auditStatus === 1) { return json(['code' => 1, 'msg' => '发布成功,等待审核', 'url' => '/blog/member/article']); } return json(['code' => 1, 'msg' => '文章发布成功', 'url' => '/blog/member/article']); } /** * 编辑文章页面(仅能编辑自己的文章) */ public function edit($id = null) { $article = ArticleModel::with(['tags']) ->where('id', $id) ->where('uid', $this->uid()) ->find(); if (! $article) { return redirect('/blog/member/article'); } $categories = CategoryModel::getAvailable($this->uid()); $tags = TagModel::select(); $articleTagsStr = implode(',', $article->tags->column('name')); return View::fetch('article/edit', [ 'article' => $article, 'categories' => $categories, 'tags' => $tags, 'articleTagsStr' => $articleTagsStr, ]); } /** * 更新文章(仅能更新自己的文章,作者不可改) */ public function update($id) { $article = ArticleModel::where('id', $id) ->where('uid', $this->uid()) ->find(); if (! $article) { return json(['code' => 0, 'msg' => '文章不存在或无权限']); } $data = Request::post(); $data['lat'] = (float) Request::post('lat', $article->lat ?? 0); $data['lng'] = (float) Request::post('lng', $article->lng ?? 0); unset($data['uid'], $data['user_id'], $data['tags'], $data['tag_names'], $data['id']); $validate = new Validate([ 'title|标题' => 'require|max:255', 'content|内容' => 'require', 'cid|分类' => 'require|number', ]); if (! $validate->check($data)) { return json(['code' => 0, 'msg' => $validate->getError()]); } $tagIds = $this->parseTags(Request::post('tags')); $article->save($data); $article->tags()->sync($tagIds); \addon\blog\service\BlogSearchService::sync($article->id); return json(['code' => 1, 'msg' => '文章更新成功', 'url' => '/blog/member/article']); } /** * 删除文章(仅能删除自己的文章) */ public function delete($id) { $article = ArticleModel::where('id', $id) ->where('uid', $this->uid()) ->find(); if ($article) { \addon\blog\service\BlogSearchService::remove($article->id); $article->tags()->detach(); $article->delete(); return json(['code' => 1, 'msg' => '文章已删除']); } return json(['code' => 0, 'msg' => '文章不存在或无权限']); } /** * 批量操作(仅限自己的文章) */ public function batch() { $action = Request::post('action'); $ids = Request::post('ids/a'); if (empty($ids)) { return json(['code' => 0, 'msg' => '请选择文章']); } switch ($action) { case 'delete': $list = ArticleModel::whereIn('id', $ids)->where('uid', $this->uid())->select(); foreach ($list as $a) { $a->tags()->detach(); $a->delete(); } return json(['code' => 1, 'msg' => '批量删除成功']); case 'publish': ArticleModel::whereIn('id', $ids)->where('uid', $this->uid())->update(['status' => 1]); return json(['code' => 1, 'msg' => '批量发布成功']); case 'draft': ArticleModel::whereIn('id', $ids)->where('uid', $this->uid())->update(['status' => 0]); return json(['code' => 1, 'msg' => '批量设为草稿成功']); default: return json(['code' => 0, 'msg' => '无效操作']); } } /** * 解析标签:逗号分隔的名称 -> 标签 id 数组(不存在则自动创建) */ protected function parseTags($raw) { $tagIds = []; if (empty($raw)) { return $tagIds; } $names = is_array($raw) ? $raw : explode(',', $raw); foreach ($names as $name) { $name = trim($name); if ($name === '') { continue; } $tag = TagModel::where('name', $name)->find() ?: TagModel::create(['name' => $name]); $tagIds[] = $tag->id; } return $tagIds; } }