- 18 张公共表更名(addon/attachment/configure/links/spider_log/spider_stat/sms/notice/ad/task/prop/medal/help/card -> common_*,member_wallets->member_wallet,score_rule/score_log -> member_*,addon_config->common_addonconf),模型全部对齐新表名,Db 直引用清零 - Attachment 模型补 表名绑定,修复富文本上传查 wxapp_attachment 1146 隐患 - install.sql + 迁移 SQL:backend_admin/backend_role delete_at 默认 0,修复软删除(NULL != 0)误过滤导致后台菜单为空 - AppService::boot() 支持插件 info.php 声明 commands 自动注册插件命令(psr-4 自动加载,坏类名自动跳过) - 各插件(haonav/mqttbroker/wxchat/articles/blog/forum 等)字段与配置同步调整 - 框架版本 1.1.0 -> 1.1.1
141 lines
4.4 KiB
PHP
141 lines
4.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace addon\download\controller\backend;
|
|
|
|
use think\exception\ValidateException;
|
|
use think\facade\Db;
|
|
use addon\download\model\Resource as ResourceModel;
|
|
use addon\download\model\Category as CategoryModel;
|
|
use ywxapp\controller\BackendBase;
|
|
class Resource extends BackendBase
|
|
{
|
|
protected $noNeedVerify = ['*'];
|
|
|
|
protected function initialize()
|
|
{
|
|
$this->model = new ResourceModel();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
if ($this->request->isAjax()) {
|
|
$title = $this->request->param('title', '');
|
|
$cid = $this->request->param('cid/d', 0);
|
|
$page = $this->request->param('page/d', 1);
|
|
$limit = $this->request->param('limit/d', 20);
|
|
$data = $this->model
|
|
->with(['category'])
|
|
->when($title, fn($q, $t) => $q->whereLike('title', "%{$t}%"))
|
|
->when($cid, fn($q, $c) => $q->where('cid', $c))
|
|
->order('id', 'desc')
|
|
->paginate(['page' => $page, 'list_rows' => $limit]);
|
|
$this->result->setCount($data->total());
|
|
$this->result->success($data->items());
|
|
}
|
|
$categories = (new CategoryModel())
|
|
->where('status', 1)
|
|
->order('sort', 'desc')
|
|
->select();
|
|
$this->view->assign('categories', $categories);
|
|
return $this->view->fetch('resource/index');
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
if ($this->request->isAjax()) {
|
|
$categories = (new CategoryModel())
|
|
->where('status', 1)
|
|
->order('sort', 'desc')
|
|
->select();
|
|
$this->result->success(['categories' => $categories]);
|
|
}
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
if ($this->request->isPost()) {
|
|
$params = $this->request->post();
|
|
$this->validateSave($params);
|
|
Db::startTrans();
|
|
try {
|
|
$this->model->save($params);
|
|
Db::commit();
|
|
$this->result->success($this->model, '保存成功');
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
$this->result->error('保存失败: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
public function edit($id = null)
|
|
{
|
|
$id = $this->request->param('id');
|
|
$model = $this->model->find($id);
|
|
if (!$model) {
|
|
$this->result->error('数据不存在');
|
|
}
|
|
if ($this->request->isAjax()) {
|
|
$categories = (new CategoryModel())
|
|
->where('status', 1)
|
|
->order('sort', 'desc')
|
|
->select();
|
|
$this->result->success(['categories' => $categories, 'info' => $model]);
|
|
}
|
|
}
|
|
|
|
public function update()
|
|
{
|
|
$id = $this->request->param('id');
|
|
if ($this->request->isAjax() && $this->request->isPut()) {
|
|
$params = $this->request->param();
|
|
$this->validateSave($params);
|
|
Db::startTrans();
|
|
try {
|
|
$model = $this->model->find($id);
|
|
if (!$model) {
|
|
throw new ValidateException('数据不存在');
|
|
}
|
|
$model->save($params);
|
|
Db::commit();
|
|
$this->result->success($model, '更新成功');
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
$this->result->error('更新失败: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
public function delete()
|
|
{
|
|
if ($this->request->isAjax() && $this->request->isDelete()) {
|
|
$ids = $this->request->param('ids', '');
|
|
if (empty($ids)) {
|
|
$this->result->error('请选择要删除的数据');
|
|
}
|
|
try {
|
|
Db::transaction(function () use ($ids) {
|
|
$this->model->destroy($ids);
|
|
});
|
|
$this->result->success();
|
|
} catch (\think\exception\HttpResponseException $e) {
|
|
throw $e;
|
|
} catch (\Throwable $th) {
|
|
$this->result->error('删除失败: ' . $th->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function validateSave(array $params): void
|
|
{
|
|
if (empty($params['title'])) {
|
|
throw new ValidateException('资源标题不能为空');
|
|
}
|
|
if (empty($params['cid'])) {
|
|
throw new ValidateException('请选择分类');
|
|
}
|
|
}
|
|
}
|