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

148 lines
4.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\GoodMatch;
use App\Models\ProfileCourtship;
use App\Models\User;
use App\Models\Wechat;
use App\Models\Appointment;
use App\Models\Pointment;
use App\Models\Matchmaker;
use App\Models\MatchmakerClient;
class GoodMatchController extends Controller
{
/**
* 佳偶会员列表
* @param Request $request [description]
* @return [type] [description]
*/
public function goodMatches(Request $request)
{
$user = auth()->user();
if ($user->type == 'single') {
//用户性别
$user_sex = ProfileCourtship::where('user_id', $user->id)->value('sex');
if ($user_sex == 1) {
$other_sex = 2;
}elseif ($user_sex == 2) {
$other_sex = 1;
}else{
return $this->failure('请在个人信息修改性别');
}
$matches = GoodMatch::with('courtship')->where('sex', $other_sex)->orderBy('id', 'desc');
}else{
$matches = GoodMatch::with('courtship')->orderBy('id', 'desc');
}
// $type = $request->input('type');
// if ($type == 'recommend') {
// $matches = $matches->where('is_recommend', 1);
// }
$matches = $matches->whereHas('user',function($sql) {
$sql->where('type', 'single');
})->where('status', 1)->paginate(10);
foreach ($matches as $match) {
$user = User::where('id', $match->user_id)->first();
if (empty($user)) {
continue;
}
$match->name = $user->name;
$match->is_approve = $user->is_approve;
$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';
}
}
$match->avatar = $avatar;
$match->industry_sub = $user->industry_sub;
if (empty($match->courtship)) {
// $match->courtship->age = '未知';
}else{
$match->courtship->age = $this->getAge($match->courtship->birthday);
}
}
return $this->success('ok', $matches);
}
/**
* 申请加入佳偶
* @param Request $request [description]
* @return [type] [description]
*/
public function joinGoodMatch(Request $request)
{
$user = auth()->user();
if ($user->type != 'single') {
return $this->failure('只有单身可申请加入');
}
$user_sex = ProfileCourtship::where('user_id', $user->id)->value('sex');
GoodMatch::create([
'user_id'=>$user->id,
'sex'=>$user_sex,
'is_recommend'=>0,
'status'=>0,
]);
//TODO 通知管理员
return $this->success('ok');
}
/**
* 约见列表
*/
public function appointments(Request $request)
{
$active_appointments = Appointment::where('type', 'active')->get();
foreach ($active_appointments as $appointment) {
$appointment->desc = json_decode($appointment->desc);
}
$passive_appointments = Appointment::where('type', 'passive')->get();
foreach ($passive_appointments as $appointment) {
$appointment->desc = json_decode($appointment->desc);
}
return $this->success('ok', compact('active_appointments', 'passive_appointments'));
}
public function applyMatchmaker(Request $request)
{
$user = auth()->user();
$maker = Matchmaker::where('user_id', $user->id)->where('status', '<>', -1)->first();
if ($maker) {
return $this->success('ok');
}
$maker = new Matchmaker();
$maker->user_id = $user->id;
$maker->status = 0;
$maker->save();
//TODO 通知管理员
return $this->success('ok');
}
public function makerClient()
{
$makers = Matchmaker::all();
foreach ($makers as $maker) {
$from_openid = Wechat::where('user_id', $maker->user_id)->value('openid');
if (empty($from_openid)) {
continue;
}
$user_ids = User::where('from_openid', $from_openid)->pluck('id');
foreach ($user_ids as $user_id) {
MatchmakerClient::firstOrCreate([
'user_id'=>$maker->user_id,
'client_user_id'=>$user_id,
'status'=>1,
]);
}
}
return $this->success('ok');
}
}