Files
xianyan/docs/toolsapi/application/admin/controller/NoteSpace.php
Developer b6441a8919 api
2026-04-27 23:47:18 +08:00

139 lines
4.3 KiB
PHP

<?php
/**
* @name 用户笔记空间管理
* @author AI Coder
* @date 2026-04-27
* @desc 管理员管理用户笔记数量上限和空间配额
* @update 初始创建
*/
namespace app\admin\controller;
use app\common\controller\Backend;
use think\Db;
class NoteSpace extends Backend
{
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = Db::name('user');
}
public function index()
{
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax()) {
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = Db::name('user')
->alias('u')
->where($where)
->field('u.id,u.username,u.nickname,u.note_limit,u.note_space,u.note_used_space,u.score')
->order($sort, $order)
->paginate($limit);
foreach ($list as &$row) {
$row['note_count'] = Db::name('user_note')
->where('user_id', $row['id'])
->where('status', 'normal')
->count();
$row['space_percent'] = $row['note_space'] > 0
? round($row['note_used_space'] / $row['note_space'] * 100, 1)
: 0;
$row['note_space_mb'] = round($row['note_space'] / 1024, 1);
$row['note_used_mb'] = round($row['note_used_space'] / 1024, 1);
}
$result = array("total" => $list->total(), "rows" => $list->items());
return json($result);
}
return $this->view->fetch();
}
public function edit($ids = null)
{
$row = Db::name('user')->where('id', $ids)->find();
if (!$row) {
$this->error(__('No Results were found'));
}
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if (!$params) {
$this->error(__('Parameter %s can not be empty', ''));
}
$data = [];
if (isset($params['note_limit'])) {
$data['note_limit'] = max(0, intval($params['note_limit']));
}
if (isset($params['note_space'])) {
$data['note_space'] = max(0, intval($params['note_space']));
}
if (!empty($data)) {
Db::name('user')->where('id', $ids)->update($data);
}
$this->success();
}
$row['note_count'] = Db::name('user_note')
->where('user_id', $row['id'])
->where('status', 'normal')
->count();
$row['space_percent'] = $row['note_space'] > 0
? round($row['note_used_space'] / $row['note_space'] * 100, 1)
: 0;
$row['note_space_mb'] = round($row['note_space'] / 1024, 1);
$row['note_used_mb'] = round($row['note_used_space'] / 1024, 1);
$this->view->assign('row', $row);
return $this->view->fetch();
}
public function recalculate($ids = null)
{
if (!$ids) $this->error('参数错误');
$notes = Db::name('user_note')
->where('user_id', $ids)
->where('status', 'normal')
->select();
$totalSize = 0;
foreach ($notes as $note) {
$totalSize += strlen($note['content'] ?? '') + strlen($note['title'] ?? '');
}
Db::name('user')->where('id', $ids)->update(['note_used_space' => $totalSize]);
$this->success('重新计算完成', null, [
'note_count' => count($notes),
'used_space' => $totalSize,
'used_space_mb' => round($totalSize / 1024, 1),
]);
}
public function batch_set()
{
$ids = $this->request->post('ids', '');
$noteLimit = $this->request->post('note_limit', 0, 'intval');
$noteSpace = $this->request->post('note_space', 0, 'intval');
if (empty($ids)) $this->error('请选择用户');
$idArr = explode(',', $ids);
$data = [];
if ($noteLimit > 0) $data['note_limit'] = $noteLimit;
if ($noteSpace > 0) $data['note_space'] = $noteSpace;
if (empty($data)) $this->error('请设置参数');
Db::name('user')->where('id', 'in', $idArr)->update($data);
$this->success('批量设置成功');
}
}