2025-06-25 11:52:01 +08:00

660 lines
23 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
/**
* @copyright Copyright (c) 2023-2024 美天智能科技
* @author 李志强
* @link http://www.meteteme.com
*/
declare(strict_types=1);
namespace app\api\controller;
use app\api\BaseController;
use think\facade\Db;
use think\Response;
use app\model\PicbedImages as PicbedList;
class Index extends BaseController
{
//上传文件
public function upload()
{
$param = get_params();
if (request()->file('file')) {
$file = request()->file('file');
} else {
return to_assign(1, '没有选择上传文件');
}
// dump($file);die;
// 获取上传文件的hash散列值
$sha1 = $file->hash('sha1');
$md5 = $file->hash('md5');
$rule = [
'image' => 'jpg,png,jpeg,gif,ai,psd',
'doc' => 'txt,doc,docx,ppt,pptx,xls,xlsx,pdf,xmind,drawio',
'file' => 'zip,gz,7z,rar,tar',
'video' => 'mpg,mp4,mpeg,avi,wmv,mov,flv,m4v',
];
$fileExt = $rule['image'] . ',' . $rule['doc'] . ',' . $rule['file'] . ',' . $rule['video'];
//1M=1024*1024=1048576字节
$fileSize = 100 * 1024 * 1024;
if (isset($param['type']) && $param['type']) {
$fileExt = $rule[$param['type']];
}
if (isset($param['size']) && $param['size']) {
$fileSize = $param['size'];
}
$validate = \think\facade\Validate::rule([
'image' => 'require|fileSize:' . $fileSize . '|fileExt:' . $fileExt,
]);
$file_check['image'] = $file;
if (!$validate->check($file_check)) {
return to_assign(1, $validate->getError());
}
// 日期前綴
$dataPath = date('Ym');
$use = 'thumb';
$filename = \think\facade\Filesystem::disk('public')->putFile($dataPath, $file, function () use ($md5) {
return $md5;
});
if ($filename) {
//写入到附件表
$data = [];
$path = get_config('filesystem.disks.public.url');
$data['filepath'] = $path . '/' . $filename;
$data['name'] = $file->getOriginalName();
$data['mimetype'] = $file->getOriginalMime();
$data['fileext'] = $file->extension();
$data['filesize'] = $file->getSize();
$data['filename'] = $filename;
$data['sha1'] = $sha1;
$data['md5'] = $md5;
$data['module'] = \think\facade\App::initialize()->http->getName();
$data['action'] = app('request')->action();
$data['uploadip'] = app('request')->ip();
$data['create_time'] = time();
$data['user_id'] = $this->uid;
if ($data['module'] = 'admin') {
//通过后台上传的文件直接审核通过
$data['status'] = 1;
$data['admin_id'] = $data['user_id'];
$data['audit_time'] = time();
}
$data['use'] = request()->has('use') ? request()->param('use') : $use; //附件用处
$res['id'] = Db::name('file')->insertGetId($data);
$res['filepath'] = $data['filepath'];
$res['name'] = $data['name'];
$res['filename'] = $data['filename'];
$res['filesize'] = $data['filesize'];
add_log('upload', $data['user_id'], $data);
return to_assign(0, '上传成功', $res);
} else {
return to_assign(1, '上传失败,请重试');
}
}
// 上传程序
public function upload_files()
{
if (request()->file('file')) {
// 获取文件信息
$file = request()->file('file');
$file_name = $file->getOriginalName();
$file_size = $file->getSize();
$file_extension = pathinfo($file->getOriginalName(), PATHINFO_EXTENSION);
// 文件大小限制(单位:字节)
$max_file_size = 500 * 1024 * 1024; // 500MB
// 检查文件大小
if ($file_size > $max_file_size) {
return json([
'code' => 2,
'msg' => '文件过大已经超过500MB请联系管理员!',
'max_size' => $max_file_size,
'file_size' => $file_size
]);
}
// 创建文件夹
$upload_dir = 'upload/programes/' . date('Y-m-d') . '/';
if (!is_dir($upload_dir)) {
if (!mkdir($upload_dir, 0777, true) && !is_dir($upload_dir)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $upload_dir));
}
}
// 生成新文件名
$new_name = $this->generateRandomName(5) . '.' . $file_extension;
// 保存文件
$file->move($upload_dir, $new_name);
// 添加数据到数据库
$data = [
'path' => $upload_dir . $new_name,
'uid' => $this->uid,
'create_time' => date('Y-m-d H:i:s', time())
];
Db::name('DownloadFiles')->insert($data);
// 获取文件上传域名
$fileDomain = $_SERVER['HTTP_HOST'];
// 构建文件访问地址
$url = $fileDomain . '/upload/programes/' . date('Y-m-d') . '/' . $new_name;
// 返回 JSON 数据
return json([
'code' => 200,
'msg' => '文件上传成功!',
'fileDomain' => $fileDomain,
'filename' => $file_name,
'filepath' => $upload_dir . $new_name,
'file_size'=> $file_size,
'file_extension'=> $file_extension,
'url' => $url,
'admin_id' => $this->uid
]);
} else {
return json(['code' => 400, 'msg' => '未选择上传文件!']);
}
}
// 图床上传
public function upload_picbed()
{
if (request()->file('file')) {
// 获取文件信息
$file = request()->file('file');
$file_name = $file->getOriginalName();
$file_size = $file->getSize();
$file_type = $file->getMime();
$file_extension = pathinfo($file->getOriginalName(), PATHINFO_EXTENSION);
// 检查文件类型
$allowed_types = Db::name('FileType')->column('suffix');
if (!in_array($file_extension, $allowed_types)) {
return json(['code' => 1, 'msg' => '该文件类型不允许上传!请联系管理员!']);
}
// 检查文件大小
$max_file_size = 10 * 1024 * 1024; // 10MB
if ($file_size > $max_file_size) {
return json(['code' => 2, 'msg' => '文件过大已经超过10M请联系管理员!']);
}
// 创建文件夹
$upload_dir = 'upload/' . date('Y-m-d') . '/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0777, true);
}
// 生成新文件名
$new_name = $this->generateRandomName(5) . '.' . $file_extension;
// 保存文件
$file->move($upload_dir, $new_name);
// 添加数据到数据库
$data = [
'name' => $file_name,
'new_name' => $new_name,
'path' => $upload_dir . $new_name,
'admin_id' => $this->uid,
'size' => $file_size,
'type' => $file_extension,
'create_time' => time()
];
Db::name('PicbedImages')->insert($data);
// 获取文件上传域名
$fileDomain = $_SERVER['HTTP_HOST'];
// 构建文件访问地址
$url = $fileDomain . '/upload/' . date('Y-m-d') . '/' . $new_name;
// 返回 JSON 数据
return json([
'code' => 200,
'msg' => '文件上传成功!',
'fileDomain' => $fileDomain,
'filename' => $file_name,
'filepath' => $upload_dir . $new_name,
'url' => $url,
'admin_id' => $this->uid
]);
} else {
return json(['code' => 400, 'msg' => '未选择上传文件!']);
}
}
// 生成随机文件名
private function generateRandomName($length)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$random_name = '';
for ($i = 0; $i < $length; $i++) {
$random_name .= $characters[rand(0, strlen($characters) - 1)];
}
return $random_name;
}
// 外部站点上传图片到本站接口
public function upload_pic_out()
{
if (request()->file('file')) {
// 获取文件信息
$file = request()->file('file');
$file_name = $file->getOriginalName();
$file_size = $file->getSize();
$file_type = $file->getMime();
$file_extension = pathinfo($file->getOriginalName(), PATHINFO_EXTENSION);
// 检查文件类型
$allowed_types = Db::name('FileType')->column('suffix');
if (!in_array($file_extension, $allowed_types)) {
return json(['code' => 1, 'msg' => '该文件类型不允许上传!请联系管理员!'], 400);
}
// 获取文件上传域名
$fileDomain = '//' . $_SERVER['HTTP_HOST'];
// 检查文件大小
$max_file_size = 10 * 1024 * 1024; // 10MB
if ($file_size > $max_file_size) {
return json(['code' => 2, 'msg' => '文件过大已经超过10M请联系管理员!'], 400);
}
// 创建文件夹
$upload_dir = './upload/' . date('Y-m-d') . '/';
$upload_dir_1 = '/upload/' . date('Y-m-d') . '/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0777, true);
}
// 生成新文件名
$file_extension = pathinfo($file->getOriginalName(), PATHINFO_EXTENSION);
$new_name = $this->generateRandomName(5) . '.' . $file_extension;
// 保存文件
$file_destination = $upload_dir . $new_name;
$file_destination1 = $upload_dir_1 . $new_name;
$result = $file->move($upload_dir, $new_name);
if (!$result) {
return json(['code' => 3, 'msg' => '文件保存失败'], 400);
}
$file_name = urlencode($file_name);
// 添加数据到数据库
$data = [
'name' => $file_name,
'new_name' => $new_name,
'path' => $file_destination,
'admin_id' => request()->param('pr_name'),
'size' => $file_size,
'type' => $file_type,
'create_time' => time()
];
// 检查 admin_id 是否为空
if (empty($data['admin_id'])) {
return json(['code' => 4, 'msg' => '上传失败,缺少 pr_name 参数!'], 400);
}
Db::name('PicbedImages')->insert($data);
// 构建文件访问地址
$url = $_SERVER['HTTP_HOST'] . '/upload/' . date('Y-m-d') . '/' . $new_name;
// 构建响应数据
$response_data = [
'code' => 5,
'msg' => '文件上传成功!',
'filename' => $file_name,
'fileDomain' => $fileDomain,
'filepath' => $file_destination1,
'url' => $url
];
// 返回 JSON 格式的响应数据
return json($response_data, 200);
} else {
return json(['code' => 6, 'msg' => '未选择上传文件!'], 400);
}
}
//编辑器图片上传
public function tinymce_upload()
{
$param = get_params();
if (request()->file('file')) {
$file = request()->file('file');
} else {
return json(['error' => 1, 'message' => '没有选择上传文件']);
}
// 获取上传文件的hash散列值
$sha1 = $file->hash('sha1');
$md5 = $file->hash('md5');
$rule = [
'image' => 'jpg,png,jpeg,gif',
'doc' => 'doc,docx,ppt,pptx,xls,xlsx,pdf,txt',
'file' => 'zip,gz,7z,rar,tar',
];
$fileExt = $rule['image'] . ',' . $rule['doc'] . ',' . $rule['file'];
$fileSize = 2 * 1024 * 1024; // 默认文件大小限制
if (isset($param['type']) && $param['type']) {
$fileExt = $rule[$param['type']];
}
if (isset($param['size']) && $param['size']) {
$fileSize = $param['size'];
}
$validate = \think\facade\Validate::rule([
'image' => 'require|fileSize:' . $fileSize . '|fileExt:' . $fileExt,
]);
$file_check['image'] = $file;
if (!$validate->check($file_check)) {
return json(['error' => 1, 'message' => $validate->getError()]);
}
$dataPath = date('Ym');
$filename = \think\facade\Filesystem::disk('public')->putFile($dataPath, $file, function () use ($md5) {
return $md5;
});
if ($filename) {
$path = get_config('filesystem.disks.public.url');
$filepath = $path . '/' . $filename;
return json(['location' => $filepath]); // Tinymce 需要的关键字段是 'location'
} else {
return json(['error' => 1, 'message' => '上传失败']);
}
}
public function md_upload()
{
$param = get_params();
if (request()->file('editormd-image-file')) {
$file = request()->file('editormd-image-file');
} else {
return to_assign(1, '没有选择上传文件');
}
// dump($file);die;
// 获取上传文件的hash散列值
$sha1 = $file->hash('sha1');
$md5 = $file->hash('md5');
$rule = [
'image' => 'jpg,png,jpeg,gif',
'doc' => 'doc,docx,ppt,pptx,xls,xlsx,pdf',
'file' => 'zip,gz,7z,rar,tar',
];
$fileExt = $rule['image'] . ',' . $rule['doc'] . ',' . $rule['file'];
//1M=1024*1024=1048576字节
$fileSize = 2 * 1024 * 1024;
if (isset($param['type']) && $param['type']) {
$fileExt = $rule[$param['type']];
}
if (isset($param['size']) && $param['size']) {
$fileSize = $param['size'];
}
$validate = \think\facade\Validate::rule([
'image' => 'require|fileSize:' . $fileSize . '|fileExt:' . $fileExt,
]);
$file_check['image'] = $file;
if (!$validate->check($file_check)) {
return to_assign(1, $validate->getError());
}
// 日期前綴
$dataPath = date('Ym');
$use = 'thumb';
$filename = \think\facade\Filesystem::disk('public')->putFile($dataPath, $file, function () use ($md5) {
return $md5;
});
if ($filename) {
//写入到附件表
$data = [];
$path = get_config('filesystem.disks.public.url');
$data['filepath'] = $path . '/' . $filename;
$data['name'] = $file->getOriginalName();
$data['mimetype'] = $file->getOriginalMime();
$data['fileext'] = $file->extension();
$data['filesize'] = $file->getSize();
$data['filename'] = $filename;
$data['sha1'] = $sha1;
$data['md5'] = $md5;
return json(['success' => 1, 'message' => '上传成功', 'url' => $data['filepath']]);
} else {
return json(['success' => 0, 'message' => '上传失败', 'url' => '']);
}
}
//清空缓存
public function cache_clear()
{
\think\facade\Cache::clear();
return to_assign(0, '系统缓存已清空');
}
//获取部门树形节点列表
public function get_department_tree()
{
$department = get_department();
$list = get_tree($department, 0, 2);
$data['trees'] = $list;
return json($data);
}
//获取部门树形节点列表2
public function get_department_select()
{
$keyword = get_params('keyword');
$selected = [];
if (!empty($keyword)) {
$selected = explode(",", $keyword);
}
$department = get_department();
$list = get_select_tree($department, 0, 0, $selected);
return to_assign(0, '', $list);
}
//获取子部门所有员工
public function get_employee($did = 0)
{
$did = get_params('did');
/*
if ($did == 1) {
$department = $did;
} else {
$department = get_department_son($did);
}
*/
$department = get_department_son($did);
$employee = Db::name('admin')
->field('a.id,a.did,a.position_id,a.mobile,a.name,a.nickname,a.sex,a.status,a.thumb,a.username,d.title as department')
->alias('a')
->join('Department d', 'a.did = d.id')
->where(['a.status' => 1])
->where('a.did', "in", $department)
->select();
return to_assign(0, '', $employee);
}
//获取部门所有员工
public function get_employee_select()
{
$employee = Db::name('admin')->field('id as value,name')->where(['status' => 1])->select();
return to_assign(0, '', $employee);
}
//获取角色列表
public function get_position()
{
$position = Db::name('Position')->field('id,title as name')->where([['status', '=', 1], ['id', '>', 1]])->select();
return to_assign(0, '', $position);
}
//获取工作类型列表
public function get_work()
{
$cate = Db::name('WorkCate')->field('id,title')->where([['status', '=', 1]])->select();
return to_assign(0, '', $cate);
}
//获取任务类型列表
public function get_task_cate()
{
$cate = Db::name('TaskCate')->field('id,title')->where([['status', '=', 1]])->select();
return to_assign(0, '', $cate);
}
//获取产品列表
public function get_product()
{
$product = Db::name('Product')->field('id,name as title')->where([['delete_time', '=', 0]])->select();
return to_assign(0, '', $product);
}
//获取客户列表
public function get_business()
{
$business = Db::name('Business')->field('id,name as title')->where([['delete_time', '=', 0]])->select();
return to_assign(0, '', $business);
}
//获取项目列表
public function get_project($pid = 0)
{
$where = [];
$where[] = ['delete_time', '=', 0];
if ($pid > 0) {
$where[] = ['product_id', '=', $pid];
}
$project = Db::name('Project')->field('id,name as title')->where($where)->select();
//$belong_project = Db::name('Project')->field('id,name_short as title')->where($where)->select();
return to_assign(0, '', $project);
}
//获取所属项目短名
public function get_belong_project()
{
$where = [];
$where[] = ['delete_time', '=', 0];
if ($pid > 0) {
$where[] = ['product_id', '=', $pid];
}
$belong_project = Db::name('Project')->field('id,name_short as title')->where($where)->select();
return to_assign(0, '', $belong_project);
}
//文档列表
public function get_doc_list($kid = 0, $tree = 0)
{
if ($tree == 2) {
$list = Db::name('knowledgeDoc')->where(['knowledge_id' => $kid, 'delete_time' => 0])
->field('id,pid as pId,title as name,type,link,knowledge_id,sort,read')
->order('sort asc,id asc')
->select();
return to_assign(0, '', $list);
} else {
$list = Db::name('knowledgeDoc')->where(['knowledge_id' => $kid, 'delete_time' => 0])
->field('id,pid,title,type,knowledge_id,sort,read')
->order('sort asc,id asc')
->select();
if ($tree == 1) {
foreach ($list as $k => &$v) {
$v['title'] = sub_str($v['title'], 9);
}
$tree = get_tree($list, 0, 4);
$data['trees'] = $tree;
return json($data);
} else {
return to_assign(0, '', $list);
}
}
}
//删除消息附件
public function del_message_interfix()
{
$id = get_params("id");
$detail = Db::name('MessageFileInterfix')->where('id', $id)->find();
if ($detail['admin_id'] == $this->uid) {
if (Db::name('MessageFileInterfix')->where('id', $id)->delete() !== false) {
$data = Db::name('MessageFileInterfix')->where('mid', $detail['mid'])->column('file_id');
return to_assign(0, "删除成功", $data);
} else {
return to_assign(1, "删除失败");
}
} else {
return to_assign(1, "您没权限删除该消息附件");
}
}
// 测试邮件发送
public function email_test()
{
$sender = get_params('email');
//检查是否邮箱格式
if (!is_email($sender)) {
return to_assign(1, '测试邮箱码格式有误');
}
$email_config = \think\facade\Db::name('config')->where('name', 'email')->find();
$config = unserialize($email_config['content']);
$content = $config['template'];
//所有项目必须填写
if (empty($config['smtp']) || empty($config['smtp_port']) || empty($config['smtp_user']) || empty($config['smtp_pwd'])) {
return to_assign(1, '请完善邮件配置信息!');
}
$send = send_email($sender, '测试邮件', $content);
if ($send) {
return to_assign(0, '邮件发送成功!');
} else {
return to_assign(1, '邮件发送失败!');
}
}
public function get_captcha()
{
return captcha();
}
// 新增接口
public function pullart()
{
// 允许来自任何来源的跨域请求
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type');
$images = Db::name('PicbedImages')->order('id', 'desc')->select()->toArray();
// 获取当前请求的域名
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'];
$baseUrl = $protocol . '://' . $host;
// 筛选出只需要的字段并拼接完整路径
$images = array_map(function ($image) use ($baseUrl) {
return [
'name' => htmlspecialchars($image['name'], ENT_QUOTES),
'path' => $baseUrl . '/' . $image['path'],
];
}, $images);
return json(['code' => 0, 'msg' => '', 'data' => $images]);
}
public function getallstaff()
{
$employees = Db::name('admin')
->field('id, name, status') // 根据需要选择字段
->where('status', 1) // 只获取在职员工
->select();
return to_assign(0, '', $employees);
}
}