xiaogang 4 éve
szülő
commit
d1ca182e6a

+ 7 - 0
app/Http/Controllers/Api/UserController.php

xqd
@@ -246,9 +246,16 @@ class UserController extends Controller
         return response()->json(['message'=>'上传成功']);
     }
 
+    /**
+     * 删除图片或者视频
+     * @param Request $request
+     * @return \Illuminate\Http\JsonResponse|void
+     */
     public function del_file(Request $request){
         try {
 
+
+            $res = $this->userService->del_file($request);
         }catch (\Exception $exception){
             return $this->response->errorForbidden($exception->getMessage());
         }

+ 57 - 0
app/Services/UserService.php

xqd xqd
@@ -8,11 +8,13 @@ use App\Http\Params\ProblemParam;
 use App\Models\PaymentLogModel;
 use App\Models\User;
 use App\Models\UserBlacklistModel;
+use App\Models\UserInfoModel;
 use App\Models\UserInviteLog;
 use App\Models\UserLookModel;
 use App\Models\UserProblemModel;
 use App\Models\UserVipLogModel;
 use App\Models\VipModel;
+use Illuminate\Http\Request;
 use Illuminate\Support\Facades\DB;
 use PHPUnit\Util\Exception;
 use function Symfony\Component\Translation\t;
@@ -200,5 +202,60 @@ class UserService
         }
         return true;
     }
+
+
+    /**
+     * 删除图片
+     */
+    public function del_file(Request $request){
+        $user = auth('api')->user();
+        $url = $request->post('url');
+        if($url==''){
+            throw new Exception("请选择要删除的图片");
+        }
+        $type = $request->post('type',1);
+        $userinfo = UserInfoModel::query()->where('user_id',$user->id)->first();
+        if($type==1){
+            //删除相册
+            $photo = json_decode($userinfo['photo'],true);
+            if(count($photo)>0){
+                $has = false;
+                foreach ($photo as $k=>$v){
+                    if($v['url']==$url){
+                        $has = true;
+                        unset($photo[$k]);
+
+                    }
+                }
+                if(!$has){
+                    throw new Exception("删除错误");
+                }else{
+                    $upd['photo'] = array_values($photo);
+                }
+            }else{
+                throw new Exception("删除错误");
+            }
+        }else{
+            //删除视频
+            $video = json_decode($userinfo['video'],true);
+            if(count($video)>0){
+                $has = false;
+                foreach ($video as $k=>$v){
+                    if($v==$url){
+                        $has = true;
+                        unset($video[$k]);
+                    }
+                }
+                if(!$has){
+                    throw new Exception("删除错误");
+                }else{
+                    $upd['video'] = array_values($video);
+                }
+            }else{
+                throw new Exception("删除错误");
+            }
+        }
+        UserInfoModel::query()->where('user_id',$user->id)->update($upd);
+    }
 }