love_php/app/Http/Controllers/HomeController.php
2026-04-02 09:20:51 +08:00

3797 lines
178 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers;
use App\Models\AnchorArticle;
use App\Models\AnchorVideo;
use App\Models\ClassArea;
use App\Models\CloseProfileHistory;
use App\Models\CommunityActivity;
use App\Models\CommunityStar;
use App\Models\Course\Course;
use App\Models\Live\Anchor;
use App\Models\Live\AnchorViewer;
use App\Models\Live\Live;
use App\Models\Live\Viewer;
use App\Models\MerchantAccount;
use App\Models\MerchantInformation;
use App\Models\MpAdvertises;
use App\Models\RecommendCommunityActivity;
use App\Models\RecommendLinkingNew;
use App\Models\Server\TouristOrder;
use Carbon\Carbon;
use Illuminate\Http\Request;
use \App\Facades\CommonUtilsService;
use App\Models\ProfileCourtship;
use App\Models\ProfileMarriage;
use App\Models\PositionHistory;
use App\Models\ArticleHistory;
use App\Models\MatchingRate;
use App\Models\Wechat;
use App\Models\User;
use App\Utils\Http;
use App\Models\Matchmaker;
use EasyWechat;
use App\Models\HomeRecommend;
use App\Models\ChatMessage;
use App\Models\Linking;
use App\Models\Announcement;
use App\Contracts\UserContract;
use App\Models\Paas;
use App\Models\AreaUser;
use App\Models\UserProfile;
use App\Models\TempMember;
use App\Models\ShareSideUser;
use App\Models\PaasAnnouncement;
use App\Models\RecommendUser;
use App\Http\Response\ResponseJson;
use App\Jobs\AddUnionUser;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Jobs\NewMerchantDefaultService;
use App\Models\ActivityMember;
use App\Models\Answers;
use App\Models\ConsultAccount;
use App\Models\Course\UserCourses;
use Illuminate\Support\Facades\Cache;
use App\Models\VersionsCheck;
use Illuminate\Support\Facades\DB;
use App\Models\LinkingBlacklist;
use App\Models\MEarningRules;
use App\Models\Questionnaire;
use App\Models\Server\Counselor;
use App\Models\Server\MerchantUser;
use App\Models\Server\SaasNotice;
use Exception;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Models\Configs;
ini_set('max_execution_time', '300');
class HomeController extends Controller
{
use ResponseJson;
protected $userCon;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct(UserContract $userCon)
{
//$this->middleware('auth');
$this->userCon = $userCon;
}
public function index()
{
return view('home');
}
/**
* 附近人
*/
public function nearSignles(Request $request)
{
$user = auth()->user();
$user_id = $user->id;
if (empty($user)) {
$user = $this->authCheck();
}
$location_latitude = $request->input('location_latitude');
$location_longitude = $request->input('location_longitude');
$southwest = $request->input('southwest');
$northeast = $request->input('northeast');
$users = User::where('type', 'single')->orderBy('id', 'desc');
if ($user) {
$users = $users->where('id', '<>', $user->id);
if ($user->type == 'single') {
$user_sex = ProfileCourtship::where('user_id', $user->id)->value('sex');
if ($user_sex == 1) {//男
$users = $users->whereHas('profileCourtship', function ($sql) {
$sql->where('sex', 2);
});
} elseif ($user_sex == 2) {
$users = $users->whereHas('profileCourtship', function ($sql) {
$sql->where('sex', 1);
});
} else {
$gender = Wechat::where('user_id', $user->id)->value('gender');
if ($gender == 1) {//男
$users = $users->whereHas('profileCourtship', function ($sql) {
$sql->where('sex', 2);
});
} elseif ($gender == 2) {
$users = $users->whereHas('profileCourtship', function ($sql) {
$sql->where('sex', 1);
});
}
}
}
if ($user->location_latitude != $location_latitude && $user->location_longitude != $location_longitude && $location_longitude != 'undefined') {
User::where('id', $user->id)->update(['location_latitude' => $location_latitude, 'location_longitude' => $location_longitude]);
$position = PositionHistory::create([
'user_id' => $user->id,
'location_latitude' => $location_latitude,
'location_longitude' => $location_longitude,
]);
}
}
if ($northeast && $southwest) {
$northeast_arr = json_decode($northeast, true);
$southwest_arr = json_decode($southwest, true);
$users = $users->whereBetween('location_latitude', [$southwest_arr['latitude'], $northeast_arr['latitude']])->whereBetween('location_longitude', [$southwest_arr['longitude'], $northeast_arr['longitude']]);
}
$keyword = $request->input('keyword');
if ($keyword) {
$keyword = trim($keyword);
$users = $users->where('name', 'like', '%' . $keyword . '%');
}
$nopage = $request->input('nopage');
if ($nopage) {
$users = $users->select('id', 'name', 'location_latitude', 'location_longitude', 'type')->limit(14)->get();
} else {
$users = $users->select('id', 'name', 'location_latitude', 'location_longitude', 'type')->paginate();
}
foreach ($users as $user) {
$distance = $this->getDistance($location_longitude, $location_latitude, $user->location_longitude, $user->location_latitude);
$avatar = Wechat::where('user_id', $user->id)->value('avatar');
if ($user->type == 'single') {
$sex = ProfileCourtship::where('user_id', $user->id)->value('sex');
} else {
$sex = ProfileMarriage::where('user_id', $user->id)->value('sex');
}
$user->distance = $distance;
$user->avatar = $avatar;
$user->sex = $sex ? $sex : 0;
$matching_weight = MatchingRate::where('user_id', $user_id)->where('other_user_id', $user->id)->value('weight');
if (empty($matching_weight)) {
$matching_weight = 0;
}
$user->matching_weight = $matching_weight;
}
return $this->success('ok', $users);
}
/**
* 问卷调查信息
*/
public function get_questionnaires(Request $request)
{
$type = $request->type ?? 'activity';
$type_id = $request->type_id ?? 1;
$result = Questionnaire::where('type', $type)->where('type_id', $type_id)->where('status', 1)->first();
$result->content = json_decode($result->content, true);
return $this->success('ok', $result);
}
/**
* 提交问卷
*/
public function post_questionnaires(Request $request)
{
$user_id = auth()->user()->id;
$content = $request->input('content');
$questionnaire_id = $request->questionnaire_id;
$result = Questionnaire::where('id', $questionnaire_id)->where('status', 1)->first();
if (!$result) return $this->failure('未查询该问卷信息~');
if ($result->type == 'activity') {
$member = ActivityMember::where('user_id', $user_id)->where('activity_id', $result->type_id)->count();
if (!$member) return $this->resp('fail', '您未报名此活动,无法填写此活动的调查问卷');
}
$exists = Answers::where('user_id', $user_id)->where('questionnaire_id', $questionnaire_id)->count();
if ($exists) return $this->failure('您已经提交过问卷啦~');
$Answers = new Answers;
$Answers->user_id = $user_id;
$Answers->content = json_encode($content);
$Answers->questionnaire_id = $questionnaire_id;
$Answers->save();
return $this->resp('ok', [], 1);
}
//
public function nearSignlesV2(Request $request)
{
$user = auth()->user();
$user_id = $user->id;
if (empty($user)) {
$user = $this->authCheck();
}
$location_latitude = $request->input('location_latitude', '22.53332');
$location_longitude = $request->input('location_longitude', '113.93041');
$southwest = $request->input('southwest');
$northeast = $request->input('northeast');
if ($user->location_latitude != $location_latitude || $user->location_longitude != $location_longitude && $location_longitude != 'undefined') {
User::where('id', $user->id)->update(['location_latitude' => $location_latitude, 'location_longitude' => $location_longitude]);
if (!empty($location_latitude) && !empty($location_longitude)) {
$position = PositionHistory::create([
'user_id' => $user->id,
'location_latitude' => $location_latitude,
'location_longitude' => $location_longitude,
]);
}
}
$keyword = $request->input('keyword');
$rates = MatchingRate::whereHas('matchingOtherUser', function ($sql) use ($user, $keyword, $northeast, $southwest) {
if ($user->type == 'single') {
if ($user->sex == 1) {
$sql = $sql->where('type', 'single')->where('sex', 2);
} elseif ($user->sex == 2) {
$sql = $sql->where('type', 'single')->where('sex', 1);
}
// $user_sex = ProfileCourtship::where('user_id', $user->id)->value('sex');
// if ($user_sex == 1) {//男
// $sql = $sql->whereHas('profileCourtship', function($sql_counrt) {
// $sql_counrt->where('sex', 2);
// });
// }elseif ($user_sex == 2) {
// $sql = $sql->whereHas('profileCourtship', function($sql_counrt) {
// $sql_counrt->where('sex', 1);
// });
// }else{
// $gender = Wechat::where('user_id', $user->id)->value('gender');
// if ($gender == 1) {//男
// $sql = $sql->whereHas('profileCourtship', function($sql_counrt) {
// $sql_counrt->where('sex', 2);
// });
// }elseif ($gender == 2) {
// $sql = $sql->whereHas('profileCourtship', function($sql_counrt) {
// $sql_counrt->where('sex', 1);
// });
// }
// }
}
if ($keyword) {
$keyword = trim($keyword);
$sql = $sql->where('name', 'like', '%' . $keyword . '%');
}
if ($northeast && $southwest) {
$northeast_arr = json_decode($northeast, true);
$southwest_arr = json_decode($southwest, true);
$sql->whereBetween('location_latitude', [$southwest_arr['latitude'], $northeast_arr['latitude']])->whereBetween('location_longitude', [$southwest_arr['longitude'], $northeast_arr['longitude']]);
}
})->where('user_id', $user_id);
$nopage = $request->input('nopage');
if ($nopage) {
$rates = $rates->select('id', 'other_user_id', 'weight')->limit(50)->orderBy('weight', 'desc')->get();
} else {
$rates = $rates->orderBy('weight', 'desc')->orderBy('id', 'desc')->paginate(10);
}
foreach ($rates as $rate) {
$rate_user = User::where('id', $rate->other_user_id)->select('id', 'name', 'location_latitude', 'location_longitude', 'type', 'circle_avatar', 'sex')->first();
$distance = $this->getDistance($location_longitude, $location_latitude, $rate_user->location_longitude, $rate_user->location_latitude);
if ($rate_user->type == 'single') {
$photos = ProfileCourtship::where('user_id', $rate_user->id)->value('photos');
if (empty($photos)) {
$photos = [];
} else {
$photos = json_decode($photos, true);
}
} else {
$photos = [];
}
if ($rate_user->circle_avatar) {
$circle_avatar = $rate_user->circle_avatar;
} else {
if ($rate_user->sex == 1) {
$circle_avatar = 'http://images.ufutx.com/201811/12/0e8b72aae6fa640d9e73ed312edeebf3.png';
} elseif ($rate_user->sex == 2) {
$circle_avatar = 'http://images.ufutx.com/201811/12/dddd79aa2c2fc6a6f35e641f6b8fb8f5.png';
} else {
$circle_avatar = '';
}
}
$rate_user->distance = $distance;
$rate_user->circle_avatar = $circle_avatar;
$rate_user->avatar = $circle_avatar;
$rate_user->photos = $photos;
$rate->rate_user = $rate_user;
}
return $this->success('ok', $rates);
}
// 大数据推荐
public function recommendLinkings(Request $request, $user_id)
{
$linkings = RecommendLinkingNew::select('id', 'id_users_left', 'id_users_right')->where('id_users_left', $user_id)->where('is_recommend', 1)->whereHas('otherUser', function ($sql) {
$sql->where('hidden_profile', 'NONE')->photo();
})->orderBy('score', 'desc')->orderBy('id', 'desc')->paginate(7);
foreach ($linkings as $link) {
$link->city = $link->otherUser->profileCourtship->city;
$link->stature = $link->otherUser->profileCourtship->stature;
$link->introduction = $link->otherUser->profileCourtship->introduction;
$link->isSuperRank = $link->otherUser->isSuperRank();
$link->birthday = $link->otherUser->profileCourtship->birthday;
$link->age = substr($link->birthday, 2, 2);
$link->company = $link->otherUser->profileCourtship->company;
$link->post = $link->otherUser->profileCourtship->post;
$link->degree = $link->otherUser->profileCourtship->degree;
$link->nickname = $link->otherUser->nickname;
$link->sex = $link->otherUser->sex;
$link->type = $link->otherUser->type;
$link->is_approved = $link->otherUser->is_approved;
$link->is_real_approved = $link->otherUser->is_real_approved;
$link->avatar = $link->otherUser->avatar;
unset($link->otherUser);
unset($link->profile_courtship);
}
return $this->success('ok', $linkings);
}
/**
* 个人距离
*/
public function nearSignle(Request $request, $id)
{
$user = User::where('id', $id)->select('id', 'name', 'location_longitude', 'location_latitude', 'type')->first();
if ($user->type == 'single') {
$profile = ProfileCourtship::where('user_id', $id)->select('sex', 'birthday')->first();
} else {
$profile = ProfileMarriage::where('user_id', $id)->select('sex', 'birthday')->first();
}
$age = 0;
$sex = 0;
if ($profile && $profile->birthday) {
$age = $this->getAge($profile->birthday);
}
if ($profile && $profile->sex) {
$sex = $profile->sex;
}
$user->age = $age;
$user->sex = $sex;
$location_latitude = $request->input('location_latitude');
$location_longitude = $request->input('location_longitude');
$wechat = Wechat::where('user_id', $user->id)->select('avatar', 'avatar2')->first();
if (!empty($wechat)) {
$avatar = $wechat->avatar2 ? $wechat->avatar2 : $wechat->avatar;
} else {
if ($user->sex == 1) {
$avatar = 'http://images.ufutx.com/201811/12/0e8b72aae6fa640d9e73ed312edeebf3.png';
} else {
$avatar = 'http://images.ufutx.com/201811/12/dddd79aa2c2fc6a6f35e641f6b8fb8f5.png';
}
}
$user->avatar = $avatar;
$distance = $this->getDistance($location_longitude, $location_latitude, $user->location_longitude, $user->location_latitude);
$user->distance = $distance;
return $this->success('ok', $user);
}
/**
* 记录阅读
*/
public function addArticleHistory(Request $request)
{
$url = $request->input('url');
if (empty($url)) {
return $this->failure('no url');
}
$user = auth()->user();
if (empty($user)) {
$user = $this->authCheck();
}
if (!empty($user)) {
$count = ArticleHistory::where(['url' => $url, 'user_id' => $user->id])->count();
if (empty($count)) {
ArticleHistory::create([
'url' => $url,
'user_id' => $user->id,
]);
}
}
$url_arr = parse_url($url);
$url_arr['host'] = 'mp.ufutx.net';
$new_url = $this->http_build_url($url_arr);
// return redirect($new_url);
return $this->success('ok', $new_url);
}
public function http_build_url($url_arr)
{
$scheme = 'https';
$new_url = $scheme . "://" . $url_arr['host'];
if (isset($url_arr['port']) && !empty($url_arr['port'])) {
$new_url = $new_url . ":" . $url_arr['port'];
}
$new_url = $new_url . $url_arr['path'];
if (isset($url_arr['query']) && !empty($url_arr['query'])) {
$new_url = $new_url . "?" . $url_arr['query'];
}
if (isset($url_arr['fragment']) && !empty($url_arr['fragment'])) {
$new_url = $new_url . "#" . $url_arr['fragment'];
}
return $new_url;
}
/**
* 活动列表
*/
public function parties(Request $request)
{
$url = config('app.party_url') . '/api/parties/v2';
$data = [];
$parties = json_decode(Http::http($url, $data, "GET"));
return $this->success('ok', $parties);
}
/**
* 活动详情
*/
public function party(Request $request, $id)
{
$url = config('app.party_url') . '/api/parties/' . $id;
$data = [];
$party = json_decode(Http::http($url, $data, "GET"));
return $this->success('ok', $party);
}
/**
* 文章列表
*/
public function articles(Request $request)
{
$url = 'scraper.hankin.ufutx.cn/label/articles';
$title = $request->input('title', '推荐');
$data = [
'title' => $title,
];
$keyword = $request->input('keyword');
if ($keyword) {
$data['keyword'] = trim($keyword);
}
$articles = json_decode(Http::http($url, $data, "GET"));
return $this->success('ok', $articles);
}
/**
* 分享图片
*/
public function shareHome()
{
$openid = $this->getOpenid();
$user = auth()->user();
if ($user->home_share) {
$pic = $user->home_share;
return $this->success('ok', compact('pic'));
}
$app = EasyWechat::miniProgram();
$data = [];
$data['is_hyaline'] = true;
$response = $app->app_code->get('/pages/users/register?from_openid=' . $openid, $data);
$time = time();
$path = $time . 'home_qrcode.png';
$filename = $response->saveAs(storage_path('qrcode'), $path);
$image1 = 'http://images.ufutx.com/201807/16/6826a6e371cc6a38e99217c7bb3e3449.jpeg';
$image2 = storage_path() . '/qrcode/' . $path;
$new_image = $this->imageAddImage($image1, $image2, 371, 371, 208, 818, 'top-leftt');
$wechat = Wechat::where('openid', $openid)->first();
$name = '';
if (!empty($wechat)) {
if (!empty($wechat->avatar) || !empty($wechat->avatar2)) {
$avatar = $wechat->avatar2 ? $wechat->avatar2 : $wechat->avatar;
$new_image = $this->imageAddImage($new_image, $avatar, 183, 183, 302, 75, 'top-left');
}
}
if ($wechat->user_id) {
$name = User::where('id', $wechat->user_id)->value('name');
} else {
$name = $wechat->nickname;
}
$new_image = $this->textAddImage($new_image, '我是' . $name . ',向您推荐福恋!', 30, 393.5, 276, '#001100', 'center');
$file_path = storage_path() . "/qrcode/" . $time . "invite_qrcode.png";
$new_image->save($file_path);
$pic = '';
if (file_exists($file_path)) {
$pic = $this->uploadFile($file_path);
try {
unlink($file_path);
} catch (Exception $e) {
return $this->failure($e->getMessage());
exit();
}
}
if (PHP_OS == 'WINNT') {
$path = storage_path() . '\qrcode\\' . $path;
} else {
$path = storage_path() . '/qrcode/' . $path;
}
unlink($path);
$user->home_share = $pic;
$user->save();
return $this->success('ok', compact('pic'));
}
/**
* 首页
* @param Request $request [description]
* @return [type] [description]
*/
public function home(Request $request)
{
$user = auth()->user();
if (empty($user)) {
$user = $this->authCheck();
}
$maker = Matchmaker::where('user_id', $user->id)->where('status', 1)->first();
$location_latitude = $request->input('location_latitude');
$location_longitude = $request->input('location_longitude');
if ($user->location_latitude != $location_latitude || $user->location_longitude != $location_longitude && $location_longitude != 'undefined') {
User::where('id', $user->id)->update(['location_latitude' => $location_latitude, 'location_longitude' => $location_longitude]);
if (!empty($location_latitude) && !empty($location_longitude)) {
$position = PositionHistory::create([
'user_id' => $user->id,
'location_latitude' => $location_latitude,
'location_longitude' => $location_longitude,
]);
}
}
$result = $this->calcScope($location_latitude, $location_longitude, 500);
$like = null;
$man_count = 0;
$woman_count = 0;
$marriage_count = 0;
$maker_count = 0;
//好友
$friend_user_ids = $this->getFriendUserIds($user->id);
if ($user->sex == 1 && $user->type == 'single') {//单身男
//单身男
$man_count = User::where('type', 'single')->where('sex', 1)->where('id', '<>', $user->id)->whereBetween('location_latitude', [$result['minLat'], $result['maxLat']])
->whereBetween('location_longitude', [$result['minLng'], $result['maxLng']])->where('hidden_profile', '<>', 'ALLSEX')->count();
//单身女
$woman_count = User::where('type', 'single')->where('sex', 2)->whereBetween('location_latitude', [$result['minLat'], $result['maxLat']])
->whereBetween('location_longitude', [$result['minLng'], $result['maxLng']])->where('hidden_profile', '<>', 'ALLSEX')->count();
//介绍人
$marriage_count = User::where('type', 'marriage')->whereBetween('location_latitude', [$result['minLat'], $result['maxLat']])
->whereBetween('location_longitude', [$result['minLng'], $result['maxLng']])->where('hidden_profile', '<>', 'ALLSEX')->count();
//红娘
$user_ids = User::whereBetween('location_latitude', [$result['minLat'], $result['maxLat']])
->whereBetween('location_longitude', [$result['minLng'], $result['maxLng']])->where('id', '<>', $user->id)->where('hidden_profile', '<>', 'ALLSEX')->pluck('id');
$maker_count = Matchmaker::whereIn('user_id', $user_ids)->where('status', 1)->count();
} elseif ($user->sex == 2 && $user->type == 'single') {//单身女
//单身男
$man_count = User::where('type', 'single')->where('sex', 1)->whereBetween('location_latitude', [$result['minLat'], $result['maxLat']])
->whereBetween('location_longitude', [$result['minLng'], $result['maxLng']])->where('hidden_profile', '<>', 'ALLSEX')->count();
//单身女
$woman_count = User::where('type', 'single')->where('sex', 2)->where('id', '<>', $user->id)->whereBetween('location_latitude', [$result['minLat'], $result['maxLat']])
->whereBetween('location_longitude', [$result['minLng'], $result['maxLng']])->where('hidden_profile', '<>', 'ALLSEX')->count();
//介绍人
$marriage_count = User::where('type', 'marriage')->whereBetween('location_latitude', [$result['minLat'], $result['maxLat']])
->whereBetween('location_longitude', [$result['minLng'], $result['maxLng']])->where('hidden_profile', '<>', 'ALLSEX')->count();
//红娘
$user_ids = User::whereBetween('location_latitude', [$result['minLat'], $result['maxLat']])
->whereBetween('location_longitude', [$result['minLng'], $result['maxLng']])->where('id', '<>', $user->id)->where('hidden_profile', '<>', 'ALLSEX')->pluck('id');
$maker_count = Matchmaker::whereIn('user_id', $user_ids)->where('status', 1)->count();
} elseif ($user->type == 'marriage' || $user->type == 'loveing') {//介绍人
//单身男
$man_count = User::where('type', 'single')->where('sex', 1)->whereBetween('location_latitude', [$result['minLat'], $result['maxLat']])
->whereBetween('location_longitude', [$result['minLng'], $result['maxLng']])->where('hidden_profile', '<>', 'ALLSEX')->count();
//单身女
$woman_count = User::where('type', 'single')->where('sex', 2)->whereBetween('location_latitude', [$result['minLat'], $result['maxLat']])
->whereBetween('location_longitude', [$result['minLng'], $result['maxLng']])->where('hidden_profile', '<>', 'ALLSEX')->count();
//介绍人
$marriage_count = User::where('type', 'marriage')->where('id', '<>', $user->id)->whereBetween('location_latitude', [$result['minLat'], $result['maxLat']])
->whereBetween('location_longitude', [$result['minLng'], $result['maxLng']])->where('hidden_profile', '<>', 'ALLSEX')->count();
//红娘
$user_ids = User::whereBetween('location_latitude', [$result['minLat'], $result['maxLat']])
->whereBetween('location_longitude', [$result['minLng'], $result['maxLng']])->where('id', '<>', $user->id)->where('hidden_profile', '<>', 'ALLSEX')->pluck('id');
$maker_count = Matchmaker::whereIn('user_id', $user_ids)->where('status', 1)->count();
}
//附近人
$avatars = User::whereBetween('location_latitude', [$result['minLat'], $result['maxLat']])
->whereBetween('location_longitude', [$result['minLng'], $result['maxLng']])->where('id', '<>', $user->id)->where('hidden_profile', '<>', 'ALLSEX')->limit(4)->select('id', 'circle_avatar')->orderBy('circle_avatar', 'desc')->get();
foreach ($avatars as $avatar) {
if (empty($avatar->circle_avatar)) {
if ($avatar->sex == 1) {
$circle_avatar = 'http://images.ufutx.com/201811/12/0e8b72aae6fa640d9e73ed312edeebf3.png';
} else {
$circle_avatar = 'http://images.ufutx.com/201811/12/dddd79aa2c2fc6a6f35e641f6b8fb8f5.png';
}
$avatar->circle_avatar = $circle_avatar;
}
}
$person = [
"man_count" => $man_count,
"woman_count" => $woman_count,
"marriage_count" => $marriage_count,
"maker_count" => $maker_count,
'circle_avatar' => $avatars,
];
//轮播推荐
$recommends = HomeRecommend::where('status', 1)->whereNotNull('user_id')->get();
foreach ($recommends as $recommend) {
$user = $recommend->user;
if (!empty($user)) {
$recommend->name = $recommend->user->name;
$recommend->type = $user->type;
$recommend->photo = $recommend->photo ?: $user->photo;
$profile = $recommend->user->profileCourtship;
if ($profile) {
$recommend->age = $this->getAge($profile->birthday);
$recommend->address = $profile->resident_province . $profile->resident_city;
$recommend->stature = $profile->stature;
$recommend->city = $profile->city;
}
}
}
//path
$path_arr = [
'use_path' => 'https://mp.weixin.qq.com/s/9uMm-ud-R5_kPWbg-_ShsA',
'push_path' => 'https://mp.weixin.qq.com/s/L6AoT3XS-QWMIqd8vPgYrQ',
'chat_path' => 'https://mp.weixin.qq.com/s/-V_iAHAb-IjKY1gYgnsmHA',
];
$time = date('Y-m-d H:i:s');
$announcements = Announcement::where('start_time', '<', $time)->where('end_time', '>', $time)->orderBy('id', 'asc')->get();
if ($request->input('debug') == true) {
return view('welcome');
}
return $this->success('ok', compact('person', 'like', 'recommends', 'path_arr', 'announcements'));
}
public function homeV2(Request $request)
{
$location_latitude = $request->input('location_latitude');
$location_longitude = $request->input('location_longitude');
//公告栏
$time = date('Y-m-d H:i:s');
$announcement_ids = PaasAnnouncement::distinct('announcement_id')->pluck('announcement_id');
$announcements = Announcement::where('start_time', '<', $time)->where('end_time', '>', $time)->whereNotIn('id', $announcement_ids)->orderBy('id', 'asc')->get();
$user = auth()->user();
//附近人
$result = $this->calcScope($location_latitude, $location_longitude, 500);
$users = User::whereBetween('location_latitude', [$result['minLat'], $result['maxLat']])
->whereBetween('location_longitude', [$result['minLng'], $result['maxLng']])->where('id', '<>', $user->id)->where('hidden_profile', '<>', 'ALLSEX')->limit(4)->select('id', 'circle_avatar')->orderBy('circle_avatar', 'desc')->get();
foreach ($users as $avatar) {
if (empty($avatar->circle_avatar)) {
if ($avatar->sex == 1) {
$circle_avatar = 'http://images.ufutx.com/201811/12/0e8b72aae6fa640d9e73ed312edeebf3.png';
} else {
$circle_avatar = 'http://images.ufutx.com/201811/12/dddd79aa2c2fc6a6f35e641f6b8fb8f5.png';
}
$avatar->circle_avatar = $circle_avatar;
}
}
if ($user->type == 'single') {
$sideSingles = [];
//匹配信息
$profile_courtship = $user->profileCourtship;
$match_profile['sex'] = $user->sex;
$match_profile['birthday'] = $profile_courtship->birthday;
$match_profile['country'] = $profile_courtship->country;
$match_profile['province'] = $profile_courtship->province;
$match_profile['city'] = $profile_courtship->city;
$match_profile['state'] = $profile_courtship->state;
$match_profile['belief'] = $user->belief;
$match_profile['min_age'] = $this->getAge($profile_courtship->min_age);
$match_profile['max_age'] = $this->getAge($profile_courtship->max_age);
//是否可以领临时会员
$temp_member = TempMember::where('user_id', $user->id)->count();
if (empty($temp_member) && $user->rank_id == 0 && $user->type == 'single') {
$can_get_temp_member = 1;
} else {
$can_get_temp_member = 0;
}
} else {
// $singles = User::where('show_user_id', $user->id)->limit(6)->get();
$other_user_ids = ShareSideUser::where('user_id', $user->id)->whereHas('user', function ($sql) {
$sql->where('type', 'single');
})->limit(6)->pluck('other_user_id');
$sideSingles = User::whereIn('id', $other_user_ids)->where('type', 'single')->whereNotNull('circle_avatar')->select('id', 'circle_avatar', 'type')->get();
$match_profile = [];
$can_get_temp_member = 0;
}
return $this->success('ok', compact('announcements', 'users', 'can_get_temp_member', 'sideSingles', 'match_profile'));
}
public function recommendUsers(Request $request, $user_id = null)
{
try {
//资料完成度
if ($user_id) {
$user = User::find($user_id);
} else {
$user = auth()->user();
}
if ($user->type == 'marriage') throw new Exception("账号不是单身角色");
$profile = $user->profileCourtship;
$complete_profile_info = $this->userCon->profileIntegrity($profile, $user);
//拉黑用户
$black_ids = (new LinkingBlacklist())->getBlackIds($user->id);
$black_ids = array_merge($black_ids, User::FULLLINKIDS);
//每日推荐七位
$now_date = date('Y-m-d H:i:s');
$recommend_users = DB::table("users as u")
->rightJoin('recommend_users as ru', 'ru.other_user_id', '=', 'u.id')
->leftJoin('profile_courtships as pc', 'pc.user_id', 'u.id')
->where('ru.user_id', $user->id)->where('ru.start_time', '<=', $now_date)->where('ru.end_time', '>=', $now_date)
->whereNull('ru.deleted_at')
->whereNotIn('ru.other_user_id',$black_ids)
->where('u.type', 'single')
->where("u.belief",$user->belief)
->where('u.negative_score', '<>', 200)
->where("u.hidden_profile", "NONE")
->where("u.is_approved",1)
->select('u.id', 'u.photo', 'u.nickname', 'u.rank_id', 'u.is_approved', 'u.is_real_approved', 'pc.city', 'pc.birthday', 'pc.stature', 'u.industry', 'u.industry_sub', 'pc.interest_hobby', 'pc.introduction', 'ru.id as ru_id', 'pc.degree')
->orderBy('u.id', 'asc')->limit(7)->get();
// if ($user->id == 12441) {
// dd($recommend_users);
// }
//
// if ($user->id == 58859) {
if (empty(count($recommend_users))) {
$start_birthday = date('Y-m-d', strtotime($user->profileCourtship->birthday) - 5 * 365 * 24*60*60);
$end_birthday = date('Y-m-d', strtotime($user->profileCourtship->birthday) + 5 * 365 * 24*60*60);
$recommend_user_ids = RecommendUser::where("user_id", $user->id)->pluck('other_user_id')->toArray();
$recommend_users = DB::table('users as u')
->leftJoin('profile_courtships as pc', 'pc.user_id', '=','u.id')
// ->leftJoin("recommend_linking_news as rl", 'rl.id_users_right', '=','u.id')
// ->leftJoin('recommend_users as ru', 'ru.other_user_id','=', 'u.id')
->where("u.sex", '<>', $user->sex)
->whereNotNull('u.photo')
->where('u.is_approved', 1)
->where('u.hidden_profile', 'NONE')
->where("u.is_approved",1)
->whereNotIn('u.id',$black_ids)
->where('u.type', 'single')
->where("u.belief",$user->belief)
->where('u.negative_score', '<>', 200)
->where("pc.state", ["从未结婚", "未婚"])
->where("pc.birthday", '>', $start_birthday)
->where('pc.birthday','<', $end_birthday)
// ->where('rl.id_users_left', $user->id)
->whereNotIn('u.id', $recommend_user_ids)
->select('u.id', 'u.photo', 'u.nickname', 'u.rank_id', 'u.is_approved', 'u.is_real_approved', 'pc.city', 'pc.birthday', 'pc.stature', 'u.industry', 'u.industry_sub', 'pc.interest_hobby', 'pc.introduction', 'pc.degree')
->limit(7)->get();
// dd($recommend_users);
if (count($recommend_users) == 7) {
$this->addRecommendUsers($recommend_users, $user);
}
}
// }
if (count($recommend_users) < 7) {
$recommend_users = DB::table('users as u')->rightJoin('area_users as au', 'au.user_id', '=', 'u.id')
->leftJoin('profile_courtships as pc', 'pc.user_id', 'u.id')
->where('au.area_id', 43)->where('au.is_audited', 1)->where("u.sex", '<>', $user->sex)
->where('hidden_profile', 'NONE')
->orderBy('au.created_at', 'desc')
->whereNotIn('au.user_id',$black_ids)
->where('u.type', 'single')
->where("u.is_approved",1)
->where("u.belief",$user->belief)
->where('u.negative_score', '<>', 200)
->select('u.id', 'u.photo', 'u.nickname', 'u.rank_id', 'u.is_approved', 'u.is_real_approved', 'pc.city', 'pc.birthday', 'pc.stature', 'u.industry', 'u.industry_sub', 'pc.interest_hobby', 'pc.introduction', 'pc.degree')->limit(7)->get();
// if ($user->id == 12441) {
// dd($recommend_users);
// }
$this->addRecommendUsers($recommend_users, $user);
}
if (count($recommend_users)) {
foreach ($recommend_users as $recommend_user) {
if (isset($recommend_user->ru_id)) {
RecommendUser::where("id", $recommend_user->ru_id)->update(['is_preview' => 1]);
}
$recommend_user->year = $recommend_user->birthday ? substr($recommend_user->birthday, 2, 2) . '年' : "未知";
}
}
return $this->success('ok', compact('complete_profile_info', 'recommend_users'));
} catch (\Exception $e) {
$this->getError($e);
return $this->failure();
}
}
public function addRecommendUsers($recommend_users, $user)
{
foreach ($recommend_users as $recommend_user) {
$h = date('H');
if ($h >= 12) {
$start_time = date('Y-m-d 12:00:00');
$end_time = date('Y-m-d 12:00:00', strtotime('+1 day'));
} else {
$start_time = date('Y-m-d 12:00:00', strtotime('-1 day'));
$end_time = date('Y-m-d 12:00:00');
}
// $data = ['user_id' => $user->id, 'other_user_id' => $recommend_user->id, 'start_time' => $start_time, 'end_time' => $end_time, 'is_preview' => 1];
RecommendUser::updateOrCreate(['user_id'=>$user->id, 'other_user_id'=>$recommend_user->id], ['start_time' => $start_time, 'end_time' => $end_time, 'is_preview' => 1]);
}
}
public function classAreas(Request $request)
{
try {
$areas = ClassArea::where('is_show', 1)->where('id', '<>', 1)->orderBy('sort', 'asc')->get();
return $this->success('oK', $areas);
} catch (Exception $e) {
$this->getError($e);
return $this->failure();
}
}
public function qualityClassArae(Request $request)
{
try {
$user = auth()->user();
$sex = $user->sex;
$ids = AreaUser::whereHas('user', function ($sql) use ($sex) {
$sql->where('sex', '<>', $sex)->where('hidden_profile', 'NONE');
})->where('area_id', 43)->where('is_audited', 1)->inRandomOrder()->limit(30)->pluck('id');
$users = DB::table('users as u')->rightJoin('area_users as au', 'au.user_id', '=', 'u.id')
->leftJoin('profile_courtships as pc', 'pc.user_id', 'u.id')
->whereIn('au.id', $ids)
->select('u.id', 'u.photo', 'u.nickname', 'u.rank_id', 'u.is_approved', 'is_real_approved', 'pc.city', 'pc.birthday', 'pc.stature', 'u.industry', 'u.industry_sub', 'pc.interest_hobby', 'pc.degree', 'u.hidden_profile')
->orderBy('u.id', 'desc')
->paginate(30)
->toArray();
$city = ProfileCourtship::where('user_id', $user->id)->value('city');
$city_users = []; //同城用户集合
foreach ($users['data'] as $key => &$item) {
$item = json_decode(json_encode($item), true);
$item['year'] = $item['birthday'] ? substr($item['birthday'], 2, 2) . '年' : "未知";
if ($item['city'] == $city) {
$city_users[] = $item;
unset($users['data'][$key]);
}
}
//每隔2位用户展示同城用户
foreach ($city_users as $key => $value) {
$length = $key == 0 ? 0 : $key * 3;
$users['data'] = array_merge(array_splice($users['data'], 0, $length), [$value], $users['data']);
}
return $this->success('ok', $users);
} catch (Exception $e) {
$this->getError($e);
return $this->failure();
}
}
public function homeV3(Request $request, $user_id = 0)
{
try {
if ($user_id) {
$user = User::find($user_id);
} else {
$user = auth()->user();
if (empty($user)) {
$user = $this->authCheck();
}
}
if (!empty($user)) {//已登录
//生成推荐id
$user_ids = $this->homeUserIds($user);
if (!is_array($user_ids)) $user_ids = $user_ids->toArray();
if (count($user_ids) < 7) {
//如果现在时间早于早上十点晚于凌晨 取昨天的时间
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'));
}
$user_ids = RecommendUser::where('user_id', $user->id)->whereHas('otherUser', function ($sql) use ($user) {
$sql->where('is_approved', 1)
->where('sex', '<>', $user->sex)
->where('belief', $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();
RecommendUser::where('user_id', $user->id)->whereIn('other_user_id', $user_ids)
->update(['start_time' => $start_time, 'end_time' => $end_time]);
}
if (count($user_ids) < 7) {
$sex = $user->sex == 1 ? 2 : 1;
$user_ids = \DB::table('followables')
->leftJoin('users', 'users.id', '=', 'followables.followable_id')
->where('followables.relation', 'follow')
->where('users.sex', $sex)
->whereNotNull('users.photo')
->where('type', 'single')
->whereNotIn('users.id', User::FULLLINKIDS)
->where('users.is_approved', 1)
->where('hidden_profile', '<>', 'ALLSEX')
->groupBy('followable_id')
->limit(7)
->get()
->pluck('followable_id')
->toArray();
$users = User::whereIn('id', $user_ids)->get();
} else {
$users = User::whereIn('id', $user_ids)
->where('is_approved', 1)
->where('sex', '<>', $user->sex)
->where('hidden_profile', '<>', 'ALLSEX')
->where('belief', $user->belief)
->whereNotIn('id', User::FULLLINKIDS)
->limit(7)
->get();
}
} else {//未登录
$users = AreaUser::where('area_id', 17)->limit(7)->where('is_audited', 1)->pluck('user_id')->toarray();
// $rands = $users;
// $data = [];
// foreach ($rands as $key => $value) {
// $data[$key] = $users[$value];
// }
$users = User::whereIn('id', $users)
->where('hidden_profile', '<>', 'ALLSEX')
->get();
}
// $cilent_user = User::where('id',101556)->get();
// $origin_users =$users->toArray();
// $client_user = $cilent_user->toArray();
// $users =array_merge($client_user,$origin_users);
// $final_ids = [];
// foreach ($users as $key => $value) {
// $final_ids []= $value['id'];
// }
// $users = User::whereIn('id',$final_ids)->orderByRaw(DB::raw('FIND_IN_SET(id, "' . implode(",", $final_ids) . '"' . ")"))->get();
foreach ($users as $user) {
$user_id = $user->id;
$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);
}
$profile = $rd_user->profileCourtship;
$age = 0;
if ($profile) {
$age = \CommonUtilsService::getAge($profile->birthday);
if (!empty($profile->interest_label)) {
$profile->interest_label = json_decode($profile->interest_label);
}
if (!empty($profile->mate_conditon)) {
$profile->mate_conditon = json_decode($profile->mate_conditon, true);
}
}
if ($profile) {
$profile->city = str_replace('市', '', $profile->city);
$profile->age = $age;
}
$user->isSuperRank = $rd_user->is_super_rank;
$user->profile = $profile;
$user->abc = 1;
$user->photo = $user->avatar;
// if($user->profileCourship && $user->profileCourtship != 'null') {
// $user->profileCourship->mate_conditon = json_decode($user->profileCourship->mate_conditon,true)??[];
// $user->profileCourship->photos = json_decode($user->profileCourship->photos,true)??[];
// }
unset($user->profile_courtship);
unset($user->mobile);
}
return $this->success('ok', $users);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('获取信息失败,请稍后再试');
}
}
public function recommend(Request $request)
{
$my_user = auth()->user();
if (empty($my_user)) {
$my_user = $this->authCheck();
}
if (!empty($user)) {//已登录
}
}
/**单身广场 */
public function singlePlaza(Request $request)
{
// try {
DB::connection()->enableQueryLog(); // 开启QueryLog
$my_user = auth()->user();
if (empty($my_user)) {
$my_user = $this->authCheck();
}
if (!empty($my_user)) {
//已经登录,获取大数据的推荐数据
$bigDataLinkModel = $this->bigDataLink($my_user);
$result = $bigDataLinkModel->paginate()->toArray();
if ($result['data'] && count($result['data']) >= 10) {
$result = $this->userProcessing($result);
return $this->success('获取大数据推荐成功', $result);
}
} else {
//没有登录,获取专区的用户数据,
$areaUser = AreaUser::where('area_id', 17)
->whereHas('user', function ($query) {
$query->where('type', 'single')
->where('hidden_profile', 'NONE');
})
->offset($this->getPageOffset() + 7)
->limit(15)
->where('is_audited', 1)
->pluck('user_id')
->toarray();
//分区的用户数据
if (count($areaUser) > 10) {
$now = date('Y-m-d H:i:s');
$users = User::whereIn('id', $areaUser)
->with('profileCourtship')
->with(['rankHistories' => function ($q) use ($now) {
$q->where('deadline', '>', $now);
$q->select();
}])
->where('hidden_profile', '<>', 'ALLSEX')
->paginate();
$users = $users->toArray();
$users = $this->userProcessing($users);
return $this->success('推荐', $users);
}
}
//无法获取相应的数据,从用户表查询数据
//如果已经登录
$where['is_approved'] = 1;
//默认获取 89 - 99
$birthday_between = ['1989-01-01 00:00:00', '1999-01-01 00:00:00'];
$max_height = null;
$min_height = null;
$city_claim = 1;
$city = null;
$hometown_claim = 1;
$unset_user_ids = [];
$resident_province = null;
if (!empty($my_user)) {
//已经登录,获取对应的用户筛选数据
$where['belief'] = $my_user->belief;
if ($my_user->type == 'single') {
$where['sex'] = $my_user->sex == 2 ? 1 : 2;
//匹配的年龄
$max_birthday = $my_user->profileCourtship ? $my_user->profileCourtship->max_age : null;
$min_birthday = $my_user->profileCourtship ? $my_user->profileCourtship->min_age : null;
$max_height = $my_user->profileCourtship ? $my_user->profileCourtship->max_height : null;
$min_height = $my_user->profileCourtship ? $my_user->profileCourtship->min_height : null;
$city_claim = $my_user->profileCourtship ? $my_user->profileCourtship->city_claim : 1;
$hometown_claim = $my_user->profileCourtship ? $my_user->profileCourtship->hometown_claim : 1;
$city = !empty($my_user->profileCourtship) ? $my_user->profileCourtship->city : null;
$resident_province = !empty($my_user->profileCourtship) ? $my_user->profileCourtship->resident_province : null;
if ((empty($max_birthday) || empty($min_birthday)) && $my_user->profileCourtship) {
$max_birthday = date('Y-m-d', strtotime('-3 year', strtotime($my_user->profileCourtship->birthday)));
$min_birthday = date('Y-m-d', strtotime('+3 year', strtotime($my_user->profileCourtship->birthday)));
}
$birthday_between = [$max_birthday, $min_birthday];
} else {
$where['sex'] = $my_user->sex;
}
//黑名单 不喜欢
$unset_user_ids = $this->getUserdDiscard($my_user);
//推荐的列表
$recommend_other_user_id = $this->homeUserIds($my_user)->toArray();
$unset_user_ids = array_merge($unset_user_ids, $recommend_other_user_id);
}
// 获取用户操作模型 传入条件 和 剔除的用户ID
$users = $this->userCon->getUserData($where, $unset_user_ids);
// 一些条件
$users = $users->whereHas('profileCourtship', function ($query) use ($resident_province, $hometown_claim, $city, $city_claim, $birthday_between, $min_height, $max_height) {
// $query->whereBetween('birthday', $birthday_between);
if ($min_height && $max_height) {
$query->where('stature', 'between', [$min_height, $max_height]);
}
if ($city_claim == 2) {
$query->where('city', $city);
}
if ($hometown_claim == 2) {
$query->where('resident_province', $resident_province);
}
});
$users = $users->whereNotIn('id', $unset_user_ids);
$users = $users->paginate();
$users = $users->toArray();
$users = $this->userProcessing($users);
return $this->success('ok', $users);
// } catch (\Exception $e) {
// $this->getError($e);
// return $this->failure('出了一点问题,请稍后再试');
// }
}
/**用户处理 */
public function userProcessing($users)
{
$user_data = $users['data'];
for ($u = 0; $u < count($user_data); $u++) {
$user = $user_data[$u];
if (!empty($user['profile_courtship'])) {
$user['profile_courtship']['interest_label'] = json_decode($user['profile_courtship']['interest_label']) ?: [];
//是否关注
$user['is_followed'] = !empty($my_user) ? $my_user->isFollowing($user) : 0;
//是否是超级会员
if ($user['rank_histories'] == []) {
$user['isSuperRank'] = 0;
} else {
$user['isSuperRank'] = 1;
}
$user['photo'] = $user['avatar'];
$user['year'] = substr($user['profile_courtship']['birthday'], 2, 2) . '年';
if (isset($user['profile_courtship']['stature'])) {
$user['stature'] = $user['profile_courtship']['stature'] . 'cm';
}
$user_data[$u] = $user;
}
}
$users['data'] = $user_data;
return $users;
}
/**获取大数据推荐列表模型 */
public function bigDataLink($my_user)
{
$is_locked = $this->isLock();
if ($is_locked) {
return [];
}
$unset_user_ids = $this->getUserdDiscard($my_user);
$recommend_other_user_id = $this->homeUserIds($my_user)->toArray();
$unset_user_ids = array_merge($unset_user_ids, $recommend_other_user_id);
$logs = RecommendLinkingNew::with('otherUser:id,nickname,sex,is_real_approved,rank_id,photo,belief,industry_sub', 'otherUser.profileCourtship:id,user_id,province,city,stature,interest_hobby,birthday,introduction,degree')
->where('id_users_left', $my_user->id)
->whereHas('otherUser', function ($sql) use ($unset_user_ids) {
$sql->where('is_recommend', 1)
->whereNotIn('id', $unset_user_ids)
->where('hidden_profile', 'NONE')
->photo();
})
->orderBy('score', 'desc')
->orderBy('id', 'desc');
return $logs;
}
/**获取屏蔽的用户名单 */
public function getUserdDiscard($my_user)
{
$blacklist_user_ids = [];
$dislike_user_ids = [];
$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
return array_merge($blacklist_user_ids, $dislike_user_ids, User::FULLLINKIDS);
}
/**获取为你推荐的操作模型 */
public function getRecommend($my_user)
{
$recommendUser = RecommendUser::where('user_id', $my_user->id)
->whereHas('otherUser', function ($sql) use ($my_user) {
$sql->where('is_approved', 1)
->where('hidden_profile', '<>', 'ALLSEX')
->where('belief', $my_user->belief)
->where('can_be_found', 1)
->where('sex', '<>', $my_user->sex);
})
->orderBy('start_time', 'asc')
->limit(7);
return $recommendUser;
}
public function isLock()
{
$result = \DB::select("show OPEN TABLES where In_use > 0;");
$is_locked = 0;
if (count($result)) {
foreach ($result as $re) {
if ($re->Table == 'ufutx_recommend_linking') {
$is_locked = 1;
break;
}
}
}
return $is_locked;
}
public function homeUserIds($user)
{
//判断当前时间是否需要刷新
$now = date('Y-m-d H:i:s');
$other_user_ids = RecommendUser::whereHas('otherUser', function ($sql) {
$sql->where('type', 'single')->where('hidden_profile', '<>', 'ALLSEX')->photo()->where('can_be_found', 1);
})->where('user_id', $user->id)->where('start_time', '<=', $now)->where('end_time', '>=', $now)->pluck('other_user_id');
if (count($other_user_ids)) {
return $other_user_ids;
}
$all_other_user_ids = RecommendUser::where('user_id', $user->id)->pluck('other_user_id');
$belief = $user->belief;
$sex = $user->sex == 1 ? 2 : 1;
//匹配的年龄
$max_birthday = $user->profileCourtship ? $user->profileCourtship->max_age : null;
$min_birthday = $user->profileCourtship ? $user->profileCourtship->min_age : null;
$max_height = $user->profileCourtship ? $user->profileCourtship->max_height : null;
$min_height = $user->profileCourtship ? $user->profileCourtship->min_height : null;
$city_claim = $user->profileCourtship ? $user->profileCourtship->city_claim : 1;
$hometown_claim = $user->profileCourtship ? $user->profileCourtship->hometown_claim : 1;
$city = !empty($user->profileCourtship) ? $user->profileCourtship->city : null;
$resident_province = !empty($user->profileCourtship) ? $user->profileCourtship->resident_province : null;
if ((empty($max_birthday) || empty($min_birthday)) && $user->profileCourtship) {
$max_birthday = date('Y-m-d', strtotime('-3 year', strtotime($user->profileCourtship->birthday)));
$min_birthday = date('Y-m-d', strtotime('+3 year', strtotime($user->profileCourtship->birthday)));
}
$is_app_route = strstr(request()->route()->uri(), 'api/app/home/v3');
$model = 'App\Models\RecommendLinkingNew';
$min_score = $is_app_route ? 0 : 153;
$other_user_ids = $model::where('score', '>=', $min_score)->whereHas('otherUser', function ($sql) use ($sex, $belief, $max_birthday, $min_birthday, $min_height, $max_height, $city_claim, $hometown_claim, $city, $resident_province) {
$sql = $sql->photo()
->where('hidden_profile', '<>', 'ALLSEX')
->where('can_be_found', 1)
->where('type', 'single')
->where('sex', $sex)
->where('belief', $belief)
->where('is_recommend', 1)
->whereNotIn('id', User::FULLLINKIDS)
->whereHas('profileCourtship', function ($sq) use ($max_birthday, $min_birthday, $min_height, $max_height, $city_claim, $hometown_claim, $city, $resident_province) {
$sq = $sq->whereBetween('birthday', [$max_birthday, $min_birthday]);
if ($min_height && $max_height) {
$sq->where('stature', 'between', [$min_height, $max_height]);
}
if ($city_claim == 2) {
$sq->where('city', $city);
}
if ($hometown_claim == 2) {
$sq->where('resident_province', $resident_province);
}
});
})->where('id_users_left', $user->id)
->whereNotIn('id_users_right', $all_other_user_ids);
if ($user->profileCourtship && !$is_app_route) {
$other_user_ids = $other_user_ids->where('score', '>=', $user->profileCourtship->min_match_score);
}
// if($is_app_route){
// $other_user_ids = $other_user_ids->where('is_recommend', 1);
// }
$other_user_ids = $other_user_ids->orderBy('score', 'desc')->orderBy('id', 'desc')->limit(7)->pluck('id_users_right')->toArray();
if (!count($other_user_ids)) {
$other_user_ids = $this->getAgeNearUserIds($user, 7, $all_other_user_ids);
} elseif (count($other_user_ids) > 0 && count($other_user_ids) < 7) {
$limit = 7 - count($other_user_ids);
$ids = $this->getAgeNearUserIds($user, $limit, $all_other_user_ids);
$other_user_ids = array_unique(array_merge($other_user_ids, $ids));
}
if (count($other_user_ids) < 7) {
$count = RecommendUser::whereHas('otherUser', function ($sql) {
$sql->where('type', 'single')->where('hidden_profile', '<>', 'ALLSEX')->where('can_be_found', 1)->photo();
})->where('user_id', $user->id)->count();
if ($count < 7) {
$other_user_ids = User::photo()->whereNotIn('id', $all_other_user_ids)->where('hidden_profile', '<>', 'ALLSEX')->where('can_be_found', 1)->where('belief', $belief)->where('sex', $sex)->where('is_approved', 1)->where('type', 'single')->orderBy('face_score', 'desc')->limit(7)->pluck('id');
}
return $other_user_ids;
}
//如果现在时间早于早上十点晚于凌晨 取昨天的时间
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'));
}
//生成推荐ids
$arr = [];
foreach ($other_user_ids as $other_user_id) {
$data['user_id'] = $user->id;
$data['other_user_id'] = $other_user_id;
$data['start_time'] = $start_time;
$data['end_time'] = $end_time;
$data['created_at'] = date('Y-m-d H:i:s');
$data['updated_at'] = date('Y-m-d H:i:s');
$arr[] = $data;
}
RecommendUser::insert($arr);
return $other_user_ids;
}
public function getAgeNearUserIds($user, $limit, $all_other_user_ids)
{
if ($user->profileCourtship) {
$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;
$other_user_ids = User::with('profileCourtship')->whereHas('profileCourtship', function ($sql) use ($max_birthday, $min_birthday, $belief) {
$sql->whereBetween('birthday', [$max_birthday, $min_birthday])->where('belief', $belief);
})->photo()->where('sex', $sex)->where('type', 'single')->where('is_approved', 1)->where('hidden_profile', '<>', 'ALLSEX')->whereNotIn('id', User::FULLLINKIDS)->whereNotIn('id', $all_other_user_ids)->orderBy('is_approved', 'desc')->orderBy('face_score', 'desc')->limit($limit)->pluck('id');
return $other_user_ids->toArray();
}
return [];
}
/**
* 刷新用户首页推荐
*/
public function freshHome(Request $request, User $user)
{
$now = date('Y-m-d H:i:s');
//删除当前阶段的推荐数据
RecommendUser::where('user_id', $user->id)->where('start_time', '<=', $now)->where('end_time', '>=', $now)->delete();
return $this->success('ok');
}
/**
* 用户首页推荐
*/
public function otherUserHome(Request $request, User $user)
{
try {
$now = date('Y-m-d H:i:s');
$user_ids = RecommendUser::whereHas('other User', function ($sql) {
$sql->where('type', 'single')->where('hidden_profile', '<>', 'ALLSEX')->photo();
})->where('user_id', $user->id)
->where('start_time', '<=', $now)
->where('end_time', '>=', $now)
->limit(7)
->pluck('other_user_id');
if (empty(count($user_ids))) {
$user_ids = $this->homeUserIds($user);
}
if (empty(count($user_ids))) {
$sex = $user->sex == 1 ? 2 : 1;
$users = \DB::table('followables')->leftJoin('users', 'users.id', '=', 'followables.followable_id')->where('followables.relation', 'follow')->where('users.sex', $sex)->whereNotNull('users.photo')->where('type', 'single')->where('nickname', 'not like', '%福恋%')->where('hidden_profile', '<>', 'ALLSEX')->select(\DB::raw('count(*) as num'), 'followable_id', 'id', 'nickname', 'sex', 'photo', 'is_approved', 'industry_sub', 'industry')->orderBy('num', 'desc')->groupBy('followable_id')->limit(7)->get();
} else {
$users = User::whereIn('id', $user_ids)->where('nickname', 'not like', '%福恋%')->select('id', 'nickname', 'sex', 'photo', 'is_approved', 'industry_sub', 'industry')->get();
}
foreach ($users as $field) {
$profile = ProfileCourtship::where('user_id', $field->id)
->select('user_id', 'sex', 'province', 'city', 'birthday', 'stature', 'state', 'belief')->first();
$age = '未知';
if (!empty($profile)) {
$age = \CommonUtilsService::getAge($profile->birthday);
$profile->age = $age;
}
$field->profile = $profile;
}
return $this->success('ok', $users);
} catch (\Exception $e) {
return $this->failure('信息获取失败');
}
}
/**
* 介绍人删除身边单身
*/
public function deleteSideSingle(Request $request, $user_id)
{
$mine = auth()->user();
ShareSideUser::where('user_id', $mine->id)->where('other_user_id', $user_id)->delete();
return $this->success('ok');
}
public function homeLikersV2(Request $request)
{
//是否保存七项数据
$user = auth()->user();
if ($user->type == 'single') {
if ($user->isCompletedHomeProfile()) {
$other_user_ids = MatchingRate::where('user_id', $user->id)->pluck('other_user_id');
$likers = UserProfile::whereIn('id', $other_user_ids)->where('hidden_profile', '<>', 'ALLSEX')->where('is_approved', 1)->photo()->paginate();
} else {
$likers = $this->singleLikers();
}
} else {
$share_other_user_ids = ShareSideUser::where('user_id', $user->id)->whereHas('user', function ($sql) {
$sql->where('type', 'single');
})->pluck('other_user_id');
$other_user_ids = MatchingRate::whereIn('user_id', $share_other_user_ids)->distinct('other_user_id')->pluck('other_user_id');
$likers = UserProfile::whereIn('id', $other_user_ids)->where('hidden_profile', '<>', 'ALLSEX')->where('is_approved', 1)->photo()->paginate();
}
foreach ($likers as $liker) {
$age = \CommonUtilsService::getAge($liker->birthday);
$avatar = $liker->photo ?: $liker->wechat->avatar2;
$liker->avatar = $avatar . '?x-oss-process=style/scale1';
$profile_courtship = [];
$profile_courtship['age'] = $age;
$profile_courtship['stature'] = $liker->stature;
$profile_courtship['resident_city'] = $liker->resident_city;
$liker->age = $age;
$liker->profile_courtship = $profile_courtship;
}
return $this->success('ok', $likers);
}
public function userSingleLikersV2(Request $request, User $user)
{
$likers = $this->userCon->largeDataSingleLikers($user);
return $this->success('ok', $likers);
}
/**
* 用户的推荐列表
*/
public function userSingleLikers(Request $request, User $user)
{
if ($user->type == 'single') {
if ($user->isCompletedHomeProfile()) {
$other_user_ids = MatchingRate::where('user_id', $user->id)->pluck('other_user_id');
} else {
$other_user_ids = MatchingRate::where('user_id', $user->id)->where('age', 3)->where('belief', '>', 0)->pluck('other_user_id')->toArray();
}
$users = User::where('hidden_profile', '<>', 'ALLSEX')->whereIn('id', $other_user_ids)->where('is_approved', 1)->where('type', 'single')->where('sex', '<>', 0)->where('sex', '<>', $user->sex)->whereNotNull('circle_avatar')->where('belief', $user->belief)->with('profileCourtship');
$keyword = $request->input('keyword');
if ($keyword) {
$keyword = trim($keyword);
$users = $users->where(function ($sql) use ($keyword) {
$sql->where('name', 'like', '%' . $keyword . '%')
->orWhere('mobile', 'like', '%' . $keyword . '%');
});
}
$users = $users->paginate();
} else {
$users = [];
}
foreach ($users as $user) {
if ($user->type == 'single') {
$user->age = \CommonUtilsService::getAge($user->profileCourtship->birthday);
$user->stature = $user->profileCourtship->stature;
$user->city = $user->profileCourtship->city;
$user->stature = $user->stature ?: 0;
}
}
return $this->success('ok', $users);
}
public function homeLikers(Request $request)
{
$user = auth()->user();
$likers = $this->userCon->largeDataSingleLikers($user, $paas = '');
return $this->success('ok', $likers);
}
public function singleLikers($paas = null)
{
$like = null;
$user = auth()->user();
if (empty($user)) {
$user = $this->authCheck();
}
$paas_user_ids = $this->userCon->paasUserIds($paas);
if (empty($user)) {
$likers = UserProfile::where('hidden_profile', '<>', 'ALLSEX')->where('type', 'single');
if (!empty($paas)) {
$likers = $likers->whereIn('id', $paas_user_ids);
}
$likers = $likers->where('is_approved', 1)->photo()->orderBy('id', 'desc')->orderBy('rank_id', 'desc')->limit(40)->paginate(16);
} else {
$belief = $user->belief;
$friend_user_ids = $this->getFriendUserIds($user->id);
if ($user->type == 'single') {//单身男
$other_user_ids = MatchingRate::where('user_id', $user->id)->where('age', 3)->where('belief', '>', 0);
/*->where(function($sql){
$sql->where('grow_province','>', 0)
->orWhere('residence_province', '>', 0);
});*/
$likers = UserProfile::where('hidden_profile', '<>', 'ALLSEX')->where('type', 'single')->where('belief', $belief)->where('is_approved', 1)->photo();
if ($user->sex == 1) {
$likers = $likers->where('sex', 2);
} elseif ($user->sex == 2) {
$likers = $likers->where('sex', 1);
}
$other_user_ids = $other_user_ids->pluck('other_user_id')->toArray();
$array = array_diff($other_user_ids, $friend_user_ids);
$likers = $likers->whereIn('id', $array);
if (!empty($paas)) {
$likers = $likers->whereIn('id', $paas_user_ids);
}
$likers = $likers->where('is_approved', 1)->photo()->orderBy('id', 'desc')->paginate(16);
} else {//介绍人
$likers = UserProfile::where('hidden_profile', '<>', 'ALLSEX')->where('type', 'single')->where('belief', $belief)->where('is_approved', 1)->photo();
if (!empty($paas)) {
$likers = $likers->whereIn('id', $paas_user_ids);
}
$likers = $likers->where('is_approved', 1)->photo()->orderBy('id', 'desc')->orderBy('rank_id', 'desc')->limit(40)->paginate(16);
}
}
foreach ($likers as $liker) {
$age = \CommonUtilsService::getAge($liker->birthday);
$liker->avatar = $liker->photo . '?x-oss-process=style/scale1';
$profile_courtship = [];
$profile_courtship['age'] = $age;
$profile_courtship['stature'] = $liker->stature;
$profile_courtship['resident_city'] = $liker->resident_city;
$liker->age = $age;
$liker->profile_courtship = $profile_courtship;
}
return $likers;
}
public function getFriendUserIds($user_id)
{
$links = Linking::where(function ($sql) use ($user_id) {
$sql->where('user_id', $user_id)->orWhere('user_linking_id', $user_id);
})->get();
$other_user_ids = [];
foreach ($links as $link) {
if ($link->user_id === $user_id) {
$other_user_ids[] = $link->other_user_id;
} else {
$other_user_ids[] = $link->user_id;
}
}
return $other_user_ids;
}
/**
* 根据经纬度和半径计算出范围
* @param string $lat 纬度
* @param String $lng 经度
* @param float $radius 半径
* @return Array 范围数组
*/
private function calcScope($lat, $lng, $radius)
{
$degree = (24901 * 1609) / 360.0;
$dpmLat = 1 / $degree;
$radiusLat = $dpmLat * $radius;
$minLat = $lat - $radiusLat; // 最小纬度
$maxLat = $lat + $radiusLat; // 最大纬度
$mpdLng = $degree * cos($lat * (pi() / 180));
$dpmLng = 1 / $mpdLng;
$radiusLng = $dpmLng * $radius;
$minLng = $lng - $radiusLng; // 最小经度
$maxLng = $lng + $radiusLng; // 最大经度
/** 返回范围数组 */
$scope = array(
'minLat' => $minLat,
'maxLat' => $maxLat,
'minLng' => $minLng,
'maxLng' => $maxLng
);
return $scope;
}
/**
* 成员列表
* @param Request $request [description]
*/
public function usersV4(Request $request)
{
$user = auth()->user();
//消息数
$message_count = 0;
//成员类型 良人 佳偶 介绍人 红娘
$type = $request->input('sex', '不限');
$min_age = $request->input('min_age', '不限');
$max_age = $request->input('max_age', '不限');
$province = $request->input('province', '不限');
$city = $request->input('city', '不限');
$is_approved = $request->input('is_approved', '不限');
$is_rank = $request->input('is_rank', '不限');
$belief = $user->belief;
$user_ids = $this->getUserIdsV2($type, $min_age, $max_age, $province, $city, $belief, $is_approved, $is_rank);
$users = User::whereIn('id', $user_ids);
$keyword = $request->input('keyword');
if ($keyword) {
$keyword = trim($keyword);
$users = $users->where(function ($sql) use ($keyword) {
$sql->where('name', 'like', '%' . $keyword . '%')
->orWhere('nickname', 'like', '%' . $keyword . '%');
});
} else {
$users = $users->where('belief', $belief)->photo();
}
$users = $users->where('can_be_found', 1)->whereNotIn('id', User::FULLLINKIDS)->select('name', 'nickname', 'id', 'location_longitude', 'location_latitude', 'sex', 'type', 'is_approved', 'circle_avatar', 'rank_id', 'photo', 'info_complete_score')->orderBy('is_approved', 'desc')->orderBy('face_score', 'desc')->orderBy('info_complete_score', 'desc')->orderBy('rank_id', 'desc')->orderBy('id', 'desc')->paginate();
$location_longitude = $request->location_longitude;
$location_latitude = $request->location_latitude;
$users = $this->userProfile($users, $location_longitude, $location_latitude);
return $this->success('ok', compact('users', 'message_count', 'user'));
}
//H5用户列表。
public function userlist(Request $request)
{
// dump('用户列表。');
try {
$user['id'] = auth()->user()->id;
$user['belief'] = auth()->user()->belief;
$user['avatar'] = auth()->user()->avatar;
$type = $request->input('sex', '不限');
$sex = 0;
if ($type == 'single_man')
$sex = 1;
if ($type == 'single_woman')
$sex = 2;
$min_age = $request->input('min_age', '不限');
$max_age = $request->input('max_age', '不限');
$province = $request->input('province', '不限');
$city = $request->input('city', '不限');
//此处应该是is_real_approved但目前收到的参数is_approved;
$is_approved = $request->input('is_approved', '不限');
$is_rank = $request->input('is_rank', '不限');
$belief = $request->input('belief', '');
$keyword = $request->input('keyword');
//学历
$degree = $request->input('degree', '不限');
//收入
$income = $request->input('income', '不限');
//行业
$industry = $request->input('industry', '不限');
//子行业
$industry_sub = $request->input("industry_sub", '不限');
$page = $request->page;
if (!$keyword && $min_age == '不限' && $max_age == '不限' && $province == '不限' && $city == '不限' &&
$is_approved == '不限' && $is_rank == '不限' && $belief == '不限' && $type == '不限' && $degree == '不限'
&& $income == '不限' && $industry == '不限' && $industry_sub == '不限') {
$users = $this->getUserBeforeSearch();
return $this->success('ok', compact('users', 'user'));
}
//拉黑用户
$black_ids = LinkingBlacklist::where('user_id', $user['id'])->pluck('other_user_id')->toArray();
//被拉黑用户
$quilt_black_ids = LinkingBlacklist::where('other_user_id', $user['id'])->pluck('user_id')->toArray();
$filter_black_ids = array_merge($black_ids, $quilt_black_ids);
//缓存相同条件数据
$key = 'userlist-' . 'userid' . $user['id'] . 'type' . $type . 'min_age' . $min_age . 'max_age' . $max_age .
'province' . $province . 'city' . $city . 'is_approved' . $is_approved . 'is_rank' . $is_rank . 'belief' . $belief . 'degree' .
$degree . 'income' . $income . 'industry' . $industry . 'industry_sub' . $industry_sub . 'keyword' . $keyword . 'page' . $page;
$users = Cache::remember($key, 1, function () use (
$user, $type, $min_age, $max_age, $province, $city,
$is_approved, $is_rank, $keyword, $sex, $belief, $degree, $income, $industry, $industry_sub, $filter_black_ids
) {
// dump($keyword);
if ($keyword) {
$keyword = trim($keyword);
$query = User::select('id', 'name', 'age', 'sex', 'nickname', 'is_approved', 'is_real_approved', 'hidden_profile', 'type', 'belief', 'industry_sub', 'photo', 'app_avatar')
->where('hidden_profile', 'NONE')
->whereNotIn('id', array_merge(User::FULLLINKIDS, $filter_black_ids))
->where(function ($sql) use ($keyword) {
$sql->where('nickname', 'like', '%' . $keyword . '%')
->orWhere('name', 'like', '%' . $keyword . '%');
});
$query = $this->getUsersQuery($query, $type, $min_age, $max_age, $province, $city, $is_approved, $is_rank, $belief, $degree, $income, $industry, $industry_sub);
} else {
$query = User::select('id', 'name', 'age', 'sex', 'nickname', 'is_approved', 'hidden_profile', 'type', 'belief', 'industry_sub', 'photo', 'app_avatar', 'is_real_approved')
->where('hidden_profile', 'NONE')
->where('can_be_found', 1)
->where('type', 'single')
->whereNotIn('id', array_merge(User::FULLLINKIDS, $filter_black_ids));
$query = $this->getUsersQuery($query, $type, $min_age, $max_age, $province, $city, $is_approved, $is_rank, $belief, $degree, $income, $industry, $industry_sub);
}
if (isset($sex) && $sex)
$query = $query->where('sex', $sex);
$users = $query->orderBy('is_approved', 'desc')
->orderBy('face_score', 'desc')
->orderBy('info_complete_score', 'desc')
->orderBy('rank_id', 'desc')
->orderBy('id', 'desc')
->paginate();
foreach ($users as $user) {
if ($user->type != 'single') {
$profile = $user->profileMarriage;
} else {
$profile = $user->profileCourtship;
}
if ($profile) {
$user->isSuperRank = $user->isSuperRank($user->id);
$user->age = $profile->age ? $profile->age . '岁' : '未知';
$user->city = $profile->city;
$user->stature = $profile->stature ? $profile->stature . 'cm' : '未知';
$user->birthday = !empty($profile->birthday) ? $profile->birthday : '';
$user->degree = !empty($profile->degree) ? $profile->degree : '';
$user->introduction = !empty($profile->introduction) ? $profile->introduction : '';
$user->year = substr($profile->birthday, 2, 2) . '年';
$user->income = $profile->income;
} else {
$user->isSuperRank = 0;
$user->city = '未知';
$user->stature = '未知';
$user->birthday = '';
$user->degree = '';
$user->introduction = '';
$user->year = '未知';
}
unset($user->profileCourtship);
unset($user->profileMarriage);
}
return $users;
});
return $this->success('ok', compact('users', 'user'));
} catch (\Exception $e) {
$this->getError($e);
return $this->failure("服务器正在开小差");
}
}
public function getUserBeforeSearch()
{
$user['id'] = auth()->user()->id;
$user['sex'] = auth()->user()->sex;
$user['avatar'] = auth()->user()->avatar;
//拉黑用户
$black_ids = LinkingBlacklist::where('user_id', $user['id'])->pluck('other_user_id')->toArray();
//被拉黑用户
$quilt_black_ids = LinkingBlacklist::where('other_user_id', $user['id'])->pluck('user_id')->toArray();
$filter_black_ids = array_merge($black_ids, $quilt_black_ids);
$query = User::select('id', 'name', 'age', 'sex', 'nickname', 'is_approved', 'hidden_profile', 'type',
'belief', 'industry_sub', 'photo', 'app_avatar', 'is_real_approved')
->where('hidden_profile', 'NONE')
->where('can_be_found', 1)
->where('type', 'single')
->whereNotIn('id', array_merge(User::FULLLINKIDS, $filter_black_ids))
->where('is_real_approved', 1)
->where('sex', '!=', $user['sex']);
$max_birthday = null;
$min_birthday = null;
$profile = ProfileCourtship::where('user_id', $user['id'])->first();
if ($profile && $profile->birthday) {
$max_age = 3;
$min_age = 3;
$max_birthday = Carbon::parse($profile->birthday)->addYear(-$max_age)->toDateTimeString();
$min_birthday = Carbon::parse($profile->birthday)->addYear($min_age)->toDateTimeString();
}
$province = null;
if ($profile && $profile->province) {
$province = $profile->province;
}
$query1 = $query->whereHas('profileCourtship', function ($sql) use ($province, $max_birthday, $min_birthday) {
if ($province) {
$sql->where('province', $province);
}
if ($max_birthday && $min_birthday) {
$sql->whereBetween('birthday', [$max_birthday, $min_birthday]);
}
})
->orderBy('last_visit', 'desc')
->paginate();
foreach ($query1 as $sql) {
$profile = $sql->profileCourtship;
if ($profile) {
$sql->isSuperRank = $sql->isSuperRank($sql->id);
$sql->age = $profile->age ? $profile->age . '岁' : '未知';
$sql->city = $profile->city;
$sql->stature = $profile->stature ? $profile->stature . 'cm' : '未知';
$sql->birthday = !empty($profile->birthday) ? $profile->birthday : '';
$sql->degree = !empty($profile->degree) ? $profile->degree : '';
$sql->introduction = !empty($profile->introduction) ? $profile->introduction : '';
$sql->year = substr($profile->birthday, 2, 2) . '年';
$sql->income = $profile->income;
} else {
$sql->isSuperRank = 0;
$sql->city = '未知';
$sql->stature = '未知';
$sql->birthday = '';
$sql->degree = '';
$sql->introduction = '';
$sql->year = '未知';
}
}
if (sizeof($query1) < 15) {
if ($profile && $profile->birthday) {
$max_age = 6;
$min_age = 6;
$max_birthday = Carbon::parse($profile->birthday)->addYear(-$max_age)->toDateTimeString();
$min_birthday = Carbon::parse($profile->birthday)->addYear($min_age)->toDateTimeString();
}
$query2 = $query->whereHas('profileCourtship', function ($sql) use ($province, $max_birthday, $min_birthday) {
if ($province) {
$sql->where('province', $province);
}
if ($max_birthday && $min_birthday) {
$sql->whereBetween('birthday', [$max_birthday, $min_birthday]);
}
})
->orderBy('last_visit', 'desc')
->limit(30)
->paginate();
if (sizeof($query2) < 15) {
$query3 = $query->whereHas('profileCourtship', function ($sql) use ($province, $max_birthday, $min_birthday) {
if ($max_birthday && $min_birthday) {
$sql->whereBetween('birthday', [$max_birthday, $min_birthday]);
}
})
->orderBy('last_visit', 'desc')
->paginate();
return $query3;
} else {
return $query2;
}
} else {
return $query1;
}
}
public function users(Request $request)
{
try {
$user = auth()->user();
//成员类型 良人 佳偶 介绍人 红娘
$type = $request->input('sex', '不限');
$min_age = $request->input('min_age', '不限');
$max_age = $request->input('max_age', '不限');
$province = $request->input('province', '不限');
$city = $request->input('city', '不限');
$is_approved = $request->input('is_approved', '不限');
$is_rank = $request->input('is_rank', '不限');
$belief = $user->belief;
$query = User::where('hidden_profile', 'NONE')->where('type', 'single')->where('belief', $belief)->whereNotIn('id', User::FULLLINKIDS)->photo();
$query = $this->getUsersQuery($query, $type, $min_age, $max_age, $province, $city, $is_approved, $is_rank);
$keyword = $request->input('keyword');
if ($keyword) {
$keyword = trim($keyword);
$query = User::where('hidden_profile', 'NONE')->where('belief', $belief)->where('type', 'single')->whereNotIn('id', User::FULLLINKIDS)->where(function ($sql) use ($keyword) {
$sql->where('name', 'like', '%' . $keyword . '%')
->orWhere('nickname', 'like', '%' . $keyword . '%');
});
}
$users = $query->orderBy('is_approved', 'desc')->orderBy('face_score', 'desc')->orderBy('info_complete_score', 'desc')->orderBy('rank_id', 'desc')->orderBy('id', 'desc')->paginate();
$location_longitude = $request->location_longitude;
$location_latitude = $request->location_latitude;
$users = $this->userProfile($users, $location_longitude, $location_latitude);
return $this->success('ok', compact('users', 'user'));
} catch (\Exception $e) {
$this->getError($e);
return $this->failure("服务器正在开小差");
}
}
public function getUsersQuery($users, $type, $min_age, $max_age, $province, $city, $is_approved, $is_rank, $belief = null, $degree = null, $income = null, $industry = null, $industry_sub = null)
{
if ($type != '不限') {
$select_sex = $type == 'single_man' ? 1 : 2;
$users = $users->where('sex', $select_sex);
}
if ($min_age != '不限' && $max_age != '不限') {
$min_age = (explode('岁', $min_age))[0];
$max_age = (explode('岁', $max_age))[0];
$max_birthday = date('Y-m-d H:i:s', strtotime("-$max_age year"));
$min_birthday = date('Y-m-d H:i:s', strtotime("-$min_age year"));
$users = $users->whereHas('profileCourtship', function ($sql) use ($min_birthday, $max_birthday) {
$sql->whereBetween('birthday', [$max_birthday, $min_birthday]);
});
}
if ($province != '不限' && $city != '不限') {
$users = $users->whereHas('profileCourtship', function ($sql) use ($province, $city) {
$sql->where('province', $province)->where('city', $city);
});
} elseif ($city == '不限' && $province != '不限') {
$users = $users->whereHas('profileCourtship', function ($sql) use ($province) {
$sql->where('province', $province);
});
}
if ($is_approved === '已认证') {
$users = $users->where('is_real_approved', 1);
} elseif ($is_approved === '未认证') {
$users = $users->where('is_real_approved', 0);
}
if ($is_rank == '超级VIP') {
$users = $users->where('rank_id', '=', 9);
} elseif ($is_rank == '普通会员') {
$users = $users->where('rank_id', 0);
}
//信仰
if ($belief && $belief != "不限") {
$users = $users->where('belief', $belief);
}
//学历
if ($degree && $degree != '不限') {
$degree_arr = ['博士', "硕士", "本科", '专科', '其他'];
switch ($degree) {
case "硕士以上":
$degree_arr = ['博士', "硕士"];
break;
case "本科以上":
$degree_arr = ['博士', "硕士", "本科"];
break;
case "专科以上":
$degree_arr = ['博士', "硕士", "本科", '专科'];
break;
}
$users = $users->whereHas('profileCourtship', function ($sql) use ($degree_arr) {
$sql->whereIn('degree', $degree_arr);
});
}
//收入
if ($income && $income != '不限') {
$users = $users->whereHas('profileCourtship', function ($sql) use ($income) {
$sql->where('income', $income);
});
}
//行业
if ($industry && $industry != '不限') {
$users = $users->where('industry', $industry);
}
//子行业
if ($industry_sub && $industry_sub != "不限") {
$users = $users->where('industry_sub', $industry_sub);
}
return $users;
}
public function getUserIdsV2($type, $min_age, $max_age, $province, $city, $belief, $is_approved, $is_rank)
{
$user_id = auth()->id();
$user_ids = User::where('hidden_profile', '<>', 'ALLSEX')->where('type', 'single');
if ($type == 'single_man') {
$user_ids = $user_ids->where('sex', 1);
} elseif ($type == 'single_woman') {
$user_ids = $user_ids->where('sex', 2);
}
if ($min_age != '不限' && $max_age != '不限') {
$min_age = (explode('岁', $min_age))[0];
$max_age = (explode('岁', $max_age))[0];
// $max_birthday = \CommonUtilsService::agetobirthday($max_age);
// $min_birthday = \CommonUtilsService::agetobirthday($min_age);
$max_birthday = date('Y-m-d H:i:s', strtotime("-$max_age year"));
$min_birthday = date('Y-m-d H:i:s', strtotime("-$min_age year"));
$user_ids = $user_ids->whereHas('profileCourtship', function ($sql) use ($min_birthday, $max_birthday) {
$sql->whereBetween('birthday', [$max_birthday, $min_birthday]);
});
}
if ($province != '不限' && $city != '不限') {
$user_ids = $user_ids->whereHas('profileCourtship', function ($sql) use ($province, $city) {
$sql->where('province', $province)->where('city', $city);
});
} elseif ($city == '不限' && $province != '不限') {
$user_ids = $user_ids->whereHas('profileCourtship', function ($sql) use ($province) {
$sql->where('province', $province);
});
}
if ($is_approved === '已认证') {
$user_ids = $user_ids->where('is_approved', 1);
} elseif ($is_approved === '未认证') {
$user_ids = $user_ids->where('is_approved', 0);
}
if ($is_rank == '超级VIP') {
$user_ids = $user_ids->where('rank_id', '>', 0);
} elseif ($is_rank == '普通会员') {
$user_ids = $user_ids->where('rank_id', 0);
}
return $user_ids->pluck('id');
}
public function userProfile($users = [])
{
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->isSuperRank = $rd_user->is_super_rank;
$user->age = $rd_user->age ? $rd_user->age . '岁' : '未知';
$profile = $user->type == 'single' ? $rd_user->profileCourtship : $rd_user->profileMarriage;
$user->city = $profile->city;
$user->stature = $profile->stature ? $profile->stature . 'cm' : '未知';
$user->birthday = !empty($profile->birthday) ? $profile->birthday : '';
$user->degree = !empty($profile->degree) ? $profile->degree : '';
$user->introduction = !empty($profile->introduction) ? $profile->introduction : '';
$user->avatar = $user->userAvatar();
$user->year = substr($profile->birthday, 2, 2) . '年';
}
return $users;
}
public function changeWechatAvatar($imgpath = null)
{
if (strstr($imgpath, 'https://wx.qlogo.cn/')) {
$imageArr = explode('/', $imgpath);
$imageArr[count($imageArr) - 1] = 0;
$imgpath = implode('/', $imageArr);
return $imgpath;
} else {
return $imgpath;
}
}
public function getUserIds($type, $age, $province, $city, $belief)
{
$user_id = auth()->id();
$user_ids = User::where('hidden_profile', '<>', 'ALLSEX')->where('belief', $belief);
$times = $this->searchAge($age);
if ($type === '不限' && $age === '不限' && $province === '不限') {
// $user_ids = $user_ids->whereHas('profileCourtship', function($sql) use($belief){
// $sql->where('belief', $belief);
// })->orWhere(function($sql) use($belief){
// $sql->where('hidden_profile', '<>', 'ALLSEX')->whereNotNull('circle_avatar')->whereHas('profileMarriage', function($sql) use($belief){
// $sql->where('belief', $belief);
// });
// })->pluck('id');
$user_ids = $user_ids->pluck('id');
return $user_ids;
}
if ($type === '不限' && $age === '不限' && $province !== '不限' && $city === '不限') {// 0 0 1 0
$user_ids = $user_ids->whereHas('profileCourtship', function ($sql) use ($province, $city, $belief) {
$sql->where('province', 'like', '%' . $province . '%')->where('belief', $belief);
});
} elseif ($type === '不限' && $age === '不限' && $province !== '不限' && $city !== '不限') {// 0 0 1 1
$user_ids = $user_ids->whereHas('profileCourtship', function ($sql) use ($province, $city, $belief) {
$sql->where('province', 'like', '%' . $province . '%')->where('city', 'like', '%' . $city . '%')->where('belief', $belief);
});
} elseif ($type === '不限' && $age !== '不限' && $province === '不限') {// 0 1 0
$user_ids = $user_ids->whereHas('profileCourtship', function ($sql) use ($times, $belief) {
$sql->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('belief', $belief);
});
} elseif ($type !== '不限' && $age === '不限' && $province === '不限') {// 1 0 0
if ($type == 'undefined') {
return $this->failure('请选择类型');
}
if ($type === 'single_woman') {
$user_ids = $user_ids->where('type', 'single')->where('sex', 2)->whereHas('profileCourtship', function ($sql) use ($times, $belief) {
$sql->where('belief', $belief);
});
} elseif ($type === 'single_man') {
$user_ids = $user_ids->where('type', 'single')->where('sex', 1)->whereHas('profileCourtship', function ($sql) use ($times, $belief) {
$sql->where('belief', $belief);
});
} elseif ($type === 'marriage') {
$user_ids = $user_ids->where(function ($sql) {
$sql->where('type', 'marriage')->orWhere('type', 'loveing');
})->whereHas('profileMarriage', function ($sql) use ($belief) {
$sql->where('belief', $belief);
});
} elseif ($type === 'maker') {
$user_ids = Matchmaker::where('status', 1)->pluck('user_id');
$user_ids = User::whereIn('id', $user_ids)->where('hidden_profile', '<>', 'ALLSEX')->whereNotNull('circle_avatar')->where(function ($sql) use ($belief) {
$sql->whereHas('profileCourtship', function ($sql) use ($belief) {
$sql->where('belief', $belief);
})->orWhereHas('profileMarriage', function ($sql) use ($belief) {
$sql->where('belief', $belief);
});
})->pluck('id');
} elseif ($type == 'single') {
$user_ids = $user_ids->where('type', $type)->whereHas('profileCourtship', function ($sql) use ($times, $belief) {
$sql->where('belief', $belief);
});
}
} elseif ($type === '不限' && $age !== '不限' && $province !== '不限' && $city === '不限') {// 0 1 1
$user_ids = $user_ids->whereHas('profileCourtship', function ($sql) use ($times, $province, $city, $belief) {
$sql->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('province', 'like', '%' . $province . '%')->where('belief', $belief);
});
} elseif ($type === '不限' && $age !== '不限' && $province !== '不限' && $city !== '不限') {// 0 1 1
$user_ids = $user_ids->whereHas('profileCourtship', function ($sql) use ($times, $province, $city, $belief) {
$sql->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')->whereHas('profileCourtship', function ($sql) use ($province, $city, $belief) {
$sql->where('province', 'like', '%' . $province . '%')->where('belief', $belief);
});
} elseif ($type === 'single_man') {
$user_ids = $user_ids->where('type', 'single')->whereHas('profileCourtship', function ($sql) use ($province, $city, $belief) {
$sql->where('province', 'like', '%' . $province . '%')->where('belief', $belief);
})->where('sex', 1);
} elseif ($type === 'marriage') {
$user_ids = $user_ids->where(function ($sql) {
$sql->where('type', 'marriage')->orWhere('type', 'loveing');
})->whereHas('profileMarriage', function ($sql) use ($belief) {
$sql->where('belief', $belief);
});
} elseif ($type === 'maker') {
$user_ids = Matchmaker::where('status', 1)->pluck('user_id');
$user_ids = User::whereIn('id', $user_ids)->whereHas('profileCourtship', function ($sql) use ($belief) {
$sql->where('belief', $belief);
})->orWhere(function ($sql) use ($belief) {
$sql->where('hidden_profile', '<>', 'ALLSEX')->whereNotNull('circle_avatar')->whereHas('profileMarriage', function ($sql) use ($belief) {
$sql->where('belief', $belief);
});
})->pluck('id');
}
} elseif ($type !== '不限' && $age === '不限' && $province !== '不限' && $city !== '不限') {// 1 0 1
if ($type === 'single_woman') {
$user_ids = $user_ids->where('type', 'single')->whereHas('profileCourtship', function ($sql) use ($province, $city, $belief) {
$sql->where('province', 'like', '%' . $province . '%')->where('city', 'like', '%' . $city . '%')->where('belief', $belief);
})->where('sex', 2);
} elseif ($type === 'single_man') {
$user_ids = $user_ids->where('type', 'single')->whereHas('profileCourtship', function ($sql) use ($province, $city, $belief) {
$sql->where('province', 'like', '%' . $province . '%')->where('city', 'like', '%' . $city . '%')->where('belief', $belief);
})->where('sex', 1);
} elseif ($type === 'marriage') {
$user_ids = $user_ids->where(function ($sql) {
$sql->where('type', 'marriage')->orWhere('type', 'loveing');
})->whereHas('profileMarriage', function ($sql) use ($belief) {
$sql->where('belief', $belief);
});
} elseif ($type === 'maker') {
$user_ids = Matchmaker::where('status', 1)->pluck('user_id');
$user_ids = User::whereIn('id', $user_ids)->whereHas('profileCourtship', function ($sql) use ($belief) {
$sql->where('belief', $belief);
})->orWhere(function ($sql) use ($belief) {
$sql->where('hidden_profile', '<>', 'ALLSEX')->whereNotNull('circle_avatar')->whereHas('profileMarriage', function ($sql) use ($belief) {
$sql->where('belief', $belief);
});
})->pluck('id');
}
} elseif ($type !== '不限' && $age !== '不限' && $province === '不限') {// 1 1 0
if ($type === 'single_woman') {
$user_ids = $user_ids->where('type', 'single')->whereHas('profileCourtship', function ($sql) use ($times, $belief) {
$sql->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('belief', $belief);
})->where('sex', 2);
} elseif ($type === 'single_man') {
$user_ids = $user_ids->where('type', 'single')->whereHas('profileCourtship', function ($sql) use ($times, $belief) {
$sql->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('belief', $belief);
})->where('sex', 1);
} elseif ($type === 'marriage') {
$user_ids = $user_ids->where(function ($sql) {
$sql->where('type', 'marriage')->orWhere('type', 'loveing');
})->whereHas('profileMarriage', function ($sql) use ($belief, $times) {
$sql->where('belief', $belief)->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time']);
});
} elseif ($type === 'maker') {
$user_ids = Matchmaker::where('status', 1)->pluck('user_id');
$user_ids = User::whereIn('id', $user_ids)->whereHas('profileCourtship', function ($sql) use ($belief) {
$sql->where('belief', $belief);
})->orWhere(function ($sql) use ($belief) {
$sql->where('hidden_profile', '<>', 'ALLSEX')->whereNotNull('circle_avatar')->whereHas('profileMarriage', function ($sql) use ($belief) {
$sql->where('belief', $belief);
});
})->pluck('id');
}
} elseif ($type !== '不限' && $age !== '不限' && $province !== '不限' && $city === '不限') {// 1 1 1
if ($type === 'single_woman') {
$user_ids = $user_ids->where('type', 'single')->whereHas('profileCourtship', function ($sql) use ($province, $city, $times, $belief) {
$sql->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('province', 'like', '%' . $province . '%')->where('belief', $belief);
})->where('sex', 2);
} elseif ($type === 'single_man') {
$user_ids = $user_ids->where('type', 'single')->whereHas('profileCourtship', function ($sql) use ($province, $city, $times, $belief) {
$sql->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('province', 'like', '%' . $province . '%')->where('belief', $belief);
})->where('sex', 1);
} elseif ($type === 'marriage') {
$user_ids = $user_ids->where(function ($sql) {
$sql->where('type', 'marriage')->orWhere('type', 'loveing');
})->whereHas('profileMarriage', function ($sql) use ($belief, $times) {
$sql->where('belief', $belief)->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time']);
});
} elseif ($type === 'maker') {
$user_ids = Matchmaker::where('status', 1)->pluck('user_id');
$user_ids = User::whereIn('id', $user_ids)->whereHas('profileCourtship', function ($sql) use ($belief) {
$sql->where('belief', $belief);
})->orWhere(function ($sql) use ($belief) {
$sql->where('hidden_profile', '<>', 'ALLSEX')->whereNotNull('circle_avatar')->whereHas('profileMarriage', function ($sql) use ($belief) {
$sql->where('belief', $belief);
});
})->pluck('id');
}
} elseif ($type !== '不限' && $age !== '不限' && $province !== '不限' && $city !== '不限') {// 1 1 1
if ($type === 'single_woman') {
$user_ids = $user_ids->where('type', 'single')->whereHas('profileCourtship', function ($sql) use ($province, $city, $times, $belief) {
$sql->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('province', 'like', '%' . $province . '%')->where('city', 'like', '%' . $city . '%')->where('belief', $belief);
})->where('sex', 2);
} elseif ($type === 'single_man') {
$user_ids = $user_ids->where('type', 'single')->whereHas('profileCourtship', function ($sql) use ($province, $city, $times, $belief) {
$sql->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time'])->where('province', 'like', '%' . $province . '%')->where('city', 'like', '%' . $city . '%')->where('belief', $belief);
})->where('sex', 1);
} elseif ($type === 'marriage') {
$user_ids = $user_ids->where(function ($sql) {
$sql->where('type', 'marriage')->orWhere('type', 'loveing');
})->whereHas('profileMarriage', function ($sql) use ($belief, $times) {
$sql->where('belief', $belief)->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time']);
});
} elseif ($type === 'maker') {
$user_ids = Matchmaker::where('status', 1)->pluck('user_id');
$user_ids = User::whereIn('id', $user_ids)->whereHas('profileCourtship', function ($sql) use ($belief) {
$sql->where('belief', $belief);
})->orWhere(function ($sql) use ($belief) {
$sql->where('hidden_profile', '<>', 'ALLSEX')->whereNotNull('circle_avatar')->whereHas('profileMarriage', function ($sql) use ($belief) {
$sql->where('belief', $belief);
});
})->pluck('id');
}
}
if ($type !== 'maker') {
$user_ids = $user_ids->pluck('id');
}
return $user_ids;
}
public function mapUsersV2(Request $request)
{
// try{
$user = auth()->user();
//不展示用户
$black_ids = $this->getUserdDiscard($user);
if ($request->location_latitude && $request->location_longitude) {
$user->location_longitude = $request->location_longitude;
$user->location_latitude = $request->location_latitude;
$user->save();
}
$user_id = $user->id;
//消息数
$message_count = ChatMessage::where('status', 0)->where('other_user_id', $user->id)->count();
//成员类型 良人 佳偶 介绍人 红娘
$type = $request->input('type');
$age = $request->input('age', '90后');
$province = $request->input('province');
$city = $request->input('city');
$belief = $user->belief;
$is_approved = $request->input('is_approved');
$is_rank = $request->input('is_rank');
$user_ids = $this->getUserIds($type, $age, $province, $city, $belief, $is_approved, $is_rank);
//黑名单 头像未审核 不展示
$users = User::whereIn('id', $user_ids)->whereNotIn('id', $black_ids)->where('is_photo_audited', 1);
$southwest = $request->input('southwest');
$northeast = $request->input('northeast');
if ($northeast && $southwest) {
$northeast_arr = json_decode($northeast, true);
$southwest_arr = json_decode($southwest, true);
$users = $users->whereBetween('location_latitude', [$southwest_arr['latitude'], $northeast_arr['latitude']])->whereBetween('location_longitude', [$southwest_arr['longitude'], $northeast_arr['longitude']]);
}
if ($request->versions == 'v5.0') {
$users = $users->whereHas('wechat', function ($sql) {
$sql->where('avatar', '<>', '');
})->select('name', 'id', 'nickname', 'location_longitude', 'location_latitude', 'sex', 'type', 'is_approved', 'circle_avatar')
->orderBy('is_approved', 'desc')
->where('type', 'single')
->whereNotIn('id', ['647'])
->orderBy('photo', 'desc')
->orderBy('rank_id', 'desc')
->orderBy('id', 'desc')
->limit(15)
->get();
} else {
$now = date('Y-m-d H:i:s');
$users = $users
->whereHas('wechat', function ($sql) {
$sql->where('avatar', '<>', '');
})
->with(['profileCourtship' => function ($q) {
$q->select(['stature', 'user_id', 'sex', 'birthday']);
}])
->whereHas('profileCourtship', function ($sql) {
$sql->where('user_id', '>', 1);
})
->with(['rankHistories' => function ($q) use ($now) {
$q->where('deadline', '>', $now);
$q->select();
}])
->with('profilePhoto')
->where('type', 'single')
->select('photo', 'name', 'age', 'id', 'nickname', 'location_longitude', 'location_latitude', 'sex', 'type', 'is_approved', 'circle_avatar')
->orderBy('is_approved', 'desc')
->whereNotIn('id', ['647'])
->orderBy('photo', 'desc')
->orderBy('rank_id', 'desc')
->orderBy('id', 'desc')
->limit(50)
->get();
}
$location_longitude = $request->location_longitude;
$location_latitude = $request->location_latitude;
foreach ($users as $other_user) {
if (!empty($other_user->location_longitude) && $other_user->location_longitude != "undefined") {
$other_user->location_longitude = ($other_user->location_longitude * 1000000 + 0.009 * 1000000) / 1000000;
}
// $match_weight = MatchingRate::where('user_id', $user_id)->where('other_user_id', $other_user->id)->value('weight');
// $other_user->match_weight = $match_weight;
$other_user->match_weight = '';
$distance = $this->getDistance($location_longitude, $location_latitude, round($other_user->location_longitude, 7), round($other_user->location_latitude, 7));
$other_user->distance = $distance;
if ($other_user->rankHistories->toArray() == []) {
$other_user['isSuperRank'] = 0;
} else {
$other_user['isSuperRank'] = 1;
}
$other_user->photos = $other_user->profilePhoto->toArray();
if ($other_user->profileCourtship) {
$other_user->sex = $other_user->profileCourtship->sex;
$other_user->age = $this->getAge($other_user->profileCourtship->birthday);
}
if (!$other_user->circle_avatar || $other_user->circle_avatar == 'https://images.ufutx.com/202007/01/e0de60525143427d4dd19515a5b387ba.png') {
if ($other_user->photo) {
$avatar = $other_user->photo;
} else if ($other_user->sex == 1) {
$avatar = 'http://images.ufutx.com/201811/12/0e8b72aae6fa640d9e73ed312edeebf3.png';
} else {
$avatar = 'http://images.ufutx.com/201811/12/dddd79aa2c2fc6a6f35e641f6b8fb8f5.png';
}
$other_user->circle_avatar = $avatar;
}
$other_user->circle_avatar = $other_user->circle_avatar . '?x-oss-process=image/auto-orient,1/resize,m_fill,w_100,h_100/quality,q_90';
}
return $this->success('ok.', compact('users', 'message_count'));
// } catch (\Exception $e) {
// $this->getError($e);
// return $this->failure('出现了一点问题,请稍后再试');
// }
}
public function mapUsersV3(Request $request)
{
try {
$user = auth()->user();
//不展示用户
$black_ids = $this->getUserdDiscard($user);
$my_user = array();
$my_user['location_offset_status'] = 0;
$my_user['location_longitude'] = $user->location_longitude;
$my_user['location_latitude'] = $user->location_latitude;
if ($user->type == 'marriage') {
$profile = ProfileMarriage::firstOrCreate(['user_id' => $user->id]);
$my_user['location_offset_status'] = $profile->location_offset_status;
} else {
$profile = ProfileCourtship::firstOrCreate(['user_id' => $user->id]);
$my_user['location_offset_status'] = $profile->location_offset_status;
}
if ($my_user['location_offset_status'] == 1) {
$new_location_longitude = (float)$request->location_longitude + 0.02;
$new_location_latitude = (float)$request->location_latitude + 0.02;
$this->userCon->updateUserLocation($user, $new_location_longitude, $new_location_latitude);
// 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();
// }
} else {
// if($request->debug){
// dump($request->location_longitude);
// dump($request->location_latitude);
// dump((float)$request->location_longitude);
// dd((float)$request->location_latitude);
// }
$this->userCon->updateUserLocation($user, $request->location_longitude, $request->location_latitude);
// if($user->location_longitude != (float)$request->location_longitude || $user->location_latitude != (float)$request->location_latitude){
// $user->location_longitude = $request->location_longitude;
// $user->location_latitude = $request->location_latitude;
// $user->save();
// }
}
$user_id = $user->id;
//消息数
$message_count = ChatMessage::where('status', 0)->where('other_user_id', $user->id)->count();
//成员类型 良人 佳偶 介绍人 红娘
$type = $request->input('type');
$age = $request->input('age', '90后');
$province = $request->input('province');
$city = $request->input('city');
$belief = $user->belief;
$is_approved = $request->input('is_approved');
$is_rank = $request->input('is_rank');
$southwest = $request->input('southwest');
$northeast = $request->input('northeast');
$users = User::where('hidden_profile', '<>', 'ALLSEX')
->where('type', 'single')
->whereNotIn('id', $black_ids)
->where('belief', $belief)
->where('is_photo_audited', 1);//头像审核通过
if ($age !== '不限') {
$times = $this->searchAge($age);
$users = $users->whereHas('profileCourtship', function ($sql) use ($times) {
$sql->where('birthday', '>=', $times['start_time'])->where('birthday', '<', $times['end_time']);
});
}
if ($northeast && $southwest) {
$northeast_arr = json_decode($northeast, true);
$southwest_arr = json_decode($southwest, true);
$users = $users->whereBetween('location_latitude', [$southwest_arr['latitude'], $northeast_arr['latitude']])
->whereBetween('location_longitude', [$southwest_arr['longitude'], $northeast_arr['longitude']]);
}
$now = date('Y-m-d H:i:s');
$users = $users
->whereHas('wechat', function ($sql) {
$sql->where('avatar', '<>', '');
})
->with(['profileCourtship' => function ($q) {
$q->select(['stature', 'user_id', 'sex', 'birthday']);
}])
->whereHas('profileCourtship', function ($sql) {
$sql->where('user_id', '>', 1);
})
->with(['rankHistories' => function ($q) use ($now) {
$q->where('deadline', '>', $now);
$q->select();
}])
->with('profilePhoto')
->where('type', 'single')
->select('photo', 'name', 'age', 'id', 'nickname', 'location_longitude', 'location_latitude', 'sex', 'type', 'is_approved', 'circle_avatar')
->orderBy('is_approved', 'desc')
->whereNotIn('id', ['647'])
->orderBy('photo', 'desc')
->orderBy('rank_id', 'desc')
->orderBy('id', 'desc')
->limit(50)
->get();
$location_longitude = $request->location_longitude;
$location_latitude = $request->location_latitude;
foreach ($users as $other_user) {
// if (!empty($other_user->location_longitude) && $other_user->location_longitude != "undefined") {
// $other_user->location_longitude = ($other_user->location_longitude * 1000000 + 0.009 * 1000000 ) /1000000;
// }
// $match_weight = MatchingRate::where('user_id', $user_id)->where('other_user_id', $other_user->id)->value('weight');
// $other_user->match_weight = $match_weight;
$other_user->match_weight = '';
$distance = $this->getDistance($location_longitude, $location_latitude, round($other_user->location_longitude, 7), round($other_user->location_latitude, 7));
$other_user->distance = $distance;
if ($other_user->rankHistories->toArray() == []) {
$other_user['isSuperRank'] = 0;
} else {
$other_user['isSuperRank'] = 1;
}
$other_user->photos = $other_user->profilePhoto->toArray();
if ($other_user->profileCourtship) {
$other_user->sex = $other_user->profileCourtship->sex;
$other_user->age = $this->getAge($other_user->profileCourtship->birthday);
}
if (!$other_user->circle_avatar || $other_user->circle_avatar == 'https://images.ufutx.com/202007/01/e0de60525143427d4dd19515a5b387ba.png') {
if ($other_user->photo) {
$avatar = $other_user->photo;
} else if ($other_user->sex == 1) {
$avatar = 'http://images.ufutx.com/201811/12/0e8b72aae6fa640d9e73ed312edeebf3.png';
} else {
$avatar = 'http://images.ufutx.com/201811/12/dddd79aa2c2fc6a6f35e641f6b8fb8f5.png';
}
$other_user->circle_avatar = $avatar;
}
$other_user->circle_avatar = $other_user->circle_avatar . '?x-oss-process=image/auto-orient,1/resize,m_fill,w_100,h_100/quality,q_90';
}
return $this->success('ok', compact('users', 'message_count', 'my_user'));
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('出现了一点问题,请稍后再试');
}
}
//更新用户当前地址
public function mapUpdateUser(Request $request)
{
$user = auth()->user();
if ($request->location_offset_status == 1) {
$user->location_longitude = $user->location_longitude + 0.02;
$user->location_latitude = $user->location_longitude + 0.02;
// $user->updateCacheUser('location_longitude');
// $user->updateCacheUser('location_latitude');
$user->save();
} else {
$user->location_longitude = $user->location_longitude;
$user->location_latitude = $user->location_latitude;
// $user->updateCacheUser('location_longitude');
// $user->updateCacheUser('location_latitude');
$user->save();
}
$res['location_longitude'] = $user->location_longitude;
$res['location_latitude'] = $user->location_latitude;
if ($user->type == 'marriage') {
$profile = ProfileMarriage::firstOrCreate(['user_id' => $user->id]);
$profile->location_offset_status = $request->location_offset_status;
$profile->save();
} else {
$profile = ProfileCourtship::firstOrCreate(['user_id' => $user->id]);
$profile->location_offset_status = $request->location_offset_status;
$profile->save();
}
return $this->success('ok', ['user' => $res]);
}
public function mapUsers(Request $request)
{
$user = auth()->user();
$user_id = $user->id;
//消息数
$message_count = ChatMessage::where('status', 0)->where('other_user_id', $user->id)->count();
//成员类型 良人 佳偶 介绍人 红娘
$type = $request->input('type', 'single_man');
$age = $request->input('age', '90后');
$province = $request->input('province');
$city = $request->input('city');
if ($user->type == 'single') {
$belief = ProfileCourtship::where('user_id', $user->id)->value('belief');
} else {
$belief = ProfileMarriage::where('user_id', $user->id)->value('belief');
}
$user_ids = $this->getUserIds($type, $age, $province, $city, $belief);
$users = User::whereIn('id', $user_ids);
$southwest = $request->input('southwest');
$northeast = $request->input('northeast');
if ($northeast && $southwest) {
$northeast_arr = json_decode($northeast, true);
$southwest_arr = json_decode($southwest, true);
$users = $users->whereBetween('location_latitude', [$southwest_arr['latitude'], $northeast_arr['latitude']])->whereBetween('location_longitude', [$southwest_arr['longitude'], $northeast_arr['longitude']]);
}
$users = $users->whereHas('wechat', function ($sql) {
$sql->where('avatar', '<>', '');
})->select('name', 'id', 'location_longitude', 'location_latitude', 'sex', 'type', 'is_approved', 'circle_avatar')
->paginate();
$location_longitude = $user->location_longitude;
$location_latitude = $user->location_latitude;
foreach ($users as $user) {
if (!empty($user->location_longitude) && $user->location_longitude != "undefined") {
$user->location_longitude = ($user->location_longitude * 1000000 + 0.009 * 1000000) / 1000000;
}
$match_weight = MatchingRate::where('user_id', $user_id)->where('other_user_id', $user->id)->value('weight');
$user->match_weight = $match_weight;
$distance = $this->getDistance($location_longitude, $location_latitude, $user->location_longitude, $user->location_latitude);
$user->distance = $distance;
$photos = [];
if ($user->type == 'single') {
$photos = ProfileCourtship::where('user_id', $user->id)->value('photos');
if (empty($photos)) {
$photos = [];
} else {
$photos = json_decode($photos, true);
}
}
$user->photos = $photos;
if (!$user->circle_avatar) {
if ($user->sex == 1) {
$avatar = 'http://images.ufutx.com/201811/12/0e8b72aae6fa640d9e73ed312edeebf3.png';
} else {
$avatar = 'http://images.ufutx.com/201811/12/dddd79aa2c2fc6a6f35e641f6b8fb8f5.png';
}
$user->circle_avatar = $avatar;
}
$user->circle_avatar = $user->circle_avatar;
}
return $this->success('ok', compact('users', 'message_count'));
}
public function addressChangeLocation(Request $request)
{
$user = auth()->user();
if (empty($user)) {
$user = $this->authCheck();
}
$address = $request->input('address');
if (empty($address)) {
return $this->failure('请选择地址');
}
$result = $this->getLocation($request, $address);
if (!count($result)) {
if ($user) {
$result = [
"lat" => $user->location_latitude,
"lng" => $user->location_longitude
];
} else {
$result = [
"lat" => '114.064505',
'lng' => '22.54918'
];
}
}
return $this->success('ok', $result);
}
public function searchAge($age)
{
// $start_time = '';
// $end_time = '';
if ($age == '00后') {
$start_time = "2000-01-01";
$end_time = "2010-01-01";
} elseif ($age == '90后') {
$start_time = "1990-01-01";
$end_time = "20001-01-01";
} elseif ($age == "80后") {
$start_time = "1980-01-01";
$end_time = "1990-01-01";
} elseif ($age == "70后") {
$start_time = "1970-01-01";
$end_time = "1980-01-01";
} elseif ($age == "60后") {
$start_time = "1960-01-01";
$end_time = "1970-01-01";
} elseif ($age == "50后") {
$start_time = "1950-01-01";
$end_time = "1960-01-01";
} elseif ($age == "40后") {
$start_time = "1940-01-01";
$end_time = "1950-01-01";
} else {
$start_time = "1940-01-01";
$end_time = date('Y-m-d', time());
}
$result = [
'start_time' => $start_time,
'end_time' => $end_time,
];
return $result;
}
/**
* 公告
* @param Request $request [description]
* @return [type] [description]
*/
public function announcements(Request $request)
{
$time = date('Y-m-d H:i:s');
$announcements = Announcement::where('start_time', '<', $time)->where('end_time', '>', $time)->orderBy('id', 'desc')->get();
return $this->success('ok', $announcements);
}
/**
* 平台首页
* @param Request $request [description]
* @return [type] [description]
*/
public function officialHome(Request $request)
{
//征婚
// $recommend = $this->userCon->homeRecommend($request);
$paas_id = 0;
$paas_name = $request->input('paas');
if ($paas_name) {
$paas_id = Paas::where('name', $paas_name)->value('id');
}
$announcements = $this->userCon->announcements($request, $paas_id);
//修改定位
$user = $this->authCheck();
if ($user) {
if ($request->has('location_latitude') && $request->has('location_longitude')) {
$user->location_latitude = $request->location_latitude;
$user->location_longitude = $request->location_longitude;
$user->save();
$user->positionHistory()->firstOrcreate(['location_latitude' => $request->location_latitude, 'location_longitude' => $request->location_longitude]);
}
}
return $this->success('ok', compact('announcements'));
}
public function officialHomeLikers(Request $request)
{
$paas_name = $request->input('paas');
//猜你喜欢
$user = auth()->user();
if (empty($user)) {
$user = $this->authCheck();
}
$likers = $this->userCon->largeDataSingleLikers($user, $paas_name);
return $this->success('ok', $likers);
}
/**
* 平台用户列表
* @param Request $request [description]
* @return [type] [description]
*/
public function officialUsers(Request $request)
{
$users = $this->userCon->users($request);
return $this->success('ok', $users);
}
public function jsConfig(Request $request)
{
try {
$app = \WechatService::officialApp('new');
$apis = ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone', 'openLocation', 'getLocation', 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'openAddress', 'updateAppMessageShareData', 'updateTimelineShareData', 'wx-open-launch-weapp', 'chooseVideo', 'hideMenuItems'];
if ($request->input('url')) {
$app->jssdk->setUrl(urldecode($request->input('url')));
} else {
$app->jssdk->setUrl(env('APP_URL') . '/wuhan/');
}
$openTagList = ['wx-open-launch-weapp'];
$config = $app->jssdk->buildConfig($apis, $debug = false, $beta = false, $json = false);
$config['openTagList'] = $openTagList;
return $this->success('ok', $config);
} catch (\Exception $e) {
return $this->failure('获取配置失败');
}
}
public function subscribeTemplateIds(Request $request)
{
$template_ids = config('wechat.sub_tpls');
$template_key = $request->input('template_key');
if ($template_key) {
$template_id = $template_ids[$template_key];
return $this->success('ok', $template_id);
} else {
return $this->success('ok', $template_ids);
}
}
/**
* 平台信息
* @param Request $request [description]
* @return [type] [description]
*/
public function paas(Request $request)
{
$paas = $request->input('paas');
if ($paas && $paas != 'null') {
$paas = Paas::where('name', $paas)->first();
} else {
$paas = null;
}
return $this->success('ok', $paas);
}
/**
* 申请首页推荐
* @param Request $request [description]
* @return [type] [description]
*/
public function applyHomeRecommend(Request $request)
{
//是否是单身
$user = auth()->user();
if ($user->type != 'single') {
return $this->failure('只允许单身申请!');
}
if (empty($user->photo)) {
return $this->failure('请在先上传个人真实照片头像!');
}
if (empty($user->rank_id)) {
return $this->failure('该功能暂时只对VIP用户开放');
}
//判断是否申请过
$home_recommend = $user->homeRecommend;
if ($home_recommend && $home_recommend->status == 1) {
return $this->failure('你已成为首页推荐');
}
if ($home_recommend && $home_recommend->status == 0) {
return $this->failure('你已申请,等待审核');
}
//提交申请
$user->homeRecommend()->create(['status' => 0]);
//返回
return $this->success('申请成功,等待后台审核!');
}
public function locationToAddress(Request $request)
{
$local_latitude = $request->input('local_latitude');
$local_longitude = $request->input('local_longitude');
$result = $this->getAddress($local_latitude, $local_longitude);
return $this->success('ok', $result);
}
/** 记录用户列表浏览 */
public function usersBrowse(Request $request)
{
try {
$result = $this->userCon->usersBrowse();
if (empty($result)) throw new \Exception("记录用户浏览失败");
} catch (\Exception $e) {
$this->getError($e);
}
return $this->success('ok');
}
//小程序审核状态
public function auditStatus(Request $request)
{
if ($request->versions == 'v5.2.28') {
$status = !empty($request->status);
} else {
$status = !empty($request->status);
}
return $this->success('ok', compact('status'));
}
//获取该小程序版本审核状态
public function auditStatusV2(Request $request)
{
$versions = $request->input('versions');
$status = VersionsCheck::where(['versions' => $versions])->value('state');
if ($status == 1) {
$status = true;
} else {
$status = false;
}
return $this->success('ok', ['status' => $status]);
}
public function officialSearch(Request $request)
{
$search_type = $request->search_type;
switch ($search_type) {
case 'article':
$model = MerchantInformation::select('id', 'title', 'pic', 'subTitle', 'is_love_show', 'created_at')
->where('status', 1)
->where('is_love_show', 1)
->orderBy('is_top', 'desc')
->orderBy('top_time', 'desc');
if (isset($request->keyword) && $request->keyword) {
$where = [['title', 'like', '%' . $request->keyword . '%']];
$model = $model->where($where);
}
break;
case 'video':
$model = AnchorVideo::select('id', 'title', 'anchor_id', 'm_id', 'cover', 'status', 'video_url', 'created_at')
->where('is_love_show', 1)
->orderBy('id', 'desc');
if (isset($request->keyword) && $request->keyword) {
$where = [['title', 'like', '%' . $request->keyword . '%']];
$model = $model->where($where);
}
break;
case 'service':
$model = CommunityActivity::select('id', 'title', 'price', 'pic', 'Subtitle', 'start_time', 'end_time')
->where('status', 1)
->where('type', 'business')
->where('is_love_show', 1)
->where('class', 'many')
->orderBy('is_top', 'desc')
->orderBy('top_time', 'desc');
if (isset($request->keyword) && $request->keyword) {
$where = [['title', 'like', '%' . $request->keyword . '%']];
$model = $model->where($where);
}
break;
case 'activity':
$model = CommunityActivity::select('id', 'title', 'price', 'pic', 'Subtitle', 'start_time', 'end_time')
->where('status', 1)
->where('type', 'business')
->where('is_love_show', 1)
->where('class', 'one')
->orderBy('is_top', 'desc')
->orderBy('top_time', 'desc');
if (isset($request->keyword) && $request->keyword) {
$where = [['title', 'like', '%' . $request->keyword . '%']];
$model = $model->where($where);
}
break;
case 'course':
$model = Course::where('is_show', 1)->where('is_love_show', 1)->withCount('videos')->orderBy('sort', 'desc');
if (isset($request->keyword) && $request->keyword) {
$where = [['title', 'like', '%' . $request->keyword . '%']];
$model = $model->where($where);
}
break;
case 'live':
$model = Live::where('is_show', 1)->select('*')
->where('id', '!=', 108)
->whereIn('status', [0, 1, 2])
->selectRaw('ABS(TIMESTAMPDIFF(HOUR,end_time,now())) as time')
->orderByRaw(DB::raw('FIELD(status, 1,3,0,2) asc'))
->orderByRaw('time asc');
if (isset($request->keyword) && $request->keyword) {
$where = [['title', 'like', '%' . $request->keyword . '%']];
$model = $model->where($where);
}
break;
case 'anchor':
$model = Anchor::select('id', 'viewer_id', 'pic', 'introduction', 'name', 'pic')->with('viewer:id,nickname,avatar,sex,mobile,user_id')->where('is_rec', '1')->orderby('sort', 'desc');
if (isset($request->keyword) && $request->keyword) {
$where = [['name', 'like', '%' . $request->keyword . '%']];
$model = $model->where($where);
}
break;
case 'friend':
$model = CommunityStar::select('id', 'user_id', 'created_at')->with(['user:id,photo,name,circle_avatar', 'profile']);
if ($request->keyword) {
$keyword = $request->keyword;
$model->whereHas('user', function ($query) use ($keyword) {
$query->where('name', 'like', '%' . $keyword . '%');
});
}
$model = $model->where('is_audit', 1)->where('is_show', 1)->orderBy('sort', 'desc')->orderBy('audited_at', 'desc');
break;
}
$result = $model->paginate();
if ($search_type == 'article') {
foreach ($result as $key => $info) {
$pv = Redis::zscore('information', $info->id) ?: 0;
$info->pv = $pv;
}
}
if ($search_type == 'video') {
foreach ($result as $key => $info) {
$info->pv = Redis::zscore('short_videopv', $info->id) ?: 0;
}
}
return $this->success('ok', $result);
}
public function homeService(Request $request)
{
//名师精讲
$anchors = Anchor::select('id', 'viewer_id', 'pic', 'introduction', 'name', 'pic')
->with('viewer:id,nickname,avatar,user_id')->where('is_rec', '1')
->orderby('sort', 'desc')->limit(3)->get();
//正在直播列表
$lives = Live::where('is_show', 1)->with('teacher:id,introduction,desc,designation,pic,name')->select('*');
$lives = $lives->whereIn('status', [0, 1, 2, 3]);
$lives = $lives->where('id', '!=', 108)
->selectRaw('ABS(TIMESTAMPDIFF(HOUR,end_time,now())) as time')
->orderByRaw(DB::raw('FIELD(status, 1,3,0,2) asc'))
->orderByRaw('time asc')->limit(3)->get();
foreach ($lives as &$live) {
$live->pv = Redis::zscore('page_pv', $live->id);
$live->start_time = date('Y-m-d H:i', strtotime($live->start_time));
if ($live->times == 0) {
$live->times = Redis::zscore('page_pv', $live->id);
}
}
//名师精选和实时直播的banner图
$banner = Redis::get('home_banner');
$banner = json_decode($banner);
//咨询老师
// $consult = Anchor::select('id','introduction','name','designation','pic','teaching_user','price','label')
// ->where('status',2)->where('is_show_consulation',1)
// ->orderBy('sort','desc')->limit(10)->get();
//推荐商户
$merchant = MerchantAccount::with('anchorV2:id,channel,pic,m_id,name,openid')->select('id', 'recommend', 'recommend_attr')
->where('recommend', 1)
->get();
foreach ($merchant as $item) {
$item['recommend_attr'] = json_decode($item['recommend_attr']);
}
$course = Course::where('is_show', 1)->where('is_love_show', 1)->withCount('videos');
//if($request->merchant_id != 88){
$course = $course->where('type', 'business');
//}
$course = $course->orderBy('sort', 'desc')->limit(3)->get();
$consult_banner = [];
//最新商户入驻信息
$settleIn = Anchor::select('id', 'name', 'created_at')->orderBy('created_at', 'desc')->limit(20)->get();
//用戶是否关注公众号
$wechatUser = session('wechat.oauth_user.new');
if (empty($wechatUser)) {
$openId = $request->openid;
} else {
$openId = $wechatUser->getId();
}
if (config('app.env') == 'production') {
$is_subscribe = $this->getSubscribeStatus($openId);
} else {
$is_subscribe = 1;
}
return $this->success('ok', compact('anchors', 'lives', 'banner', 'course', 'merchant', 'settleIn', 'is_subscribe'));
}
public function userInfo(Request $request)
{
$wechatUser = session('wechat.oauth_user.new');
if ($wechatUser) {
$openid = $wechatUser->getId();
} else {
$openid = $request->openid;
}
$user_id = $this->getUseridByOpenid($openid, 1);
$rand_str = $this->randString(8);
$nickname = '用户' . $rand_str;
if ($user_id) {
$nickname = User::where('id', $user_id)->value('nickname') ?: '用户' . $rand_str;
}
$data['openid'] = $openid;
$data['nickname'] = $nickname;
return $this->success('ok', $data);
}
//名师推荐列表
public function recommendTeachers(Request $request)
{
//名师精讲
$wechatUser = session('wechat.oauth_user.new');
if (empty($wechatUser)) {
$openid = $request->openid;
} else {
$openid = $wechatUser->getId();
}
$viewer_id = Viewer::where('openid', $openid)->value('id');
$anchors = Anchor::with('viewer:id,nickname,avatar,user_id')->select('id', 'm_id', 'viewer_id', 'pic', 'introduction', 'name', 'pic', 'designation')
->with('viewer:id,nickname,avatar,user_id')->where('is_rec', '1')
->orderby('sort', 'desc')->paginate();
foreach ($anchors as $key => $value) {
$is_focusing = AnchorViewer::where('anchor_id', $value->id)->where('viewer_id', $viewer_id)->value('focusing');
//是否关注
$value->is_focusing = $is_focusing == 1 ? 1 : 0;
if (isset($value->viewer) && $value->viewer->user_id != 0) {
$course_count = Course::where('user_id', $value->viewer->user_id)->where('is_show', 1)->count();
if ($value->id == 58) $course_count = 0;
} else {
$course_count = 0;
}
$value->course_count = $course_count;
}
return $this->success('ok', $anchors);
}
//名师详情
public function recommendAnchor(Request $request, $anchor_id)
{
//我的信息
$wechatUser = session('wechat.oauth_user.new');
if (empty($wechatUser)) {
$openid = $request->openid;
} else {
$openid = $wechatUser->getId();
}
$teacher_id = $request->user_id;
$viewer = Viewer::where('openid', $openid)->first();
if (empty($viewer)) return $this->failure('用户信息有误,请重新登录');
$user = User::where('id', $viewer->user_id)->first();
$anchor = Anchor::where('id', $anchor_id)->select('id', 'm_id', 'viewer_id', 'name', 'designation', 'pic')->first();
//粉丝数
$fans_count = AnchorViewer::where('anchor_id', $anchor_id)->where('focusing', 1)->count();
//是否关注
$focusing = AnchorViewer::where('anchor_id', $anchor_id)->where('viewer_id', $viewer->id)->value('focusing');
$is_focusing = $focusing == 1 ? 1 : 0;
//热度
$anchor->pv = Redis::zscore('anchor_red', $anchor_id);
$anchor->is_focusing = $is_focusing;
$anchor->fans_count = $fans_count;
//老师课程
if ($teacher_id == 0)
$teacher_id = -1;
$courses = Course::where('is_show', 1)->where('user_id', $teacher_id)->select('id', 'title', 'thumb', 'charge', 'discount_price', 'start_time_discount', 'end_time_discount')->orderBy('sort', 'desc')->orderBy('id', 'asc')->paginate();
if ($anchor_id == 58) {
$courses = Course::where('is_show', 1)->where('user_id', -1)->select('id', 'title', 'thumb', 'charge', 'discount_price', 'start_time_discount', 'end_time_discount')->orderBy('sort', 'desc')->orderBy('id', 'asc')->paginate();
}
foreach ($courses as $key => $value) {
//原价
$value->original_price = $value->charge;
$value->discount_status = 0;
if ($value->end_time_discount > now() && $value->start_time_discount < now() && $value->discount_price) {
$value->charge = $value->discount_price;
$value->discount_status = 1;
}
$value['videos_count'] = $value->videos_count;
$value['paymentStatus'] = false;
if (isset($user->id))
$value['paymentStatus'] = UserCourses::where('course_id', $value->id)->where('user_id', $user->id)->where('status', 1)->count() ? true : false;
}
return $this->success('ok', compact('anchor', 'courses'));
}
public function homeContent(Request $request)
{
try {
$page = $request->page ?: 1;
// //用戶是否关注公众号
$wechatUser = session('wechat.oauth_user.new');
if (empty($wechatUser)) {
$openId = $request->openid;
} else {
$openId = $wechatUser->getId();
}
$merchant_user = MerchantUser::where('openid', $openId)->first();
if ($merchant_user) {
$key = 'homedata-user' . $merchant_user->id . 'page-' . $page;
} else {
$key = 'homedata-user-page-' . $page;
}
$result = Cache::remember($key, 5, function () use ($request, $merchant_user) {
//活动列表
$time = date('Y-m-d H:i:s');
$activity = CommunityActivity::select('id', 'title', 'price', 'pic', 'Subtitle', 'start_time', 'end_time', DB::raw("'activity' as type"))
->where('apply_deadline', '>', $time)
->where('status', 1)
->where('type', 'business')
->where('is_love_show', 1)
->where('class', 'one')
->orderBy('is_top', 'desc')
->orderBy('top_time', 'desc')->get()->toarray();
//课程列表
$course = Course::select('id', 'title', 'user_count', 'thumb as logo', 'charge', 'short_description', DB::raw("'course' as type"))
->where('is_show', 1)->where('is_love_show', 1)->orderBy('sort', 'desc')->get()->toarray();
//短视频列表
$videos = AnchorVideo::select('id', 'title', 'anchor_id', 'm_id', 'cover', 'status', 'video_url', 'created_at', 'is_love_show')
->where('status', 1)->where('is_love_show', 1)
->whereNotNull('m_id')
->orderBy('id', 'desc')
->get();
//未观看的文章
$watch_video = [];
// 已观看的文章
$watched_video = [];
foreach ($videos as $key => &$value) {
if ($merchant_user && $merchant_user->haslog($value)) {
array_push($watched_video, $value);
} else {
array_push($watch_video, $value);
}
$value['pv'] = Redis::zscore('short_videopv', $value['id']) ?: 0;
}
$videos = array_merge($watch_video, $watched_video);
//文章列表
$article = MerchantInformation::select('id', 'merchant_id', 'title', 'pic', 'subTitle', 'is_love_show', 'content', 'created_at', DB::raw("'article' as type"))->with(['merchant:mobile'])
->where('status', 1)
->where('is_love_show', 1)
->orderBy('is_top', 'desc')
->orderBy('top_time', 'desc')->get();
$article_num = $article->count();
$article_num = intval($article_num / 5);
$videos_num = 0;
$data = [];
//未观看的文章
$watch_article = [];
// 已观看的文章
$watched_article = [];
foreach ($article as $key => $value) {
if ($merchant_user && $merchant_user->haslog($value)) {
array_push($watched_article, $value);
} else {
array_push($watch_article, $value);
}
}
$article = array_merge($watch_article, $watched_article);
// 先整理拼接的数据
for ($i = 1; $i <= $article_num; $i++) {
if ($i % 3 == 1) {
if ($course) {
$data[] = array_shift($course);
} else {
continue;
}
} elseif ($i % 3 == 2) {
if ($activity) {
$activity['type'] = 'activity';
$data[] = array_shift($activity);
} else {
continue;
}
} elseif ($i % 3 == 0) {
if ($videos) {
$insert_video['type'] = 'video';
$insert_video['data'] = array_slice($videos, $videos_num, 5);
$data[] = $insert_video;
$videos_num += 5;
} else {
continue;
}
}
}
$homedata = [];
foreach ($article as $key => $value) {
$value->content = str_replace(array("&nbsp;", "&ensp;", "&emsp;", "&thinsp;", "&zwnj;", "&zwj;", "&ldquo;", "&rdquo;", '&middot', " ", "\r\n", "\r", "\n"), '', strip_tags(htmlspecialchars_decode($value->content)));
$time = date('Y-m-d H:i', strtotime($value->created_at));
$value->time = $time;
unset($value->created_at);
$value->pv = Redis::zscore('information', $value->id) ?: 0;
$anchor = Anchor::where('m_id', $value->merchant_id)->first();
$value->merchant_name = $anchor->name;
if ($key > 1 && $key % 5 == 0 && count($data) >= 1) {
//五条插入一条课程,第十条一个活动,每十五条五条视频
$homedata[] = array_shift($data);
$homedata[] = $value;
continue;
}
//unset($value->merchant);
$homedata[] = $value;
}
$page = $request->page ?? 1;
$perPage = 15;
$offset = ($page * $perPage) - $perPage;
$result = new LengthAwarePaginator(
array_slice($homedata, $offset, $perPage),
count($homedata),
$perPage,
$page,
['path' => $request->url(), 'query' => $request->query()]
);
return $result;
});
return $this->success('ok', $result);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure("服务器休息,请稍后再试");
}
}
public function agreeRecruite(Request $request)
{
$mobile = $request->mobile;
$password = MerchantAccount::where('mobile', $mobile)->value('password');
$password = decrypt($password);
$data['mobile'] = $mobile;
$data['password'] = $password;
return $this->success('ok', $data);
}
public function test(Request $result)
{
$counselors = Counselor::select('mobile')->get();
foreach ($counselors as $key => $value) {
$this->agreeRecruiting($value->mobile);
}
}
//咨询师同意入驻 MerchantAccount Anchor
public function agreeRecruiting($mobile)
{
try {
$value = MerchantAccount::where('mobile', $mobile)->first();
if ($value) return;
$sql = 'SELECT
*
FROM
ufutx_counselors
LEFT JOIN ufutx_counselor_details ON (ufutx_counselors.mobile = ufutx_counselor_details.mobile)
LEFT JOIN ufutx_counselor_studios ON (ufutx_counselors.mobile = ufutx_counselor_studios.mobile)
LEFT JOIN ufutx_counselor_services on (ufutx_counselors.mobile = ufutx_counselor_services.mobile)
WHERE
ufutx_counselors.mobile = ' . $mobile . '';
$result = DB::select($sql);
$result[0]->service = json_decode($result[0]->service);
$result[0]->contact = json_decode($result[0]->contact);
$result[0]->services_info = strip_tags($result[0]->services_info);
// return $this->success('ok',$result);
$str = $this->getTradeNO();
$password = substr($str, 2, 8);
DB::beginTransaction();
$array = [
'mobile' => $result[0]->mobile,
'password' => encrypt($password),
'qr_code' => $result[0]->mobile,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
];
$account_id = MerchantAccount::insertGetId($array);
unset($array);
$type = ['service', 'activity', 'consult', 'course', 'shop'];
foreach ($type as $key => $value) {
$rules = new MEarningRules();
$rules->m_id = $account_id;
$rules->name = $value;
$rules->ratio = 0;
$rules->first_sharer = 0;
$rules->last_sharer = 0;
$rules->other_sharer = 0;
$rules->forzen_time = 1;
$rules->save();
}
$array = [
'm_id' => $account_id,
'introduction' => $result[0]->details ?? '暂无',
'name' => $result[0]->name ?? '匿名',
'designation' => $result[0]->certification,
'pic' => $result[0]->imgsrc,
'mobile' => $result[0]->mobile,
'cooperation_type' => json_encode(['咨询辅导']),
'status' => 0,
'branch' => $result[0]->organization ?? $result[0]->office_name,
'service_nature' => 'person',
'is_show' => 2,
'is_show_consulation' => 1,
'Years' => 8,
'resume' => $result[0]->details,
'channel' => 4,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
];
$anchor_id = Anchor::insertGetId($array);
unset($array);
$array = [
'merchant_id' => $account_id,
'name' => $result[0]->name ?? '匿名',
'mobile' => $result[0]->mobile,
'pic' => $result[0]->imgsrc,
'introduction' => $result[0]->details ?? '暂无',
'label' => '[{"name":"\u5fc3\u7406\u54a8\u8be2"}]',
'designation' => '心理咨询师',
'experience' => 8,
'seniority' => $result[0]->introduce,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
];
ConsultAccount::insert($array);
unset($array);
DB::commit();
$account = MerchantAccount::where('id', $account_id)->first();
//增加入驻记录通知
$content = '恭喜您,成功入驻福恋智能商家版,欢迎使用!';
SaasNotice::addRecord($account->id, 0, 'recruiting', $account->id, $content, 1);
$data = ['merchant_id' => $account_id, 'anchor_id' => $anchor_id];
NewMerchantDefaultService::dispatch($data)->onQueue('love');
AddUnionUser::dispatch($account, $password, 'SMS')->onQueue('love');
return $result;
} catch (\Exception $e) {
DB::rollBack();
throw new Exception($e);
}
}
public function getSubscribeStatus($openid)
{
$app = \WechatService::officialApp('new');
$wechat_user = $app->user->get($openid);
$result = !empty($wechat_user) && count($wechat_user) && isset($wechat_user['subscribe']);
$is_subscribe = $result ? $wechat_user['subscribe'] : 0;
return $is_subscribe;
}
//小程序获取广告位数据
public function getMpAdvertise(Request $request)
{
try {
$advertises = MpAdvertises::where('status', 1)->orderBy('id', 'desc')->get();
return $this->success('ok', $advertises);
} catch (\Exception $e) {
Log::error($e->getMessage());
return $this->failure('数据错误');
}
}
/**
* 首页活动弹框
*/
public function homePopOut(Request $request)
{
try {
$version_check = VersionsCheck::where('state', 1)->where('versions',$request->versions)->first();
if($version_check){
return $this->success('ok', null);
}
$user = $this->authCheck();//单身个和介绍人首页弹框不一致
if (!$user) {
return $this->success('ok', null);
}
if ($user->type != 'single') {
$type = 'marriage';
} else {
$type = 'single';
}
//查看用户是否存在
$model = $type == 'single' ? new ProfileCourtship : new ProfileMarriage;
$where = [
['user_id', '=', $user->id],
];
$match = $model->where($where)->first();
if (!$match) return $this->success('ok', null);
//查看地区是否存在,不存在则return
if (empty($match->province) && empty($match->city)) {
return $this->success('ok', null);
}
$new_value = $province_value = $city_value = null;
$config = Configs::where('key', 'mp_home_popout')->orderBy('id','asc')->get();
if (!$config) {
return $this->success('ok', null);
}
foreach ($config as $item) {
$value = json_decode($item->value);
$value->home_popout_id = $item->id;
$area_status = $value->area_status ?? 0;
$status = $value->status ?? 0;
if (!$status) {
continue;
}
if(!$area_status){
$new_value = $value;
continue;
}
$province = $value->province ?? '';
$city = $value->city ?? '';
if (empty($province) && empty($city)) {
continue;
}
//匹配地区,如果市有数据,市的优先级最高,如果市没有,就按照省的拿,再往上就是不限地区
if ($province == $match->province) {
$province_value = $value;
}
if($city == $match->city){
$city_value = $value;
}
}
//没有就先返回
if(!$new_value){
return $this->success('ok', $new_value);
}
//匹配优先级,最高市,其次省,最后不限
if(!empty($province_value)){
$new_value = $province_value;
}
//如果市有匹配数据,重新覆盖value
if(!empty($city_value)){
$new_value = $city_value;
}
//之前的逻辑
if ($type == 'marriage' && isset($new_value->marriage_pic)) {
$new_value->pic = $new_value->marriage_pic;
}
$service = CommunityActivity::find($new_value->type_id);
if(!$service){
return $this->success('ok', $new_value);
}
$time = time();
//过滤下架 截至报名的
if (!$service->status || $time > strtotime($service->start_time ?: date('Y-m-d H:i:s', $time))) {
return $this->success('ok', null);
}
return $this->success('ok', $new_value);
} catch (Exception $e) {
$this->getError($e);
return $this->success('ok');
}
}
/**
* 置顶推荐
*/
public function singleTopRecommend(Request $request)
{
try {
$mine = auth()->user();
$users = DB::table('users')->whereNotNull('photo')->where('hidden_profile', 'NONE');
if ($mine->sex) {
$sex = $mine->sex == 1 ? 2 : 1;
$users = $users->where('users.sex', $sex);
}
$users = $users->rightJoin('profile_courtships', 'users.id', '=', 'profile_courtships.user_id')->select('users.id', 'users.photo', 'users.nickname', 'users.hidden_profile', 'users.sex', 'profile_courtships.city', 'profile_courtships.birthday');
$area_users = DB::table('area_users')->leftJoinSub($users, 'ufutx_users', function ($join) {
$join->on('area_users.user_id', '=', 'users.id');
})->where('area_users.area_id', 1)->where('area_users.is_show', 1)->where('area_users.is_audited', 1)->where('users.id', '>', 0)->select('area_users.user_id', 'users.*')->get()->toArray();
if (count($area_users) > 3) {
$area_users_index = array_rand($area_users, 3);
$users = [];
foreach ($area_users_index as $index) {
$users[] = $area_users[$index];
}
$area_users = $users;
}
foreach ($area_users as $user) {
$user->birthday = $user->birthday ? substr($user->birthday, 2, 2) : '';
}
return $this->success("ok", $area_users);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('数据加载失败,请稍后再试');
}
}
/**
* 置顶推荐
*/
public function singleTopRecommendV2(Request $request)
{
try {
$mine = auth()->user();
$users = DB::table('users')->whereNotNull('photo')->where('hidden_profile', 'NONE');
if ($mine->sex && $mine->type != 'marriage') {
$sex = $mine->sex == 1 ? 2 : 1;
$users = $users->where('users.sex', $sex);
}
$users = $users->rightJoin('profile_courtships', 'users.id', '=', 'profile_courtships.user_id')->select('users.id', 'users.photo', 'users.nickname', 'users.hidden_profile', 'users.sex', 'profile_courtships.city','profile_courtships.province', 'profile_courtships.birthday');
$area_users = DB::table('area_users')->leftJoinSub($users, 'ufutx_users', function ($join) {
$join->on('area_users.user_id', '=', 'users.id');
})->where('area_users.area_id', 1)->where('area_users.is_show', 1)->where('area_users.is_audited', 1)->where('users.id', '>', 0)->select('area_users.user_id', 'users.*')->get()->toArray();
if (count($area_users) > 5) {
$area_users_index = array_rand($area_users, 5);
$users = [];
foreach ($area_users_index as $index) {
$users[] = $area_users[$index];
}
$area_users = $users;
}
foreach ($area_users as $user) {
$user->birthday = $user->birthday ? substr($user->birthday, 2, 2) : '';
}
return $this->success("ok", $area_users);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('数据加载失败,请稍后再试');
}
}
/**
* 活动推荐
*/
public function areaRecommend(Request $request)
{
try {
//最近订单
$orders = TouristOrder::with('mUser:id,nickname', 'activityService')->where('pay_status', '<>', 0)->select('id', 'pay_status', 'merchant_id', 'account_id', 'desc', 'type_id', 'type')->where('merchant_id', 491)->where('type', 'community')->whereHas('mUser')->orderBy('id', 'desc')->limit(20)->get();
foreach ($orders as $order) {
$order->text = mb_substr($order->mUser->nickname, 0, 1) . '**刚刚报名了《' . $order->desc . '》';
$order->class = $order->activityService ? $order->activityService->class : '';
unset($order->activityService);
}
$now = date('Y-m-d H:i:s');
//推荐活动
$activities = DB::table('recommend_community_activities as r_act')->join('community_activities as act', 'act.id', '=', 'r_act.activity_id')->where('r_act.is_show', 1)->where(function ($sql) use ($now) {
$sql->where('act.end_time', ">", $now)->orWhere(function ($sq) {
$sq->whereNull('start_time')->whereNull('end_time');
});
})->select('r_act.activity_id', 'act.id', 'act.title', 'act.Subtitle', 'act.pic', 'act.class','act.join_type')
->orderByDesc('r_act.is_top')
->get();
//专区
$areas = ClassArea::where('id', '<>', 1)->where('is_show', 1)->select('id', 'pic', 'title')->orderBy('sort', 'asc')->get();
return $this->success('ok', compact('orders', 'activities', 'areas'));
} catch (Exception $e) {
$this->getError($e);
return $this->failure('数据加载失败,请稍后再试');
}
}
/**
* 单身广场
* @param Request $request
*/
public function singleSquare(Request $request)
{
try {
$mine = auth()->user();
//是否有系统计算
$count = RecommendLinkingNew::where('id_users_left', $mine->id)->count();
//分页数
$limit = 16;
if ($count >= 50) {
$users = DB::table('users as u')
->rightJoin('recommend_linking_news as l', 'l.id_users_right', '=', 'u.id')
->leftjoin("profile_courtships as c", 'c.user_id', '=', 'u.id')
->leftJoin('test_users as t_u', 't_u.user_id', '=', 'u.id')
->leftJoin('system_blacklists as s_b', 's_b.user_id', '=', 'u.id');
if ($mine->sex) {
$sex = $mine->sex == 1 ? 2 : 1;
$users->where('u.sex', $sex);
}
$users = $users->where('l.id_users_left', $mine->id)->whereNotNull('c.user_id');
$users = $this->publicOptions($mine, $users);
$users = $users->select('u.id', 'u.photo', 'u.sex', 'u.nickname', 'u.industry', 'u.industry_sub', 'u.is_real_approved', 'u.rank_id', 'u.type', 'c.birthday', 'c.stature', 'c.degree', 'c.introduction', 'l.score')->orderBy('l.score', 'desc')->orderBY('u.id', 'desc')->paginate($limit);
} else {
$users = DB::table('users as u')
->rightJoin("profile_courtships as c", 'c.user_id', '=', 'u.id')
->leftJoin('test_users as t_u', 't_u.user_id', '=', 'u.id')
->leftJoin('system_blacklists as s_b', 's_b.user_id', '=', 'u.id');
if ($mine->sex && $mine->type == 'single') {
$sex = $mine->sex == 1 ? 2 : 1;
$users->where('u.sex', $sex);
}
$users = $this->publicOptions($mine, $users);
$users = $users->select('u.id', 'u.photo', 'u.sex', 'u.nickname', 'u.industry', 'u.industry_sub', 'u.is_real_approved', 'u.rank_id', 'u.type', 'c.birthday', 'c.stature', 'c.degree', 'c.introduction', 'u.face_score')->orderBy('u.face_score', 'desc')->orderBY('u.id', 'desc')->paginate($limit);
}
foreach ($users as $user) {
$user->birthday = $user->birthday ? substr($user->birthday, 2, 2) : '';
$user->is_friend = $mine->isFriend($user) ? 1 : 0;
$user->is_rank = $user->rank_id > 0 ? 1 : 0;
$user->status = 'person';
}
$service = CommunityActivity::where('id', 1353)->select('id', 'title', 'Subtitle', DB::raw('pic as photo'), 'class')->first();
if ($service && count($users)) {
$service->status = 'service';
$pics = [
'https://image.fulllinkai.com/202204/08/d2e0c718b811b1d721533adac5cdfa18.png',
'https://image.fulllinkai.com/202204/09/e83ec30e8c337572902b15ca39059bae.png',
'https://image.fulllinkai.com/202204/09/fe67d1809e2dfa63666d403398870741.png',
'https://image.fulllinkai.com/202204/09/2b8db7bcc28cd3da48a22d036b67c319.png',
'https://image.fulllinkai.com/202204/09/c48ace8a981d182d0130fa3f6c5a4bf4.png',
];
$service->photo = $pics[array_rand($pics, 1)];
$index = $this->getSortIndex($users, $limit);
if ($index) {
$users = $this->sortEdit($users, $index, $service);
}
}
return $this->success('ok', $users);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure("数据加载失败,请稍后再试");
}
}
public function publicOptions($mine, $query)
{
$query = $query->whereNotNull('u.nickname')->where('type', 'single')->where('u.hidden_profile', 'NONE')->where('u.is_photo_audited', 1)->whereNull('u.deleted_at')->whereRaw('char_length(introduction) > 10')->whereNull('t_u.id')->where(function ($sql) {
$sql->whereNull('s_b.id')->orWhereNotNull('s_b.deleted_at');
})->whereNotExists(function ($sql) use ($mine) {
$sql->select(DB::raw(1))->from('user_dislike_logs as udl')->whereRaw('ufutx_udl.type_id = ufutx_u.id ')->where('udl.user_id', $mine->id)->where('udl.type', 'user');
})->whereNotExists(function ($sql) use ($mine) {
$sql->select(DB::raw(1))->from('linking_blacklists as lb')->whereRaw('ufutx_lb.other_user_id = ufutx_u.id ')->where('lb.user_id', $mine->id)->whereNull('lb.deleted_at');
});
return $query;
}
public function getSortIndex($users, $limit)
{
$page = $users->currentPage();
$array = [];
$index = 1;
$last_index = 1;
$target = 8;
while (true) {
$res = $index * $target;
$last_index = $last_index + 2;
$index = $index + $last_index;
if ($res > $users->total()) break;
$array[] = $res;
}
$current_point = 0;
foreach ($array as $key => $arr) {
if ($page == 1) {
$current_point = $array[0];
} else {
if ($key > 0) {
$last_arr = $arr + $limit;
if ($page * $limit >= $arr && $page * $limit < $last_arr) {
$current_point = $arr;
}
}
}
}
$min_point = $page * $limit - $limit;
$max_point = $page * $limit;
if ($current_point > $min_point && $current_point <= $max_point) {
if ($current_point % $limit) return 8;
return 16;
} else {
return 0;
}
}
public function sortEdit($sort, $num, $chose)
{
$sorts = $sort->chunk($num);
$sorts[0]->push($chose);
$data = [
'current_page' => $sort->currentPage(),
'data' => $sorts->collapse(),
'first_page_url' => request()->url() . '?page=1',
'from' => $sort->perPage() * ($sort->currentPage() - 1) + 1,
'last_page' => $sort->lastPage(),
'last_page_url' => request()->url() . '?page=' . $sort->lastPage(),
'next_page_url' => $sort->currentPage() + 1 > $sort->lastPage() ? null : request()->url() . '?page=' . ($sort->currentPage() + 1),
'path' => request()->url(),
'per_page' => $sort->perPage(),
'per_page_url' => ($sort->currentPage() - 1) ? request()->url() . '?page=' . ($sort->currentPage() - 1) : null,
'to' => $sort->perPage() * $sort->currentPage(),
'total' => $sort->total(),
];
return $data;
}
/**
* 关闭资料 - 找到伴侣
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Http\JsonResponse|\Illuminate\View\View
*/
public function findPartnerByCloseProfile(Request $request)
{
try {
$data = CloseProfileHistory::with('user:id,nickname,app_avatar')
->whereHas('user', function ($query) {
$query->where('hidden_profile','ALLSEX');
})
->whereIn('reason', ['已经在福恋找到伴侣了', '通过其它方式找到伴侣了'])
->groupBy('user_id')
->orderBy('created_at','desc')
->paginate();
return $this->success('ok',$data);
}catch (\Exception $e){
$this->getError($e);
return $this->failure($e->getMessage());
}
}
}