commonUtils = new CommonUtilsService(); } /** * 成为临时会员 * @return [type] [description] */ public function getTempMember($user_id) { $user = User::find($user_id); if (empty($user)) { return; } if ($user->type == 'marriage') { return; } $temp_member = TempMember::where('user_id', $user->id)->count(); if (empty($temp_member)) { TempMember::create([ 'user_id' => $user_id, 'status' => 0, ]); $user->temp_member = 1; //隐身 // $user->hidden = 1; $user->save(); } return; } /** * 隐身用户 * @param [type] $user_id [description] * @return [type] [description] */ public function userHidden($user_id, $other_user_id = null) { $user = User::find($user_id); if ($other_user_id) { $history = UserHiddenHistory::where(['user_id' => $user_id, 'other_user_id' => $other_user_id])->first(); if (empty($history)) { UserHiddenHistory::create([ 'user_id' => $user_id, 'other_user_id' => $other_user_id ]); } else { $history->delete(); } } else { if ($user->hidden) { $user->hidden = 0; } else { $user->hidden = 1; } $user->save(); } return; } /** * 是否隐身 * @param [type] $user_id [description] * @param [type] $other_user_id [description] * @return boolean [description] */ public function isHidden($user_id, $other_user_id, $type) { //是否对所有人隐身 $user = User::find($user_id); if ($type == 'all') { if ($user->hidden) { return true; } else { $history = UserHiddenHistory::where(['user_id' => $user_id, 'other_user_id' => $other_user_id])->first(); if ($history) { return true; } return false; } } elseif ($type == 'alone') { $history = UserHiddenHistory::where(['user_id' => $user_id, 'other_user_id' => $other_user_id])->first(); if ($history) { return true; } return false; } elseif ($type == 'unify') { if ($user->hidden) { return true; } return false; } } public function isHiddenV2($user, $other_user, $type) { //是否对所有人隐身 if ($type == 'all') { if ($user->hidden) { return true; } else { $history = UserHiddenHistory::where(['user_id' => $user->id, 'other_user_id' => $other_user->id])->first(); if ($history) { return true; } return false; } } elseif ($type == 'alone') { $history = UserHiddenHistory::where(['user_id' => $user->id, 'other_user_id' => $other_user->id])->first(); if ($history) { return true; } return false; } elseif ($type == 'unify') { if ($user->hidden) { return true; } return false; } } /** * 拉入黑名单 * @param [type] $user_id [description] * @param [type] $other_user_id [description] * @return [type] [description] */ public function blacklistFriend($user_id, $other_user_id) { Linking::where('user_id', $user_id)->where('user_linking_id', $other_user_id)->forceDelete(); Linking::where('user_id', $other_user_id)->where('user_linking_id', $user_id)->forceDelete(); LinkingBlacklist::firstOrCreate([ 'user_id' => $user_id, 'other_user_id' => $other_user_id, ]); //删除聊天联系人 MessageLinkman::where(function ($sql) use ($user_id, $other_user_id) { $sql->where(['user_id' => $user_id, 'other_user_id' => $other_user_id]) ->orWhere(function ($query) use ($user_id, $other_user_id) { $query->where(['user_id' => $other_user_id, 'other_user_id' => $user_id]); }); })->delete(); //更新缓存 $user = User::find($user_id); $other_user = User::find($other_user_id); if(!$user || !$other_user){ return ; } $user->updateCacheUser('friend_count'); $other_user->updateCacheUser('friend_count'); return; } /** * 黑名单 * @param [type] $user_id [description] * @return [type] [description] */ public function blacklist($user_id) { $blacklist = LinkingBlacklist::where('user_id', $user_id)->orderBy('id', 'desc')->paginate(); foreach ($blacklist as $black) { $linking_user = User::find($black->other_user_id); if ($linking_user) { $avatar = Wechat::where('user_id', $linking_user->id)->value('avatar2'); $linking_user->avatar = $avatar; } $black->linking_user = $linking_user; } return $blacklist; } /** * 移除黑名单 */ public function deleteBlacklist($id) { LinkingBlacklist::where('id', $id)->delete(); return; } /** * 移除黑名单 * @param [type] $user_id [description] * @return [type] [description] */ public function deleteBlacklistByUser($user_id) { $blacklist = LinkingBlacklist::where('user_id', auth()->id())->where('other_user_id', $user_id)->first(); if ($blacklist) { $blacklist->delete(); } return; } /** * 举报 * @param [type] $user_id [description] * @param [type] $other_user_id [description] * @return [type] [description] */ public function complaint($request, $user_id, $other_user_id) { $type = $request->input('type', 'user'); $content = $request->input('content'); $photos = $request->input('photos', []); ComplaintHistory::create([ 'user_id' => $user_id, 'complaint_id' => $other_user_id, 'type' => is_array($type) ? json_encode($type) : $type, 'content' => $content, 'photos' => count($photos) > 0 ? json_encode($photos) : null, 'label' => $request->input('label', null), ]); //添加負面分 $user = User::where('id', $other_user_id)->increment('negative_score', 2); $nickname = User::where('id', $user_id)->value('nickname'); $mobiles = Redis::smembers('repot_notice_mobile'); $param['message'] = '用户【' . $nickname . '】提交了反馈【' . $content . '】'; foreach ($mobiles as $mobile) { $param['mobile'] = $mobile; SendEasySms::dispatch($param)->onQueue('love'); } return; } /** * 反馈 * @param [type] $request [description] * @param [type] $user_id [description] * @return [type] [description] */ public function feedback($request, $user_id) { $content = $request->input('content'); $photos = $request->input('photos', []); FeedbackHistory::create([ 'user_id' => $user_id, 'type' => $request->input('type', 'other'), 'type_id' => $request->type_id ?: 0, 'content' => $content, 'photos' => json_encode($photos), 'contact' => $request->input('contact'), 'star' => $request->input('star',null), 'address' => $request->input('address',null), ]); $nickname = User::where('id', $user_id)->value('nickname'); $mobiles = Redis::smembers('repot_notice_mobile'); $param['message'] = '用户【' . $nickname . '】提交了反馈【' . $content . '】'; foreach ($mobiles as $mobile) { $param['mobile'] = $mobile; SendEasySms::dispatch($param)->onQueue('love'); } return true; } //是否在黑名单 public function isBlacklist($user_id, $other_user_id) { $count = LinkingBlacklist::where(['user_id' => $other_user_id, 'other_user_id' => $user_id])->count(); $status = 0; if ($count) { //是否在别人黑名单 return $status = 1; } $count = LinkingBlacklist::where(['user_id' => $user_id, 'other_user_id' => $other_user_id])->count(); if ($count) { //别人是否在你的黑名单 return $status = 2; } return $status; } /** * 朋友圈动态数 * @param [type] $user_id [description] * @return [type] [description] */ public function momentCount($user_id) { $moment_count = Moment::where('user_id', $user_id)->count(); return $moment_count; } /** * 反馈列表 * @return [type] [description] */ public function feedbacks($request) { $status = $request->input('status', 0); $histories = FeedbackHistory::with('user')->whereHas('user', function ($sql) { $sql->where('id', '>', 0); })->orderBY('id', 'desc'); if ($status != 2) { $histories = $histories->where('status', $status); } $keyword = $request->input('keyword'); if ($keyword) { $keyword = trim($keyword); $histories = $histories->where(function ($sql) use ($keyword) { $sql->whereHas('user', function ($sql) use ($keyword) { $sql->where('name', 'like', '%' . $keyword . '%') ->where('nickname', 'like', '%' . $keyword . '%') ->orWhere('mobile', 'like', '%' . $keyword . '%') ->orWhere('id', $keyword); })->orWhere('content', 'like', '%' . $keyword . '%'); }); } $admin_type = $request->session()->get('admin_type'); if ($admin_type == 'paas_admin') { $paas_obj = $request->session()->get('paas_obj'); $paas_user_ids = $this->paasUserIds($paas_obj->name, 'MAIN'); $histories = $histories->whereIn('user_id', $paas_user_ids); } $start_time = $request->input('start_time'); $end_time = $request->input('end_time'); if ($start_time && $end_time) { $users = $histories->whereBetween('created_at', [$start_time, $end_time]); } if ($request->type) { $histories = $histories->where('type', $request->type); } else { $histories = $histories->where('type', '!=', 'team'); } $histories = $histories->paginate(); foreach ($histories as $history) { $history->photos = json_decode($history->photos, true); if (!empty($history->user)) { $history->user->avatar = $history->user->userAvatar(); } } return $histories; } /** * 反馈列表 * @return [type] [description] */ public function feedbacksV2($request) { $status = $request->input('status', 0); $keyword = $request->input('keyword'); $histories = FeedbackHistory::with('user:id,nickname,sex,app_avatar,circle_avatar,mobile','user.profileCourtship:id,user_id,city' ,'operatorUser:id,nickname,circle_avatar,app_avatar') ->whereHas('user', function ($sql) { $sql->where('id', '>', 0); })->orderBY('id', 'desc'); if ($status != 3) { $histories = $histories->where('status', $status); } if($request->appraise){ $histories = $histories->whereNotNull('star'); if($request->status == 0) $histories = $histories->groupBy('user_id'); }else{ $histories = $histories->whereNull('star'); } if ($keyword) { $keyword = trim($keyword); $histories = $histories->where(function ($sql) use ($keyword) { $sql->whereHas('user', function ($sql) use ($keyword) { $sql->where('nickname', 'like', '%' . $keyword . '%') ->orWhere('mobile', 'like', '%' . $keyword . '%') ->orWhere('id', $keyword); }) ->orWhere('content', 'like', '%' . $keyword . '%') ->orWhere('address', 'like', '%' . $keyword . '%'); }); } if($request->city){ $histories = $histories->whereHas('user.profileCourtship',function($sql) use($request){ $sql->where('city','like','%'.$request->city.'%'); }); } $admin_type = $request->session()->get('admin_type'); if ($admin_type == 'paas_admin') { $paas_obj = $request->session()->get('paas_obj'); $paas_user_ids = $this->paasUserIds($paas_obj->name, 'MAIN'); $histories = $histories->whereIn('user_id', $paas_user_ids); } $start_time = $request->input('start_time'); $end_time = $request->input('end_time'); if ($start_time && $end_time) { $users = $histories->whereBetween('created_at', [$start_time, $end_time]); } if ($request->type) { $histories = $histories->where('type', $request->type); } else { $histories = $histories->where('type', '!=', 'team'); } $histories = $histories->paginate(); foreach ($histories as $history) { $history->photos = json_decode($history->photos, true); if (!empty($history->user)) { $history->user->avatar = $history->user->userAvatar(); } if ($history->remark_type == 2) { $history->remark = json_decode($history->remark, true); } $history->user->mobile = ''; } return $histories; } /** * 修改反馈状态 */ public function changeFeedbackStatus($request, $feedback_id) { $feedback = FeedbackHistory::findOrFail($feedback_id); $status = $request->input('status', 0); $feedback->status = $status; $feedback->save(); return; } /** * 修改投诉状态 * @param [type] $request [description] * @param [type] $complaint_id [description] * @return [type] [description] */ public function changeComplaintStatus($request, $complaint_id) { $complaint = ComplaintHistory::findOrFail($complaint_id); $status = $request->input('status', 0); $remark = $request->content; $complaint->status = $status; $complaint->remark = $remark; $complaint->save(); return; } public function changeFeedbackStatusV2($request, $feedback_id) { $user_id = auth()->id(); $feedback = FeedbackHistory::findOrFail($feedback_id); $status = $request->input('status', 0); $remark = $request->input('remark'); $feedback->status = $status; $feedback->operator = $user_id; $feedback->remark = $remark; $feedback->operate_time = date('Y-m-d H:i:s'); $feedback->save(); return; } public function changeFeedbackStatusV3($request, $feedback_id) { //处理人信息 $user_id = auth()->id(); $user = User::find($user_id); $nickname = $user->nickname ? $user->nickname : $user->name; $avatar = $user->userAvatar($user_id); $feedback = FeedbackHistory::findOrFail($feedback_id); $status = $request->input('status', 0); $content = $request->content; if (empty($content)) return $this->failure('请输入详细的处理信息'); if (empty($feedback->remark)) { $record = array( array('沟通方式' => $content[0], '跟进内容' => $content[1], '达成结果' => $content[2], '跟进人' => auth()->id(), '跟进时间' => date('Y-m-d H:i:s'), '跟进人头像' => $avatar, '跟进人昵称' => $nickname)); } else { $desc = json_decode($feedback->remark, true); $array = ['沟通方式' => $content[0], '跟进内容' => $content[1], '达成结果' => $content[2], '跟进人' => auth()->id(), '跟进时间' => date('Y-m-d H:i:s'), '跟进人头像' => $avatar, '跟进人昵称' => $nickname]; array_push($desc, $array); $record = $desc; } $feedback->status = $status; $feedback->operator = $user_id; $feedback->remark = json_encode($record, JSON_UNESCAPED_UNICODE); $feedback->remark_type = 2; $feedback->operate_time = date('Y-m-d H:i:s'); $feedback->save(); return; } public function changeComplaintStatusV2($request, $complaint_id) { $user_id = auth()->id(); $complaint = ComplaintHistory::findOrFail($complaint_id); $status = $request->input('status', 0); $remark = $request->content; $complaint->status = $status; $complaint->remark = $remark; $complaint->operator = $user_id; $complaint->operate_time = date('Y-m-d H:i:s'); $complaint->save(); return; } public function changeComplaintStatusV3($request, $complaint_id) { //处理人信息 $user_id = auth()->id(); $user = User::find($user_id); $nickname = $user->nickname ? $user->nickname : $user->name; $avatar = $user->userAvatar($user_id); $status = $request->input('status', 0); $content = $request->content; if (empty($content)) return $this->failure('请输入详细的处理信息'); $complaint = ComplaintHistory::findOrFail($complaint_id); if (empty($complaint->remark)) { $record = array( array('沟通方式' => $content[0], '跟进内容' => $content[1], '达成结果' => $content[2], '跟进人' => auth()->id(), '跟进时间' => date('Y-m-d H:i:s'), '跟进人头像' => $avatar, '跟进人昵称' => $nickname)); } else { $desc = json_decode($complaint->remark); $array = ['沟通方式' => $content[0], '跟进内容' => $content[1], '达成结果' => $content[2], '跟进人' => auth()->id(), '跟进时间' => date('Y-m-d H:i:s'), '跟进人头像' => $avatar, '跟进人昵称' => $nickname]; array_push($desc, $array); $record = $desc; } $complaint->status = $status; $complaint->remark = json_encode($record, JSON_UNESCAPED_UNICODE); $complaint->operator = $user_id; $complaint->remark_type = 2; $complaint->operate_time = date('Y-m-d H:i:s'); $complaint->save(); return; } /** * 修改隐藏信息状态 * @param [type] $request [description] * @return [type] [description] */ public function changeHiddenProfile($request) { $type = $request->input('type', 'NONE'); $reason = $request->input('reason', ''); $convert_marriage = $request->input('convert_marriage',0);//选择已经在福恋找到伴侣了 询问是否转为介绍人 $user = auth()->user(); $cph_status = false; //隐藏资料状态 $user->hidden_profile = $type; if ($type === 'ALLSEX') { //隐身访问 $user->hidden = 1; $cph_status = true; $else_type = 'NONE';//旧数据 old_data; //增加用户备注 ClientComment::create([ 'user_id'=>$user->id, 'maker_user_id'=>$user->id, 'type'=>'active', 'comment'=>'关闭资料,原因:'.$reason, ]); $find_love_type = ''; switch ($reason){ case '已经在福恋找到伴侣了': $find_love_type = 1; break; case '通过其它方式找到伴侣了': $find_love_type = 2; break; } //增加脱单记录 if ($find_love_type) { LoveLink::withTrashed()->updateOrCreate(['user_id'=>$user->id], ['deleted_at'=>null,'type'=>$find_love_type]); } //转为介绍人 if ($find_love_type && $convert_marriage == 1) { $user->type = 'marriage'; $user->hidden = 0; $user->hidden_profile = 'NONE'; $marry = ProfileMarriage::firstOrCreate(['user_id' => $user->id]); $court = ProfileCourtship::firstOrCreate(['user_id' => $user->id]); $marry->belief = $court->belief; $marry->sex = $court->sex; $marry->company = $court->company; $marry->birthday = $court->birthday; $marry->city = $court->city; $marry->province = $court->province; $marry->location_offset_status = $court->location_offset_status; $marry->state = $court->state; $marry->degree = $court->degree; $marry->wechat_id = $court->wechat_id; $marry->wechat_qrcode = $court->wechat_qrcode; $marry->wechat_id = $court->wechat_id; $marry->save(); } } elseif ($type === 'NONE') { $user->hidden = 0; $else_type = 'ALLSEX';//旧数据 old_data; //关闭脱单记录 LoveLink::where('user_id', $user->id)->delete(); } AreaUser::where('user_id', $user->id)->delete(); $user->save(); $user->cacheUser(); // $CloseProfileHistory = new CloseProfileHistory(); CloseProfileHistory::updateOrCreate(['user_id' => $user->id], ['operator' => 0, 'reason' => $reason]); $old_data['hidden_profile'] = $else_type; $new_data['hidden_profile'] = $type; ProfileChangeHistory::create([ 'user_id' => $user->id, 'owner_user_id' => $user->id, 'new_content' => json_encode($new_data), 'old_content' => json_encode($old_data) ]); return; } /** * 发系统消息 */ public function sendNotice($user_id, $send_user_id, $type, $content, $message = '', $type_id = 0, $path_url=null, $path_type = null) { $data = [ 'user_id' => $user_id, 'send_user_id' => $send_user_id, 'type' => $type, 'type_id' => $type_id, 'content' => $content, 'message' => $message, 'status' => 0, 'path_url'=> $path_url, 'path_type' => $path_type, ]; $notice = Notice::where('user_id', $user_id)->where('send_user_id', $send_user_id)->where('type', $type)->where('type_id', $type_id)->orderBy('id', 'desc')->first(); if ($notice && $type == 'follow') { $notice->update($data); } else { $notice = Notice::create($data); } return $notice; } /** * 修改系统消息 */ public function updateNotice($notice, $param = []) { $notice->update($param); return; } /** * 浏览人数 * @param [type] $user_id [description] * @param [type] $start_time [description] * @param [type] $end_time [description] * @return [type] [description] */ public static function previewCount($user_id, $start_time, $end_time) { $other_user_ids = UserPreviewHistory::where('user_id', $user_id)->where('created_at', '>', $start_time)->where('created_at', '<', $end_time)->distinct('preview_user_id')->pluck('preview_user_id'); return count($other_user_ids); } /** * 是否完成资料 * @param [type] $user_id [description] * @return boolean [description] */ public static function isCompleteProfile($user_id) { $user = User::findOrFail($user_id); if ($user->type == 'single') { $profile = ProfileCourtship::where('user_id', $user_id)->first(); if (empty($profile)) { return false; } if (!$profile->sex || !$profile->birthday || !$profile->state || !$profile->stature || !$profile->weight || !$profile->province || !$profile->resident_province || !$profile->belief || !$profile->introduction || !$profile->ideal_mate) { return false; } if (!$user->industry || !$user->industry_sub || !$user->nickname || !$user->photo) { return false; } return true; } else { $profile = ProfileMarriage::where('user_id', $user_id)->first(); if (empty($profile)) { return false; } if (empty($profile->sex) || empty($profile->birthday) || empty($profile->belief)) { return false; } return true; } } public function profileIntegrity($profile, $user, $profile_data = [ 'birthday', 'state', 'stature', 'weight', 'province', 'city', 'resident_province', 'resident_city', 'introduction', 'ideal_mate', 'interest_hobby', 'income','marry_by_time' ]) { $user_data = ['nickname', 'industry', 'industry_sub', 'belief', 'sex', 'mobile', 'photo']; $count = 0; if (!empty($profile)) { foreach ($profile_data as $field) { if ($profile->$field) { $count++; } } } foreach ($user_data as $field) { if ($user->$field) { $count++; } } $total_count = count($user_data) + count($profile_data); return compact('count', 'total_count'); } //修改资料时判断 public static function isCompleteProfileV4($user, $platform = 'mp') { if ($user->type == 'single') { $profile = $user->profileCourtship; if (empty($profile)) { return ['msg' => '生日', 'level' => 1]; } unset($user->profileCourtship); if (empty($profile->introduction)) { return ['msg' => '个人介绍', 'level' => 1]; } if (empty($profile->ideal_mate)) { return ['msg' => '理想对象', 'level' => 1]; } if ($platform == 'mp') { if (empty($profile->interest_hobby)) { return ['msg' => '兴趣爱好']; } } elseif ($platform == 'app') { if (empty($profile->interest_label)) { return ['msg' => '兴趣爱好', 'level' => 1]; } if (empty($profile->income)) { return ['msg' => '收入', 'level' => 2]; } } if (empty($user->nickname)) { return ['msg' => '昵称', 'level' => 2]; } if (empty($user->sex)) { return ['msg' => '性别', 'level' => 2]; } if (empty($profile->birthday)) { return ['msg' => '生日', 'level' => 2]; } if (empty($profile->state)) { return ['msg' => '单身状态', 'level' => 2]; } if (empty($profile->stature)) { return ['msg' => '身高', 'level' => 2]; } if (empty($profile->weight)) { return ['msg' => '体重', 'level' => 2]; } if (empty($profile->province) || empty($profile->city)) { return ['msg' => '所在城市', 'level' => 2]; } if (empty($profile->resident_province) || empty($profile->resident_city)) { return ['msg' => '成长地', 'level' => 2]; } // if (empty($profile->graduate_school)) { // return ['msg'=>'毕业院校', 'level'=>2]; // } // if (empty($profile->degree)) { // return ['msg'=>'最高学历', 'level'=>2]; // } if (empty($user->industry) || empty($user->industry_sub)) { return ['msg' => '行业/职业', 'level' => 2]; } if (empty($profile->belief)) { return ['msg' => '信仰', 'level' => 2]; } return false; } else { if ($platform == 'app') { return false; } $profile = $user->profileMarriage; if (empty($profile)) { return ['msg' => '性别']; } if (empty($profile->sex) || empty($profile->birthday) || empty($profile->company) || empty($profile->belief)) { return ['msg' => '性别']; } return false; } } //判断资料是否完善 public function isCompleteProfileV5($user) { $profile = $user->profileCourtship; if (empty($profile) || empty($user->belief) || empty($user->nickname) || empty($user->sex) || empty($profile->state) || empty($profile->birthday) || empty($profile->stature) || empty($profile->weight) || empty($profile->province) || empty($profile->city) || empty($profile->resident_province) || empty($profile->resident_city) || empty($user->industry) || empty($user->industry_sub) || empty($profile->introduction) || empty($profile->ideal_mate)) { unset($user->profileCourtship); return false; } unset($user->profileCourtship); return true; } public function isCompleteProfileV3($user) { if ($user->type == 'single') { $profile = $user->profileCourtship; if (empty($profile)) { return false; } if (!$profile->sex || !$profile->birthday || !$profile->state || !$profile->stature || !$profile->weight || !$profile->province || !$profile->resident_province || !$profile->degree || !$profile->graduate_school || !$profile->company || !$profile->belief || !$profile->introduction || !$profile->ideal_mate) { return false; } return true; } else { $profile = $user->profileMarriage; if (empty($profile)) { return false; } if (empty($profile->birthday) || empty($profile->company) || empty($profile->belief)) { return false; } return true; } } public static function isCompleteProfileV2($profile, $type) { if (empty($profile)) { return false; } if ($type == 'single') { if (!$profile->sex || !$profile->birthday || !$profile->state || !$profile->stature || !$profile->weight || !$profile->province || !$profile->resident_province || !$profile->degree || !$profile->graduate_school || !$profile->company || !$profile->belief || !$profile->introduction || !$profile->ideal_mate) { return false; } return true; } else { if (empty($profile->birthday) || empty($profile->company) || empty($profile->belief)) { return false; } return true; } } /** * 是否完善资料 * app调用 * @return boolean [description] */ public function isCompleteProfileV6($user = null) { if (empty($user)) { $user = auth()->user(); } $profile = $user->profileCourtship; if (empty($profile)) { $profile = new ProfileCourtship; $profile->user_id = $user->id; $profile->save(); } unset($user->profileCourtship); if (empty($user->nickname)) return ['code' => 1, 'msg' => '请完善昵称信息']; if (empty($user->sex) || empty($profile->sex)) return ['code' => 1, 'msg' => '请完善性别信息']; if (empty($user->photo)) return ['code' => 1, 'msg' => '请完善真人照片信息']; if (empty($user->is_approved)) return ['code' => 1, 'msg' => '请完善实名认证信息']; if (empty($user->industry_sub) || empty($user->industry)) return ['code' => 1, 'msg' => '请完善行业信息']; if (empty($user->belief) || empty($profile->belief)) return ['code' => 1, 'msg' => '请完善信仰信息']; if (empty($user->belief) || empty($profile->belief)) return ['code' => 1, 'msg' => '请完善信仰信息']; if (empty($profile->birthday)) return ['code' => 1, 'msg' => '请完善生日信息']; if (empty($profile->state)) return ['code' => 1, 'msg' => '请完善婚姻状态信息']; if (empty($profile->stature)) return ['code' => 1, 'msg' => '请完善身高信息']; if (empty($profile->weight)) return ['code' => 1, 'msg' => '请完善体重信息']; if (empty($profile->province) || empty($profile->city)) return ['code' => 1, 'msg' => '请完善生活城市信息']; if (empty($profile->resident_province) || empty($profile->resident_city)) return ['code' => 1, 'msg' => '请完善出生城市信息']; if (empty($profile->graduate_school)) return ['code' => 1, 'msg' => '请完善毕业学校信息']; if (empty($profile->degree)) return ['code' => 1, 'msg' => '请完善毕业学校信息']; if (empty($profile->introduction)) return ['code' => 1, 'msg' => '请完善个人简介信息']; if (empty($profile->ideal_mate)) return ['code' => 1, 'msg' => '请完善理想对象信息']; if (empty($profile->interest_label)) return ['code' => 1, 'msg' => '请完善兴趣爱好信息']; return true; } /** * 生活照 * @param [type] $user_id [description] * @return [type] [description] */ public function lifePhotos($user_id, $limit = 0, $desc = 'desc') { $photos = ProfilePhoto::where('user_id', $user_id); if ($limit) { $photos = $photos->limit($limit); } $photos = $photos->orderBy('id', $desc)->get(); return $photos; } /** * 单身信息 * @param [type] $user_id [description] * @return [type] [description] */ public function singleProfile($user_id) { $profile = ProfileCourtship::where("user_id", $user_id)->first(); if (!empty($profile)) { $profile->age = $this->commonUtils->getAge($profile->birthday); } else { $profile = ProfileCourtship::create([ 'user_id' => $user_id, ]); } return $profile; } /** * 介绍人信息 * @param [type] $user_id [description] * @return [type] [description] */ public function marriageProfile($user_id) { $profile = ProfileMarriage::where("user_id", $user_id)->first(); if (!empty($profile)) { $profile->age = $this->commonUtils->getAge($profile->birthday); } else { $profile = ProfileMarriage::create([ 'user_id' => $user_id, ]); } return $profile; } /** * 人脸识别 * @param [type] $img_url [description] * @param [type] $user_id [description] * @return [type] [description] */ public function faceDelect($img_url) { $akId = "LTAI4OgfBjALi3PX"; $akSecret = "CXFfHf0AesO4chUixsZjvqti0CX6i7"; $bodys = "{\"type\": 0, \"image_url\":\"$img_url\"}"; $url = "https://dtplus-cn-shanghai.data.aliyuncs.com/face/detect"; $result = $this->signature($url, $akId, $akSecret, $bodys); return $result; } public function faceDelectBaiDu($image_url) { $url = 'https://aip.baidubce.com/oauth/2.0/token'; $post_data['grant_type'] = 'client_credentials'; $post_data['client_id'] = 'IvSaovDBUDtghkV6komGmlhA'; $post_data['client_secret'] = 'baVStCHlKQ08X5M6SR4Q5lnA2GVge4t1'; $o = ""; foreach ($post_data as $k => $v) { $o .= "$k=" . urlencode($v) . "&"; } $post_data = substr($o, 0, -1); $res = Http::post($url, $post_data); $access_token = json_decode($res)->access_token; $url = 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=' . $access_token; // $bodys = "{\"image\":\"027d8308a2ec665acb1bdf63e513bcb9\",\"image_type\":\"FACE_TOKEN\",\"face_field\":\"faceshape,facetype\"}"; $data['image_type'] = 'URL'; $data['image'] = $image_url; $data['face_field'] = 'age,beauty,expression,face_shape,gender,glasses,landmark,landmark150,race,quality,eye_status,emotion,face_type,mask'; $data['max_face_num'] = 2; // $data['liveness_control'] = "LOW"; $bodys = json_encode($data); $res = Http::post($url, $bodys); return json_decode($res); } public function faceDelectByData($data) { $data = base64_encode($data); $akId = "LTAI4OgfBjALi3PX"; $akSecret = "CXFfHf0AesO4chUixsZjvqti0CX6i7"; $bodys = "{\"type\": 1, \"content\":\"$data\"}"; $url = "https://dtplus-cn-shanghai.data.aliyuncs.com/face/detect"; $result = $this->signature($url, $akId, $akSecret, $bodys); return $result; } public function signature($url, $akId, $akSecret, $content) { //更新api信息 $options = array( 'http' => array( 'header' => array( 'accept' => "application/json", 'content-type' => "application/json", 'date' => gmdate("D, d M Y H:i:s \G\M\T"), 'authorization' => '' ), 'method' => "POST", //可以是 GET, POST, DELETE, PUT 'content' => $content, //如有数据,请用json_encode()进行编码 ) ); $http = $options['http']; $header = $http['header']; $urlObj = parse_url($url); if (empty($urlObj["query"])) $path = $urlObj["path"]; else $path = $urlObj["path"] . "?" . $urlObj["query"]; $body = $http['content']; if (empty($body)) $bodymd5 = $body; else $bodymd5 = base64_encode(md5($body, true)); $stringToSign = $http['method'] . "\n" . $header['accept'] . "\n" . $bodymd5 . "\n" . $header['content-type'] . "\n" . $header['date'] . "\n" . $path; $signature = base64_encode( hash_hmac( "sha1", $stringToSign, $akSecret, true ) ); $authHeader = "Dataplus " . "$akId" . ":" . "$signature"; $options['http']['header']['authorization'] = $authHeader; $options['http']['header'] = implode( array_map( function ($key, $val) { return $key . ":" . $val . "\r\n"; }, array_keys($options['http']['header']), $options['http']['header'] ) ); $context = stream_context_create($options); $file = file_get_contents($url, false, $context); return json_decode($file); } /** * 检测人脸识别结果 * @param [type] $result [description] * @return [type] [description] */ public function checkPhotoResult($result, $user_id = null) { if ($result->error_code != 0) return ['code' => 1, 'msg' => "图片无法识别,请重新上传图片"]; if (empty($result->result)) return ['code' => 1, 'msg' => "图片解析失败,请重新上传图片"]; if ($result->result->face_num > 1) return ['code' => 1, 'msg' => "头像上传失败,系统识别多张人像"]; //人像数 $face_list = $result->result->face_list; if (empty($face_list)) return ['code' => 1, 'msg' => "头像上传失败,未获取图片人脸信息"]; //是否是卡通 $face = $face_list[0]; if ($face->face_type->type == "cartoon" && $face->face_type->probability > 0.8) return ['code' => 1, 'msg' => "头像上传失败,系统识别为卡通图片"]; //是否脸部有遮盖 if ($face->mask->type && $face->mask->probability > 0.8) return ['code' => 1, 'msg' => '头像上传失败,系统识别戴口罩']; //人脸是否模糊 if ($face->quality->blur > 0.3) return ['code' => 1, 'msg' => '头像上传失败,系统识别模糊']; //性别审核 $result = $this->checkPhotoGender($result->result->face_list, $user_id); if (empty($result)) return ['code' => 1, 'msg' => '头像上传失败,性别审核失败']; if ($result && is_array($result) && $result['code']) return $result; return true; } // public function checkPhotoResultV2($result, $user_sex) { try { if ($result->error_code != 0) return ['code' => 0, 'msg' => $result->error_msg]; if (empty($result->result)) return ['code' => 0, 'msg' => "图片解析失败,请重新上传图片"]; if ($result->result->face_num > 1) return ['code' => 0, 'msg' => "头像上传失败,系统识别多张人像"]; //人像数 $face_list = $result->result->face_list; if (empty($face_list)) return ['code' => 0, 'msg' => "头像上传失败,未获取图片人脸信息"]; //是否是卡通 $face = $face_list[0]; if ($face->face_type->type == "cartoon" && $face->face_type->probability > 0.8) return ['code' => 0, 'msg' => "头像上传失败,系统识别为卡通图片"]; //是否脸部有遮盖 if ($face->mask->type && $face->mask->probability > 0.8) return ['code' => 0, 'msg' => '头像上传失败,系统识别戴口罩']; //人脸是否模糊 if ($face->quality->blur > 0.3) return ['code' => 0, 'msg' => '头像上传失败,系统识别模糊']; //性别审核 $gender = $face_list[0]->gender->type; if ($gender && $gender == 'female') { $sex = 2; } else if ($gender && $gender == 'male') { $sex = 1; } if ($sex && $user_sex != $sex) return ['code' => 0, 'msg' => '图片性别与个人信息性别不符']; if (empty($result)) return ['code' => 0, 'msg' => '图片性别与个人信息性别不符']; return ['code' => 1, 'msg' => '头像上传成功']; } catch (\Exception $e) { $this->getError($e); return ['code' => 0, 'msg' => '头像上传失败']; } } public function checkPhotoGender($face_list, $user_id = null) { if ($user_id) { $user = User::find($user_id); } else { $user = auth()->user(); } $gender = $face_list[0]->gender->type; $sex = 0; if ($gender && $gender == 'female') { $sex = 2; } elseif ($gender && $gender == 'male') { $sex = 1; } if ($user->sex && $sex && $sex != $user->sex) { return ['code' => 1, 'msg' => '图片性别与个人信息性别不符']; } return true; } /** * 用户邀请注册用户列表 * @param [type] $user_id [description] * @return [type] [description] */ public function inviteUsers($user_id, $nopage = 0) { $wechat = Wechat::where('user_id', $user_id)->first(); if (empty($wechat)) { $openid = '无'; $official_openid = '无'; } else { $openid = $wechat->openid ?: '无'; $official_openid = $wechat->official_openid ?: '无'; } $users = User::orderBy('id', 'desc')->where(function ($sql) use ($openid, $user_id, $official_openid) { $sql->where('from_openid', $openid) ->orWhere('from_official_openid', $official_openid) ->orWhere('from_user_id', $user_id); }); $keyword = request()->input('keyword'); if ($keyword) { $keyword = trim($keyword); $users = $users->where(function ($sql) use ($keyword) { $sql->where('name', 'like', '%' . $keyword . '%') ->orWhere('id', 'like', '%' . $keyword . '%') ->orWhere('mobile', 'like', '%' . $keyword . '%'); }); } if ($nopage) { $users = $users->get(); } else { $users = $users->paginate(); } foreach ($users as $user) { $user->rank_name = $this->getRankName($user->id); $user->avatar = $user->userAvatar($user->id); } return $users; } public function inviteUsersCount($wechat, $user) { if (empty($wechat)) { $openid = '无'; } else { $openid = $wechat->openid; } $user_id = $user->id; $users = User::where(function ($sql) use ($openid, $user_id) { $sql->where('from_openid', $openid) ->orWhere('from_user_id', $user_id); }); $users = $users->count(); return $users; } public function inviteUsersFromUserAndPlatform() { } /** * 获取会员名称 * @param [type] $user_id [description] * @return [type] [description] */ public function getRankName($user_id) { $user = User::findOrFail($user_id); $rank_name = ''; if ($user->temp_member && $user->rank_id == 1) { $rank_name = '临时VIP'; } elseif (empty($user->temp_member) && $user->rank_id == 1) { $rank_name = '市级VIP'; } elseif ($user->rank_id == 2) { $rank_name = '黄金VIP'; } elseif ($user->rank_id == 3) { $rank_name = "钻石VIP"; } return $rank_name; } /** * 是否已注册 * @return boolean [description] */ public function getUserByMobile($mobile) { //包含软删除 $user = User::withTrashed()->where('mobile', $mobile)->first(); return $user; } /** * 建立平台与用户连接 * @param [type] $pass [description] * @param [type] $user_id [description] * @param [type] $type [description] */ public function addPaasUser($paas, $user_id, $type) { if ($paas != 'null' && !empty($paas)) { $paas_id = Paas::where('name', $paas)->value('id'); } else { $paas_id = Paas::where('name', 'FL')->value('id'); } if (empty($paas_id)) { return; } $paas_user = PaasUser::where('paas_id', $paas_id)->where('user_id', $user_id)->where('type', $type)->first(); if (empty($paas_user)) { PaasUser::create([ 'user_id' => $user_id, 'paas_id' => $paas_id, 'type' => $type, ]); } return; } /** * 注册用户信息 * 1.创建user * 2.创建wechat * 3.创建profiel * 3.创建公众号 * 3.获取临时会员 * @param [type] $request [description] * @return [type] [description] */ public function registerAfficial($request) { //创建用户 $user = $this->addUser($request); $this->addWechat($request, $openid = null, $user->id); //创建个人信息 $this->addProfile($user->id); //创建平台 $this->addPlatForm($request); //赠送临时会员 $this->getTempMember($user->id); //人脸识别 $this->wechatfaceDelect($user->id); return $user; } /** * 人脸识别(注册提交微信图片) * @param [type] $user_id [description] * @return [type] [description] */ public function wechatfaceDelect($user_id) { //延迟二分钟 // FaceDelect::dispatch($user_id)->onQueue('love')->delay(now()->addMinutes(2)); } /** * 创建用户 * @param [type] $request [description] */ public function addUser($request, $mobile = null) { Log::info("增加用户信息"); try { $mobile = $request->input('mobile', $mobile); Log::info("手机号信息".$mobile); $user = User::withTrashed()->where('mobile', $mobile)->first(); if ($user && empty($user->deleted_at)) { return $user; } elseif ($user && !empty($user->deleted_at)) { $user->deleted_at = null; $user->save(); return $user; } if (empty($mobile)) { return null; } $password = $request->input('password'); $nickname = $request->input('nickname'); $name = $request->input('name'); $from_official_openid = $request->input('from_official_openid'); $from_activity_id = $request->input('from_activity_id'); $from_user_id = $this->getFromParam('from_user_id'); $from_platform = $this->getFromParam('from_platform'); $from_openid = Wechat::where(['user_id' => $from_user_id])->value('openid'); // $from_openid = $request->input('from_openid'); $scene = $request->input('scene'); $sex = $request->input('sex', 0); $type = $request->input('type', 'single'); $photo = $request->input('avatar'); $device_idfa = $request->header('device-idfa'); $device_imei = $request->header('unique-device'); $client_os = $request->header('client-os'); $user = new User(); $user->name = $name; $user->nickname = $nickname; $user->mobile = $mobile; $user->email = $mobile . '@ufutx.com'; $user->is_new = 1; $user->from_openid = $from_openid; $user->from_platform = $from_platform; $user->from_user_id = $from_user_id; $user->from_official_openid = $from_official_openid; $user->from_activity_id = $from_activity_id; $user->approve_time = 3; $user->type = $type; $user->password = $password ? bcrypt($password) : null; $user->sex = $sex; $user->scene = $scene; $user->idfa = $device_idfa; $user->imei = $device_imei; $user->system_info = $client_os; $user->save(); if (env('APP_ENV') == 'alpha') { $user->id = $user->id + 10000000; $user->save(); } return $user; } catch (\Exception $e) { $this->getError($e); return false; } } /** * 注册添加小天使 * @param $from_user_id * @return void */ public function registerAddCherub($from_user_id) { $to_user_id = User::CHERUB_ID; $to_user = User::find(User::CHERUB_ID); $from_user = User::find($from_user_id); //添加好友 $linking = $this->setLinking($from_user_id, $to_user_id); //添加网易好友 $result = $to_user->addIMFriend($from_user); if ($result) { $linking->is_im = 1; $linking->save(); } //发送IM消息 $im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET')); $msg = "你好,我是福恋小天使, 陪伴你一起找对象,有什么情感困惑可以随时找我!"; $body = ['msg' => $msg]; $im_service->sendMsg($to_user_id, 0, $from_user_id, 0, $body); } public function getFromParam($from_param) { try { $from_value = request()->input($from_param); $prefix = request()->route()->getAction()['prefix']; $value = null; if ($prefix != 'api/app') { switch ($from_param) { case 'from_user_id': $from_values = explode(',', $from_value); if (count($from_values) > 1) { $value = $from_values[1]; } else { $value = $from_values[0]; } break; default: $value = $from_value; break; } } else { $client = request()->header('client-os'); if ($client == 'IOS') { $trace = ShareTrace::where('device_idfa', request()->header('device-idfa'))->first(); } else { $trace = ShareTrace::where('device_imei', request()->header('unique-device'))->first(); } if (empty($trace)) return null; $params = $trace->params_data; //解析参数字符串 $params_arr = explode('&', $params); foreach ($params_arr as $param) { if (strstr($param, $from_param)) { $sub_params = explode('=', $param); $value = $sub_params[1]; break; } } } return $value; } catch (\Exception $e) { $this->getError($e); return false; } } /** * 创建wechat * @param [type] $request [description] * @param [type] $user_id [description] */ public function addWechat($request, $official_openid = null, $user_id) { $data['avatar'] = $request->input('avatar'); $data['nickname'] = $request->input('nickname'); $data['user_id'] = $user_id; if ($official_openid && $official_openid != 'undefined') { $data['official_openid'] = $official_openid; } $wechat = Wechat::where('user_id', $user_id)->first(); if (empty($wechat)) { $wechat = Wechat::create($data); } else { $wechat->update($data); } return $wechat; } /** * 创建个人信息 * @param [type] $user_id [description] */ public function addProfile($request, $user_id) { $user = User::find($user_id); if (empty($user)) { return; } $belief = $request->input('belief'); $sex = $request->input('sex') ?: $user->sex; $birthday = $request->input('birthday'); if ($user->type == 'single') { $user->is_new = 1; $user->save(); $profile = ProfileCourtship::firstOrCreate([ 'user_id' => $user_id ]); $profile->country = '中国'; } else { $user->approve_time = 3; $user->save(); $profile = ProfileMarriage::firstOrCreate([ 'user_id' => $user_id ]); } $profile->belief = $belief; $profile->sex = $sex; $profile->birthday = $birthday; $profile->save(); return $profile; } /** * 添加平台分享参数 * @param [type] $request [description] */ public function addPlatForm($request) { $from_platform = $request->input('from_platform'); if ($from_platform) { $p = Platform::firstOrCreate([ 'app_id' => $from_platform ]); } return; } /** * 如果有推荐人 * 1.推荐人奖励 * 2,成为客户 * 3.成为好友 * @param [type] $request [description] * @return boolean [description] */ public function hasFromUserId($from_user_id, $user_id) { if ($from_user_id) { //獎勵 $this->officialReferreAward($from_user_id, $user_id, 'register'); //成为客户 $this->setMakerClient($from_user_id, $user_id); //成为好友 $this->setLinking($from_user_id, $user_id); //添加分享记录 $this->addInviteHistory($user_id, $from_user_id); } return; } /** * 添加分享记录 * @param [type] $user_id [description] * @param [type] $from_user_id [description] */ public function addInviteHistory($user_id = 0, $from_user_id = 0, $openid = null, $from_openid = null) { $status = 0; //相同账号跳过 if ($user_id && $from_user_id && $user_id == $from_user_id) return true; if ($openid && $from_openid && $openid == $from_openid) return true; if ($user_id && $from_user_id) { //已注册 $history = InviteHistory::where(['user_id' => $user_id, 'invite_user_id' => $from_user_id])->first(); $openid = Wechat::where('user_id', $user_id)->value('openid'); $from_openid = Wechat::where('user_id', $from_user_id)->value('openid'); $history2 = InviteHistory::where(['openid' => $openid, 'invite_openid' => $from_openid])->first(); $status = ($openid && $from_openid && $user_id && $from_user_id) ? 1 : 0; if ($history2) { $history2->update(['user_id' => $user_id, 'invite_user_id' => $from_user_id, 'status' => $status]); } if (empty($history) && empty($history2)) { InviteHistory::create([ 'openid' => $openid, 'invite_openid' => $from_openid, 'user_id' => $user_id, 'invite_user_id' => $from_user_id, 'status' => $status, 'type' => 'OTHER', ]); } elseif ($history && empty($status)) { $history->update(['openid' => $openid, 'invite_openid' => $from_openid, 'status' => $status]); } } elseif ($openid && $from_openid) { //正在注册 $history = InviteHistory::where(['openid' => $openid, 'invite_openid' => $from_openid])->first(); $history2 = InviteHistory::where('openid', $openid)->first(); $user_id = Wechat::where('openid', $openid)->orderBy('user_id', 'desc')->value('user_id'); $from_user_id = Wechat::where('openid', $from_openid)->orderBy('user_id', 'desc')->value('user_id'); $status = ($openid && $from_openid && $user_id && $from_user_id) ? 1 : 0; if ($history) { //已经分享过 更新信息 $history->update(['user_id' => $user_id, 'invite_user_id' => $from_user_id, 'status' => $status]); } if (empty($history) && empty($history2)) { //首次被分享 InviteHistory::create([ 'openid' => $openid, 'invite_openid' => $from_openid, 'user_id' => $user_id, 'invite_user_id' => $from_user_id, 'status' => $status, 'type' => 'FIRST', ]); } elseif (empty($history) && $history2) { //非首次 InviteHistory::create([ 'openid' => $openid, 'invite_openid' => $from_openid, 'user_id' => $user_id, 'invite_user_id' => $from_user_id, 'status' => $status, 'type' => 'Other', ]); } elseif ($history && empty($status)) { $history->update(['user_id' => $user_id, 'invite_user_id' => $from_user_id, 'status' => $status]); } } if ($status) { //赠送福币 //SendInviteCoin::dispatch($user_id, $from_user_id)->onQueue('love'); } return; } /** * 设置红娘客户 */ public function setMakerClient($from_user_id, $client_user_id) { if (!empty($from_user_id)) { $maker = Matchmaker::where('user_id', $from_user_id)->where('status', 1)->first(); if (!empty($maker)) { MatchmakerClient::firstOrCreate([ 'user_id' => $from_user_id, 'client_user_id' => $client_user_id, ]); } } return; } /** * 设置成好友 * @param [type] $from_openid [description] 别人 * @param [type] $user_id [description] 自己 */ public function setLinking($from_user_id, $user_linking_id) { if (empty($from_user_id)) { return; } $linking = Linking::where(function ($sql) use ($from_user_id, $user_linking_id) { $sql->where('user_id', $from_user_id)->where('user_linking_id', $user_linking_id); })->orWhere(function ($sql) use ($from_user_id, $user_linking_id) { $sql->where('user_id', $user_linking_id)->where('user_linking_id', $from_user_id); })->first(); if (empty($linking)) { $other_user = User::find($from_user_id); $user = User::find($user_linking_id); if (empty($other_user) || empty($user)) return $linking; $linking = Linking::create([ 'user_id' => $from_user_id, 'user_linking_id' => $user_linking_id, ]); if ($user_linking_id == User::LOVEID) { $name = $other_user->is_real_approved ? $other_user->name : $other_user->nickname; $content = $name . "你好,我是福恋脱单小助手小恋, 陪伴你一起找对象,有什么情感困惑可以随时找我!"; } elseif ($user_linking_id == User::CHERUB_ID) { $content = "你好,我是福恋小天使, 陪伴你一起找对象,有什么情感困惑可以随时找我!"; } else { //自己发聊天消息给别人 $content = LinkingRequest::where(['user_id' => $user_linking_id, 'user_linking_id' => $from_user_id])->value('message'); $content = $content ?? '玩相遇地图,享智能推荐,向你推荐福恋,用科技让交友变简单!'; } //更新好友数 $user->updateCacheUser('friend_count'); $other_user->updateCacheUser('friend_count'); if ($user->id == User::LOVEID || $user->id == User::CHERUB_ID) { $this->sendChatMessage($user, $other_user, $content); }else { $this->sendChatMessage($other_user, $user, $content); } //通知对方 if (!config('app.debug')) { broadcast(new NoticeServer($other_user)); } } return $linking; } /** * 推荐人奖励 * @param [type] $from_user_id [description] * @param [type] $user_id [description] * @param [type] $type [description] * @return [type] [description] */ public function officialReferreAward($from_user_id, $user_id, $type) { $data['from_user_id'] = $from_user_id; $data['user_id'] = $user_id; $data['type'] = $type; OfficialReferreAward::dispatch($data)->onQueue('love'); return; } /** * 恢复用户数据 * @param [type] $request [description] * @param [type] $user_id [description] * @return [type] [description] */ public function restoreUser($request, $user_id) { User::onlyTrashed()->where('id', $user_id)->restore(); $wechat = Wechat::withTrashed()->where('user_id', $user_id)->first(); if ($wechat && $wechat->deleted_at) { $wechat->restore(); return; } elseif ($wechat && empty($wechat->deleted_at)) { return; } elseif (empty($wechat)) { $unionid = $request->input('unionid'); if ($unionid) { $wechat = Wechat::withTrashed()->where('unionid', $unionid)->first(); if ($wechat && $wechat->deleted_at) { $wechat->restore(); return; } elseif ($wechat && empty($wechat->deleted_at)) { return; } elseif (empty($wechat)) { $wechat_data = $this->getWechatData($request); $wechat = Wechat::create($wechat_data); $wechat->user_id = $user_id; $wechat->save(); return; } } } } /** * 获取用户微信信息 * @param [type] $request [description] * @return [type] [description] */ public function getWechatDataByOpenid($openid) { $app = WechatService::officialApp(); $wechat_data = []; $data = $app->user->get($openid); if (is_array($data) && count($data)) { $wechat_data['afficial_openid'] = ''; $wechat_data['afficial_openid'] = $openid; $wechat_data['nickname'] = $data['nickname']; $wechat_data['gender'] = $data['sex']; $wechat_data['city'] = $data['city']; $wechat_data['province'] = $data['province']; $wechat_data['country'] = $data['country']; $wechat_data['unionid'] = isset($data['unionid']) ? $data['unionid'] : ''; $wechat_data['avatar'] = $data['headimgurl']; } return $wechat_data; } public function isRegisterByOpenid($openid) { $wechat = Wechat::where('afficial_openid', $openid)->first(); if ($wechat) { return $wechat; } $wechat_data = $this->getWechatDataByOpenid($openid); if (isset($wechat_data['unionid']) && $wechat_data['unionid']) { $wechat::where('unionid', $wechat_data['unionid'])->first(); return $wechat; } return null; } //手机号登录 public function loginByMobile($request, $openid = null) { $openid = $request->session()->get('official_openid'); //检测是否注册过 $user = $this->getUserByMobile($request->mobile); if ($user && $user->deleted_at) { //软删除 //恢复数据 $this->restoreUser($request, $user->id); //添加平台关联 $this->addPaasUser($request->input('paas'), $user->id, 'MINOR'); //更新微信公众号openid $this->addWechat($request, $openid, $user->id); } elseif ($user && empty($user->deleted_at)) { //添加平台关联 $this->addPaasUser($request->input('paas'), $user->id, 'MINOR'); //更新微信公众号openid $this->addWechat($request, $openid, $user->id); } elseif (empty($user)) { $user = $this->addUser($request); $profile = $this->addProfile($request, $user->id); $wechat = $this->addWechat($request, $openid, $user->id); //添加平台关联 $this->addPaasUser($request->input('paas'), $user->id, 'MAIN'); //领取临时会员 $this->getTempMember($user->id); //是否是社群分享 $this->communityShare($request, $user); } return $user; } /** * 社群分享获得收益 * @param [type] $request [description] * @param [type] $user [description] * @param [type] $wechat [description] * @return [type] [description] */ public function communityShare($request, $user) { $community_share = $request->input('community_share', 0); // dd($community_share); if (empty($community_share)) { return; } //分享人 $from_official_openid = $request->input('from_official_openid'); if (empty($from_official_openid)) { return; } $from_user_mobile = null; $from_wechat = Wechat::where('official_openid', $from_official_openid)->first(); if (empty($from_wechat)) { $from_user_name = ''; $from_user_id = 0; return; } elseif ($from_wechat && empty($from_wechat->user_id)) { $from_user_id = 0; $from_user_name = $from_wechat->nickname; return; } elseif ($from_wechat && $from_wechat->user_id) { $from_user_id = $from_wechat->user_id; $from_user = User::where('id', $from_wechat->user_id)->first(); $from_user_mobile = $from_user ? $from_user->mobile : null; $from_user_name = $from_user ? $from_user->name : null; } $red_packet = \CommonUtilsService::randAmount(); $data = [ 'user_name' => $user->name, 'from_official_openid' => $from_official_openid, 'from_user_name' => $from_user_name, 'from_user_id' => $from_user_id, 'from_user_mobile' => $from_user_mobile, 'red_packet' => $red_packet, 'type' => 'SHARE' ]; CommunityShare::dispatch($data)->onQueue('love'); } //手机号注册 public function registerByMobile($request) { //检测是否注册过 $user = $this->getUserByMobile($request->mobile); if (empty($user)) { //创建账号 $user = $this->addUser($request); //创建个人信息 $this->addProfile($request, $user->id); //创建小平台 $this->addPlatForm($request); $this->hasFromUserId($request->input('from_user_id'), $user->id); $this->addPaasUser($request->input('paas'), $user->id, 'MAIN'); } return $user; } /** * 首页推荐 * @param [type] $request [description] * @return [type] [description] */ public function homeRecommend($request) { $recommends = HomeRecommend::with('user')->where('status', 1)->whereNotNull('user_id'); //轮播推荐 $paas = $request->input('paas'); if ($paas != 'null' && !empty($paas)) { // $paas_id = Paas::where('name', $paas)->value('id'); // $user_ids = PaasHomeRecommend::where('paas_id', $paas_id)->pluck('user_id'); // $recommends = $recommends->whereIn('user_id', $user_ids); $recommends = $recommends->whereNotNull('photo'); } $recommends = $recommends->get(); foreach ($recommends as $recommend) { if (empty($recommend->user)) { continue; } $recommend->user = $this->userProfile($recommend->user); $recommend->photo = $recommend->photo ?: $recommend->user->photo; } return $recommends; } /** * 用户信息 * @param [type] $user [description] * @return [type] [description] */ public function userProfile($user) { if ($user->type == 'single') { $profile = ProfileCourtship::where('user_id', $user->id)->first(); $user->weight = $profile->weight; $user->stature = $profile->stature; $user->province = $profile->province; $user->city = $profile->city; $user->resident_province = $profile->resident_province; $user->resident_city = $profile->resident_city; } else { $profile = ProfileMarriage::where('user_id', $user->id)->first(); } $user->age = $this->commonUtils->getAge($profile->birthday); return $user; } /** * 平台用户id组 * @param [type] $request [description] * @return [type] [description] */ public function paasUserIds($paas = null, $type = null) { $user_ids = []; if ($paas != 'null' && !empty($paas)) { $paas_id = Paas::where('name', $paas)->value('id'); $user_ids = PaasUser::where('paas_id', $paas_id); if ($type) { $user_ids = $user_ids->where('type', $type); } $user_ids = $user_ids->distinct('user_id')->pluck('user_id'); } return $user_ids; } /** * 平台注册更新信息 * @param [type] $request [description] * @return [type] [description] */ public function updateUserProfile($request) { $this->updateUser($request); $this->updateProfile($request); return; } /** * 修改账号 * @param [type] $request [description] * @return [type] [description] */ public function updateUser($request, $user_id = null) { $user = $user_id ? User::find($user_id) : auth()->user(); if ($request->input('name') && $user->name != $request->name) { $user->name = $request->name; } if ($request->input('sex') && $user->sex != $request->sex) { $user->sex = $request->sex; } if ($request->input('type') && $user->type != $request->type) { $user->type = $request->type; } if ($request->input('photo') && $user->photo != $request->photo) { $user->photo = $request->photo; } if ($request->input('industry') && $user->industry != $request->industry) { $user->industry = $request->industry; } if ($request->input('industry_sub') && $user->industry_sub != $request->industry_sub) { $user->industry_sub = $request->industry_sub; } if ($request->input('belief') && $user->belief != $request->belief) { $user->belief = $request->belief; } //用户在线状态 if ($request->has('app_online') && $user->app_online != $request->app_online) { $user->app_online = $request->app_online; } $user->save(); return $user; } /** * 修改微信信息 */ public function updateWechat($wechat_obj, $param = []) { if (array_key_exists('nickName', $param) && $param['nickName'] != $wechat_obj->nickname) { $wechat_obj->nickname = $param['nickName']; } if (array_key_exists('gender', $param) && $param['gender'] != $wechat_obj->gender) { $wechat_obj->gender = $param['gender']; } if (array_key_exists('city', $param) && $param['city'] != $wechat_obj->city) { $wechat_obj->city = $param['city']; } if (array_key_exists('province', $param) && $param['province'] != $wechat_obj->province) { $wechat_obj->province = $param['province']; } if (array_key_exists('country', $param) && $param['country'] != $wechat_obj->country) { $wechat_obj->country = $param['country']; } if (array_key_exists('unionid', $param) && $param['unionid'] != $wechat_obj->unionid) { $wechat_obj->unionid = $param['unionid']; } if (array_key_exists('avatarUrl', $param) && $param['avatarUrl'] != $wechat_obj->avatar && $param['avatarUrl'] != 'null' && $param['avatarUrl'] != 'undefined') { $wechat_obj->avatar = $param['avatarUrl']; } if (array_key_exists('avatar2', $param) && $param['avatar2'] != $wechat_obj->avatar2) { $wechat_obj->avatar2 = $param['avatar2']; } if (array_key_exists('user_id', $param) && $param['user_id'] != $wechat_obj->user_id) { $wechat_obj->user_id = $param['user_id']; } if (array_key_exists('type', $param) && $param['type'] != $wechat_obj->type) { $wechat_obj->type = $param['type']; } if (array_key_exists('openid', $param) && $param['openid'] != $wechat_obj->openid) { $wechat_obj->openid = $param['openid']; } if (array_key_exists('official_openid', $param) && $param['official_openid'] != $wechat_obj->official_openid) { $wechat_obj->official_openid = $param['official_openid']; } $wechat_obj->save(); return $wechat_obj; } /** * 修改个人信息 * @param [type] $request [description] * @return [type] [description] */ public function updateProfile($request) { $user = auth()->user(); if ($user->type == 'single') { $profile = ProfileCourtship::firstOrCreate(['user_id' => $user->id]); if ($request->input('state') && $profile->state != $request->state) { $profile->state = $request->state; } if ($request->input('stature') && $profile->stature != $request->stature) { $profile->stature = $request->stature; } if ($request->input('weight') && $profile->weight != $request->weight) { $profile->weight = $request->weight; } if ($request->input('province') && $profile->province != $request->province) { $profile->province = $request->province; } if ($request->input('city') && $profile->city != $request->city) { $profile->city = $request->city; } if ($request->input('habitat') && $profile->habitat != $request->habitat) { $profile->habitat = $request->habitat; } if ($request->input('resident_province') && $profile->resident_province != $request->resident_province) { $profile->resident_province = $request->resident_province; } if ($request->input('resident_city') && $profile->resident_city != $request->resident_city) { $profile->resident_city = $request->resident_city; } if ($request->input('resident_city') && $profile->resident_city != $request->resident_city) { $profile->resident_city = $request->resident_city; } if ($request->input('degree') && $profile->degree != $request->degree) { $profile->degree = $request->degree; } if ($request->input('graduate_school') && $profile->graduate_school != $request->graduate_school) { $profile->graduate_school = $request->graduate_school; } if ($request->input('post') && $profile->post != $request->post) { $profile->post = $request->post; } if ($request->input('work_sort') && $profile->work_sort != $request->work_sort) { $profile->work_sort = $request->work_sort; } if ($request->input('wechat_id') && $profile->wechat_id != $request->wechat_id) { $profile->wechat_id = $request->wechat_id; } if ($request->input('country') && $profile->country != $request->country) { $profile->country = $request->country; } if ($request->input('introduction') && $profile->introduction != $request->introduction) { $profile->introduction = $request->introduction; } if ($request->input('ideal_mate') && $profile->ideal_mate != $request->ideal_mate) { $profile->ideal_mate = $request->ideal_mate; } if ($request->input('interest_hobby') && $profile->interest_hobby != $request->interest_hobby) { $profile->interest_hobby = $request->interest_hobby; } } else { $profile = ProfileMarriage::firstOrCreate(['user_id' => $user->id]); if ($request->input('slogan') && $profile->slogan != $request->slogan) { $profile->slogan = $request->slogan; } } if (!empty($user->sex) && $profile->sex != $user->sex) { $profile->sex = $user->sex; } if ($request->input('belief') && $profile->belief != $request->belief) { $profile->belief = $request->belief; } if ($request->input('birthday') && $profile->birthday != $request->birthday) { $profile->birthday = $request->birthday; } if ($request->input('company') && $profile->company != $request->company) { $profile->company = $request->company; } $profile->save(); return $profile; } /** * 用户列表 * @param [type] $request [description] * @return [type] [description] */ public function users($request) { $user = auth()->user(); //成员类型 良人 佳偶 介绍人 红娘 $type = $request->input('type', '不限'); $age = $request->input('age', '不限'); $province = $request->input('province', '不限'); $city = $request->input('city', '不限'); $belief = $user->belief; $paas = $request->input('paas'); $keyword = $request->input('keyword'); $users = $this->getUsers($type, $age, $province, $city, $belief, $paas, $keyword); foreach ($users as $user) { if (empty($user->photo) && $user->wechat) { $user->photo = $user->wechat->avatar2; } } return $users; } public function getUsers($type, $age, $province, $city, $belief, $paas, $keyword = null) { $times = $this->commonUtils->searchAge($age); $paas_user_id = $this->paasUserIds($paas); $user_ids = UserProfile::where('hidden_profile', '<>', 'ALLSEX')->where('is_approved', 1)->whereNotNull('photo')->where('belief', $belief)->orderBy('rank_id', 'desc')->orderBy('is_approved', 'desc'); if (!empty($paas)) { $user_ids = $user_ids->whereIn('id', $paas_user_id); } if ($keyword) { $keyword = trim($keyword); $user_ids = $user_ids->where('name', 'like', '%' . $keyword . '%'); } if ($type === '不限' && $age === '不限' && $province === '不限') { return $user_ids->paginate(); } if ($type == 'maker') { $user_ids = Matchmaker::where('status', 1)->pluck('user_id'); $users = User::whereIn('id', $user_ids)->orderBy('rank_id', 'desc')->orderBy('is_approved', 'desc'); if ($keyword) { $keyword = trim($keyword); $users = $users->where('name', 'like', '%' . $keyword . '%'); } if (!empty($paas)) { $users = $users->whereIn('id', $paas_user_id); } $users = $users->paginate(); return $users; } if ($type === '不限' && $age === '不限' && $province !== '不限' && $city === '不限') { // 0 0 1 0 $user_ids = $user_ids->where('province', $province); } elseif ($type === '不限' && $age === '不限' && $province !== '不限' && $city !== '不限') { // 0 0 1 1 $user_ids = $user_ids->where('province', $province)->where('city', $city); } elseif ($type === '不限' && $age !== '不限' && $province === '不限') { // 0 1 0 $user_ids = $user_ids->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time']); } elseif ($type !== '不限' && $age === '不限' && $province === '不限') { // 1 0 0 if ($type === 'single_woman') { $user_ids = $user_ids->where('type', 'single')->where('sex', 2); } elseif ($type === 'single_man') { $user_ids = $user_ids->where('type', 'single')->where('sex', 1); } elseif ($type === 'marriage') { $user_ids = $user_ids->where('type', '<>', 'single')->whereNotNull('type'); } } elseif ($type === '不限' && $age !== '不限' && $province !== '不限' && $city === '不限') { // 0 1 1 $user_ids = $user_ids->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('province', 'like', '%' . $province . '%'); } elseif ($type === '不限' && $age !== '不限' && $province !== '不限' && $city !== '不限') { // 0 1 1 $user_ids = $user_ids->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('province', 'like', '%' . $province . '%')->where('city', 'like', '%' . $city . '%')->where('belief', $belief); } elseif ($type !== '不限' && $age === '不限' && $province !== '不限' && $city === '不限') { // 1 0 1 if ($type === 'single_woman') { $user_ids = $user_ids->where('type', 'single')->where('province', 'like', '%' . $province . '%')->where('sex', 2); } elseif ($type === 'single_man') { $user_ids = $user_ids->where('type', 'single')->where('province', 'like', '%' . $province . '%')->where('sex', 1); } elseif ($type === 'marriage') { $user_ids = $user_ids->where('type', '<>', 'marriage')->whereNotNull('type'); } } elseif ($type !== '不限' && $age === '不限' && $province !== '不限' && $city !== '不限') { // 1 0 1 if ($type === 'single_woman') { $user_ids = $user_ids->where('type', 'single')->where('province', 'like', '%' . $province . '%')->where('city', 'like', '%' . $city . '%')->where('sex', 2); } elseif ($type === 'single_man') { $user_ids = $user_ids->where('type', 'single')->where('province', 'like', '%' . $province . '%')->where('city', 'like', '%' . $city . '%')->where('sex', 1); } elseif ($type === 'marriage') { $user_ids = $user_ids->where('type', '<>', 'marriage')->whereNotNull('type'); } } elseif ($type !== '不限' && $age !== '不限' && $province === '不限') { // 1 1 0 if ($type === 'single_woman') { $user_ids = $user_ids->where('type', 'single')->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('sex', 2); } elseif ($type === 'single_man') { $user_ids = $user_ids->where('type', 'single')->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('sex', 1); } elseif ($type === 'marriage') { $user_ids = $user_ids->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('type', '<>', 'marriage')->whereNotNull('type'); } } elseif ($type !== '不限' && $age !== '不限' && $province !== '不限' && $city === '不限') { // 1 1 1 if ($type === 'single_woman') { $user_ids = $user_ids->where('type', 'single')->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('province', 'like', '%' . $province . '%')->where('sex', 2); } elseif ($type === 'single_man') { $user_ids = $user_ids->where('type', 'single')->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('province', 'like', '%' . $province . '%')->where('sex', 1); } elseif ($type === 'marriage') { $user_ids = $user_ids->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('type', '<>', 'marriage')->whereNotNull('type'); } } elseif ($type !== '不限' && $age !== '不限' && $province !== '不限' && $city !== '不限') { // 1 1 1 if ($type === 'single_woman') { $user_ids = $user_ids->where('type', 'single')->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('province', 'like', '%' . $province . '%')->where('city', 'like', '%' . $city . '%')->where('sex', 2); } elseif ($type === 'single_man') { $user_ids = $user_ids->where('type', 'single')->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('province', 'like', '%' . $province . '%')->where('city', 'like', '%' . $city . '%')->where('sex', 1); } elseif ($type === 'marriage') { $user_ids = $user_ids->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('type', '<>', 'marriage')->whereNotNull('type'); } } $users = $user_ids->with('wechat')->paginate(); return $users; } /** * 首页公告 * @param [type] $request [description] * @return [type] [description] */ public function announcements($request, $paas_id = null) { //公告栏 $time = date('Y-m-d H:i:s'); $paas = $request->input('paas'); $announcements = Announcement::where('start_time', '<', $time)->where('end_time', '>', $time); if ($paas_id) { $announcement_ids = PaasAnnouncement::where('paas_id', $paas_id)->pluck('announcement_id'); $announcements = $announcements->whereIn('id', $announcement_ids); } $announcements = $announcements->orderBy('id', 'asc')->get(); return $announcements; } /** * 聊天通知数 * @param integer $status [description] */ public function chatMessageNum($status = 0) { $user_id = auth()->id(); if (empty($user_id)) { $user = $this->authCheck(); if ($user) { $user_id = $user->id; } } $count = 0; if ($user_id) { $count = ChatMessage::where('other_user_id', $user_id)->where('user_id', '<>', 1)->where('type', 'CHAT')->where('status', $status)->count(); } return $count ? $count : 0; } public function authCheck() { $guards = config('auth.guards'); $result = false; foreach ($guards as $key => $guard) { if (\Auth::guard($key)->check()) { $result = \Auth::guard($key)->user(); continue; } } return $result; } /** * 系统通知数 * @param integer $status [description] * @return [type] [description] */ public function noticeNum($status = 0) { $user_id = auth()->id(); if (empty($user_id)) { $user = $this->authCheck(); if ($user) { $user_id = $user->id; } } $count = 0; if ($user_id) { $count = Notice::where('user_id', $user_id)->where('status', $status)->count(); } return $count ? $count : 0; } /** * 我的 */ public function officialMine() { $user = auth()->user(); //会员名称 $rank_name = $this->getRankName($user->id); if (empty($rank_name) && $user->temp_member == 1) { $rank_name = "临时VIP"; } $user->rank_name = $rank_name; //vip时间 $deadline = RankHistory::where('user_id', $user->id)->where('rank_id', $user->rank_id)->value('deadline'); $user->rank_deadline = $deadline; //粉丝 $fans_count = $user->followers()->where('hidden_profile', '<>', 'ALLSEX')->count(); $user->fans_count = $fans_count ? $fans_count : 0; //关注数 $follow_count = $user->followings()->where('hidden_profile', '<>', 'ALLSEX')->count(); $user->follow_count = $follow_count ? $follow_count : 0; //好有数 $user->friend_count = $this->friendCount($user->id); //生活照 $profile_photos = $this->lifePhotos($user->id); $user->profile_photos = $profile_photos; //是否购买认证 $order = Order::where('type', 'approve')->where('user_id', $user->id)->where('pay_status', "PAID")->count(); $user->approve_order = $order ? 1 : 0; //openid $official_openid = Wechat::where('user_id', $user->id)->value('official_openid'); $user->official_openid = $official_openid; return $user; } /** * 用户详情 */ public function officialUser($user_id) { $user = UserProfile::where('id', $user_id)->first(); if (empty($user)) { return false; } //会员名称 $rank_name = $this->getRankName($user->id); $user->rank_name = $rank_name; //生活照 $photos = $this->lifePhotos($user_id); $user->life_photos = $photos; //年龄 $user->age = \CommonUtilsService::getAge($user->birthday); //是否是好友 $is_friend = $this->isFriend($user_id); $user->is_friend = $is_friend; //是否关注 $is_followed = $this->isFollowed($user_id); $user->is_followed = $is_followed; //粉丝数 $fans_count = User::find($user_id)->followers()->where('hidden_profile', '<>', 'ALLSEX')->count(); $user->fans_count = $fans_count; return $user; } /** * 个人信息 */ public function officialUserProfile() { // ProfileMarriage $user = auth()->user(); if ($user->type == 'single') { $profile = $this->singleProfile($user->id); } else { $profile = $this->marriageProfile($user->id); } $profile->industry_sub = $user->industry_sub; $profile->industry = $user->industry; return $profile; } /** * 是否关注 */ public function isFollowed($user_id) { $user = User::find($user_id); $is_followed = auth()->user()->isFollowing($user); $is_followed = $is_followed ? 1 : 0; return $is_followed; } /** * 是否是好友 * @param [type] $user_id [description] * @return boolean [description] */ public function isFriend($user_id, $id = 0) { $id = $id ?: auth()->id(); $count = Linking::where(function ($sql) use ($id, $user_id) { $sql->where(['user_id' => $user_id, 'user_linking_id' => $id]); })->orWhere(function ($sql) use ($id, $user_id) { $sql->where(['user_id' => $id, 'user_linking_id' => $user_id]); })->count(); return $count ? 1 : 0; } /** * 好友数 * @param [type] $user_id [description] * @return [type] [description] */ public function friendCount($user_id) { $friend_count = Linking::where(function ($sql) use ($user_id) { $sql->where('user_id', $user_id)->orWhere('user_linking_id', $user_id); })->count(); return $friend_count ? $friend_count : 0; } public function officialMessageLinkmanList() { $user = auth()->user(); $user_id = $user->id; //良人 $single_man_user_ids = MessageLinkman::where('user_id', $user_id)->where('type', 'single_man')->pluck('other_user_id'); //新消息数 $single_man_count = ChatMessage::where('status', 0)->where('other_user_id', $user->id)->whereIn('user_id', $single_man_user_ids)->count(); //发新消息人数 $send_user_ids = ChatMessage::where('status', 0)->where('other_user_id', $user->id)->whereIn('user_id', $single_man_user_ids)->pluck('user_id'); $single_men = MessageLinkman::where('user_id', $user_id)->whereIn('other_user_id', $send_user_ids)->where('type', 'single_man')->orderBy('updated_at', 'desc')->get(); foreach ($single_men as $single_man) { $single_man->circle_avatar = User::where('id', $single_man->other_user_id)->value('circle_avatar'); } //佳偶 $single_woman_user_ids = MessageLinkman::where('user_id', $user_id)->where('type', 'single_woman')->pluck('other_user_id'); //新消息数 $single_woman_count = ChatMessage::where('status', 0)->where('other_user_id', $user->id)->whereIn('user_id', $single_woman_user_ids)->count(); //发新消息人数 $send_user_ids = ChatMessage::where('status', 0)->where('other_user_id', $user->id)->whereIn('user_id', $single_woman_user_ids)->pluck('user_id'); $single_women = MessageLinkman::where('user_id', $user_id)->whereIn('other_user_id', $send_user_ids)->where('type', 'single_woman')->orderBy('updated_at', 'desc')->get(); foreach ($single_women as $single_woman) { $single_woman->circle_avatar = User::where('id', $single_woman->other_user_id)->value('circle_avatar'); } //红娘 $maker_user_ids = MessageLinkman::where('user_id', $user_id)->where('type', 'maker')->pluck('other_user_id'); //新消息数 $maker_count = ChatMessage::where('status', 0)->where('other_user_id', $user->id)->whereIn('user_id', $maker_user_ids)->count(); //发新消息人数 $send_user_ids = ChatMessage::where('status', 0)->where('other_user_id', $user->id)->whereIn('user_id', $maker_user_ids)->pluck('user_id'); $makers = MessageLinkman::where('user_id', $user_id)->whereIn('other_user_id', $send_user_ids)->where('type', 'maker')->orderBy('updated_at', 'desc')->get(); foreach ($makers as $maker) { $maker->circle_avatar = User::where('id', $maker->other_user_id)->value('circle_avatar'); } //介绍人 $marriage_user_ids = MessageLinkman::where('user_id', $user_id)->where(function ($sql) { $sql->where('type', 'marriage')->orWhere('type', 'loveing'); })->pluck('other_user_id'); //新消息数 $marriage_count = ChatMessage::where('status', 0)->where('other_user_id', $user->id)->whereIn('user_id', $marriage_user_ids)->count(); //发新消息人数 $send_user_ids = ChatMessage::where('status', 0)->where('other_user_id', $user->id)->whereIn('user_id', $marriage_user_ids)->pluck('user_id'); $marriages = MessageLinkman::where('user_id', $user_id)->whereIn('other_user_id', $send_user_ids)->where('type', 'marriage')->orderBy('updated_at', 'desc')->get(); foreach ($marriages as $marriage) { $marriage->circle_avatar = User::where('id', $marriage->other_user_id)->value('circle_avatar'); } $result = [ 'single_man_count' => $single_man_count, 'single_men' => $single_men, 'single_woman_count' => $single_woman_count, 'single_women' => $single_women, 'maker_count' => $maker_count, 'makers' => $makers, 'marriage_count' => $marriage_count, 'marriages' => $marriages, ]; return $result; } public function officailAddFriend($user_id) { $user = auth()->user(); $other_user_id = $user->id; $target_user = User::find($user_id); $linking_id = LinkingRequest::where(['user_id' => $user_id, 'user_linking_id' => $other_user_id])->where('status', 0)->value('id'); if (!$linking_id) { $linking_request = new LinkingRequest; $linking_request->user_id = $user_id; $linking_request->user_linking_id = $other_user_id; $linking_request->status = 0; $linking_request->message = '申请加入你的福恋好友'; $linking_request->save(); //发消息 $sms = $target_user->name . '您好,' . $user->name . '申请加你为福恋好友!请访问福恋小程序查看.'; \CommonUtilsService::sentMessage($target_user->mobile, $sms); // $this->sms->sentMessage('18682191714', $sms); $content = $target_user->name . '您好,' . $user->name . '申请加你为好友!'; $this->sendNotice($user_id, $other_user_id, 'friend', $content); } return true; } public function officialMessageLinkmen($request) { $user_id = auth()->id(); $linkmen = MessageLinkman::whereHas('otheruser', function ($sql) { $sql->where('id', '>', 0); })->where('user_id', $user_id); $type = $request->input('type'); if ($type) { $linkmen = $linkmen->where('type', $type); } $linkmen = $linkmen->orderBy('updated_at', 'desc')->paginate(); foreach ($linkmen as $linkman) { $new_count = ChatMessage::where('status', 0)->where('user_id', $linkman->other_user_id)->where('other_user_id', $linkman->user_id)->count(); $user = User::where('id', $linkman->other_user_id)->select('id', 'name', 'photo', 'app_avatar', 'circle_avatar')->first(); $user->photo = $user->avatar; if (empty($user)) { continue; } $linkman->new_count = $new_count; $linkman->user = $user; $other_user_id = $user->id; $last_message = ChatMessage::where(function ($sql) use ($user_id, $other_user_id) { $sql->where(['user_id' => $user_id, 'other_user_id' => $other_user_id]); })->orWhere(function ($sql) use ($user_id, $other_user_id) { $sql->where(['user_id' => $other_user_id, 'other_user_id' => $user_id]); })->orderBy('id', 'desc')->first(); $linkman->last_message = $last_message; } return $linkmen; } /** * 管理员列表 * @param [type] $request [description] * @return [type] [description] */ public function admins($request) { $admins = Admin::with('user', 'paas')->has('user')->orderBy('id', 'desc'); $type = $request->input('type'); if ($type && $type == 'SUPER') { //平台管理员 $admins = $admins->where('type', $type); } elseif ($type && $type != 'SUPER') { //平台管理员 $admins = $admins->where('type', '<>', $type); } $keyword = $request->input('keyword'); if ($keyword) { $keyword = trim($keyword); $admins = $admins->whereHas('user', function ($sql) use ($keyword) { $sql->where('name', 'like', '%' . $keyword . '%') ->orWhere('mobile', 'like', '%' . $keyword . '%'); }); } $admins = $admins->paginate(); foreach ($admins as &$admin) { $admin->user->photo = $admin->user->userAvatar($admin->user->id); } return $admins; } /** * 创建管理员 * @param [type] $request [description] * @return [type] [description] */ public function createAdmin($request) { $user_id = $request->input('user_id'); $type = $request->input('type'); $admin = Admin::with('role:id,name')->where(['user_id' => $user_id])->first(); try { DB::beginTransaction(); if (empty($admin)) { $admin_id = Admin::insertGetId([ 'user_id' => $user_id, 'type' => $type, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s'), ]); } else { $admin->type = $type; $admin->save(); $admin_id = $admin->id; } $user = User::where('id', $user_id)->first(); if (empty($user->paasword)) { $user->password = bcrypt($user->mobile); $user->save(); } $add_roles = $request->add_roles; $roles = []; if ($add_roles && is_array($add_roles)) { foreach ($add_roles as $role_id) { $role_user_count = RoleUser::where('role_id', $role_id)->where('user_id', $admin_id)->count(); if (!$role_user_count) { $me = []; $me['role_id'] = $role_id; $me['user_id'] = $admin_id; $me['created_at'] = date("Y-m-d H:i:s"); $me['updated_at'] = date("Y-m-d H:i:s"); array_push($roles, $me); } } unset($me); } $add_permissions = $request->add_permissions; $permissions = []; if ($add_permissions && is_array($add_permissions)) { foreach ($add_permissions as $permission_id) { $user_permission_count = UserPermission::where('permission_id', $permission_id)->where('user_id', $admin_id)->count(); if (!$user_permission_count) { $me = []; $me['permission_id'] = $permission_id; $me['user_id'] = $admin_id; $me['created_at'] = date("Y-m-d H:i:s"); $me['updated_at'] = date("Y-m-d H:i:s"); array_push($permissions, $me); } } } if ($roles) { RoleUser::insert($roles); } if ($permissions) { UserPermission::insert($permissions); } DB::commit(); $admin = $admin ?: true; return $admin; } catch (\Exception $e) { DB::rollBack(); return false; } } /** * 修改管理员 * @param [type] $request [description] * @param [type] $admin_id [description] * @return [type] [description] */ public function updateAdmin($request, $admin_id) { $user_id = $request->input('user_id'); $type = $request->input('type'); $admin = Admin::findOrFail($admin_id); if ($user_id && $user_id != $admin->user_id) { $admin->user_id = $request->user_id; } if ($type && $type != $admin->type) { $admin->type = $request->type; } $admin->save(); return $admin; } /** * 删除管理员 * @param [type] $admin_id [description] * @return [type] [description] */ public function deleteAdmin($admin_id) { try { DB::beginTransaction(); Admin::where('id', $admin_id)->delete(); UserPermission::where('user_id', $admin_id)->delete(); RoleUser::where('user_id', $admin_id)->delete(); DB::commit(); return true; } catch (\Exception $e) { DB::rollBack(); return false; } } /** * 隐藏手机号 * @param [type] $request [description] * @param [type] $user_id [description] * @return [type] [description] */ public function hideMobile($request, $user_id) { $admin_type = $request->session()->get('admin_type'); if ($admin_type == 'paas_admin') { $paas_obj = $request->session()->get('paas_obj'); $user_ids = $this->paasUserIds($paas_obj->name)->toArray(); } elseif ($admin_type == 'matchmaker') { $user_ids = MatchmakerClient::where('user_id', auth()->id())->pluck('client_user_id')->toArray(); } else { return false; } if (in_array($user_id, $user_ids)) { return false; } else { return true; } } /** * 未查看某人的聊天数 * @return [type] [description] */ public function newChatMessageCount($user_id, $other_user_id) { $new_count = ChatMessage::where('status', 0)->where('other_user_id', $user_id)->where('user_id', $other_user_id)->count(); return $new_count ? $new_count : 0; } public function newChatMessageAllCount($user_id) { $friend_ids = MessageLinkman::query()->where('user_id',$user_id)->pluck('other_user_id'); $new_count = ChatMessage::where('other_user_id', $user_id)->whereIn('user_id',$friend_ids)->where('status', 0)->whereHas('user', function ($sql) { $sql->where('hidden_profile', 'NONE'); })->count(); return $new_count ? : 0; } /** * 未查看的系统消息 * @param [type] $user_id [description] * @return [type] [description] */ public function newNoticeCount($user_id) { $new_count = Notice::where('user_id', $user_id)->whereNotIn('type', ['friend'])->where('status', 0); if (request()->input('versions') == 'v5.2.81') { $new_count = $new_count->where('type', '<>', 'system'); } $new_count = $new_count->count(); return $new_count ?: 0; } public function assistantMessageCount($user_id) { $new_count = AssistantUser::where('user_id', $user_id)->where('status', 0)->count(); return $new_count ? $new_count : 0; } public function visitCount($user_id) { $count = UserPreview::whereHas('previewUser', function ($sql) { $sql->where('hidden_profile', '<>', 'ALLSEX'); })->where('user_id', $user_id)->where('preview_user_id', '<>', $user_id)->where('status', 0)->count(); return $count ?: 0; } public function newsCount($user_id) { //聊天消息 $message_count = $this->newChatMessageAllCount($user_id); //系统消息 $notice_count = $this->newNoticeCount($user_id); //来访记录 $visit_count = $this->visitCount($user_id); //关注的数量 $follow_count = $this->systemNoticeCount($type = 'follow') ?: 0; //好友请求 $friend_count = $this->systemNoticeCount($type = 'friend') ?: 0; // if($user_id == 103331){ // \CommonUtilsService::HandleLogsAdd(3,'消息数量',[$message_count,$notice_count,$visit_count,$follow_count,$friend_count],'getUserValidPhoto'); // } // Log::info('用户id'.$user_id.': '.$message_count.'-'.$notice_count.'-'.$visit_count.'-'.$follow_count.'-'.$friend_count); return $message_count + $notice_count + $visit_count + $follow_count + $friend_count; // //聊天消息 // $message_count = $this->newChatMessageAllCount($user_id); // //系统消息 // $notice_count = $this->newNoticeCount($user_id); // //助手消息 // // $assistan_notice_count = $this->assistantMessageCount($user_id); // //来访记录 // $visit_count = $this->visitCount($user_id); // return $message_count + $notice_count /*+ $assistan_notice_count*/ + $visit_count; } /** * 聊天联系人 * @return [type] [description] */ public function messageLinkmen($request = null) { $user_id = auth()->id(); $types = $request->input('type') ? [$request->input('type')] : ['single', 'loveing', 'marriage']; $linkmen = MessageLinkman::with('otherUser:id,nickname,photo,app_avatar,circle_avatar,type,hidden_profile')->whereHas('otherUser', function ($sql) use ($types) { // $sql->where('id', '>', 0) // ->where('hidden_profile', '<>', 'ALLSEX') // ->whereIn('type', $types); })/*->where('other_user_id', '<>', 1)*/ ->select('id', 'user_id', 'other_user_id')->where('user_id', $user_id); $linkmen = $linkmen->orderBy('updated_at', 'desc')->paginate(); foreach ($linkmen as $linkman) { //新消息数 $new_count = $this->newChatMessageCount($linkman->user_id, $linkman->other_user_id); $linkman->new_count = $new_count; $linkman->type = auth()->user()->type; $other_user_id = $linkman->other_user_id; $user_id = $linkman->user_id; // \Log::info('other_user_id'. $other_user_id); // \Log::info('user_id'. $user_id); $last_message = ChatMessage::whereIn('user_id', [$user_id, $other_user_id]) ->whereIn('other_user_id', [$user_id, $other_user_id])->select('user_id', 'other_user_id', 'content', 'content_type', 'created_at') ->orderBy('id', 'desc')->first(); if ($last_message) { $last_time = $this->commonUtils->getLastTime(strtotime($last_message->created_at->toDateTimeString())); $last_time = (explode(' ', $last_time))[0]; $last_message->last_time = $last_time; if ($last_message->user_id != $user_id) { switch ($last_message->content_type) { case 'source': $content = "[你收到一条语音消息,请在福恋APP上查看]"; break; case 'video': $content = "[你收到一个视频,请在福恋APP上查看]"; break; case 'image': $content = "[图片]"; break; default: $content = $last_message->content; } } else { switch ($last_message->content_type) { case 'source': $content = "[你发送一条语音消息,请在福恋APP上查看]"; break; case 'video': $content = "[你发送一个视频,请在福恋APP上查看]"; break; case 'image': $content = "[图片]"; break; default: $content = $last_message->content; } } $last_message->content = $content; } $linkman->last_message = $last_message; $linkman->otherUser->circle_avatar = $linkman->otherUser->userAvatar(); $linkman->last_time = $last_message ? strtotime($last_message->created_at->toDateTimeString()) : 0; $linkman->chat_type = 'linkmen'; } return $linkmen; } /** * 最近一条消息 * @return [type] [description] */ public function lastNotice() { $user_id = auth()->id(); $notice = Notice::where('user_id', $user_id)->whereHas('otherUser', function ($sql) { $sql->where('hidden_profile', '<>', 'ALLSEX'); }); $prefix = request()->route()->getAction()['prefix']; if ($prefix == 'api') { //小程序 //版本判断 $version = request()->input('versions'); if ($version && $version == config('app.versions')) { $notice = $notice->where('type', '!=', 'friend'); } } else { $notice = $notice->whereNotIn('type', ['friend', 'moment', 'vote']); } $notice = $notice->orderBy('id', 'desc')->select('id', 'content', 'created_at')->first(); if (!empty($notice)) { $new_count = Notice::where('user_id', $user_id)->where('type', '!=', 'moment')->where('status', 0)->count(); $notice->new_count = $new_count ? $new_count : 0; //时间转换 $last_time = $this->commonUtils->getLastTime(strtotime($notice->created_at->toDateTimeString())); $last_time = (explode(' ', $last_time))[0]; $notice->last_time = $last_time; } return $notice; } public function lastGreetLog() { $user_id = auth()->id(); $notice = Notice::where('user_id', $user_id)->where('type', 'friend')->whereHas('otherUser', function ($sql) { $sql->where('hidden_profile', '<>', 'ALLSEX'); })->orderBy('id', 'desc')->select('id', 'user_id', 'message', 'created_at')->first(); if (!empty($notice)) { $new_count = Notice::where('user_id', $user_id)->where('type', 'friend')->where('status', 0)->count(); $notice->new_count = $new_count ? $new_count : 0; //时间转换 $last_time = $this->commonUtils->getLastTime(strtotime($notice->created_at->toDateTimeString())); $last_time = (explode(' ', $last_time))[0]; $notice->last_time = $last_time; } return $notice; } /** * 最近访问 */ public function lastVisit($request = null) { $user = auth()->user(); $types = $request->input('type') ? [$request->input('type')] : ['single', 'loveing', 'marriage']; $user_preview = UserPreview::where('preview_user_id', '<>', $user->id)->where('user_id', $user->id)->whereHas('previewUser', function ($sql) use ($types) { $sql->where('hidden_profile', '<>', 'ALLSEX')->whereIn('type', $types); })->orderBy('preview_time', 'desc')->first(); $array = []; if ($user_preview) { $other_user = $user_preview->previewUser; $nickname = $other_user->nickname ?: $other_user->name; //时间转换 $last_time = $this->commonUtils->getLastTime(strtotime($user_preview->preview_time)); $last_time = (explode(' ', $last_time))[0]; //总被访问人数 $count = UserPreview::whereHas('user', function ($sql) { $sql->where('hidden_profile', '<>', 'ALLSEX'); })->where('user_id', $user->id)->where('preview_user_id', '<>', $user->id)->where('status', 0)->count(); if ($user->id == 76249) { $count = 0; } $array = [ 'content' => '用户【' . $nickname . '】最近访问了你', 'last_time' => $last_time, 'count' => $count, ]; return $array; } return $array; } /** * 最近一条助手消息 * @return [type] [description] */ public function lastAssistantMessage() { $user_id = auth()->id(); $chat_message = AssistantUser::where('user_id', $user_id)->select('id', 'content', 'created_at')->orderBy('id', 'desc')->first(); if (!empty($chat_message)) { $new_count = AssistantUser::where('user_id', $user_id)->where('status', 0)->count(); $chat_message->new_count = $new_count ? $new_count : 0; //时间转换 $last_time = $this->commonUtils->getLastTime(strtotime($chat_message->created_at->toDateTimeString())); $last_time = (explode(' ', $last_time))[0]; $chat_message->last_time = $last_time; } return $chat_message; } /** * 消息未读数 * @param [type] $type [description] * @return [type] [description] */ public function systemNoticeCount($type = null) { try { $count = Notice::where('user_id', auth()->id()); if ($type) { if ($type == 'system') { $count = $count->whereNotIn('type', ['friend']); } else { $count = $count->where('type', $type); } } if (request()->input('versions') == 'v5.2.81') { $count = $count->where('type', '<>', 'system'); } $count = $count->where('status', 0)->count(); return $count ?: 0; } catch (\Exception $e) { $this->getError($e); return false; } } public function userPreviewCount() { $preview_count = auth()->user()->userPreview()->whereHas('previewUser', function ($sql) { $sql->where('hidden_profile', '!=','ALLSEX')->where('type','single'); })->where('status', 0)->count(); return $preview_count; } public function sendAssistantMessage($user_ids, $content, $msg_type = 'text', $service_user_id = 0) { $data = []; $time = date('Y-m-d H:i:s'); //创建单个消息 //一个队列处理不完,多加几个 $user_ids_arr = array_chunk($user_ids, 2000); foreach ($user_ids_arr as $user_ids_next) { foreach ($user_ids_next as $user_id) { $me = []; $me['user_id'] = $user_id; $me['assistant_user_id'] = 1; $me['service_user_id'] = $service_user_id; $me['msg_type'] = $msg_type; $me['content'] = $content; $me['created_at'] = $time; $me['updated_at'] = $time; array_push($data, $me); //触发websocket } AssistantMessage::dispatch($data, $user_ids)->onQueue('love'); } return; } /** * 发聊天消息 * user 发消息给 other_user */ public function sendChatMessage($user, $other_user, $content, $content_type = 'TEXT') { $user_id = $user->id; $other_user_id = $other_user->id; //我的聯係人 $my_linkman = MessageLinkman::firstOrCreate(['user_id' => $user_id, 'other_user_id' => $other_user_id]); $my_linkman->updated_at = date('Y-m-d H:i:s'); $my_linkman->save(); //Ta的联系人 $other_linkman = MessageLinkman::firstOrCreate(['user_id' => $other_user_id, 'other_user_id' => $user_id]); $other_linkman->updated_at = date('Y-m-d H:i:s');; $other_linkman->save(); } /** * 额外奖励 * @param string $value [description] * @return [type] [description] */ public function addedBonus($from_openid, $user_id) { $from_wechat = Wechat::where('openid', $from_openid)->first(); if (empty($from_wechat)) { return; } $from_user = $from_wechat->user; if (empty($from_user)) { return; } //判断是否是推荐人 $referre = $from_user->referre; if (empty($referre)) { return; } //判断是否是七天内邀请的 $start_time = $referre->created_at->toDateTimeString(); $unix_start_time = strtotime($start_time); $unix_end_time = $unix_start_time + 7 * 24 * 3600; if (time() >= $unix_start_time && time() < $unix_end_time) { //第一周邀请的 AddedBonus::create([ 'user_id' => $from_user->id, 'other_user_id' => $user_id, 'amount' => 7, ]); } else { AddedBonus::create([ 'user_id' => $from_user->id, 'other_user_id' => $user_id, 'amount' => 0, ]); } } /** * 福恋用户导入共享平台 * @return [type] [description] */ public function usersToPaas($from_openid, $user_id) { $from_user_id = Wechat::where('openid', $from_openid)->value('user_id'); if (empty($from_user_id)) { return; } //管理员 $paas = Admin::where('user_id', $from_user_id)->where('type', '<>', 'SUPER')->value('type'); if (empty($paas)) { //同工 $paas_id = PaasWorker::where('user_id', $from_user_id)->orderBy('id', 'desc')->value('paas_id'); if (empty($paas_id)) { return; } $paas = Paas::where('id', $paas_id)->value('name'); } if ($paas) { $this->addPaasUser($paas, $user_id, 'MAIN'); } return; } public function usersToPaasV2($platform, $user_id) { $paas_id = Platform::where("app_id", $platform)->value('paas_id'); if (empty($paas_id)) { return; } $paas_name = Paas::where('id', $paas_id)->value('name'); if (empty($paas_name)) { return; } $this->addPaasUser($paas_name, $user_id, 'MAIN'); return; } /** * 添加头像信息 */ public function addImageInfo($user, $img_url, $face_list) { $data['user_id'] = $user->id; $data['url'] = $img_url; $data['face_token'] = $face_list->face_token; $data['gender'] = $face_list->gender->type; $data['expression'] = $face_list->expression->type; $data['face_shape'] = $face_list->face_shape->type; $data['age'] = $face_list->age; $data['beauty'] = $face_list->beauty; $data['emotion'] = $face_list->emotion->type ? $face_list->emotion->type : Null; $data['race'] = $face_list->race->type; $data['face_type'] = $face_list->face_type->type; if ($data['face_type'] == 'cartoon') { return; } $count = ImageInfo::where(['face_token' => $data['face_token'], 'user_id' => $user->id])->count(); if (empty($count)) { ImageInfo::create($data); } $user->face_score = $data['beauty']; $user->save(); return; } public function largeDataSingleLikers($user, $paas = null) { $paas_user_ids = []; if ($paas) { $paas_user_ids = $this->paasUserIds($paas); } if (empty($user)) { $linkings = User::with('profileCourtship')->whereNotNull('photo')->orderBy('is_approved', 'desc')->orderBy('face_score', 'desc')->limit(30); } else { if ($user->type == 'single') { $sex = $user->sex == 1 ? 2 : 1; $belief = $user->belief; $linkings = RecommendLinkingNew::with('otherUser.profileCourtship')->whereHas('otherUser', function ($sql) use ($paas_user_ids, $paas, $sex, $belief) { $sql = $sql->whereNotNull('photo')->where('hidden_profile', '<>', 'ALLSEX')->where('type', 'single')->where('sex', $sex)->where('belief', $belief); if ($paas && $paas != 'FL') { $sql->whereIn('id', $paas_user_ids); } })->where('id_users_left', $user->id); } else { $friend_user_ids = $user->userFriendIds($user); if (!count($friend_user_ids)) { $linkings = User::with('profileCourtship')->whereNotNull('photo')->orderBy('is_approved', 'desc')->orderBy('face_score', 'desc')->limit(30); } else { $linkings = RecommendLinkingNew::whereIn('id_users_left', $friend_user_ids)->whereHas('otherUser', function ($sql) { $sql->where('type', 'single')->whereNotNull('photo'); })->groupBy('id_users_right'); } } } $linkings = $linkings->paginate(); \Log::info('liker count:' . $linkings->count()); if (empty($linkings->count()) && !empty($user) && $user->type == 'single') { //如果没有匹配到用户 $linkings = $this->getPreferenceLikers($user); } // dd($linkings); foreach ($linkings as $linking) { $class_name = get_class($linking); if ($class_name == 'App\Models\User') { $other_user = User::find($linking->id); $linking->otherUser = $other_user; } $other_user = $linking->otherUser; if (empty($other_user)) { continue; } $profile = $linking->otherUser->profileCourtship; if (empty($profile)) { continue; } $age = \CommonUtilsService::getAge($profile->birthday); $other_user->photo = $other_user->photo . '?x-oss-process=style/scale1'; $other_user->age = $age; $linking->other_user = $other_user; } // dd($linkings); return $linkings; } public function getPreferenceLikers($user) { $max_birthday = $user->profileCourtship->max_age; $min_birthday = $user->profileCourtship->min_age; if (empty($max_birthday) || empty($min_birthday)) { $max_birthday = date('Y-m-d H:i:s', strtotime('- 3 year', strtotime($user->ProfileCourtship->birthday))); $min_birthday = date('Y-m-d H:i:s', strtotime('+ 3 year', strtotime($user->ProfileCourtship->birthday))); } $belief = $user->profileCourtship->belief; $sex = $user->sex == 1 ? 2 : 1; $likers = User::with('profileCourtship')->whereHas('profileCourtship', function ($sql) use ($max_birthday, $min_birthday, $belief) { $sql->whereBetween('birthday', [$max_birthday, $min_birthday])->where('belief', $belief); })->whereNotNull('photo')->where('sex', $sex)->orderBy('is_approved', 'desc')->orderBy('face_score', 'desc')->paginate(); return $likers; } /** * faceid 人脸核身 */ public function face($name, $card_number) { // $name = $request->input('name'); // $card_number = $request->input('card_number'); $sign = $this->faceSign(); $data = [ "sign" => $sign, //签名 "sign_version" => "hmac_sha1", //签名类型 "liveness_type" => "meglive", //活体验证流程 meglive:动作活体,still:静默活体,flash:炫彩活体 "comparison_type" => "1", # 本次身份验证服务的类型。 #0:表示活体照片和参考照片进行比对(人脸比对)。1:表示活体照片和权威数据源进行比对(人脸核身) "idcard_name" => $name, #姓名 "idcard_number" => $card_number, #身份证 ]; $url = "https://api.megvii.com/faceid/v3/sdk/get_biz_token"; $res = Http::http($url, $data, 'POST'); return json_decode($res, true); } public function h5Face($user_id, $name, $card_number) { $biz_no = date('YmdHis') . mt_rand(1000, 9999); $data = [ "api_key" => "D3VUJjLo_JNI8l9eUpOeLnM_s0oNh9SM", "api_secret" => "jm2X4EKHAA-z6I5ihCefPGuShjj70auL", "return_url" => env('APP_URL') . '/face', "notify_url" => env('APP_URL') . "/api/official/face/notify/url", 'comparison_type' => 1, 'idcard_mode' => 0, 'idcard_name' => $name, 'idcard_number' => $card_number, 'biz_no' => $biz_no, ]; $url = "https://api.megvii.com/faceid/lite/get_token"; $res = Http::post($url, $data); //生成face记录 FaceIdHistory::create([ 'user_id' => $user_id, 'biz_no' => $biz_no, ]); return json_decode($res); } public function mpFace($user_id, $name, $card_number) { $sign = $this->faceSign(); $url = "https://openapi.faceid.com/lite/v1/get_biz_token"; $biz_no = date('YmdHis') . mt_rand(1000, 9999); $data = [ 'sign' => $sign, 'sign_version' => 'hmac_sha1', "return_url" => env('APP_URL') . '/face', "notify_url" => env('APP_URL') . "/api/official/face/notify/url", 'biz_no' => $biz_no, 'comparison_type' => 1, 'idcard_name' => $name, 'idcard_number' => $card_number, 'liveness_type' => 'video_number', ]; $res = Http::post($url, $data); //生成face记录 FaceIdHistory::create([ 'user_id' => $user_id, 'biz_no' => $biz_no, ]); return json_decode($res); } /** * 签名 */ public function faceSign() { $api_key = "D3VUJjLo_JNI8l9eUpOeLnM_s0oNh9SM"; $api_secret = "jm2X4EKHAA-z6I5ihCefPGuShjj70auL"; $valid_durtion = 100; # 有效时间100秒 $current_time = time(); $expired_time = $current_time + $valid_durtion; $rdm = rand(); $srcStr = "a=%s&b=%d&c=%d&d=%d"; $srcStr = sprintf($srcStr, $api_key, $expired_time, $current_time, $rdm); $sign = base64_encode(hash_hmac('SHA1', $srcStr, $api_secret, true) . $srcStr); return $sign; } public function updateAppProfile($request, $user) { try { //额外信息(网易) $ex = []; $old_data = []; //是否修改信息 $change = 0; //是否更新H5的昵称 $is_replenishUserOtherInfo = false; $await_checkoutTextArray = []; if ($request->input('hidden_profile') && $request->hidden_profile != $user->hidden_profile) { $old_data['hidden_profile'] = $user->hidden_profile; $user->hidden_profile = $request->hidden_profile; $change = 1; $ex['hidden_profile'] = $user->hidden_profile; } if ($request->input('app_avatar') && $request->app_avatar != $user->app_avatar) { $old_data['app_avatar'] = $user->app_avatar; $user->app_avatar = $request->app_avatar; $old_data['photo'] = $user->photo; $user->photo = $request->app_avatar; $user->circle_avatar = null; $user->is_audited = 0; $user->is_photo_audited = 0; //异步人脸识别 $array = [ 'user_id' => $user->id, 'avatar' => $request->app_avatar ]; CheckAvatarFace::dispatch($array)->onQueue('love'); $change = 1; $ex['app_avatar'] = $user->app_avatar; $ex['photo'] = $user->app_avatar; } if ($request->input('nickname') && $request->nickname != $user->nickname) { if (preg_match("/^1[3-56789]\d{9}$/", $request->nickname) || preg_match("/^19[89]\d{8}$/", $request->nickname) || preg_match("/^166\d{8}$/", $request->nickname)) return ['code' => 1, 'msg' => '昵称不符合规则']; $old_data['nickname'] = $user->nickname; $user->nickname = $request->nickname; $await_checkoutTextArray[] = $request->nickname; $change = 1; $ex['nickname'] = $user->nickname; $is_replenishUserOtherInfo = true; if (strpos($request->nickname , 'frank' ) !== false || strpos($request->nickname , 'Frank' ) !== false) { $data['touser'] = 'oPC_2vneOWpQbicNZQAUCxuwZ4mw'; $data['template_id'] = config('wechat.tpls.error_notice'); $data['url'] = ''; $data['data'] = [ 'first' => '可疑用户提示', 'keyword1' => '用户['.$user->nickname.']id--'.$user->id, 'keyword2' => '福恋', 'keyword3' => date('Y-m-d H:i:s'), 'remark' => '', ]; SendTemplateMsg::dispatch($data)->onQueue('error_email'); } } if ($request->input('industry') && $request->industry != $user->industry) { $old_data['industry'] = $user->industry; $user->industry = $request->industry; $change = 1; $ex['industry'] = $user->industry; } if ($request->input('industry_sub') && $request->industry_sub != $user->industry_sub) { $old_data['industry_sub'] = $user->industry_sub; $user->industry_sub = $request->industry_sub; $change = 1; $ex['industry_sub'] = $user->industry_sub; } $profile = $user->profileCourtship; if (empty($profile)) { $profile = new ProfileCourtship; $profile->user_id = $user->id; } if ($request->input('stature') && $request->stature != $profile->stature) { $old_data['stature'] = $profile->stature; $profile->stature = $request->stature; $change = 1; $ex['stature'] = $profile->stature; } if ($request->input('weight') && $request->weight != $profile->weight) { $old_data['weight'] = $profile->weight; $profile->weight = $request->weight; $change = 1; $ex['weight'] = $profile->weight; } if ($request->input('province') && $request->province != $profile->province) { //中文? if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $request->province) > 0) { $old_data['province'] = $profile->province; $profile->province = $request->province; $change = 1; $ex['province'] = $profile->province; } } if ($request->input('city') && $request->city != $profile->city) { //中文? if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $request->city) > 0) { $old_data['city'] = $profile->city; $profile->city = $request->city; $change = 1; $ex['city'] = $profile->city; } } if ($request->input('state') && $request->state != $profile->state) { $old_data['state'] = $profile->state; $profile->state = $request->state; $user->type = ($request->state == '已婚') ? "marriage" : 'single'; $change = 1; $ex['state'] = $request->state; } $is_educate_approved = DB::table('users')->where('id', $user->id)->value('is_educate_approved'); if ($request->input('degree') && $request->degree != $profile->degree && $is_educate_approved == 0) { $old_data['degree'] = $profile->degree; $profile->degree = $request->degree; $change = 1; $ex['degree'] = $profile->degree; } if ($request->input('graduate_school') && $request->graduate_school != $profile->graduate_school && $is_educate_approved == 0) { $old_data['graduate_school'] = $profile->graduate_school; $profile->graduate_school = $request->graduate_school; $change = 1; $ex['graduate_school'] = $profile->graduate_school; } if ($request->input('educate_photo') && $profile->educate_photo != $request->educate_photo && $is_educate_approved == 0) { $old_data['educate_photo'] = $profile->educate_photo; $profile->educate_photo = $request->educate_photo; $change = 1; $ex['educate_photo'] = $profile->educate_photo; //创建认证记录 $user->updateApproveInfo('educate', 0); } if ($request->input('resident_province') && $request->resident_province != $profile->resident_province) { $old_data['resident_province'] = $profile->resident_province; $profile->resident_province = $request->resident_province; $change = 1; $ex['resident_province'] = $profile->resident_province; } if ($request->input('resident_city') && $request->resident_city != $profile->resident_city) { $old_data['resident_city'] = $profile->resident_city; $profile->resident_city = $request->resident_city; $change = 1; $ex['resident_city'] = $profile->resident_city; } if ($request->input('introduction') && $request->introduction != $profile->introduction) { $old_data['introduction'] = $profile->introduction; $profile->introduction = $request->introduction; $change = 1; $ex['introduction'] = $profile->introduction; } if ($request->input('ideal_mate') && $request->ideal_mate != $profile->ideal_mate) { $old_data['ideal_mate'] = $profile->ideal_mate; $profile->ideal_mate = $request->ideal_mate; $change = 1; $ex['ideal_mate'] = $profile->ideal_mate; } if ($request->input('mate_conditon') && json_encode($request->mate_conditon) != $profile->mate_conditon) { $old_data['mate_conditon'] = $profile->mate_conditon; $profile->mate_conditon = json_encode($request->mate_conditon); $change = 1; $ex['mate_conditon'] = $profile->mate_conditon; } if ($request->input('income') && $request->income != $profile->income) { $income = $request->income; $value = $income['mValue']??$income; $old_data['income'] = $value; $profile->income = $value; $change = 1; $ex['income'] = $value; } if ($request->has('wechat_id') && !empty($request->wechat_id) && $request->wechat_id != $profile->wechat_id) { $old_data['wechat_id'] = $profile->wechat_id; $profile->wechat_id = $request->wechat_id; $change = 1; $ex['wechat_id'] = $profile->wechat_id; } if ($request->input('birthday') && $request->birthday != $profile->birthday) { $old_data['birthday'] = $profile->birthday; //检查是否已经认证并且出生日期是否对应 if (empty($this->checkProfileBirthday($user, $request->birthday))) { return ['code' => 1, 'msg' => '修改出生日期失败,和身份证信息不符']; } $profile->birthday = $request->birthday; $change = 1; $ex['birthday'] = $profile->birthday; } if ($request->input('belief') && $request->belief != $user->belief) { $old_data['belief'] = $profile->belief; $user->belief = $request->belief; $profile->belief = $request->belief; $change = 1; $ex['belief'] = $user->belief; $user->save(); //删除当天小程序每日推荐 $this->deleteRecommendUsers(); \App\Jobs\NewUserLinkData::dispatch($user->id)->onQueue('love'); } if ($request->input('sex') && $request->sex != $user->sex && $request->sex != $profile->sex) { if ($user->sex && $request->input('update_type') == 'update') { //普通修改 return ['code' => 1, 'msg' => '暂不支持更改性别,请联系客服:4000401707']; } $old_data['sex'] = $user->sex; $user->sex = $request->sex; $profile->sex = $user->sex; $change = 1; $ex['sex'] = $user->sex; } //是否是单身 if ($request->input('type') && $request->type != $user->type) { $old_data['type'] = $user->type; $user->type = $request->type; $change = 1; $ex['type'] = $user->type; } //修改完善分数 $result = $this->profileIntegrity($profile, $user, explode(',', User::APPCOMPLETEINFO)); $user->info_complete_score = $result['count']; //敏感词汇过滤 $result = \CommonUtilsService::checkoutTextArray($await_checkoutTextArray); if($result['code'] == 1){ return $this->failure($result['cause']); } \DB::beginTransaction(); $profile->save(); $user->save(); if ($change) { //修改IM $birthday = $user->profileCourtship ? $user->profileCourtship->birthday : null; $im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET')); $result = $im_service->updateUinfo($user->id, $user->nickname, $user->app_avatar, $sign = '', $user->email, $birthday, $user->mobile, $user->sex); //增加修改资料记录 $user->addProfileChangeLog($old_data, $ex); //缓存 $user->cacheUser(); } \DB::commit(); if ($is_replenishUserOtherInfo) { $this->replenishUserOtherInfo($user->id, $user->nickname); } return true; } catch (\Exception $e) { \DB::rollBack(); $this->getError($e); return false; } } public function checkProfileBirthday($user, $req_birthday) { try { //拿到身份证的出生日期 $result = \CommonUtilsService::checkCardBirhday($user->card_num); if ($result['error'] != 2) { return true; } $birthday = $result['tdate']; if ($user->is_approved && $birthday != $req_birthday) { return false; } return true; } catch (\Exception $e) { $this->getError($e); return true; } } /** * app在线列表 * @return [type] [description] */ public function onlineUsers() { try { $sex = request()->input('sex'); $users = User::with('profileCourtship:user_id,birthday,city')->where('sex', $sex)->where('app_online', 1)->where('type', 'single')->where('is_approved', 1)->select('id', 'app_avatar', 'nickname')->orderBy('rank_id', 'desc')->orderBy('face_score', 'desc')->paginate(16); foreach ($users as $user) { if (empty($user->profileCourtship)) { continue; } $age = \CommonUtilsService::getAge($user->profileCourtship->birthday); $user->age = $age; $user->city = $user->profileCourtship->city; unset($user->profileCourtship); } return $users; } catch (Exception $e) { $this->getError($e); return false; } } /** * 检查提现 * 金额是否符合 * @return [type] [description] */ public function checkWithdrawCoin() { try { $coin_value = request()->input('coin_value'); $transfer_type = request()->input('transfer_type'); $user = auth()->user(); //获取用户总福币 $total_coin = CoinLog::where('user_id', $user->id) ->where('type', 'RECSYSTEM') ->where(function ($query) { $query->where('remark', 'like', '%邀请好友%') ->orWhere('remark', 'like', '%城市群主服务%') ->orWhere('remark', 'like', '%奖励%') ->orWhere('remark', 'like', '%刘勇芳%') ->orWhere('remark', 'like', '%钉钉%'); })->sum('coin'); //已提现 $coin = CoinWithdrawLog::where('user_id', $user->id) ->where(function ($query) { $query->where('remark', 'like', '%邀请好友%'); })->where('status', 1) ->where('is_hooked', 1) ->sum('value'); $remain_amount = $total_coin - $coin; if (!$remain_amount) return ['code' => 1, 'msg' => '申请提现福币不足']; if ($transfer_type == 'wechat' && (empty($user->wechat) || empty($user->wechat->app_openid))) return ['code' => 1, 'msg' => '请先绑定微信账号']; if ($transfer_type == 'alipay' && empty($user->coin->alipay_account)) return ['code' => 1, 'msg' => '请先绑定支付宝账号']; //余额判断 if (empty($coin_value) || $coin_value > $remain_amount) return ['code' => 1, 'msg' => '申请提现福币不足']; return true; } catch (\Exception $e) { $this->getError($e); return false; } } /** * 申请提现 * 等待后台审核 * @return [type] [description] */ public function withdrawCoin() { try { $value = request()->input('coin_value'); //增加提现记录 CoinWithdrawLog::create([ 'user_id' => auth()->id(), 'value' => $value, 'status' => 0, 'remark' => '邀请好友', 'transfer_type' => request()->input('transfer_type'), ]); return true; } catch (\Exception $e) { $this->getError($e); return false; } } /** * 检查审核提现记录 * @return [type] [description] */ public function checkOperateWithdrawCoin($log_id) { try { $status = request()->input('status'); if (empty($status)) return ['code' => 1, 'msg' => '请选择审核状态']; if ($status == -1 && empty(request()->input('reason'))) return ['code' => 1, 'msg' => '请输入拒绝理由']; //todo 权限判断 //记录判断 $log = CoinWithdrawLog::find($log_id); if ($log->status != 0) return ['code' => 1, 'msg' => '记录不是审核中状态']; return true; } catch (\Exception $e) { $this->getError($e); return false; } } public function dealWithdrawCoinLogs($log_id) { try { \DB::beginTransaction(); $log = CoinWithdrawLog::find($log_id); $user = $log->user; $coin = $user->coinInfo(); $status = request()->input('status'); if ($status == 1) { //转账 todo $log->trade_no = \CommonUtilsService::getTradeNO(); if ($log->transfer_type == 'wechat') { $result = $this->transferWechatWithdrawCoin($log); } elseif ($log->transfer_type == 'alipay') { $result = $this->transferAlipayWithdrawCoin($log, $coin); } if (empty($result)) throw new \Exception("转账失败", 1); if (is_array($result) && $result['code']) { //修改转账失败原因 $log->is_hooked = -1; $log->message = $result['msg']; } elseif ($result == true) { $log->is_hooked = 1; } } elseif ($status == -1) { //修改记录状态 $reason = request()->input('reason'); $log->message = $reason; //退还冻结金额 } $log->status = $status; $log->remark = '邀请好友'; $log->operate_user_id = auth()->id(); $log->operate_time = date('Y-m-d H:i:s'); $log->save(); \DB::commit(); return true; } catch (\Exception $e) { \DB::rollBack(); $this->getError($e); return false; } } public function transferWechatWithdrawCoin($log) { return false; } public function transferAlipayWithdrawCoin($log, $coin) { try { $service = new LiveAlipayService; $data['out_biz_no'] = $log->trade_no; $data['amount'] = $log->value * 0.1; $data['payee_account'] = $coin->alipay_account; $data['payee_real_name'] = $coin->alipay_real_name; $data['remark'] = '福币提现'; $service = new LiveAlipayService; $result = $service->transferAccount($data); if (empty($result)) throw new \Exception("支付宝转账失败", 1); return $result; } catch (\Exception $e) { $this->getError($e); return false; } } /** * 礼物榜单 * @return [type] [description] */ public function userGiftList($user_id) { try { $list = InteractLiveGiftLog::where('user_id', $user_id)->selectRaw('sum(total_coin) as sum, user_id, gift_id, send_user_id')->with('otherUser:id,nickname,sex,app_avatar,photo', 'otherUser.profileCourtship')->with(['gifts' => function ($sql) use ($user_id) { $sql->where('user_id', $user_id)->groupBy('gift_id', 'send_user_id'); }])->groupBy('send_user_id')->orderBy('sum', 'desc')->paginate(); foreach ($list as $li) { $gift_icons = []; foreach ($li->gifts as $gift) { $gift_icons[] = $gift->gift->icon; } $li->gift_icons = $gift_icons; if (empty($li->otherUser)) continue; $li->otherUser->app_avatar = $li->otherUser->userAvatar(); //年龄 $li->otherUser->age = $li->otherUser->profileCourtship ? \CommonUtilsService::getAge($li->otherUser->profileCourtship->birthday) : 0; //会员 $li->otherUser->isSuperRank = $li->otherUser->isSuperRank(); unset($li->gifts, $li->otherUser->profileCourtship, $li->otherUser->photo); } return $list; } catch (\Exception $e) { $this->getError($e); return false; } } /** * 分享列表 * @return [type] [description] */ public function inviteLogs() { try { $type = request()->input('type'); // $start_time = env('APP_ENV') == 'producation'?'2020-12-24':''; $user_id = auth()->id(); $openid = Wechat::where('user_id', $user_id)->value('openid'); $logs = InviteHistory::where(function ($sql) use ($user_id, $openid) { $sql->where('invite_user_id', $user_id)->orWhere('invite_openid', $openid); }); if ($type == 'INVALID') { $logs = $logs->where(function ($sql) { $sql->whereNull('user_id')->orWhereNull('invite_user_id'); }); } else { $logs = $logs->where('invite_user_id', $user_id)->whereNotNull('user_id')->where('type', $type)->whereHas('user'); } // if ($start_time) { // $logs = $logs->date($start_time); // } $logs = $logs->with('user:id,nickname,photo,app_avatar,sex,rank_id', 'user.profileCourtship:user_id,birthday')->orderBy('id', 'desc')->paginate(); foreach ($logs as $log) { if ($log->user) { $log->user->avatar = $log->user->userAvatar(); $birthday = $log->user->profileCourtship ? $log->user->profileCourtship->birthday : null; $log->user->age = \CommonUtilsService::getAge($birthday); $log->user->is_super_rank = $log->user->isSuperRank(); unset($log->user->profileCourtship); } else { unset($log->user); $user['id'] = 0; $user['nickname'] = '未知'; $user['avatar'] = 'http://images.ufutx.com/201811/12/0e8b72aae6fa640d9e73ed312edeebf3.png'; $user['is_super_rank'] = 0; $user['age'] = '未知'; $user['sex'] = 0; $log->user = (object)$user; } if ($type == 'INVALID') $log->type = "INVALID"; } return $logs; } catch (\Exception $e) { $this->getError($e); return false; } } /** * 分享用户数 * @return [type] [description] */ public function inviteMemberCount() { try { $count = auth()->user()->inviteLogs()->whereHas('user')->whereNotNull('type')->distinct('openid')->count(); return $count ?: 0; } catch (\count $e) { $this->getError($e); return false; } } /** * 分享福币数 * @return [type] [description] */ public function inviteCoinCount() { try { $user_ids = auth()->user()->inviteLogs()->whereHas('user')->whereNotNull('type')->distinct('user_id')->pluck('user_id'); $coin = auth()->user()->coinLogs()->shareLogs()->whereIn('type_id', $user_ids)->sum('coin'); return (int)$coin; } catch (\Exception $e) { $this->getError($e); return false; } } public function gainFlashRose() { try { \DB::beginTransaction(); //增加领取玫瑰记录 $result = $this->addFlashRoseLog('GAIN'); if (empty($result)) throw new \Exception("增加限时玫瑰记录失败", 1); //更新玫瑰数 $result = $this->updateFlashRose('GAIN'); if (empty($result)) throw new \Exception("更新限时玫瑰数失败", 1); \DB::commit(); return true; } catch (\Exception $e) { \DB::rollBack(); $this->getError($e); return false; } } /** * 增加玫瑰记录 * @param [type] $type [description] * @param [type] $type_id [description] */ public function addFlashRoseLog($type, $other_user_id = null, $num = 1) { try { auth()->user()->flashRoseLogs()->create([ 'type' => $type, 'other_user_id' => $other_user_id, 'num' => $num ]); return true; } catch (\Exception $e) { $this->getError($e); return false; } } /** * 修改玫瑰数 * @param [type] $type [description] * @param [type] $num [description] * @return [type] [description] */ public function updateFlashRose($type, $num = 1) { try { $rose = auth()->user()->flashRose()->firstOrCreate(['user_id' => auth()->id()]); if ($type == 'GAIN') { $rose->increment('remain_amount', $num); } else { $rose->increment('use_amount', $num); $rose->decrement('remain_amount', $num); } return true; } catch (\Exception $e) { $this->getError($e); return false; } } public function giveFlashRose($other_user_id, $num) { try { \DB::beginTransaction(); $other_user = User::find($other_user_id); if (empty($other_user)) throw new \Exception("获赠用户不存在", 1); //增加扣除记录 $result = $this->addFlashRoseLog('GIVE', $other_user_id, $num); if (empty($result)) throw new \Exception("增加玫瑰扣除记录失败", 1); //修改扣除后的玫瑰数 $result = $this->updateFlashRose('GIVE', $num); if (empty($result)) throw new \Exception("修改玫瑰数失败", 1); //增加福币获得记录 $value = $num * 10; $remark = "获得限时玫瑰赠予"; $result = $other_user->addCoinLog(CoinLog::RECROSE, auth()->id(), $value, $remark); if (empty($result)) throw new \Exception("增加福币记录失败", 1); //修改获得后的福币信息 $result = $other_user->updateCoinInfo('add', $value, 'other'); if (empty($result)) throw new \Exception("修改福币信息失败", 1); \DB::commit(); return true; } catch (\Exception $e) { \DB::rollback(); $this->getError($e); return false; } } /** * 资料完成进度 * @return [type] [description] */ public function infoTaskProgress() { try { $first_progress = 0.5; $user = auth()->user(); if (empty($user)) { $user = $this->authCheck(); } if (empty($user)) return 0; foreach (User::MPOTHERUSERINFO as $value) { if (empty($user->$value)) { $first_progress = 0; break; } } if (empty($user->profileCourtship)) return 0; foreach (User::MPOTHERPROFILEINFO as $value) { if (empty($user->profileCourtship->$value)) { $first_progress = 0; break; } } if (empty($first_progress)) return $first_progress; if (empty($user->profilePhoto()->count())) return $first_progress; $second_progress = 1; foreach (User::RECOMMENDINFO as $value) { if ($value == 'is_real_approved' && $user->$value != 1) { $second_progress = $first_progress; break; } if (empty($user->$value)) { $second_progress = $first_progress; break; } } return $second_progress; } catch (\Exception $e) { $this->getError($e); return 0; } } /** * [infoTasks description] * @return [type] [description] */ public function infoTasks() { try { $user = auth()->user(); if (empty($user)) { $user = $this->authCheck(); } if ($user) { $profile = $user->profileCourtship; $arr['coin_user_info'] = $this->coinUserInfo($user); $arr['is_approved'] = $user->is_approved; $arr['is_real_approved'] = $user->is_real_approved; $arr['profile_photo'] = $user->profilePhoto()->count() ? 1 : 0; $arr['interest_hobby'] = ($profile && $profile->interest_hobby) ? 1 : 0; $arr['introduction'] = ($profile && $profile->introduction) ? 1 : 0; $arr['ideal_mate'] = ($profile && $profile->ideal_mate) ? 1 : 0; $arr['is_educate_approved'] = $user->is_educate_approved; } else { $arr['coin_user_info'] = 0; $arr['is_approved'] = 0; $arr['is_real_approved'] = 0; $arr['profile_photo'] = 0; $arr['interest_hobby'] = 0; $arr['introduction'] = 0; $arr['ideal_mate'] = 0; $arr['is_educate_approved'] = 0; } return $arr; } catch (\Exception $e) { $this->getError($e); return false; } } public function coinUserInfo($user) { //资料是否完善 $user_data = User::COINUSER; $profile_data = User::COINPROFILE; $res = 1; foreach ($user_data as $attribute) { if (empty($user->$attribute)) { $res = 0; break; } } if ($res) return $res; $profile = $user->profileCourtship; if (empty($profile)) { $res = 0; } if ($res) return $res; foreach ($profile_data as $attribute) { if (empty($profile->$attribute)) { $res = 0; break; } } return $res; } /** * app登录其他属性 * @param [type] $request [description] * @param [type] $user [description] * @return [type] [description] */ public function otherAppLoginParam($request, $user, $has_mobile = 1) { \Debugbar::startMeasure('other', 'Time for other'); //赋值中间件同步IMUSER $request->user = $user; //登录所需的token $user->access_token = $user->createToken($user->mobile)->accessToken; //创建网易账号 $wyy_user = $this->createWYYUser($user); $user->wyy_user = $wyy_user; unset($user->IMUser, $user->wechat); //是否有手机号 $user->has_mobile = $has_mobile; //是否完善注册资料 $user->complete_base_info = $user->isCompletedProfile("app_register_info"); //是否完善其他资料 $user->complete_other_info = $user->isCompletedProfile('app_other_info'); //是否有密码 $user->has_password = $user->hasPassword(); //是否是直播间红娘 $user->is_match_maker = $user->live_match_maker; //超级会员 $user->isSuperRank = $user->isSuperRank(); \Debugbar::stopMeasure('other'); return $user; } /** * 创建网易账号 * @param [type] $user [description] * @return [type] [description] */ public function createWYYUser($user) { //判断是否已创建accid; $wyy_user = $user->IMUser; $im_service = new IMService(env('IM_APP_KEY'), env("IM_APP_SECRET")); if (empty($wyy_user)) { $wyy_user = $user->createIMUser(); } elseif (empty($wyy_user->token)) { //更新网易云token $result = $im_service->updateUserToken($user->id); if ($result['code'] == 200) { $user->IMUser()->update(['token' => $result['info']['token']]); $wyy_user->token = $result['info']['token']; $wyy_user->save(); } } // $birthday = $user->profileCourtship ? $user->profileCourtship->birthday : null; // $avatar = $this->getUserValidPhoto($user); // $result = $im_service->updateUinfo($user->id, $user->nickname, $avatar, $sign = '', $user->email, $birthday, $user->mobile, $user->sex); return $wyy_user; } /** * 删除当天首页推荐七位 * @return [type] [description] */ public function deleteRecommendUsers() { try { if (strtotime(date('Y-m-d')) < time() && time() < strtotime(date('10:00:00'))) { $start_time = date('Y-m-d 10:00:00', strtotime('-1 day'));; $end_time = date('Y-m-d 09:59:59'); } else { $start_time = date('Y-m-d 10:00:00'); $end_time = date('Y-m-d 09:59:59', strtotime('+1 day')); } auth()->user()->recommendUsers()->where('start_time', $start_time)->where('end_time', $end_time)->delete(); return true; } catch (\Exception $e) { $this->getError($e); return false; } } public function addCoinWithdrawer() { try { $withdrawer = new CoinWithdrawer; $withdrawer->user_id = request()->user_id; $withdrawer->save(); return $withdrawer; } catch (\Exception $e) { $this->getError($e); return false; } } public function deleteCoinWithdrawer() { try { CoinWithdrawer::where('user_id', request()->user_id)->delete(); return true; } catch (\Exception $e) { $this->getError($e); return false; } } /** * 增加真人认证信息记录 * @param [type] $result [description] */ public function addLiveperson($result) { try { $result = !is_array($result)?$result:json_decode(json_encode($result)); if(empty($result)){ return false; } Liveperson::create([ 'user_id' => auth()->id(), 'status' => $result->status, 'taskId' => $result->taskId, 'reasonType' => $result->reasonType, 'picType' => $result->picType, 'avatar' => $result->avatar, 'faceMatched' => $result->faceMatched, 'similarityScore' => $result->similarityScore, ]); return true; } catch (\Exception $e) { $this->getError($e); return false; } } public function faceMatchToken() { try { $url = "https://aip.baidubce.com/oauth/2.0/token"; $post_data['grant_type'] = 'client_credentials'; $post_data['client_id'] = config('baiduface.client_id'); $post_data['client_secret'] = config('baiduface.client_secret'); $result = \App\Utils\Http::post($url, $post_data); $result = json_decode($result); return $result; } catch (\Exception $e) { $this->getError($e); return false; } } public function faceMatch($image1, $image2) { try { $result = $this->faceMatchToken(); if (empty($result)) throw new \Exception("获取token失败", 1); $access_token = $result->access_token??null; if (empty($access_token)) return false; $url = "https://aip.baidubce.com/rest/2.0/face/v3/match?access_token=" . $access_token; $bodys = json_encode([ ['image' => $image1, 'image_type' => 'URL', 'face_type' => 'LIVE', "quality_control" => "NONE", "liveness_control" => "NONE"], ['image' => $image2, 'image_type' => 'URL', 'face_type' => 'LIVE', "quality_control" => "NONE", "liveness_control" => "NONE"], ]); $result = \App\Utils\Http::post($url, $bodys); $result = json_decode($result); if (empty($result) || $result->error_code != 0) return $result; return $result->result; } catch (\Exception $e) { $this->getError($e); return false; } } /** 用户列表浏览记录 */ public function usersBrowse() { try { //获取用户id列表 $user_ids_str = request()->input('user_ids'); if (empty($user_ids_str)) throw new \Exception("未传入用户id"); $user_ids = explode(',', $user_ids_str); foreach ($user_ids as $other_user_id) { UserBrowse::updateOrcreate(['user_id' => auth()->id(), 'other_user_id' => $other_user_id]); } return true; } catch (\Exception $e) { $this->getError($e); return false; } } //获取视频认证各种错误 public static function getData($reason_type) { switch ($reason_type) { case 2: $type = 1; $message = '认证失败,姓名身份证号一致,人脸比对非同一人'; break; case 3: $type = 1; $message = '认证失败,姓名身份证号不一致'; break; case 4: $type = 3; $message = "认证失败,活体检测不通过"; $extra = ["1、录制时,没有眨眼睛;", "2、录制时间过短,请超过3秒时长;", "3、在灯光较暗或曝光下,无法识别人脸;", "4、人脸拍摄不全,无法检测到人脸;", "5、保持正对手机,不要斜视屏幕;", "6、手机网络不顺畅,请检查网络状况"]; break; case 5: $type = 1; $message = '认证失败,活体检测超时'; break; case 6: $type = 2; $message = '认证失败,库中无此身份证照片'; break; case 7: $type = 2; $message = '认证失败,库中无此身份证照片'; break; case 8: $type = 1; $message = '认证失败,请勿过于靠近手机屏幕拍摄'; break; case 9: $type = 1; $message = '认证失败,系统繁忙,请稍后重试'; break; default: $type = 1; $message = '认证失败'; break; } $data = [ 'type' => $type, 'message' => $message ]; if (!empty($extra)) { $data['extra'] = $extra; } return $data; } public function getUserData($profileCourtshipWhere, $unset_user_ids) { if (!array_key_exists('province', $profileCourtshipWhere)) { $profileCourtshipWhere['province'] = '不限'; } if (!array_key_exists('city', $profileCourtshipWhere)) { $profileCourtshipWhere['city'] = '不限'; } if (!array_key_exists('sex', $profileCourtshipWhere)) { $profileCourtshipWhere['sex'] = '不限'; } if (!array_key_exists('age', $profileCourtshipWhere)) { $profileCourtshipWhere['age'] = '不限'; } if (!array_key_exists('is_approved', $profileCourtshipWhere)) { $profileCourtshipWhere['is_approved'] = '不限'; } if (!array_key_exists('keyword', $profileCourtshipWhere)) { $profileCourtshipWhere['keyword'] = '不限'; } if (!array_key_exists('belief', $profileCourtshipWhere)) { $profileCourtshipWhere['belief'] = '不限'; } $now = date('Y-m-d H:i:s'); $users = User::with('profileCourtship') ->with(['rankHistories' => function ($q) use ($now) { $q->where('deadline', '>', $now); $q->select(); }]) ->whereHas('profileCourtship', function ($sql) use ($profileCourtshipWhere) { if ($profileCourtshipWhere['province'] != '不限') { $sql = $sql->where('province', $profileCourtshipWhere['province']); } if ($profileCourtshipWhere['city'] != "不限") { $sql = $sql->where('city', $profileCourtshipWhere['city']); } if ($profileCourtshipWhere['sex'] != "不限") { $sql = $sql->where('sex', $profileCourtshipWhere['sex']); } if (config('app.env') == 'production') { $sql = $sql->where('is_real_approved', 1); } if ($profileCourtshipWhere['age'] != '不限') { $times = \CommonUtilsService::searchAge($profileCourtshipWhere['age']); $sql = $sql->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time']); } else { $sql = $sql->where('birthday', '>=', '1986-01-01')->where('birthday', '<', '1998-01-01'); } if (is_numeric($profileCourtshipWhere['is_approved'])) { $sql = $sql->where('is_approved', $profileCourtshipWhere['is_approved']); } }); if ($profileCourtshipWhere['keyword'] != '不限') { $users = $users->where(function ($sql) use ($profileCourtshipWhere) { $sql->where('nickname', 'like', '%' . $profileCourtshipWhere['keyword'] . '%'); }); } if ($profileCourtshipWhere['belief'] != '不限') { $users = $users->where('belief', $profileCourtshipWhere['belief']); } $users = $users ->whereNotIn('id', $unset_user_ids) ->where('hidden_profile', 'NONE') ->orderBy('rank_id', 'desc') ->where('type', 'single') ->orderBy('is_real_approved', 'desc') ->orderBy('is_approved', 'desc') ->orderBy('liveness', 'desc') ->orderBy('info_complete_score', 'desc') ->orderBy('last_visit', 'desc'); return $users; } /**获取生日礼物的领取资格 */ public function getBirthdayStatus($userinfo) { $res['code'] = 0; $res['msg'] = ''; $initial_date = date('Y-01-01'); $rankHistoryCount = RankHistory::where('user_id', $userinfo->id) ->where('created_at', '>=', $initial_date) ->where('type', 'birthday_give') ->count(); if ($rankHistoryCount > 0) { $res['msg'] = '你已领取生日礼物,不能重复领取'; } else if ($userinfo->is_real_approved <> 1) { $res['msg'] = '你还未真人认证,请认证完毕后,再来领取会员。'; } else if ($userinfo->type != 'single') { $res['msg'] = '该生日礼物仅限单身用户领取'; } else if ($userinfo->hidden_profile != 'NONE') { $res['msg'] = '你资料已关闭,请打开资料后领取'; } else if ($userinfo->profileCourtship->birthday) { $my_timestamp = strtotime($userinfo->profileCourtship->birthday); if (date('m-d') != date('m-d', $my_timestamp)) { $res['msg'] = '今天不是您的生日,请生日当天前来领取.'; } else { $res['code'] = 1; } } else { $res['msg'] = '今天不是您的生日,请生日当天前来领取..'; } return $res; } //补充用户头像其他信息 public function replenishPhotoInfo($user, $photo, $birthday, $face_list) { try { if (count($face_list) > 0) { $this->addImageInfo($user, $photo, $face_list[0]); } FaceMatchJob::dispatch($user->id)->onQueue('love'); $im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET')); $result = $im_service->updateUinfo($user->id, $user->nickname, $photo, $sign = '', $user->email, $birthday, $user->mobile, $user->sex); return true; } catch (\Exception $e) { return false; } } /**获取用户是信息是否完善 跳过介绍人 */ public function getUserinfoPerfect($user, $versions) { $msg = ''; $status = 1; if ($user->type == 'single') { if ($user->sex != 1 && $user->sex != 2) { $status = 0; $msg = '资料未完善,性别'; // return $this->fail('资料未完善,性别',4); } else if (!$user->type) { $status = 0; $msg = '资料未完善,类型'; // return $this->fail('资料未完善,类型',4); } else if (!$user->nickname) { $status = 0; $msg = '资料未完善,昵称'; // return $this->fail('资料未完善,昵称',4); } if (!$user->profileCourtship) { $status = 0; $msg = '资料未完善,用户资料'; // return $this->fail('资料未完善,用户资料',4); } else if (!$user->profileCourtship->belief) { $status = 0; $msg = '资料未完善,信仰'; // return $this->fail('资料未完善,信仰',4); } else if (!$user->profileCourtship->sex) { $status = 0; $msg = '资料未完善,性别'; // return $this->fail('资料未完善,性别',4); } else if (!$user->profileCourtship->city || !$user->profileCourtship->province) { $status = 0; $msg = '资料未完善,居住地'; // return $this->fail('资料未完善,居住地',4); } } return array('status' => $status, 'msg' => $msg); } /**获取用户是否被封禁的账号 */ public function getUserBannedState($user_id) { $banned_state = 0; if ($user_id == 0 || !$user_id) { return 1; } $blacklist = SystemBlacklist::where('user_id', $user_id)->first(); if ($blacklist) { $date = date('Y-m-d H:i:s'); if ($blacklist->end_time >= $date && $blacklist->start_time <= $date) { Log::info("违规账号id: ".$user_id); $banned_state = 1; // SendEasySms::dispatch(['message'=>"账号id:".$user_id.'涉及违规被禁用', "mobile"=>15872844805])->onQueue('love'); } } return $banned_state; } /**获取用户是否被冻结的账号 */ public function getUserFrozenState($user_id) { $frozen_state = 0; $info = WrongInfoHistories::where('type', 'frozen')->where('user_id', $user_id)->first(); if ($info) { $frozen_state = 1;//未超过3天 $date = date('Y-m-d H:i:s'); $frozen_time = $info->created_at; $expire_time = date('Y-m-d H:i:s', strtotime('+3 days', strtotime($frozen_time))); if ($date > $expire_time) { $frozen_state = 2; //超过3天 } } return $frozen_state; } /** 更新用户的坐标 */ public function updateUserLocation($user, $new_location_longitude, $new_location_latitude) { if (!$user) { return false; } if ($user->location_longitude != $new_location_longitude || $user->location_latitude != $new_location_latitude) { $user->location_longitude = $new_location_longitude; $user->location_latitude = $new_location_latitude; $user->save(); PositionHistory::create([ 'user_id' => $user->id, 'location_latitude' => $new_location_latitude, 'location_longitude' => $new_location_longitude, ]); return true; } } /**获取今天生日的用户列表 */ public static function getTodayBirthdayUserList() { // $tody_date = date('m-d'); // $new_tody_date = str_replace('0','',$tody_date); // $todayBirthdayUserList = User::where('type','single') // ->where('is_real_approved','1') // ->where('hidden_profile','NONE') // ->whereHas('profileCourtship',function($q) use ($tody_date,$new_tody_date){ // $q->where('birthday','like',"%$tody_date") // ->orWhere('birthday','like',"%$new_tody_date"); // })->get(['nickname','id','mobile']); // return $todayBirthdayUserList; // $tody_date = date('m-d'); // $result = mb_substr($tody_date,0,2); // $new_tody_date = $tody_date; // if($result < 10){ // $new_tody_date = str_replace('0','',$tody_date); // } // $todayBirthdayUserList = User::where('type','single') // ->where('is_real_approved','1') // ->where('hidden_profile','NONE') // ->whereHas('profileCourtship',function($q) use ($tody_date,$new_tody_date){ // $q->where('birthday','like',"%$tody_date") // ->orWhere('birthday','like',"%$new_tody_date"); // })->get(['nickname','id','mobile']); // return $todayBirthdayUserList; $tody_date = date('m-d'); $todayBirthdayUserList = User::where('type', 'single') ->where('is_real_approved', '1') ->where('hidden_profile', 'NONE') ->whereHas('profileCourtship', function ($q) use ($tody_date) { $q->where('birthday', 'like', '%' . $tody_date . '%'); })->get(['nickname', 'id', 'mobile']); return $todayBirthdayUserList; } /**获取群聊 */ public function getMyGroupChat($user, $keyword, $size = 15) { $teams = $user->team()->withCount('members'); if ($keyword) { $teams = $teams->where('tname', 'like', '%' . $keyword . '%'); } return $teams->paginate($size); } /**获取好友 */ public function getMyFriends($user, $keyword, $type, $size = 15) { $user_id = $user->id; $types = $type ? [$type] : ['marriage', 'single', 'loveing']; $ids = Linking::where('user_id', $user_id)->pluck('user_linking_id'); $other_ids = Linking::where('user_linking_id', $user_id)->pluck('user_id'); $users = User::where(function ($sql) use ($ids, $other_ids) { $sql->whereIn('id', $ids)->orWhereIn('id', $other_ids); })->whereIn('type', $types); if ($keyword) { $keyword = trim($keyword); $users = $users->where(function ($sql) use ($keyword) { $sql->where('name', 'like', '%' . $keyword . '%') ->orWhere('nickname', 'like', '%' . $keyword . '%') ->orWhere('mobile', 'like', '%' . $keyword . '%'); }); } $users = $users->orderBy('id', 'desc')->paginate($size); foreach ($users as $following) { $wechat = Wechat::where('user_id', $following->id)->first(); if (!empty($wechat)) { $following->photo = $following->photo ?: ($wechat->avatar2 ?: $wechat->avatar); } else { $following->photo = $following->photo ?: null; } $following->wechat = $wechat; $sex = null; $age = 0; $stature = null; $province = null; if ($following->type == 'single') { $court = ProfileCourtship::where('user_id', $following->id)->first(); if (!empty($court)) { $sex = $court->sex; if ($court->birthday) { $age = $this->commonUtils->getAge($court->birthday); } $stature = $court->stature; $province = $court->province; } } else { $court = ProfileMarriage::where('user_id', $following->id)->first(); if (!empty($court)) { $sex = $court->sex; if ($court->birthday) { $age = $this->commonUtils->getAge($court->birthday); } $stature = $court->stature; $province = $court->province; } } $following->sex = $sex; $following->age = $age; $following->stature = $stature; $following->province = $province; if ($following->type == 'single') { $following->profile = $following->profileCourtship; } else { $following->profile = $following->profileMarriage; } $linking = Linking::where('user_id', $user_id)->where('user_linking_id', $following->id)->first(); if (empty($linking)) { $linking = Linking::where('user_linking_id', $user_id)->where('user_id', $following->id)->first(); } $following->pivot = $linking; } return $users; } /** 获取用户列表 */ public function usersList($my_user, $sex, $age, $province, $city, $is_approve, $keyword) { //成员类型 良人 佳偶 介绍人 红娘 $select_sex = $my_user->sex; $sex = $sex ?: ($select_sex == 1 ? 2 : 1); $age = $age ?: '不限'; $times = \CommonUtilsService::searchAge($age); $province = $province ?: '不限'; $city = $city ?: '不限'; $users = User::with('profileCourtship')->whereHas('profileCourtship', function ($sql) use ($province, $city, $sex, $age, $times, $is_approve) { if ($province && $province != '不限') { $sql = $sql->where('province', $province); } if ($city && $city != "不限") { $sql = $sql->where('city', $city); } $sql = $sql->where('sex', $sex); if ($age != '不限') { $sql = $sql->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time']); } if (is_numeric($is_approve)) { $sql = $sql->where('is_approved', $is_approve); } }); if ($keyword) { $users = $users->where(function ($sql) use ($keyword) { $sql->where('nickname', 'like', '%' . $keyword . '%') ->orWhere('name', 'like', '%' . $keyword . '%'); }); } //增加黑名单隐藏 $blacklist_user_ids = LinkingBlackList::where('user_id', $my_user->id)->pluck('other_user_id')->toArray(); $dislike_user_ids = $my_user->dislikeLogs()->where('type', 'user')->pluck('type_id')->toArray(); //客服id; $unset_user_ids = array_merge($blacklist_user_ids, $dislike_user_ids, User::FULLLINKIDS); //小程序查看22-35的用户 $users = $users->whereHas('profileCourtship', function ($query) { $query->whereBetween('birthday', ['1989-01-01 00:00:00', '1999-01-01 00:00:00']); }); if (empty($sex) && empty($age) && empty($province) && empty($city) && !is_numeric($is_approve) && empty($keyword)) { $user_ids = RecommendUser::where('user_id', $my_user->id)->whereHas('otherUser', function ($sql) use ($my_user) { $sql->where('is_approved', 1)->where('sex', '<>', $my_user->sex)->where('belief', $my_user->belief)->where('hidden_profile', '<>', 'ALLSEX')->whereNotIn('id', User::FULLLINKIDS)->where('can_be_found', 1); })->limit(7)->orderBy('start_time', 'asc')->pluck('other_user_id')->toArray(); $users = $users->whereNotIn('id', $user_ids); } $users = $users->whereNotIn('id', $unset_user_ids); $users = $users->where('hidden_profile', 'NONE')->orderBy('rank_id', 'desc')->where('type', 'single')->orderBy('is_real_approved', 'desc')->orderBy('is_approved', 'desc')->orderBy('liveness', 'desc')->orderBy('info_complete_score', 'desc')->orderBy('last_visit', 'desc')->paginate(); foreach ($users as $user) { $rd_user_key = User::cacheUserKey($user->id); if (Cache::has($rd_user_key)) { $rd_user = Cache::get($rd_user_key); } else { $user->cacheUser(); $rd_user = Cache::get($rd_user_key); } $user->age = $rd_user->age; $user->profile_courtship = $rd_user->profileCourtship; $user->profile_courtship->interest_label = json_decode($user->profile_courtship->interest_label) ?: []; //是否关注 // $user->is_followed = $my_user->isFollowing($user); //是否是超级会员 $user->isSuperRank = $rd_user->is_super_rank; $user->photo = $user->avatar; unset($user->face_score, $user->profileCourtship->interest_label); } return $users; } /**获取好友的总数量 */ public function getMyFriendCount($user, $type) { $user_id = $user->id; $types = $type ? [$type] : ['marriage', 'single', 'loveing']; $ids = Linking::where('user_id', $user_id)->pluck('user_linking_id'); $other_ids = Linking::where('user_linking_id', $user_id)->pluck('user_id'); $users = User::where(function ($sql) use ($ids, $other_ids) { $sql->whereIn('id', $ids)->orWhereIn('id', $other_ids); })->whereIn('type', $types); return $users->orderBy('id', 'desc')->count(); } /**获取校验结果 */ public function geTextVerifyCode($key, $text) { if (Cache::has($key)) { $code = Cache::get($key); return $code; } else { $res = \CommonUtilsService::verifyText([$text], 3); $val = $res['code']; Cache::set($key, $val, 1296000); return $val; } } /**获取用户的可以用的头像 */ public function getUserValidPhoto($user) { try { if ($user->app_avatar) { $avatar = strstr($user->app_avatar, '?x-oss-process=style/scale1') ? $user->app_avatar : $user->app_avatar . '?x-oss-process=style/scale1'; } elseif ($user->photo) { $avatar = strstr($user->photo, '?x-oss-process=style/scale1') ? $user->photo : $user->photo . '?x-oss-process=style/scale1'; } elseif ($user->circle_avatar) { $avatar = $user->circle_avatar; } elseif (empty($user->photo) && empty($user->app_avatar)) { if ($user->sex == 1) { $avatar = 'http://images.ufutx.com/201811/12/0e8b72aae6fa640d9e73ed312edeebf3.png'; } elseif ($user->sex == 2) { $avatar = 'http://images.ufutx.com/201811/12/dddd79aa2c2fc6a6f35e641f6b8fb8f5.png'; } else { $avatar = 'https://images.ufutx.com/202007/01/e0de60525143427d4dd19515a5b387ba.png'; } } return $avatar; } catch (\Exception $e) { \CommonUtilsService::HandleLogsAdd(3, '获取最适合的头像失败', [], 'getUserValidPhoto'); return 'https://images.ufutx.com/202007/01/e0de60525143427d4dd19515a5b387ba.png';; } return 'https://images.ufutx.com/202007/01/e0de60525143427d4dd19515a5b387ba.png';; } public function defaultAvatar($sex) { if ($sex == 1) { return 'http://images.ufutx.com/201811/12/0e8b72aae6fa640d9e73ed312edeebf3.png'; } else { return 'http://images.ufutx.com/201811/12/dddd79aa2c2fc6a6f35e641f6b8fb8f5.png'; } } /**对比IM和我方的用户数据,如果不一致,则更新 */ public function comparisonImUserinfo($wyy_user, $user) { if (empty($wyy_user)) { return true; } if (empty($user)) { return true; } // $im_is_updateInfo = false; // if($wyy_user->nickname != $user->nickname){ // $wyy_user->nickname = $user->nickname // } } /**补充用户的其他信息 */ public function replenishUserOtherInfo($user_id, $nickname) { try { $viewer = Viewer::where('user_id', $user_id)->first(); if ($viewer) { $viewer->nickname = $nickname; $viewer->save(); } } catch (\Exception $e) { $this->getError($e); \CommonUtilsService::HandleLogsAdd(3, '更新H5头像失败', [], 'replenishUserOtherInfo'); } } /** * @param $mobile * @param $password */ public function addUnionUser($mobile, $password, $source, $email, $openid) { try { $user = null; if ($email) { //判断是否有邮箱 $user = UnionUser::where('email', $email)->first(); } if (empty($user) && $openid) { $user = UnionUser::where('openid', $openid)->first(); } if (empty($user) && $mobile) { $user = UnionUser::where('mobile', $mobile)->first(); } //增加账号 if (empty($user)) { $user = new UnionUser; $user->mobile = $mobile; $user->email = $email; $user->openid = $openid; if ($password) { $user->password = md5(sha1($password) . 'ufutx2021$'); } $user->source = $source; } //同步密码 if ($user && empty($user->password) && $password) { $user->password = md5(sha1($password) . 'ufutx2021$'); } //同步email if ($user && empty($user->email) && $email) { $user->email = $email; } //同步openid if ($user && empty($user->openid) && $openid) { $user->openid = $openid; } $user->save(); return $user; } catch (\Exception $e) { $this->getError($e); return null; } } /** * @param string $mobile * @param string $password * @param int $uuid */ // public function changeUnionUser($mobile, $password, $uuid) // { // try{ // if ($uuid){ // $user = UnionUser::where('id', $uuid)->first(); // $m_user = UnionUser::where('mobile', $mobile)->first(); // if ($) // if ($user && ($user->mobile != $mobile)){ // $user->mobile = $mobile; // $user->save(); // } // } else{ // $user = $this->addUnionUser($mobile, $password); // } // return $user; // } catch (\Exception $e) { // $this->getError($e); // return null; // } // } /** * @param $user 小程序用户 * @param $openid 公众号openid * @return MerchantUser saas用户 */ public function syncSaasUser($user = null, $openid = null) { try { if ($user) { $userId = $user->id; $wechat = $user->wechat; $unionid = null; if ($wechat) { $openid = $wechat->official_openid; $unionid = $wechat->unionid; } //添加商家用户 $merchant_user = MerchantUser::where('user_id', $user->id)->first(); $rand_str = \CommonUtilsService::randString(8); if (!$merchant_user) {//没有绑定user_id //是否有手机号存在的账号 if ($user->mobile) { $merchant_user = MerchantUser::where('mobile', $user->mobile)->whereNull('user_id')->first(); } if (empty($merchant_user)) {//手机号不存在账号 //是否微信openid存在账号 if ($openid) { $merchant_user = MerchantUser::where('openid', $openid)->whereNull('user_id')->first(); if ($merchant_user) { //同步手机号 if (empty($merchant_user->mobile)) { $merchant_user_by_mobile = MerchantUser::where('mobile', $user->mobile)->first(); if (empty($merchant_user_by_mobile)) { $merchant_user->mobile = $user->mobile; } } $merchant_user->user_id = $userId; $merchant_user->save(); } else { if ($unionid) { $merchant_user = MerchantUser::where('unionid', $unionid)->first(); if ($merchant_user && empty($merchant_user->user_id)) { $merchant_user->user_id = $userId; $merchant_user->save(); }else { $has_openid = MerchantUser::where('openid', $openid)->first(); $has_mobile = MerchantUser::where('mobile', $user->mobile)->first(); $merchant_user = new MerchantUser(); $merchant_user->user_id = $userId; $merchant_user->openid = $has_openid ? null : $openid; $merchant_user->unionid = null; $merchant_user->mobile = $has_mobile ? null : $user->mobile; $merchant_user->rand_str = $rand_str; $merchant_user->pic = $user->userAvatar(); $merchant_user->nickname = $user->nickname; $merchant_user->save(); } }else { $has_openid = MerchantUser::where('openid', $openid)->first(); $has_mobile = MerchantUser::where('mobile', $user->mobile)->first(); $merchant_user = new MerchantUser(); $merchant_user->user_id = $userId; $merchant_user->openid = $has_openid ? null : $openid; $merchant_user->unionid = $unionid; $merchant_user->mobile = $has_mobile ? null : $user->mobile; $merchant_user->rand_str = $rand_str; $merchant_user->pic = $user->userAvatar(); $merchant_user->nickname = $user->nickname; $merchant_user->save(); } } } else { if ($unionid) { $merchant_user = MerchantUser::where('unionid', $unionid)->first(); if ($merchant_user && empty($merchant_user->user_id)) { $merchant_user->user_id = $userId; $merchant_user->save(); }else { $has_mobile = MerchantUser::where('mobile', $user->mobile)->first(); $merchant_user = new MerchantUser(); $merchant_user->user_id = $userId; $merchant_user->openid = $openid; $merchant_user->unionid = null; $merchant_user->mobile = $has_mobile ? null : $user->mobile; $merchant_user->rand_str = $rand_str; $merchant_user->pic = $user->userAvatar(); $merchant_user->nickname = $user->nickname; $merchant_user->save(); } }else { $has_mobile = MerchantUser::where('mobile', $user->mobile)->first(); $merchant_user = new MerchantUser(); $merchant_user->user_id = $userId; $merchant_user->openid = $openid; $merchant_user->unionid = $unionid; $merchant_user->mobile = $has_mobile ? null : $user->mobile; $merchant_user->rand_str = $rand_str; $merchant_user->pic = $user->userAvatar(); $merchant_user->nickname = $user->nickname; $merchant_user->save(); } } } else { //同步openid 和user_id if (empty($merchant_user->openid) && $openid) { //openid是否是同一账号 $merchant_user_by_openid = MerchantUser::where('openid', $openid)->first(); if (empty($merchant_user_by_openid)) { $merchant_user->openid = $openid; } } $merchant_user->user_id = $userId; $merchant_user->save(); } } else { //同步openid 和mobile if (empty($merchant_user->openid) && $openid) { //openid是否是同一账号 $merchant_user_by_openid = MerchantUser::where('openid', $openid)->first(); if (empty($merchant_user_by_openid)) { $merchant_user->openid = $openid; } } //同步openid 和user_id if (empty($merchant_user->mobile) && $user->mobile) { //openid是否是同一账号 $merchant_user_by_mobile = MerchantUser::where('mobile', $user->mobile)->first(); if (empty($merchant_user_by_mobile)) { $merchant_user->mobile = $user->mobile; } } $merchant_user->save(); } return $merchant_user; } elseif ($openid) { $merchant_user = MerchantUser::where('openid', $openid)->first(); if (empty($merchant_user)) { $merchant_user = new MerchantUser(); $merchant_user->openid = $openid; $merchant_user->rand_str = \CommonUtilsService::randString(8); $merchant_user->nickname = '用户' . $merchant_user->rand_str; $merchant_user->save(); } } } catch (\Exception $e) { $this->getError($e); return null; } } /** * 通过openid获取wechat * @param $openid * @param null $unionid * @return void|null */ public function wechatByOpenid($openid, $unionid = null) { try { $wechat = Wechat::withTrashed()->where('openid', $openid)->first(); //判断是否注册过 if (empty($wechat)) { //判断是否有同样的unionid $wechat = Wechat::where('unionid', $unionid)->whereNull('openid')->first(); if (empty($wechat)) { $wechat = new Wechat(); $wechat->openid = $openid; $wechat->unionid = $unionid; $wechat->save(); } else { $wechat->openid = $openid; $wechat->save(); } } elseif ($wechat->deleted_at) {//已删除用户 $wechat->restore(); } if (empty($wechat->unionid)) { $wechat->unionid = $unionid; $wechat->save(); } return $wechat; } catch (\Exception $e) { $this->getError($e); return null; } } /** * 通过wechat 获取 user * @param $wechat * @return User|null */ public function userByWechat($wechat) { try { $user = $wechat->user; if ($user) return $wechat->user->where('id', $wechat->user_id)->select('id', 'photo', 'nickname', 'mobile', 'type', 'rank_id', 'sex', 'belief', 'is_subscribe', 'hidden_profile')->first();; DB::beginTransaction(); $rand_str = \CommonUtilsService::randString(7); $user = new User(); $user->mobile = null; $user->nickname = "用户" . $rand_str; $user->from_user_id = request()->input('from_user_id'); $user->from_openid = request()->input('from_openid'); $user->from_merchant_id = request()->input('from_merchant_id',0); $user->photo = null; $user->type = "single"; $user->rank_id = 0; $user->regist_channel = "mini_service"; $user->hidden_profile = "NONE"; $user->is_subscribe = 0; $user->save(); $profile = new ProfileCourtship(); $profile->user_id = $user->id; $profile->save(); $wechat->user_id = $user->id; $wechat->save(); //添加小天使 // $this->registerAddCherub($user->id); DB::commit(); return $user; } catch (\Exception $e) { DB::rollBack(); $this->getError($e); return null; } } /** * 是否完善基本资料 * @param $user * @return false|int 0:没有手机号没有其他基本资料,1:有手机号没有其他基本资料,2:完善资料 */ public function isCompleteBaseInfo($user) { try { $is_complete = 2; foreach (User::MPBASEUSERINFO as $value) { if (empty($user->$value)) { if ($value == 'mobile') return $is_complete = 0; $is_complete = 1; } } $user->profile_courtship = $user->profileCourtship()->where('user_id', $user->id) ->select('id', 'user_id', 'sex', 'belief', 'birthday', 'state', 'province', 'city') ->first(); if (empty($is_complete)) return $is_complete = 1; if ($user->type == 'single' && empty($user->profile_courtship)) return $is_complete = 1; foreach (User::MPBASEPROFILEINFO as $value) { if (empty($user->profileCourtship->$value)) { $is_complete = 1; break; } } return $is_complete; } catch (\Exception $e) { $this->getError($e); return false; } } /** * 是否完成所有资料 * @param $user */ public function isCompleteAllInfo($user) { try { foreach (User::MPALLUSER as $value) { if (empty($user->$value)) return false; } foreach (User::MPALLPROFILE as $value) { if (empty($user->profileCourtship)) return false; if (empty($user->profileCourtship->$value)) return false; } return true; }catch (\Exception $e) { $this->getError($e); return false; } } /** * 检查手机号 * @return false|string */ public function checkMobile($mobile) { try { if (!Str::isMobile(request()->mobile)) return "您输入的手机号有误,手机号无效"; $user = User::withTrashed()->where('mobile', request()->mobile)->first(); if ($user) $user->restore(); if ($user && ($user->id != auth()->id())) return '您输入的手机号已存在其他账号【'.$user->nickname.'--'.auth()->id().'】'; return ''; } catch (\Exception $e) { $this->getError($e); return false; } } /** * 生成客服备注 * @return false|true * channel 0:saas h5 1:福恋h5 2:小程序 * openid 用户openid * from_openid 分享人openid * type 类型 活动 服务 课程 咨询 商品 测评 * type_id 对应类型id * title 对应服务标题 */ public function generateClientComment($channel = 0,$openid,$from_openid,$type,$type_id,$title){ if(empty($openid) || empty($from_openid)) return false; if($openid == $from_openid) return false; if($channel == 0 || $channel == 1){ $merchant_user = MerchantUser::where('openid',$openid)->first(); $from_merchant_user = MerchantUser::where('openid',$from_openid)->first(); if($merchant_user && $merchant_user->user_id){ $user_id = $merchant_user->user_id; } if(!isset($user_id)){ $wechat = Wechat::where('official_openid',$openid)->first(); if($wechat && $wechat->user_id){ $user_id = $wechat->user_id; } } if(!isset($user_id)){ $viewer = Viewer::where('openid',$openid)->first(); if($viewer && $viewer->user_id){ $user_id = $viewer->user_id; } } if($from_merchant_user && $from_merchant_user->user_id){ $from_user_id = $from_merchant_user->user_id; } if(!isset($from_user_id)){ $from_wechat = Wechat::where('official_openid',$from_openid)->first(); if($from_wechat && $from_wechat->user_id){ $from_user_id = $from_wechat->user_id; } } if(!isset($from_user_id)){ $from_viewer = Viewer::where('openid',$openid)->first(); if($from_viewer && $from_viewer->user_id){ $from_user_id = $from_viewer->user_id; } } $way = $channel == 0 ? 'saasH5' : '福恋H5'; }elseif($channel == 2){ //小程序分享 $wechat = Wechat::where('openid',$openid)->first(); $from_wechat = Wechat::where('openid',$from_openid)->first(); if($wechat && $wechat->user_id){ $user_id = $wechat->user_id; } if($from_wechat && $from_wechat->user_id){ $from_user_id = $from_wechat->user_id; } $way = '小程序'; } $match_type = 'service'; if(isset($user_id) && isset($from_user_id)){ $match_type = $this->getServiceType($type); $boolean = PortraitRecord::where('user_id',$user_id)->where('recommend_user_id',$from_user_id)->where('match_type',$match_type)->where('channel',$channel)->exists(); if($boolean) return false; $from_nickname = User::where('id',$from_user_id)->value('nickname'); ClientComment::create([ 'user_id'=> $user_id, 'maker_user_id'=>$from_user_id, 'comment'=>'用户【'.$from_nickname.'】通过'.$way.'向他推荐了'.$type.' 【'.$title.'】 ', ]); PortraitRecord::create([ 'user_id'=>$user_id, 'recommend_type'=>'user', 'recommend_user_id'=>$from_user_id, 'match_type'=>$match_type, 'match_type_id'=>$type_id, 'match_user_id'=>null, 'pay_status'=>0, 'channel'=>$channel ]); return true; } return false; } public function getServiceType($type){ switch ($type) { case '课程': return 'course'; break; case '活动': return 'activity'; break; case '服务': return 'service'; break; case '测评': return 'evaluate'; break; case '课程': return 'course'; break; case '咨询': return 'consult'; break; case '商品': return 'shop'; break; default: return 'system'; break; } } }