Browse Source

上传图片接口

wufeng 2 years ago
parent
commit
e642486f37
4 changed files with 63 additions and 11 deletions
  1. 13 5
      app/admin/controller/Common.php
  2. 10 2
      app/admin/logic/CommonLogic.php
  3. 3 3
      app/admin/route/app.php
  4. 37 1
      app/common.php

+ 13 - 5
app/admin/controller/Login.php → app/admin/controller/Common.php

@@ -2,14 +2,14 @@
 
 namespace app\admin\controller;
 
-use app\admin\logic\LoginLogic;
+use app\admin\logic\CommonLogic;
 use app\BaseController;
 use think\exception\ValidateException;
 use think\facade\Config;
 use think\facade\Validate;
 
-//登录相关
-class Login extends BaseController
+//公共
+class Common extends BaseController
 {
 
     //登录
@@ -21,13 +21,21 @@ class Login extends BaseController
 
         if (!$val->check($param)) throw new ValidateException($val->getError());
 
-        return LoginLogic::Logic($param);
+        return CommonLogic::Logic($param);
     }
 
     //登出
     public function logout()
     {
-        return LoginLogic::logout($this->request->post('token',''));
+        return CommonLogic::logout($this->request->post('token', ''));
+    }
+
+    //上传图片
+    public function upload()
+    {
+        $files = $this->request->file('image');
+
+        return CommonLogic::upload($files);
     }
 
 

+ 10 - 2
app/admin/logic/LoginLogic.php → app/admin/logic/CommonLogic.php

@@ -7,7 +7,7 @@ use app\model\AdminTokenModel;
 use app\model\CommonModel;
 use think\response\Json;
 
-class LoginLogic
+class CommonLogic extends BaseLogic
 {
     //登录
     public static function Logic(array $data = []): Json
@@ -38,9 +38,17 @@ class LoginLogic
     public static function logout(string $token = ''): Json
     {
 
-        $info = AdminTokenModel::where(['token' => $token])->save(['token' => '','expiretime'=>date('Y-m-d H:i:s')]);
+        $info = AdminTokenModel::where(['token' => $token])->save(['token' => '', 'expiretime' => date('Y-m-d H:i:s')]);
 
         return $info ? json_show(CommonModel::$success, '登出成功') : json_show(CommonModel::$error_param, '登出失败');
     }
 
+    //上传图片
+    public static function upload($files): Json
+    {
+        $list = UploadImg($files);
+
+        return (is_array($list) && !empty($list)) ? json_show(CommonModel::$success, '上传成功', $list) : json_show(CommonModel::$error_default, '上传失败');
+    }
+
 }

+ 3 - 3
app/admin/route/app.php

@@ -2,9 +2,9 @@
 
 use think\facade\Route;
 
-Route::rule('login', 'admin/Login/login');//登录
-Route::rule('logout', 'admin/Login/logout');//登出
-Route::rule('upload', 'admin//upload');//上传图片?????????????????
+Route::rule('login', 'admin/Common/login');//登录
+Route::rule('logout', 'admin/Common/logout');//登出
+Route::rule('upload', 'admin/Common/upload');//上传图片
 
 //【版本】
 Route::rule('version', 'admin/Version/lastVersion');//最新版本

+ 37 - 1
app/common.php

@@ -163,4 +163,40 @@ if (!function_exists('randomkeys')) {
         }
         return $returnStr;
     }
-}
+}
+
+
+//上传图片
+/**
+ * @param $files
+ * @return array
+ */
+if (!function_exists('UploadImg')) {
+
+    function UploadImg($files): array
+    {
+        $savename = [];
+        $files = !is_array($files) ? [$files] : $files;
+        //验证
+        validate([
+            'imgFile' => [
+                'fileSize' => 10240000,
+                'fileExt' => 'jpg,jpeg,png,bmp,gif',
+                'fileMime' => 'image/jpeg,image/png,image/gif'
+            ]
+        ])->check(['imgFile' => $files]);
+
+        foreach ($files as $file) {
+            $url = Filesystem::disk('public')
+                ->putFile('topic/' . date("Ymd"), $file, function () use ($file) {
+                    return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . "_" . date('YmdHis'));
+                });
+            $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
+            $temp = ["url" => $url, "name" => $name];
+            $savename[] = $temp;
+        }
+
+        return $savename;
+
+    }
+}