|
|
@@ -1,150 +1,209 @@
|
|
|
<?php
|
|
|
|
|
|
- namespace app\controller;
|
|
|
+namespace app\controller;
|
|
|
+
|
|
|
+use Exception;
|
|
|
+use Intervention\Image\ImageManagerStatic as Image;
|
|
|
+use plugin\admin\app\controller\Crud;
|
|
|
+use plugin\admin\app\model\Upload;
|
|
|
+
|
|
|
+use support\Db;
|
|
|
+use support\exception\BusinessException;
|
|
|
+use support\Request;
|
|
|
+use support\Response;
|
|
|
+use hg\apidoc\annotation as Apidoc;
|
|
|
+
|
|
|
+#[Apidoc\Title("上传")]
|
|
|
+#[Apidoc\Group("Upload")]
|
|
|
+#[Apidoc\Sort(1)]
|
|
|
+class UploadController extends Crud
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * @var Upload
|
|
|
+ */
|
|
|
+ protected $model = null;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 只返回当前管理员数据
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $dataLimit = 'personal';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ $this->model = new Upload;
|
|
|
+ }
|
|
|
|
|
|
- use Exception;
|
|
|
- use Intervention\Image\ImageManagerStatic as Image;
|
|
|
- use plugin\admin\app\controller\Crud;
|
|
|
- use plugin\admin\app\model\Upload;
|
|
|
+ #[Apidoc\Title("上传")]
|
|
|
+ #[Apidoc\Url("api/file")]
|
|
|
+ #[Apidoc\Method("POST")]
|
|
|
+ #[Apidoc\Param("file", type: "file", require: true, desc: "上传图片")]
|
|
|
+ #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
|
|
|
+ #[Apidoc\Returned(name: "url", type: "string", require: true, desc: '前端展示地址', default: '12317309127904')]
|
|
|
+ public function image(Request $request): Response
|
|
|
+ {
|
|
|
+ $data = $this->base($request, '/upload/img/' . date('Ymd'));
|
|
|
+ $realpath = $data['realpath'];
|
|
|
+ try {
|
|
|
+ $img = Image::make($realpath);
|
|
|
+ $max_height = 1170;
|
|
|
+ $max_width = 1170;
|
|
|
+ $width = $img->width();
|
|
|
+ $height = $img->height();
|
|
|
+ $ratio = 1;
|
|
|
+ if ($height > $max_height || $width > $max_width) {
|
|
|
+ $ratio = $width > $height ? $max_width / $width : $max_height / $height;
|
|
|
+ }
|
|
|
+ $img->resize($width * $ratio, $height * $ratio)->save($realpath);
|
|
|
+ } catch (Exception $e) {
|
|
|
+ unlink($realpath);
|
|
|
+ return error('处理图片发生错误');
|
|
|
+ }
|
|
|
|
|
|
- use support\Db;
|
|
|
- use support\exception\BusinessException;
|
|
|
- use support\Request;
|
|
|
- use support\Response;
|
|
|
- use hg\apidoc\annotation as Apidoc;
|
|
|
+ /* Db::table('wa_users')->where('id',$request->user_data['id'])->update(['img'=>$data['url']]);*/
|
|
|
+ return success([
|
|
|
+ 'url' => imageToBase64($data['url']),
|
|
|
+ 'path' => $data['url'],
|
|
|
+ ]);
|
|
|
+ }
|
|
|
|
|
|
- #[Apidoc\Title("上传")]
|
|
|
- #[Apidoc\Group("Upload")]
|
|
|
- #[Apidoc\Sort(1)]
|
|
|
- class UploadController extends Crud
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取上传数据
|
|
|
+ * @param Request $request
|
|
|
+ * @param $relative_dir
|
|
|
+ * @return array
|
|
|
+ * @throws BusinessException|RandomException
|
|
|
+ */
|
|
|
+ protected function base(Request $request, $relative_dir): array
|
|
|
{
|
|
|
- /**
|
|
|
- * @var Upload
|
|
|
- */
|
|
|
- protected $model = null;
|
|
|
-
|
|
|
- /**
|
|
|
- * 只返回当前管理员数据
|
|
|
- * @var string
|
|
|
- */
|
|
|
- protected $dataLimit = 'personal';
|
|
|
-
|
|
|
- /**
|
|
|
- * 构造函数
|
|
|
- * @return void
|
|
|
- */
|
|
|
- public function __construct()
|
|
|
- {
|
|
|
- $this->model = new Upload;
|
|
|
+ $relative_dir = ltrim($relative_dir, '\\/');
|
|
|
+ $file = current($request->file());
|
|
|
+ if (!$file || !$file->isValid()) {
|
|
|
+ throw new BusinessException('未找到上传文件', 400);
|
|
|
}
|
|
|
|
|
|
- #[Apidoc\Title("上传")]
|
|
|
- #[Apidoc\Url("api/file")]
|
|
|
- #[Apidoc\Method("POST")]
|
|
|
- #[Apidoc\Param("file", type: "file", require: true, desc: "上传图片")]
|
|
|
- #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
|
|
|
- #[Apidoc\Returned(name: "url", type: "string", require: true, desc: '前端展示地址', default: '12317309127904')]
|
|
|
- public function image(Request $request): Response
|
|
|
- {
|
|
|
- $data = $this->base($request, '/upload/img/' . date('Ymd'));
|
|
|
- $realpath = $data['realpath'];
|
|
|
- try {
|
|
|
- $img = Image::make($realpath);
|
|
|
- $max_height = 1170;
|
|
|
- $max_width = 1170;
|
|
|
- $width = $img->width();
|
|
|
- $height = $img->height();
|
|
|
- $ratio = 1;
|
|
|
- if ($height > $max_height || $width > $max_width) {
|
|
|
- $ratio = $width > $height ? $max_width / $width : $max_height / $height;
|
|
|
- }
|
|
|
- $img->resize($width * $ratio, $height * $ratio)->save($realpath);
|
|
|
- } catch (Exception $e) {
|
|
|
- unlink($realpath);
|
|
|
- return error('处理图片发生错误');
|
|
|
- }
|
|
|
+ $admin_public_path = rtrim(config('app.public_path', ''), '\\/');
|
|
|
+ $base_dir = $admin_public_path ? $admin_public_path . DIRECTORY_SEPARATOR : base_path() . '/plugin/admin/public/';
|
|
|
+ $full_dir = $base_dir . $relative_dir;
|
|
|
+ if (!is_dir($full_dir)) {
|
|
|
+ mkdir($full_dir, 0777, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ $ext = $file->getUploadExtension() ?: null;
|
|
|
+ $mime_type = $file->getUploadMimeType();
|
|
|
+ $file_name = $file->getUploadName();
|
|
|
+ $file_size = $file->getSize();
|
|
|
|
|
|
- /* Db::table('wa_users')->where('id',$request->user_data['id'])->update(['img'=>$data['url']]);*/
|
|
|
- return success([
|
|
|
- 'url' => imageToBase64($data['url']),
|
|
|
- 'path' => $data['url'],
|
|
|
- ]);
|
|
|
+ if (!$ext && $file_name === 'blob') {
|
|
|
+ [$___image, $ext] = explode('/', $mime_type);
|
|
|
+ unset($___image);
|
|
|
}
|
|
|
|
|
|
+ $ext = strtolower($ext);
|
|
|
+ $ext_forbidden_map = ['php', 'php3', 'php5', 'css', 'js', 'html', 'htm', 'asp', 'jsp'];
|
|
|
+ if (in_array($ext, $ext_forbidden_map)) {
|
|
|
+ throw new BusinessException('不支持该格式的文件上传', 400);
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 获取上传数据
|
|
|
- * @param Request $request
|
|
|
- * @param $relative_dir
|
|
|
- * @return array
|
|
|
- * @throws BusinessException|RandomException
|
|
|
- */
|
|
|
- protected function base(Request $request, $relative_dir): array
|
|
|
- {
|
|
|
- $relative_dir = ltrim($relative_dir, '\\/');
|
|
|
- $file = current($request->file());
|
|
|
- if (!$file || !$file->isValid()) {
|
|
|
- throw new BusinessException('未找到上传文件', 400);
|
|
|
- }
|
|
|
+ $relative_path = $relative_dir . '/' . bin2hex(pack('Nn', time(), random_int(1, 65535))) . ".$ext";
|
|
|
+ $full_path = $base_dir . $relative_path;
|
|
|
+ $file->move($full_path);
|
|
|
+ $image_with = $image_height = 0;
|
|
|
+ if ($img_info = getimagesize($full_path)) {
|
|
|
+ [$image_with, $image_height] = $img_info;
|
|
|
+ $mime_type = $img_info['mime'];
|
|
|
+ }
|
|
|
+ return [
|
|
|
+ 'url' => "/$relative_path",
|
|
|
+ 'name' => $file_name,
|
|
|
+ 'realpath' => $full_path,
|
|
|
+ 'size' => $file_size,
|
|
|
+ 'mime_type' => $mime_type,
|
|
|
+ 'image_with' => $image_with,
|
|
|
+ 'image_height' => $image_height,
|
|
|
+ 'ext' => $ext,
|
|
|
+ ];
|
|
|
+ }
|
|
|
|
|
|
+ #[Apidoc\Title("上传图片base64")]
|
|
|
+ #[Apidoc\Url("api/base_file.html")]
|
|
|
+ #[Apidoc\Method("POST")]
|
|
|
+ #[Apidoc\Param("baseimg", type: "string", require: true, desc: "base64编码")]
|
|
|
+ #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
|
|
|
+ #[Apidoc\Returned(name: "url", type: "string", require: true, desc: '前端展示地址', default: '12317309127904')]
|
|
|
+ public function base_file(Request $request): Response
|
|
|
+ {
|
|
|
+ $param = $request->all();
|
|
|
+ try {
|
|
|
+ $base64Image_one = $param['baseimg']; // 获取Base64字符串
|
|
|
+ $imageData = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64Image_one)); // 移除数据URL的前缀并解码
|
|
|
+ $imageName = $request->user_data['id'] . time() . '.png'; // 生成一个唯一的文件名
|
|
|
+
|
|
|
+ $data = '/upload/img/' . date('Ymd');
|
|
|
+ $relative_dir = ltrim($data, '\\/');
|
|
|
$admin_public_path = rtrim(config('app.public_path', ''), '\\/');
|
|
|
$base_dir = $admin_public_path ? $admin_public_path . DIRECTORY_SEPARATOR : base_path() . '/plugin/admin/public/';
|
|
|
$full_dir = $base_dir . $relative_dir;
|
|
|
if (!is_dir($full_dir)) {
|
|
|
mkdir($full_dir, 0777, true);
|
|
|
}
|
|
|
+ $imagePath_one = '/upload/img/' . date('Ymd') . '/' . $imageName; // 指定保存路径和文件名
|
|
|
|
|
|
- $ext = $file->getUploadExtension() ?: null;
|
|
|
- $mime_type = $file->getUploadMimeType();
|
|
|
- $file_name = $file->getUploadName();
|
|
|
- $file_size = $file->getSize();
|
|
|
+ // 保存图片到服务器
|
|
|
+ file_put_contents($base_dir . $imagePath_one, $imageData);
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return error('处理图片发生错误');
|
|
|
+ }
|
|
|
+ Db::table('wa_users')->where('id', $request->user_data['id'])->update(['protocol_img' => $imagePath_one]);
|
|
|
+ return adminsuccess([
|
|
|
+ 'url' => imageToBase64($imagePath_one),
|
|
|
+ 'path' => $imagePath_one,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
|
|
|
- if (!$ext && $file_name === 'blob') {
|
|
|
- [$___image, $ext] = explode('/', $mime_type);
|
|
|
- unset($___image);
|
|
|
- }
|
|
|
|
|
|
- $ext = strtolower($ext);
|
|
|
- $ext_forbidden_map = ['php', 'php3', 'php5', 'css', 'js', 'html', 'htm', 'asp', 'jsp'];
|
|
|
- if (in_array($ext, $ext_forbidden_map)) {
|
|
|
- throw new BusinessException('不支持该格式的文件上传', 400);
|
|
|
- }
|
|
|
+ #[Apidoc\Title("上传图片base64")]
|
|
|
+ #[Apidoc\Url("api/identityimg.html")]
|
|
|
+ #[Apidoc\Method("POST")]
|
|
|
+ #[Apidoc\Param("identity_imgone", type: "string", require: true, desc: "base64编码")]
|
|
|
+ #[Apidoc\Param("identity_imgtwo", type: "string", require: true, desc: "base64编码")]
|
|
|
+ #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
|
|
|
+ #[Apidoc\Returned(name: "url", type: "string", require: true, desc: '前端展示地址', default: '12317309127904')]
|
|
|
+ public function identityimg(Request $request): Response
|
|
|
+ {
|
|
|
+ $param = $request->all();
|
|
|
+ try {
|
|
|
+ if (!empty($param['identity_imgone'])) {
|
|
|
+ $base64Image_one = $param['identity_imgone']; // 获取Base64字符串
|
|
|
+ $imageData = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64Image_one)); // 移除数据URL的前缀并解码
|
|
|
+ $imageName = $request->user_data['id'] . time() . '.png'; // 生成一个唯一的文件名
|
|
|
|
|
|
- $relative_path = $relative_dir . '/' . bin2hex(pack('Nn', time(), random_int(1, 65535))) . ".$ext";
|
|
|
- $full_path = $base_dir . $relative_path;
|
|
|
- $file->move($full_path);
|
|
|
- $image_with = $image_height = 0;
|
|
|
- if ($img_info = getimagesize($full_path)) {
|
|
|
- [$image_with, $image_height] = $img_info;
|
|
|
- $mime_type = $img_info['mime'];
|
|
|
- }
|
|
|
- return [
|
|
|
- 'url' => "/$relative_path",
|
|
|
- 'name' => $file_name,
|
|
|
- 'realpath' => $full_path,
|
|
|
- 'size' => $file_size,
|
|
|
- 'mime_type' => $mime_type,
|
|
|
- 'image_with' => $image_with,
|
|
|
- 'image_height' => $image_height,
|
|
|
- 'ext' => $ext,
|
|
|
- ];
|
|
|
- }
|
|
|
+ $data = '/upload/img/11' . date('Ymd');
|
|
|
+ $relative_dir = ltrim($data, '\\/');
|
|
|
+ $admin_public_path = rtrim(config('app.public_path', ''), '\\/');
|
|
|
+ $base_dir = $admin_public_path ? $admin_public_path . DIRECTORY_SEPARATOR : base_path() . '/plugin/admin/public/';
|
|
|
+ $full_dir = $base_dir . $relative_dir;
|
|
|
+ if (!is_dir($full_dir)) {
|
|
|
+ mkdir($full_dir, 0777, true);
|
|
|
+ }
|
|
|
+ $imagePath_one = '/upload/img/' . date('Ymd') . '/' . $imageName; // 指定保存路径和文件名
|
|
|
+
|
|
|
+ // 保存图片到服务器
|
|
|
+ file_put_contents($base_dir . $imagePath_one, $imageData);
|
|
|
+ Db::table('wa_users')->where('id', $request->user_data['id'])->update(['identity_imgone' => $imagePath_one]);
|
|
|
|
|
|
- #[Apidoc\Title("上传图片base64")]
|
|
|
- #[Apidoc\Url("api/base_file.html")]
|
|
|
- #[Apidoc\Method("POST")]
|
|
|
- #[Apidoc\Param("baseimg", type: "string", require: true, desc: "base64编码")]
|
|
|
- #[Apidoc\Header("token", type: "string", require: true, desc: "身份令牌Token", mock: "@token")]
|
|
|
- #[Apidoc\Returned(name: "url", type: "string", require: true, desc: '前端展示地址', default: '12317309127904')]
|
|
|
- public function base_file(Request $request): Response
|
|
|
- {
|
|
|
- $param = $request->all();
|
|
|
- try {
|
|
|
- $base64Image_one = $param['baseimg']; // 获取Base64字符串
|
|
|
+ }
|
|
|
+ if (!empty($param['identity_imgtwo'])) {
|
|
|
+ $base64Image_one = $param['identity_imgtwo']; // 获取Base64字符串
|
|
|
$imageData = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64Image_one)); // 移除数据URL的前缀并解码
|
|
|
$imageName = $request->user_data['id'] . time() . '.png'; // 生成一个唯一的文件名
|
|
|
|
|
|
- $data = '/upload/img/' . date('Ymd');
|
|
|
+ $data = '/upload/img/22' . date('Ymd');
|
|
|
$relative_dir = ltrim($data, '\\/');
|
|
|
$admin_public_path = rtrim(config('app.public_path', ''), '\\/');
|
|
|
$base_dir = $admin_public_path ? $admin_public_path . DIRECTORY_SEPARATOR : base_path() . '/plugin/admin/public/';
|
|
|
@@ -156,14 +215,17 @@
|
|
|
|
|
|
// 保存图片到服务器
|
|
|
file_put_contents($base_dir . $imagePath_one, $imageData);
|
|
|
- } catch (Exception $e) {
|
|
|
- return error('处理图片发生错误');
|
|
|
+ Db::table('wa_users')->where('id', $request->user_data['id'])->update(['identity_imgtwo' => $imagePath_one]);
|
|
|
+
|
|
|
}
|
|
|
- Db::table('wa_users')->where('id',$request->user_data['id'])->update(['protocol_img'=>$imagePath_one]);
|
|
|
- return adminsuccess([
|
|
|
- 'url' => imageToBase64($imagePath_one),
|
|
|
- 'path' => $imagePath_one,
|
|
|
- ]);
|
|
|
- }
|
|
|
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return error('处理图片发生错误');
|
|
|
+ }
|
|
|
+ return adminsuccess([
|
|
|
+ 'url' => imageToBase64($imagePath_one),
|
|
|
+ 'path' => $imagePath_one,
|
|
|
+ ]);
|
|
|
}
|
|
|
+
|
|
|
+}
|