chore: 重写初始提交(清空历史,整理后全量提交)
This commit is contained in:
@@ -0,0 +1,294 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller\member;
|
||||
|
||||
use addon\blog\model\BlogCategory as CategoryModel;
|
||||
use think\facade\Request;
|
||||
use think\facade\View;
|
||||
use think\Validate;
|
||||
use ywxapp\controller\MemberBase;
|
||||
|
||||
/**
|
||||
* 会员中心「我的分类」管理(用户自建分类)
|
||||
* 每个会员只能管理自己 uid 下的分类;全局共享分类(uid=0)由后台管理。
|
||||
*/
|
||||
class Category extends MemberBase
|
||||
{
|
||||
protected $noNeedLogin = [];
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
// 当前登录会员 id(会员主键为 uid)
|
||||
|
||||
protected function uid()
|
||||
{
|
||||
return $this->auth->model->uid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 我的分类列表(仅当前会员自己的分类)
|
||||
* 以 layui treeTable 所需的扁平结构返回(带 pid / is_parent),由前端 treeTable 自动成树。
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$categories = CategoryModel::where('uid', $this->uid())
|
||||
->withCount('articles')
|
||||
->order('sort', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 计算哪些分类拥有子分类(用于 treeTable 的 is_parent 标记)
|
||||
$parentIds = array_unique(array_filter(array_column($categories, 'pid')));
|
||||
|
||||
$treeData = [];
|
||||
foreach ($categories as $c) {
|
||||
$treeData[] = [
|
||||
'id' => $c['id'],
|
||||
'pid' => $c['pid'],
|
||||
'title' => $c['name'], // treeTable 节点显示名称
|
||||
'name' => $c['name'],
|
||||
'description' => $c['description'] ?? '',
|
||||
'sort' => $c['sort'],
|
||||
'status' => $c['status'],
|
||||
'is_parent' => in_array($c['id'], $parentIds, true) ? 1 : 0,
|
||||
'articles_count' => $c['articles_count'] ?? 0,
|
||||
];
|
||||
}
|
||||
|
||||
return View::fetch('category/index', [
|
||||
'treeData' => json_encode($treeData, JSON_UNESCAPED_UNICODE | JSON_HEX_TAG),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加分类页面
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$categories = CategoryModel::where(function ($query) {
|
||||
$query->where('uid', 0)->whereOr('uid', $this->uid());
|
||||
})->order('sort', 'asc')->select();
|
||||
return View::fetch('category/create', ['categories' => $categories]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存分类(归属当前会员)
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
$data = Request::post();
|
||||
$data['uid'] = $this->uid();
|
||||
$data['pid'] = (int) Request::post('pid', 0);
|
||||
$data['status'] = 1;
|
||||
$data['create_at'] = time();
|
||||
$data['update_at'] = time();
|
||||
|
||||
$validate = new Validate([
|
||||
'name|分类名称' => 'require|unique:BlogCategory,name,0,id,uid,' . $this->uid(),
|
||||
'pid|上级分类' => 'number',
|
||||
'sort|排序' => 'number|between:0,999',
|
||||
]);
|
||||
if (! $validate->check($data)) {
|
||||
return json(['code' => 0, 'msg' => $validate->getError()]);
|
||||
}
|
||||
|
||||
CategoryModel::create($data);
|
||||
return json(['code' => 1, 'msg' => '分类添加成功', 'url' => '/blog/member/category']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑分类页面(仅自己的分类)
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
$category = CategoryModel::where('id', $id)
|
||||
->where('uid', $this->uid())
|
||||
->find();
|
||||
if (! $category) {
|
||||
return redirect('/blog/member/category');
|
||||
}
|
||||
|
||||
$categories = CategoryModel::where(function ($query) {
|
||||
$query->where('uid', 0)->whereOr('uid', $this->uid());
|
||||
})->order('sort', 'asc')->select();
|
||||
return View::fetch('category/edit', ['category' => $category, 'categories' => $categories]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新分类(仅自己的分类)
|
||||
*/
|
||||
public function update($id)
|
||||
{
|
||||
$category = CategoryModel::where('id', $id)
|
||||
->where('uid', $this->uid())
|
||||
->find();
|
||||
if (! $category) {
|
||||
return json(['code' => 0, 'msg' => '分类不存在或无权限']);
|
||||
}
|
||||
|
||||
if ((int) Request::post('pid', 0) === (int) $id) {
|
||||
return json(['code' => 0, 'msg' => '上级分类不能选择自己']);
|
||||
}
|
||||
|
||||
$data = Request::post();
|
||||
$data['pid'] = (int) Request::post('pid', 0);
|
||||
$data['update_at'] = time();
|
||||
|
||||
$validate = new Validate([
|
||||
'name|分类名称' => 'require|unique:BlogCategory,name,' . $id . ',id,uid,' . $this->uid(),
|
||||
'pid|上级分类' => 'number',
|
||||
'sort|排序' => 'number|between:0,999',
|
||||
]);
|
||||
if (! $validate->check($data)) {
|
||||
return json(['code' => 0, 'msg' => $validate->getError()]);
|
||||
}
|
||||
|
||||
$category->save($data);
|
||||
return json(['code' => 1, 'msg' => '分类更新成功', 'url' => '/blog/member/category']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除分类(仅自己的分类,有文章时禁止删除)
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$category = CategoryModel::where('id', $id)
|
||||
->where('uid', $this->uid())
|
||||
->find();
|
||||
if (! $category) {
|
||||
return json(['code' => 0, 'msg' => '分类不存在或无权限']);
|
||||
}
|
||||
if ($category->children()->count() > 0) {
|
||||
return json(['code' => 0, 'msg' => '该分类下有子分类,请先删除子分类']);
|
||||
}
|
||||
if ($category->articles()->count() > 0) {
|
||||
return json(['code' => 0, 'msg' => '该分类下有文章,无法删除']);
|
||||
}
|
||||
$category->delete();
|
||||
return json(['code' => 1, 'msg' => '分类已删除']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量操作(仅限自己的分类)
|
||||
*/
|
||||
public function batch()
|
||||
{
|
||||
$action = Request::post('action');
|
||||
$ids = Request::post('ids/a');
|
||||
if (empty($ids)) {
|
||||
return json(['code' => 0, 'msg' => '请选择分类']);
|
||||
}
|
||||
|
||||
if ($action === 'delete') {
|
||||
$list = CategoryModel::whereIn('id', $ids)
|
||||
->where('uid', $this->uid())
|
||||
->select();
|
||||
foreach ($list as $c) {
|
||||
if ($c->articles()->count() > 0) {
|
||||
continue; // 跳过有文章的分类
|
||||
}
|
||||
if (CategoryModel::where('pid', $c->id)->whereNotIn('id', $ids)->count() > 0) {
|
||||
continue; // 跳过有外部子分类的分类
|
||||
}
|
||||
$c->delete();
|
||||
}
|
||||
return json(['code' => 1, 'msg' => '批量删除成功']);
|
||||
}
|
||||
|
||||
return json(['code' => 0, 'msg' => '无效操作']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\controller\member;
|
||||
|
||||
use think\facade\View;
|
||||
use ywxapp\controller\MemberBase;
|
||||
|
||||
/**
|
||||
* 会员中心入口页(addon/blog/controller/member/Index.php)
|
||||
*
|
||||
* 继承 MemberBase,登录校验统一由 MemberBase::_initialize() 调用 Auth::verifyAuth 处理
|
||||
* (本控制器设 $noNeedLogin = [] 表示强制会员登录,未登录跳 /user/login)。
|
||||
* 容器 auth 默认即为 \ywxapp\library\Auth,故 $this->auth->info 即当前会员。
|
||||
*/
|
||||
class Index extends MemberBase
|
||||
{
|
||||
// 会员中心强制登录(由 MemberBase 的 verifyAuth 统一校验)
|
||||
protected $noNeedLogin = [];
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
|
||||
protected function initialize()
|
||||
{}
|
||||
|
||||
/**
|
||||
* 会员中心首页
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$user = $this->auth->info ?? null;
|
||||
|
||||
return View::fetch('index/index', [
|
||||
'member' => $user,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 个人资料
|
||||
*/
|
||||
public function profile()
|
||||
{
|
||||
$user = $this->auth->info ?? null;
|
||||
|
||||
return View::fetch('index/profile', [
|
||||
'member' => $user,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user