This commit is contained in:
彭桃 2023-01-30 17:03:10 +08:00
parent 4596616f99
commit 260a4f3f51
2 changed files with 233 additions and 103 deletions

View File

@ -14,6 +14,8 @@ namespace app\api\controller;
use app\api\BaseController;
use app\api\middleware\Auth;
use OSS\Core\OssException;
use OSS\OssClient;
use think\facade\Config;
use app\common\model\Area;
use app\common\model\Version;
@ -59,116 +61,232 @@ class Common extends BaseController
}
}
/**
* 上传文件.
* @ApiMethod (POST)
*
* @param File $file 文件流
*/
// /**
// * 上传文件.
// * @ApiMethod (POST)
// *
// * @param File $file 文件流
// */
// public function upload()
// {
// $file = $this->request->file('file');
// if (empty($file)) {
// $this->apiError('未上传文件或超出服务器上传限制');
// }
//
// //判断是否已经存在附件
// $sha1 = $file->hash();
//
// $upload = Config::get('upload');
//
// preg_match('/(\d+)(\w+)/', $upload['maxsize'], $matches);
// $type = strtolower($matches[2]);
// $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
// $size = (int) $upload['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
// $fileInfo['name'] = $file->getOriginalName(); //上传文件名
// $fileInfo['type'] = $file->getOriginalMime(); //上传文件类型信息
// $fileInfo['tmp_name'] = $file->getPathname();
// $fileInfo['size'] = $file->getSize();
// $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
// $suffix = $suffix && preg_match('/^[a-zA-Z0-9]+$/', $suffix) ? $suffix : 'file';
//
// $mimetypeArr = explode(',', strtolower($upload['mimetype']));
// $typeArr = explode('/', $fileInfo['type']);
//
// //禁止上传PHP和HTML文件
// if (in_array($fileInfo['type'], ['text/x-php', 'text/html']) || in_array($suffix, ['php', 'html', 'htm', 'phar', 'phtml']) || preg_match("/^php(.*)/i", $suffix)) {
// $this->apiError('上传文件格式受限制');
// }
//
// //Mimetype值不正确
// if (stripos($fileInfo['type'], '/') === false) {
// $this->apiError('上传文件格式受限制');
// }
//
// //验证文件后缀
// if ($upload['mimetype'] !== '*' &&
// (
// !in_array($suffix, $mimetypeArr)
// || (stripos($typeArr[0] . '/', $upload['mimetype']) !== false && (!in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr)))
// )
// ) {
// $this->apiError('上传文件格式受限制');
// }
//
// //验证是否为图片文件
// $imagewidth = $imageheight = 0;
// if (in_array($fileInfo['type'],
// ['image/gif', 'image/jpg', 'image/jpeg', 'image/bmp', 'image/png', 'image/webp']) || in_array($suffix,
// ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'webp'])) {
// $imgInfo = getimagesize($fileInfo['tmp_name']);
// if (! $imgInfo || ! isset($imgInfo[0]) || ! isset($imgInfo[1])) {
// $this->apiError('上传文件不是有效的图片文件');
// }
// $imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth;
// $imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight;
// }
//
// $_validate[] = 'filesize:'.$size;
// if ($upload['mimetype']) {
// $_validate[] = 'fileExt:'.$upload['mimetype'];
// }
// $validate = implode('|', $_validate);
//
// $event_config = Event::trigger('upload_init', $upload,true);
// if($event_config){
// $upload = array_merge($upload, $event_config);
// }
// try {
// $savename = upload_file($file, $upload['driver'], 'uploads', $validate, $upload['cdnurl']);
// } catch (\Exception $e) {
// $savename = false;
// $this->apiError($e->getMessage());
// }
// if (! $savename) {
// $this->apiError('上传失败');
// }
// $category = request()->post('category');
// $category = array_key_exists($category, config('site.attachmentcategory') ?? []) ? $category : '';
//// $urls= Env::get('APP.URL_MY')? Env::get('APP.URL_MY'):'https://ceshi.excellentkk.cn';
// $params = [
// 'admin_id' => 0,
// 'user_id' => (int) JWT_UID,
// 'category' => $category,
// 'filename' => mb_substr(htmlspecialchars(strip_tags($fileInfo['name'])), 0, 100),
// 'filesize' => $fileInfo['size'],
// 'imagewidth' => $imagewidth,
// 'imageheight' => $imageheight,
// 'imagetype' => $suffix,
// 'imageframes' => 0,
// 'mimetype' => $fileInfo['type'],
// 'url' => $savename,
// 'uploadtime' => time(),
// 'storage' => $upload['driver'],
// 'sha1' => $sha1,
// ];
// $attachment = new Attachment();
// $attachment->data(array_filter($params));
// $attachment->save();
// \think\facade\Event::trigger('upload_after', $attachment);
// $this->apiSuccess('上传成功', [
// 'url' =>$savename,
// ]);
// }
//上传文件
public function upload()
{
$file = $this->request->file('file');
if (empty($file)) {
$this->apiError('未上传文件或超出服务器上传限制');
$param = get_params();
//var_dump($param);exit;
$sourse = 'file';
if(isset($param['sourse'])){
$sourse = $param['sourse'];
}
//判断是否已经存在附件
$sha1 = $file->hash();
$upload = Config::get('upload');
preg_match('/(\d+)(\w+)/', $upload['maxsize'], $matches);
$type = strtolower($matches[2]);
$typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
$size = (int) $upload['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
$fileInfo['name'] = $file->getOriginalName(); //上传文件名
$fileInfo['type'] = $file->getOriginalMime(); //上传文件类型信息
$fileInfo['tmp_name'] = $file->getPathname();
$fileInfo['size'] = $file->getSize();
$suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
$suffix = $suffix && preg_match('/^[a-zA-Z0-9]+$/', $suffix) ? $suffix : 'file';
$mimetypeArr = explode(',', strtolower($upload['mimetype']));
$typeArr = explode('/', $fileInfo['type']);
//禁止上传PHP和HTML文件
if (in_array($fileInfo['type'], ['text/x-php', 'text/html']) || in_array($suffix, ['php', 'html', 'htm', 'phar', 'phtml']) || preg_match("/^php(.*)/i", $suffix)) {
$this->apiError('上传文件格式受限制');
}
//Mimetype值不正确
if (stripos($fileInfo['type'], '/') === false) {
$this->apiError('上传文件格式受限制');
}
//验证文件后缀
if ($upload['mimetype'] !== '*' &&
(
!in_array($suffix, $mimetypeArr)
|| (stripos($typeArr[0] . '/', $upload['mimetype']) !== false && (!in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr)))
)
) {
$this->apiError('上传文件格式受限制');
}
//验证是否为图片文件
$imagewidth = $imageheight = 0;
if (in_array($fileInfo['type'],
['image/gif', 'image/jpg', 'image/jpeg', 'image/bmp', 'image/png', 'image/webp']) || in_array($suffix,
['gif', 'jpg', 'jpeg', 'bmp', 'png', 'webp'])) {
$imgInfo = getimagesize($fileInfo['tmp_name']);
if (! $imgInfo || ! isset($imgInfo[0]) || ! isset($imgInfo[1])) {
$this->apiError('上传文件不是有效的图片文件');
if($sourse == 'file' || $sourse == 'tinymce'){
if(request()->file('file')){
$file = request()->file('file');
}
else{
return to_assign(1, '没有选择上传文件');
}
$imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth;
$imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight;
}
$_validate[] = 'filesize:'.$size;
if ($upload['mimetype']) {
$_validate[] = 'fileExt:'.$upload['mimetype'];
else{
if (request()->file('editormd-image-file')) {
$file = request()->file('editormd-image-file');
} else {
return to_assign(1, '没有选择上传文件');
}
}
$validate = implode('|', $_validate);
$event_config = Event::trigger('upload_init', $upload,true);
if($event_config){
$upload = array_merge($upload, $event_config);
}
try {
$savename = upload_file($file, $upload['driver'], 'uploads', $validate, $upload['cdnurl']);
} catch (\Exception $e) {
$savename = false;
$this->apiError($e->getMessage());
}
if (! $savename) {
$this->apiError('上传失败');
}
$category = request()->post('category');
$category = array_key_exists($category, config('site.attachmentcategory') ?? []) ? $category : '';
// $urls= Env::get('APP.URL_MY')? Env::get('APP.URL_MY'):'https://ceshi.excellentkk.cn';
$params = [
'admin_id' => 0,
'user_id' => (int) JWT_UID,
'category' => $category,
'filename' => mb_substr(htmlspecialchars(strip_tags($fileInfo['name'])), 0, 100),
'filesize' => $fileInfo['size'],
'imagewidth' => $imagewidth,
'imageheight' => $imageheight,
'imagetype' => $suffix,
'imageframes' => 0,
'mimetype' => $fileInfo['type'],
'url' => $savename,
'uploadtime' => time(),
'storage' => $upload['driver'],
'sha1' => $sha1,
// 获取上传文件的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',
'video' => 'mpg,mp4,mpeg,avi,wmv,mov,flv,m4v',
];
$attachment = new Attachment();
$attachment->data(array_filter($params));
$attachment->save();
\think\facade\Event::trigger('upload_after', $attachment);
$this->apiSuccess('上传成功', [
'url' =>$savename,
$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';
$accessKeyId = "LTAI5t7mhH3ij2cNWs1zhPmv"; ;
$accessKeySecret = "gqo2wMpvi8h5bDBmCpMje6BaiXvcPu";
$endpoint = "oss-cn-chengdu.aliyuncs.com";
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
} catch (OssException $e) {
return to_assign(1, $e->getMessage());
}
$bucket = "lihai001";
$object = 'storage/'.$dataPath.'/'.$md5.'.jpg';
// $filename = \think\facade\Filesystem::disk('public')->putFile($dataPath, $file, function () use ($md5) {
// return $md5;
// });
try {
$filename=$ossClient->uploadFile($bucket, $object,$file);
} catch (OssException $e) {
return to_assign(1, $e->getMessage());
}
if ($filename) {
//写入到附件表
$data = [];
$path = get_config('filesystem.disks.public.url');
$data['filepath'] = $filename['info']['url'];
$data['name'] = $file->getOriginalName();
$data['mimetype'] = $file->getOriginalMime();
$data['fileext'] = $file->extension();
$data['filesize'] = $file->getSize();
$data['filename'] = $object;
$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'] = get_login_admin('id') ? get_login_admin('id') : 0;
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'];
add_log('upload', $data['user_id'], $data);
if($sourse == 'editormd'){
//editormd编辑器上传返回
return json(['success'=>1,'message'=>'上传成功','url'=>$data['filepath']]);
}
else if($sourse == 'tinymce'){
//tinymce编辑器上传返回
return json(['success'=>1,'message'=>'上传成功','location'=>$data['filepath']]);
}
else{
//普通上传返回
return to_assign(0, '上传成功', $res);
}
} else {
return to_assign(1, '上传失败,请重试');
}
}
public function category($id=0,$type='',$pid=0,$is_category=false){

View File

@ -72,6 +72,8 @@ class Maintainentry extends BaseController
$www[] = ['status','=','1'];
$www[] = ['enter_id','=',JWT_UID];
$have = Db::table('fa_szxc_information_usermsg')->where($www)->count();
$man_num = Db::table('fa_szxc_information_usermsg')->where($www)->where('gender',1)->count();
$woman_num = Db::table('fa_szxc_information_usermsg')->where($www)->where('gender',2)->count();
$page = get_params('page')??1;
$limit = get_params('limit')??10;
// $name = get_params('name', '');
@ -91,7 +93,7 @@ class Maintainentry extends BaseController
// 获取已录入的人员信息
$res = Db::table('fa_szxc_information_usermsg')->where($where)
->order('id desc')
->field("id,user_id,name,age,gender,family_num,political_outlook,householder_id,is_hz,address_name")
->field("id,user_id,name,age,gender,family_num,political_outlook,householder_id,is_hz,address_name,phone")
->page($page, $limit)
->withAttr('householder_name',function ($data,$value){
if($value['is_hz']==1){
@ -135,9 +137,19 @@ class Maintainentry extends BaseController
return '女';
}
})
->withAttr('avatar',function ($data,$value){
if($value['is_hz']==1){
return 'https://lihai001.oss-cn-chengdu.aliyuncs.com/uploads/20230129/0dc8d0a0f3bcc168550ef263a9a170dc.png';
}else{
$msg= Db::table('fa_user')->where('id',$value['user_id'])->value('avatar');
return $msg??'';
}
})
->select();
$return['total_num'] = $total;
$return['now_num'] = $have;
$return['man_num'] = $man_num;
$return['woman_num'] = $woman_num;
$return['data'] = $res;
$this->apiSuccess('获取成功', $return, 1);
}