2025-06-25 10:53:11 +08:00

360 lines
14 KiB
PHP

<?php
/**
* @copyright Copyright (c) 2023-2024 美天智能科技
* @link http://www.meteteme.com
*/
declare(strict_types=1);
namespace app\picbed\controller;
use app\base\BaseController;
use app\model\PicbedImages as PicbedList;
use app\model\PicbedFolder as PicbedFolderList;
use think\facade\Db;
use think\facade\View;
class Index extends BaseController
{
public function index()
{
if (request()->isAjax()) {
$param = get_params();
$folderId = $param['folder'] ?? null;
$query = PicbedList::withoutField('')
->where('delete_time', null);
// 如果 folderId 存在且不为 'all',则添加条件
if ($folderId === 'all') {
$query->whereNull('folder');
} elseif ($folderId) {
$query->where('folder', $folderId);
}
$list = $query->order('id desc')->cursor();
// 处理每个项
$items = [];
foreach ($list as $item) {
if ($item->delete_time !== null) {
continue;
}
$picbedImage = Db::name('PicbedImages')->where(['id' => $item->id])->find();
if (!ctype_digit($picbedImage['admin_id'])) {
$item->admin_name = $picbedImage['admin_id'];
} else {
$admin = Db::name('Admin')->where(['id' => $picbedImage['admin_id']])->find();
$item->admin_name = $admin ? $admin['name'] : '';
}
$item->path = $picbedImage['path'];
$item->size = $picbedImage['size'];
$item->type = $picbedImage['type'];
$item->new_name = $picbedImage['new_name'];
$item->create_time = $picbedImage['create_time'];
$item->update_time = $picbedImage['update_time'];
$item->delete_time = $picbedImage['delete_time'];
$items[] = $item; // 收集处理后的项
}
return json(['code' => 0, 'msg' => '', 'count' => count($items), 'data' => $items]);
} else {
return view();
}
}
//添加
public function add()
{
// 添加admin_id
$param['admin_id'] = $this->uid;
$param = get_params();
if (request()->isPost()) {
//markdown数据处理
if (isset($param['table-align'])) {
unset($param['table-align']);
}
if (isset($param['docContent-html-code'])) {
$param['content'] = $param['docContent-html-code'];
$param['md_content'] = $param['docContent-markdown-doc'];
unset($param['docContent-html-code']);
unset($param['docContent-markdown-doc']);
}
if (isset($param['ueditorcontent'])) {
$param['content'] = $param['ueditorcontent'];
$param['md_content'] = '';
}
if (!empty($param['id']) && $param['id'] > 0) {
$picbed = (new PicbedList())->detail($param['id']);
if ($this->uid == $picbed['admin_id']) {
try {
validate(PicbedCheck::class)->scene('edit')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$param['update_time'] = time();
$res = PicbedList::where('id', $param['id'])->strict(false)->field(true)->update($param);
if ($res) {
add_log('edit', $param['id'], $param, $picbed);
}
return to_assign();
} else {
return to_assign(1, '只有创建人或者负责人才有权限编辑');
}
} else {
try {
validate(PicbedCheck::class)->scene('add')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$param['create_time'] = time();
$param['admin_id'] = $this->uid;
$sid = PicbedList::strict(false)->field(true)->insertGetId($param);
if ($sid) {
add_log('add', $sid, $param);
$log_data = array(
'module' => 'picbed',
'id' => $sid,
'new_content' => $param['name'],
'field' => 'new',
'action' => 'add',
'admin_id' => $this->uid,
'create_time' => time(),
);
Db::name('Log')->strict(false)->field(true)->insert($log_data);
}
return to_assign();
}
} else {
$id = isset($param['id']) ? $param['id'] : 0;
if ($id > 0) {
$detail = (new PicbedList())->detail($id);
if (empty($detail)) {
return to_assign(1, '1.信息不存在');
}
View::assign('detail', $detail);
}
View::assign('id', $id);
return view();
}
}
//创建文件夹
public function createfolder()
{
$param = get_params();
if (request()->isPost()) {
$param['sort'] = $param['sort'] ?? 0; // 默认排序为 0
if (!empty($param['id']) && $param['id'] > 0) {
$createfolder = (new PicbedFolderList())->detail($param['id']);
$param['update_time'] = date('Y-m-d H:i:s', time());
$res = PicbedFolderList::where('id', $param['id'])->strict(false)->field(true)->update($param);
if ($res) {
add_log('edit', $param['id'], $param, $createfolder);
}
return to_assign();
} else {
$param['create_time'] = date('Y-m-d H:i:s', time());
$param['admin_id'] = $this->uid;
$sid = PicbedFolderList::strict(false)->field(true)->insertGetId($param);
return to_assign();
}
} else {
$id = isset($param['id']) ? $param['id'] : 0;
if ($id > 0) {
$detail = (new PicbedList())->detail($id);
if (empty($detail)) {
return to_assign(1, '1.信息不存在');
}
View::assign('detail', $detail);
}
View::assign('id', $id);
return view();
}
}
//移动图片到文件夹
public function movefolder()
{
$param = get_params();
if (request()->isPost()) {
// 支持批量移动
if (!empty($param['ids']) && is_array($param['ids'])) {
$folderId = $param['folder']; // 获取目标文件夹ID
foreach ($param['ids'] as $id) {
if ($id > 0) {
PicbedList::where('id', $id)->strict(false)->update(['folder' => $folderId]); // 更新每个图片的文件夹
}
}
return to_assign(); // 返回成功响应
}
} else {
$id = isset($param['id']) ? $param['id'] : 0;
if ($id > 0) {
$detail = (new PicbedList())->detail($id);
if (empty($detail)) {
return to_assign(1, '1.信息不存在');
}
View::assign('detail', $detail);
}
View::assign('id', $id);
return view();
}
}
//查看
public function view()
{
$param = get_params();
$id = isset($param['id']) ? $param['id'] : 0;
$detail = (new PicbedList())->detail($id);
// print_r($id);
if (empty($detail)) {
return to_assign(1, '2.信息不存在');
} else {
if ($this->uid != $detail['admin_id']) { // 当前登录用户必须是上传者才能查看
return to_assign(1, '您没权限查看该信息');
}
$role_uid = [$detail['admin_id'], $detail['director_uid']];
$role_edit = 'edit';
$view_uid = $role_uid;
// if (!empty($detail['check_admin_ids'])) {
// $view_uid = array_merge($role_uid, explode(",", $detail['check_admin_ids']));
// }
if (!in_array($this->uid, $view_uid) && $detail['is_open'] == 1) {
return to_assign(1, '您没权限查看该信息');
}
$contact_array = Db::name('Businesscontact')
->field('i.id,i.business_id,i.contact,i.post,i.phone,i.email,i.wechat,i.qq,a.name as admin_name')
->alias('i')
->join('Admin a', 'i.admin_id = a.id', 'LEFT')
->join('PicbedImages b', 'i.business_id = b.id', 'LEFT')
->order('i.create_time desc')
->where(array('i.business_id' => $id))
->where('i.delete_time', null)
->select()->toArray();
$file_array = Db::name('FileInterfix')
->field('i.id,i.topic_id,i.admin_id,f.name,f.filesize,f.filepath,a.name as admin_name')
->alias('i')
->join('File f', 'i.file_id = f.id', 'LEFT')
->join('Admin a', 'i.admin_id = a.id', 'LEFT')
->order('i.create_time desc')
->where(array('i.topic_id' => $id, 'i.module' => 'picbed'))
->select()->toArray();
$link_array = Db::name('LinkInterfix')
->field('i.id,i.topic_id,i.admin_id,i.desc,i.url,a.name as admin_name')
->alias('i')
->join('Admin a', 'i.admin_id = a.id', 'LEFT')
->order('i.create_time desc')
->where(array('i.topic_id' => $id, 'i.module' => 'picbed', 'delete_time' => 0))
->select()->toArray();
View::assign('detail', $detail);
View::assign('contact_array', $contact_array);
View::assign('file_array', $file_array);
View::assign('link_array', $link_array);
View::assign('role_edit', $role_edit);
View::assign('id', $id);
return view();
}
}
//删除
public function delete()
{
$request = request();
if ($request->isDelete() || $request->isAjax()) {
$id = get_params("id");
$detail = Db::name('PicbedImages')->where('id', $id)->find();
if ($detail['admin_id'] != $this->uid) {
return to_assign(1, "你不是图片的上传人,无权限删除");
}
if (Db::name('PicbedImages')->where('id', $id)->update(['delete_time' => time()]) !== false) {
$log_data = array(
'module' => 'picbed',
'field' => 'delete',
'action' => 'delete',
'admin_id' => $this->uid,
'old_content' => '',
'new_content' => $detail['name'],
'create_time' => time(),
);
Db::name('Log')->strict(false)->field(true)->insert($log_data);
return to_assign(0, "删除成功");
} else {
return to_assign(0, "删除失败");
}
} else {
return to_assign(1, "错误的请求");
}
}
//批量删除
public function batch_delete()
{
$request = request();
if ($request->isDelete() || $request->isAjax()) {
$ids = get_params("ids");
foreach ($ids as $id) {
$detail = Db::name('PicbedImages')->where('id', $id)->find();
if (Db::name('PicbedImages')->where('id', $id)->update(['delete_time' => date('Y-m-d H:i:s')]) !== false) {
$log_data = array(
'module' => 'PicbedImages',
'field' => 'delete',
'action' => 'delete',
'admin_id' => $this->uid,
'old_content' => '',
'new_content' => $detail['name'],
'create_time' => time(),
);
Db::name('Log')->strict(false)->field(true)->insert($log_data);
}
}
return to_assign(0, "批量删除成功");
} else {
return to_assign(1, "错误的请求");
}
}
//获取文件夹目录
public function getpicbedfolder()
{
$folder = PicbedFolderList::field('id, admin_id, name, sort')
->where('delete_time', null)
->where('status', '<>', 2)
->select();
return json(['code' => 0, 'msg' => '', 'data' => $folder]);
}
// 获取对应文件夹内文件列表
public function getfloderfiles()
{
$param = get_params();
// 使用 getpicbedfolder 获取的 id 作为 folder_id
$folderId = $param['folder_id'] ?? null;
if ($folderId) {
$files = PicbedList::where('folder', $folderId)
->where('delete_time', null)
->field('name') // 只获取 name 字段
->select()
->toArray(); // 转换为数组
return json(['code' => 0, 'msg' => '', 'data' => $files]);
} else {
return json(['code' => 1, 'msg' => '文件夹ID不能为空']);
}
}
}