first commit
This commit is contained in:
95
pgserver/application/api/controller/AppUpload.php
Normal file
95
pgserver/application/api/controller/AppUpload.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\admin\service\FileService;
|
||||
use app\model\Attachment;
|
||||
use think\Db;
|
||||
use think\File;
|
||||
use think\Log;
|
||||
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Administrator
|
||||
* Date: 2020/3/4
|
||||
* Time: 9:37
|
||||
*/
|
||||
class AppUpload extends Base
|
||||
{
|
||||
public function upload(FileService $FileService)
|
||||
{
|
||||
$base64_img = input('image/a');
|
||||
if (empty($base64_img)) return $this->buildFailed(-1, "上传文件不能为空!");
|
||||
//匹配出图片的格式
|
||||
$up_dir = config('uploadFile.img_path') . 'uploads' . DS . date('Ymd');//存放在当前目录的upload文件夹下
|
||||
if (!is_dir($up_dir)) {
|
||||
mkdir($up_dir, 0755, true);
|
||||
}
|
||||
$arr = [];
|
||||
// 启动事务
|
||||
Db::startTrans();
|
||||
foreach ($base64_img as $k => $v) {
|
||||
//匹配出图片的格式
|
||||
if (!preg_match('/^(data:\s*image\/(\w+);base64,)/', $v, $result)) return $this->buildFailed(-1, "请上传base64文件!");
|
||||
$base64_code = str_replace($result[1], '', $v);
|
||||
if (empty($base64_code)) {
|
||||
return $this->buildFailed(-1, "请上传base64文件!!");
|
||||
}
|
||||
$type = $result[2];
|
||||
//判断库中是否存在
|
||||
$id = Db::name('Attachment')->where([
|
||||
'md5' => md5(base64_decode($base64_code)),
|
||||
'sha1' => sha1(base64_decode($base64_code)),
|
||||
])->value('id');
|
||||
if ($id) {
|
||||
$arr[] = $id;
|
||||
continue;
|
||||
}
|
||||
if (!in_array($type, array('jpeg', 'jpg', 'gif', 'png', 'webp'))) return $this->buildFailed(-1, "请上传图片类型的文件!");
|
||||
$savename = md5(microtime(true)) . '.' . $type;
|
||||
$new_file = $up_dir . DS . $savename;
|
||||
if (!file_put_contents($new_file, base64_decode($base64_code))) return $this->buildFailed(-1, "上传失败!");
|
||||
$info = new File($new_file);
|
||||
$image = \think\Image::open($new_file);
|
||||
$thumPath690 = 'uploads' . DS . 'thumb690' . DS . date('Ymd', time());
|
||||
$thumPath1024 = 'uploads' . DS . 'thumb1024' . DS . date('Ymd', time());
|
||||
$insert_url = DS . 'uploads' . DS . date('Ymd') . DS . $savename;
|
||||
$data = [
|
||||
'name' => $savename, //图片原始名称
|
||||
'savename' => $savename, //新的图片名称
|
||||
'filesize' => $info->getSize(), //文件大小
|
||||
'ext' => $info->getExtension(), //文件后缀
|
||||
'md5' => $info->hash('md5'),
|
||||
'sha1' => $info->hash('sha1'),
|
||||
'mimetype' => $info->getMime(), //mime类型
|
||||
'path' => config('uploadFile.img_path'), //路径
|
||||
'url' => $insert_url,
|
||||
'imagewidth' => $image->width(),
|
||||
'imageheight' => $image->height(),
|
||||
'create_time' => time(),
|
||||
];
|
||||
$thumb1 = $FileService->saveThumbCOS($new_file, $thumPath690, $data['imagewidth'], $data['imageheight'], $data['savename'], 690);
|
||||
$thumb2 = $FileService->saveThumbCOS($new_file, $thumPath1024, $data['imagewidth'], $data['imageheight'], $data['savename'], 1024);
|
||||
$data['thum1'] = $thumb1 != false ? $thumb1 : 'uploads' . DS . date('Ymd') . DS . $savename;
|
||||
$data['thum2'] = $thumb2 != false ? $thumb2 : 'uploads' . DS . date('Ymd') . DS . $savename;
|
||||
$file_path = config('uploadFile.img_path') . trim($insert_url, DS);
|
||||
$res = ossUpload(str_replace("\\", "/", trim($insert_url, DS)), $file_path);
|
||||
unset($info);
|
||||
@unlink($new_file);
|
||||
if ($res['code'] != 1) {
|
||||
Db::rollback();
|
||||
return $this->buildFailed(-1, '移动到阿里云图片服务器有误');
|
||||
}
|
||||
$result = Attachment::create($data);
|
||||
if (empty($result)) {
|
||||
// 回滚事务
|
||||
Db::rollback();
|
||||
return $this->buildFailed(-1, '添加附件表失败');
|
||||
}
|
||||
$arr[] = $result->id;
|
||||
}
|
||||
// 提交事务
|
||||
Db::commit();
|
||||
return $this->buildSuccess(['idstr' => implode(',', $arr)]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user