public static function findLinkById($id_1, $id_2) { if (!$id_1 || !$id_2) { throw new Exception("请传入有效的用户ID", 1); } if ($id_1 < $id_2) { $u_id_1 = $id_1; $u_id_2 = $id_2; } else { $u_id_1 = $id_2; $u_id_2 = $id_1; } $userFriend = UsersFriend::where('u_id_1', '=', $u_id_1)->where('u_id_2', '=', $u_id_2)->first(); if (empty($userFriend->t_id)) { return UsersFriend::$RELATION_NONE; } return $userFriend; }
private function getUserList($u_id, $status = 0, $gender = 0) { $query = UsersFriend::with(['user1', 'user2']); if ($status) { $query = $query->where('t_status', '=', $status); } $list = $query->where(function ($q) use($u_id) { $q->where('u_id_1', '=', $u_id)->orWhere('u_id_2', '=', $u_id); })->get(); $data = []; foreach ($list as $key => $userLink) { if ($userLink->t_status == 1) { $type = $userLink->t_inviter == $u_id ? UsersFriend::$RELATION_INVITED : UsersFriend::$RELATION_PEDDING_CONFIRM; } else { $type = UsersFriend::$RELATION_CONFIRMED; } if ($userLink->u_id_1 == $u_id) { $tmp = $userLink->user2->showInList(); } else { $tmp = $userLink->user1->showInList(); } if ($gender && $tmp['gender'] != $gender) { continue; } $tmp['status'] = $type; $data[] = $tmp; } return $data; }
/** * Display the specified resource. * * @param int $id * @return Response */ public function show($id) { $token = Input::get('token', ''); $u_id = Input::get('u_id', 0); try { $user = User::chkUserByToken($token, $u_id); $show_user = User::find($id); if (empty($show_user)) { throw new Exception("请求的用户不存在", 3001); } $show_user->load(['school', 'favorites' => function ($q) { $q->where('favorites.u_id', '=', $this->u_id); }, 'praises' => function ($q) { $q->where('praises.u_id', '=', $this->u_id); }]); $data = $show_user->showDetail(); $is_friend = UsersFriend::$RELATION_NONE; $userFriend = UsersFriend::findLinkById($u_id, $show_user->u_id); if ($userFriend === UsersFriend::$RELATION_NONE) { } else { if ($userFriend->t_status == 1) { $is_friend = $userFriend->t_inviter == $u_id ? UsersFriend::$RELATION_INVITED : UsersFriend::$RELATION_PEDDING_CONFIRM; } else { $is_friend = UsersFriend::$RELATION_CONFIRMED; } } $data['is_friend'] = $is_friend; $data['is_praised'] = 0; $data['is_favorited'] = 0; if (count($show_user->praises) > 0) { $data['is_praised'] = 1; } if (count($show_user->favorites) > 0) { $data['is_favorited'] = 1; } $re = ['result' => 2000, 'data' => $data, 'info' => '读取用户成功']; } catch (Exception $e) { $re = ['result' => 2001, 'data' => [], 'info' => $e->getMessage()]; } return Response::json($re); }