2023-06-06 10:47:49 +08:00

109 lines
3.9 KiB
PHP

<?php
namespace app\api\controller;
use Illuminate\Support\Env;
use support\Request;
use plugin\admin\app\model\Admin;
use plugin\admin\app\controller\Base;
use support\exception\BusinessException;
use plugin\admin\app\common\Util;
use plugin\admin\app\model\Project as ProjectModel;
use plugin\admin\app\model\Projectdata as ProjectdataModel;
use think\facade\Db;
class Project extends Base
{
public function list(Request $request){
$select=ProjectModel::paginate();
return json(['code'=>200,'msg'=>'获取成功','count'=>$select->total(),'data'=>$select->items()]);
}
public function edit(Request $request){
Db::name('Projects')->where('id', $request->post('id'))->update(['indexImage'=>$request->post('indexImage')]);
return json(['code'=>200,'msg'=>'更新成功']);
}
public function create(Request $request){
$data = $request->post();
$id= ProjectModel::insertGetId($data);
$res=ProjectModel::where('id',$id)->first();
$res['CreateTime']=$res['created_at'];
$res['CreateUserId']=1;
$res['id']=$res['Id'];
return $this->json(200,'ok',$res->toArray());
}
public function publish(Request $request){
$data = $request->post();
$find=ProjectModel::find($data['id']);
$find['state']=$data['state'];
$find->save();
if($find){
return $this->json(200,'操作成功');
}else{
return $this->json(500,'操作失败');
}
}
public function getData(Request $request){
$data = $request->get();
$find=ProjectModel::find($data['projectId']);
$ProjectdataModel=ProjectdataModel::where('projectId',$data['projectId'])->first();
if($ProjectdataModel){
$find['content']=$ProjectdataModel['content'];
}else{
$find['content']='';
}
return $this->json(200,'ok',$find->toArray());
}
public function data(Request $request){
$data = $request->post();
$find=Db::name('projectdatas')->where('projectId', $data['projectId'])->find();
if($find){
Db::name('projectdatas')->where('projectId',$data['projectId'])->update(['content'=>$data['content']]);
}else{
$id=Db::name('projectdatas')->insertGetId(['projectId'=>$data['projectId'],'content'=>$data['content']]);
$find=Db::name('projectdatas')->where('id', $id)->find();
}
return $this->json(200,'ok',$find);
}
public function upload(Request $request){
$file = current($request->file());
if (!$file || !$file->isValid()) {
return $this->json(1, '未找到文件');
}
$relative_dir='/upload/img/'.date('Ymd');
$relative_dir = ltrim($relative_dir, '/');
$file = current($request->file());
if (!$file || !$file->isValid()) {
throw new BusinessException('未找到上传文件', 400);
}
$base_dir = base_path() . '/plugin/admin/public/';
$full_dir = $base_dir . $relative_dir;
if (!is_dir($full_dir)) {
mkdir($full_dir, 0777, true);
}
$ext = strtolower($file->getUploadExtension());
$ext_forbidden_map = ['php', 'php3', 'php5', 'css', 'js', 'html', 'htm', 'asp', 'jsp'];
if (in_array($ext, $ext_forbidden_map)) {
throw new BusinessException('不支持该格式的文件上传', 400);
}
$relative_path = $relative_dir . '/' . bin2hex(pack('Nn',time(), random_int(1, 65535))) . ".$ext";
$full_path = $base_dir . $relative_path;
var_dump($full_path);
$file_size = $file->getSize();
$file_name = $file->getUploadName();
$file->move($full_path);
return $this->json(200, '上传成功', [
'fileName' => "/app/admin/$relative_path",
'name' => $file_name,
'size' => $file_size,
]);
}
}