user = auth('api')->user(); $this->userId = $this->user ? $this->user->id : 0; //如果用户被删除,会自动退出登录 if (!empty($this->user->deleted_at)) { $this->user->online = 0; $this->user->save(); auth('api')->logout(); } } /** * 获取用户详情 */ public function show(Request $request) { $id = $request->get('id'); if(!empty($id)){ $id = $id; }else{ $id = $this->userId; } $info = User::query()->where('id',$id)->first(); if(!$info){ return $this->error("用户不存在!"); } // 会员是否到期 if($info && !empty($info['end_time'])){ if(time() > strtotime($info['end_time'])){ $info->member_type = 1; $info->save(); } } $info->follow_count = UserFollow::query()->where('user_id',$id)->count(); $info->is_follow = UserFollow::query()->where('user_id',$this->userId)->where('to_user_id',$id)->count(); $info->ko_remaining_time = "0 일"; $info->zh_remaining_time = "0 天"; // 计算剩余时间 if(strtotime($info->end_time) > time() && $info->member_type == 2){ $info->ko_remaining_time = $this->koTimeCalculation(time(),strtotime($info->end_time)); $info->zh_remaining_time = $this->zhTimeCalculation(time(),strtotime($info->end_time)); } return $this->success($info); } /** * @param $beginDate 开始日期 2020-08-06 11:00:00 * @param $endDate 结束日期 2020-08-10 12:10:01 * @return string */ public function koTimeCalculation($beginDate, $endDate) { $common = $endDate - $beginDate; $a = floor($common/86400/360); //整数年 $b = floor($common/86400/30) - $a*12; //整数月 $c = floor($common/86400) - $a*360 - $b*30; //整数日 $d = floor($common/86400); //总的天数 $str = ''; if($a){ $str = $a."년"; } if($b){ $str = $str.$b."월"; } if($c){ $str = $str.$c."일"; } return $str; } /** * @param $beginDate 开始日期 2020-08-06 11:00:00 * @param $endDate 结束日期 2020-08-10 12:10:01 * @return string */ public function zhTimeCalculation($beginDate, $endDate) { $common = $endDate - $beginDate; $a = floor($common/86400/360); //整数年 $b = floor($common/86400/30) - $a*12; //整数月 $c = floor($common/86400) - $a*360 - $b*30; //整数日 $d = floor($common/86400); //总的天数 $str = ''; if($a){ $str = $a."年"; } if($b){ $str = $str.$b."月"; } if($c){ $str = $str.$c."天"; } return $str; } /** * 更新用户 */ public function upUserInfo(Request $request) { $res = (new UserService())->update($this->userId,$request->all()); return $this->success($res); } /** * @return void * 添加关注 */ public function addFollow(Request $request){ $user = User::query()->where('id',$request->get('user_id'))->first(); if(!$user){ return $this->error("用户不存在!"); } $collect = UserFollow::query()->where('user_id',$this->userId)->where('to_user_id',$user->id)->first(); if($collect){ return $this->error("您已关注过了!"); } $data = [ 'user_id' => $this->userId, 'to_user_id' => $user->id, ]; $res = UserFollow::query()->create($data); if(!$res){ return $this->error("操作失败!"); } return $this->success(); } /** * 取消关注 * * @param Request $request * @return \Illuminate\Http\JsonResponse */ public function cancelFollow(Request $request) { $user = User::where('id',$request->get('user_id'))->first(); if (!$user){ return $this->error('用户不存在!'); } $collect = UserFollow::where('user_id',$this->userId)->where('to_user_id',$user->id)->first(); if(!$collect){ return $this->error("您还未关注!"); } $delete = UserFollow::where('user_id',$this->userId)->where('to_user_id',$user->id)->delete(); if (!$delete){ return $this->error('取消关注失败!'); } return $this->success(); } /** * 删除用户 */ public function destroy(Request $request) { $user = User::query()->where('id',$this->userId)->first(); if(!$user){ return $this->error("用户不存在!"); } $user->delete(); return $this->success(); } /** * 更新用户头像 */ public function avatar(Request $request) { $validator = Validator::make($request->all(), [ 'avatar' => 'required', ]); if($validator->fails()){ return $this->error($validator->errors()->first(),ErrorCode::PARAMS_ERROR); } $res = (new UserService())->update($this->userId,$request->all()); return $this->success($this->userId); } //按照首字母进行分组排序,类似手机通讯录(注意:get()后面直接 ->toArray()在数据为空的时候导致报错) public function groupByInitial(Request $request) { $keyword = $request->input('keyword', ''); $list = User::query() ->select('id', 'name', 'avatar') ->when($keyword, function ($query, $keyword){ $query->where('name', 'like', '%'.$keyword.'%'); }) ->whereNotNull('name') ->whereNull('deleted_at') ->get(); if($list->isNotEmpty()){ $list = $list->toArray(); $list = SortService::getResultList($list); }else{ $list = []; } return $this->success($list); } //判断是否允许解绑 public function isAllowUnbound() { $user = $this->user; $n = 0; if(!empty($user->mobile)){ $n++; } if(!empty($user->email)){ $n++; } if(!empty($user->open_id)){ $n++; } if($n==1){ return false; } return true; } /** * 账号停用 * * @return \Illuminate\Http\JsonResponse */ public function accountStop(){ if ($this->user->is_stop){ return $this->error('当前账号已停用!'); } $this->user->is_stop = 1; $this->user->save(); return $this->success(); } /** * 账号启用 * * @return \Illuminate\Http\JsonResponse */ public function accountOpen(){ if ($this->user->is_stop == 0){ return $this->error('当前账号已启用!'); } $this->user->is_stop = 0; $this->user->save(); return $this->success(); } }