chore: 重写初始提交(清空历史,整理后全量提交)
This commit is contained in:
@@ -0,0 +1,231 @@
|
||||
<?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 app\member\controller;
|
||||
|
||||
use think\facade\Db;
|
||||
use think\Request;
|
||||
use ywxapp\controller\MemberBase;
|
||||
use addon\articles\model\Article as ArticleModel;
|
||||
use addon\articles\model\ArticleCategory as CategoryModel;
|
||||
/**
|
||||
* Article 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class Article extends MemberBase
|
||||
{
|
||||
|
||||
/**
|
||||
* Summary of needLogin
|
||||
* @var array
|
||||
*/
|
||||
protected $noNeedLogin = [];
|
||||
|
||||
/**
|
||||
* Summary of needRight
|
||||
* @var array
|
||||
*/
|
||||
protected $noNeedVerify = ['*'];
|
||||
|
||||
/**
|
||||
* 控制器初始化 initialize
|
||||
* @return void
|
||||
*/
|
||||
protected function initialize()
|
||||
{
|
||||
$this->model = new ArticleModel();
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示资源列表
|
||||
*
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function index($page = 1, $limit = 15)
|
||||
{
|
||||
$param = $this->request->param();
|
||||
$user = $this->user();
|
||||
//$this->result->success($user,$user->uid);
|
||||
$query = ArticleModel::with(['category'])
|
||||
->where('uid', $user->uid)
|
||||
->order('create_at', 'desc');
|
||||
if (! empty($param['title'])) {
|
||||
$query->whereLike('title', "%{$param['title']}%");
|
||||
}
|
||||
|
||||
if (! empty($param['cid'])) {
|
||||
$query->where('cid', $param['cid']);
|
||||
}
|
||||
|
||||
if (isset($param['status'])) {
|
||||
$query->where('status', $param['status']);
|
||||
}
|
||||
|
||||
$list = $query->page((int) $page, (int) $limit)->select()->toArray();
|
||||
$count = $query->count();
|
||||
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => 'success',
|
||||
'count' => $count,
|
||||
'data' => $list,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示创建资源表单页.
|
||||
*
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
try {
|
||||
$user = app()->auth->info;
|
||||
$data = ArticleModel::create([
|
||||
'title' => '未命名草稿',
|
||||
'uid' => $user->uid,
|
||||
'content' => '',
|
||||
'status' => 0, // 草稿状态
|
||||
'create_at' => time(),
|
||||
'update_at' => time(),
|
||||
]);
|
||||
$cates = CategoryModel::select();
|
||||
$this->result->success([
|
||||
'category' => $cates,
|
||||
'info' => $data,
|
||||
], '草稿创建成功');
|
||||
} catch (\Exception $e) {
|
||||
// 记录日志,但不要因为入库失败而中断上传流程(或者根据需求决定)
|
||||
\think\facade\Log::error($e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存新建的资源
|
||||
*
|
||||
* @param \think\Request $request
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function save(Request $request)
|
||||
{
|
||||
if ($this->request->isAjax() && $this->request->isPut()) {
|
||||
$param = $this->request->param();
|
||||
Db::startTrans();
|
||||
try {
|
||||
$info = $this->model->findOrEmpty($param['id']);
|
||||
if ($info->isEmpty()) {
|
||||
$this->result->error("数据获取错误!");
|
||||
}
|
||||
$info->save($param);
|
||||
Db::commit();
|
||||
$this->result->success($info, '数据修改成功!');
|
||||
} catch (\Exception $e) {
|
||||
// 回滚事务
|
||||
Db::rollback();
|
||||
$this->result->error('数据修改失败: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示指定的资源
|
||||
*
|
||||
* @param int $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function read($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示编辑资源表单页.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
if ($this->request->isAjax()) {
|
||||
$info = $this->model->findOrEmpty($id);
|
||||
if ($info->isEmpty()) {
|
||||
$this->result->error("数据获取错误!");
|
||||
}
|
||||
$cates = CategoryModel::select();
|
||||
$this->result->success([
|
||||
'category' => $cates,
|
||||
'info' => $info,
|
||||
], '数据创建成功');
|
||||
# code...
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存更新的资源
|
||||
*
|
||||
* @param int $id
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function update()
|
||||
{
|
||||
if ($this->request->isAjax() && $this->request->isPut()) {
|
||||
$param = $this->request->param();
|
||||
Db::startTrans();
|
||||
try {
|
||||
$info = $this->model->findOrEmpty($param['id']);
|
||||
if ($info->isEmpty()) {
|
||||
$this->result->error("数据获取错误!");
|
||||
}
|
||||
$info->save($param);
|
||||
Db::commit();
|
||||
$this->result->success($info, '数据修改成功!');
|
||||
} catch (\Exception $e) {
|
||||
// 回滚事务
|
||||
Db::rollback();
|
||||
$this->result->error('数据修改失败: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
*
|
||||
* @param \think\Request $request
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
if ($this->request->isAjax() && $this->request->isDelete()) {
|
||||
$ids = $this->request->param('ids', '');
|
||||
$force = $this->request->param('force', false);
|
||||
if (empty($ids)) {
|
||||
$this->result->error('请选择要删除的数据');
|
||||
}
|
||||
try {
|
||||
Db::transaction(function () use ($ids, $force) {
|
||||
if ($force) {
|
||||
ArticleModel::onlyTrashed()->whereIn('id', $ids)->select()->each(function ($item) {
|
||||
$item->force()->delete();
|
||||
});
|
||||
} else {
|
||||
ArticleModel::destroy($ids);
|
||||
}
|
||||
});
|
||||
$this->result->success();
|
||||
} catch (\think\exception\HttpResponseException $e) {
|
||||
throw $e;
|
||||
} catch (\Throwable $th) {
|
||||
$this->result->error('删除失败: ' . $th->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user