Files
YwxAppThink/addon/blog/controller/backend/Category.php
T
yangwx 18101cf11a feat: 上线前安全加固与发布工程化;appmall 由插件迁移为独立应用
安全(P0)
- 发布包签名:新增 scripts/package_keygen.php、scripts/package_verify.php、
  ywxapp\service\PackageVerifier;FrameworkService::apply() 解压前强制验签
  (ywxapp.framework_package_verify,默认开启)
- JWT 收紧:config/jwt.php 移除公开默认密钥,JwtService::assertStrongSecret()
  校验非空 / 长度≥32 / 已知弱值;.env.production.example 键名由 JWT_SECRET_KEY 修正为 JWT_SECRET
- /api/v1/Article:免登录白名单收窄为只读接口,写操作字段白名单 + assertOwner() 越权校验
- 开发者令牌:查找接口不再回显明文、统一失败文案 + 限流
- TemplateInstaller:文件名白名单 + realpath 归一化 + 目录内断言,修复 Zip Slip;补 use ZipArchive
- 后台登录:接入验证码、失败锁定与登录限流(config/ratelimit.php)
- 缓存 / Session 驱动可由 .env 覆盖(CACHE_DRIVER / SESSION_TYPE),集群部署改 redis
- paycenter 支付日志开关改为 PAY_LOG,不再跟随 APP_DEBUG

工程化 / 交付
- 新增 CHANGELOG.md 与 .env.production.example;composer.lock 入库锁定依赖版本
- 插件离线安装 CLI 增加 --uid,后台上传安装透传 uid,付费插件授权校验可通过
- 授权拒绝文案区分「未携带 uid」与「未找到有效授权」,便于排障
- .gitignore 放行 .env.production.example / .env.addon.example / .gitattributes / public/.htaccess

结构
- appmall 由 addon/appmall 迁移为独立应用 app/appmall(市场 / 开发者中心 / 审核 / 分润)
2026-10-22 00:53:39 +08:00

218 lines
7.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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\backend;
use addon\blog\model\BlogCategory as CategoryModel;
use think\facade\Request;
use think\facade\View;
use think\Validate;
use ywxapp\controller\BackendBase;
/**
* Category 类
*
* @author ywxapp <admin@ywxapp.cn>
*/
class Category extends BackendBase
{
protected $noNeedVerify = [];
protected function initialize()
{}
/**
* 分类列表(后台管理全部分类:全局共享 + 各会员自建)
* 以 layui treeTable 所需的扁平结构返回(带 pid / is_parent),由前端 treeTable 自动成树。
*/
public function index()
{
$categories = CategoryModel::withCount('articles')
->order('uid', 'asc')
->order('sort', 'asc')
->select()
->toArray();
// 收集归属会员昵称
$uids = [];
foreach ($categories as $c) {
if (!empty($c['uid'])) {
$uids[] = $c['uid'];
}
}
$owners = [];
if ($uids) {
$owners = \think\facade\Db::name('member_user')
->whereIn('uid', array_unique($uids))
->column('nickname', 'uid');
}
// 计算哪些分类拥有子分类(用于 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,
'owner' => empty($c['uid']) ? '全局共享' : ($owners[$c['uid']] ?? ('#' . $c['uid'])),
'articles_count' => $c['articles_count'] ?? 0,
];
}
return View::fetch('category/index', [
'treeData' => json_encode($treeData, JSON_UNESCAPED_UNICODE | JSON_HEX_TAG),
]);
}
/**
* 添加分类页面
*/
public function add()
{
$categories = CategoryModel::order('sort', 'asc')->select();
return View::fetch('category/add', ['categories' => $categories]);
}
/**
* 保存分类
*/
public function save()
{
$data = Request::post();
$data['uid'] = 0; // 后台管理的分类为全局共享分类
$data['pid'] = (int) Request::post('pid', 0);
$data['create_at'] = time();
$data['update_at'] = time();
$validate = new Validate([
'name|分类名称' => 'require|unique:BlogCategory,name,0,id,uid,0',
'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' => (string) url('blog/backend/category')]);
}
/**
* 编辑分类页面
*/
public function edit($id = null)
{
$category = CategoryModel::find($id);
$categories = CategoryModel::order('sort', 'asc')->select();
return View::fetch('category/edit', ['category' => $category, 'categories' => $categories]);
}
/**
* 更新分类
*/
public function update($id)
{
$category = CategoryModel::find($id);
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,' . $category->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' => (string) url('blog/backend/category')]);
}
/**
* 删除分类
*/
public function delete($id)
{
$category = CategoryModel::find($id);
if ($category) {
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' => '分类已删除']);
}
return json(['code' => 0, 'msg' => '分类不存在']);
}
/**
* 批量删除分类(前端 treeTable 勾选后调用)
*/
public function batchDelete()
{
$ids = Request::post('ids', '');
$idArr = array_filter(array_map('intval', explode(',', (string) $ids)));
if (empty($idArr)) {
return json(['code' => 0, 'msg' => '请选择要删除的分类']);
}
$deleted = 0;
$skipped = 0;
foreach ($idArr as $id) {
$category = CategoryModel::find($id);
if (! $category) {
continue;
}
if ($category->children()->count() > 0) {
$skipped++;
continue;
}
if ($category->articles()->count() > 0) {
$skipped++;
continue;
}
$category->delete();
$deleted++;
}
if ($deleted === 0) {
return json(['code' => 0, 'msg' => '选中的分类均包含子分类或文章,无法删除']);
}
$msg = '成功删除 ' . $deleted . ' 个分类';
if ($skipped > 0) {
$msg .= ',' . $skipped . ' 个因含子分类/文章已跳过';
}
return json(['code' => 1, 'msg' => $msg]);
}
}