| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- namespace App\Http\Controllers\V1;
- use App\Models\User;
- use App\Models\UserFollow;
- use App\Services\Api\SortService;
- use App\Services\Api\UserService;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Validator;
- use App\Services\Base\ErrorCode;
- use PHPUnit\Util\Exception;
- use App\Services\SmsServer;
- class UserController extends Controller
- {
- public function __construct()
- {
- $this->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();
- }
- /**
- * 删除用户
- */
- 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;
- }
- }
|