Browse Source

内容图片

wzq 5 years ago
parent
commit
a26d39340a
2 changed files with 44 additions and 0 deletions
  1. 4 0
      app/Repositories/Post/PostRepository.php
  2. 40 0
      app/Traits/UserTrait.php

+ 4 - 0
app/Repositories/Post/PostRepository.php

@@ -81,6 +81,10 @@ class PostRepository
                 'status_code' => 500
             ]);
         }
+        if(isset($request['is_point']) && $request['is_point']){
+            $request['img'] = $this->uploadImage($request['img']);
+            Log::debug('内容上传图片结果'.json_encode($request['img']));
+        }
         //验证话题
         $topicIdsArray = $this->topic->whereIn('id', explode(',', $request['topic_ids']))->pluck('id')->toArray();
         $topicCount = count($topicIdsArray);

+ 40 - 0
app/Traits/UserTrait.php

@@ -36,4 +36,44 @@ trait UserTrait
         }
 
     }
+
+    public function uploadImage($img) {
+        $path = public_path('image');
+        if (!file_exists($path)){
+            mkdir ($path,0777,true);
+        }
+        $fileUrl = $path.date('/Ymd').time().'.jpg';
+        $content = file_get_contents($img);
+        file_put_contents($fileUrl, $content);
+
+
+        try {
+            $url = config("customer.manage_service_url").'/config/upload';
+            $array = [
+                'multipart' => [
+                    [
+                        'name'     => 'image',
+                        'contents' => 'data',
+                        'headers'  => ['X-Baz' => 'bar'],
+                        'filename' => $fileUrl
+                    ]
+                ],
+                'query' => [],
+                'http_errors' => false,
+                'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
+            ];
+            $ossUrl = http($url,$array, false, 'post');
+        } catch (\Exception $e) {
+            $ossUrl = '';
+        }
+        unlink($fileUrl);
+        return $ossUrl;
+
+    }
+
+    function downDown($file_url)
+    {
+
+    }
+
 }