This commit is contained in:
Hankin 2026-08-20 17:56:59 +08:00
parent 23599a7786
commit 9fa390cd92
4 changed files with 388 additions and 221 deletions

View File

@ -39,7 +39,8 @@ class MakeFriendsController extends Controller
$merchant_user = MerchantUsers::where('id', $merchant_user_id)->first();
if ($merchant_user) {
$exist = IdentityAuthorization::where('m_user_id', $merchant_user->id)->where('m_id', $merchant_id)->exists();
if ($exist) return $this->failure('该用户已添加');
if ($exist)
return $this->failure('该用户已添加');
}
$nickname = $request->nickname;
$pic = $request->pic;
@ -167,7 +168,8 @@ class MakeFriendsController extends Controller
try {
$id = $request->merchant_user_id;
$merchant_user = MerchantUsers::where('id', $id)->first();
if (!$merchant_user) return $this->failure('该用户不存在');
if (!$merchant_user)
return $this->failure('该用户不存在');
$nickname = $request->nickname;
$pic = $request->pic;
@ -345,7 +347,8 @@ class MakeFriendsController extends Controller
$user = User::select('id', 'photo as pic', 'nickname', 'industry_sub', 'industry', 'hidden_profile', 'created_at')
->where('mobile', $mobile)
->first();
if (!$user) return $this->resp('fail', ['status' => 1]);
if (!$user)
return $this->resp('fail', ['status' => 1]);
$merchant_user_mobile = MerchantUser::where('mobile', $mobile)
->first();
if ($merchant_user_mobile) {
@ -374,7 +377,7 @@ class MakeFriendsController extends Controller
/**
* 用户列表
* @param Request $Request
* @param Request $request
* @return JsonResponse|string
*/
public function userList(Request $request)
@ -407,20 +410,153 @@ class MakeFriendsController extends Controller
}
}
public function userListV2(Request $request)
{
try {
$m_id = $request->merchant_id;
// 1. 获取所有关联的商户用户 ID仍需要一次查询
$ids = IdentityAuthorization::where('m_id', $m_id)
->orderBy('id', 'desc')
->pluck('m_user_id');
if ($ids->isEmpty()) {
return $this->success('ok', []);
}
// 2. 批量加载用户、profile 和 photos仅一次查询附带 m_id 条件)
$usersWithData = MerchantUsers::whereIn('id', $ids)
->with([
'profile' => function ($q) use ($m_id) {
$q->where('m_id', $m_id);
},
'photos' => function ($q) use ($m_id) {
$q->where('m_id', $m_id)->select('photo');
}
])
->get();
// 3. 在内存中计算分数,收集符合条件(分数 ≥ 60的 ID
$fixIds = [];
foreach ($usersWithData as $user) {
$score = $this->calculateScoreFromModel($user); // 使用已加载的数据,不再查询
if ($score >= 60) {
$fixIds[] = $user->id;
}
}
// 4. 用符合条件的分页查询(再次预加载,避免 N+1
$users = MerchantUsers::whereIn('id', $fixIds)
->with([
'profile' => function ($q) use ($m_id) {
$q->where('m_id', $m_id);
},
'photos' => function ($q) use ($m_id) {
$q->where('m_id', $m_id)->select('photo');
}
])
->paginate();
// 5. 组装输出数据
foreach ($users as $user) {
$profile = $user->profile;
if ($profile) {
// 手机号脱敏注意mobile 在 MerchantUsers 表中)
$profile->mobile = substr_replace($user->mobile, '****', 3, 4);
$profile->age = $this->getAge($profile->birthday);
$profile->mate_conditon = json_decode($profile->mate_conditon);
}
// 照片列表
$user->photos = $user->photos->pluck('photo');
// 将 profile 挂载到 info 字段(保持原结构)
$user->info = $profile;
}
return $this->success('ok', $users);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
/**
* 根据已加载的模型计算资料完整分(不再查询数据库)
*/
private function calculateScoreFromModel($user)
{
$total_score = 0;
$profile = $user->profile;
if (!$user || !$profile) {
return $total_score;
}
$keys = [
'belief',
'birthday',
'degree',
'ideal_mate',
'income',
'industry',
'industry_sub',
'interest_hobby',
'introduction',
'province',
'city',
'mate_conditon',
'state',
'stature',
'weight',
'sex'
];
foreach ($keys as $key) {
if (!empty($profile->$key)) {
$total_score += 5;
}
}
// 生活照额外加分
if ($user->photos && $user->photos->isNotEmpty()) {
$total_score += 5;
}
return $total_score;
}
public function completeInfoScoreV2($user_id, $m_id)
{
try {
$total_score = 0;
$user = MerchantUsers::find($user_id);
$profile = MerchantUserProfile::where(['m_id' => $m_id, 'user_id' => $user_id])->first();
if (!$user || !$profile) return $total_score;
$keys = ['belief', 'birthday', 'degree', 'ideal_mate', 'income', 'industry', 'industry_sub', 'interest_hobby', 'introduction', 'province', 'city', 'mate_conditon',
'state', 'stature', 'weight', 'sex'];
if (!$user || !$profile)
return $total_score;
$keys = [
'belief',
'birthday',
'degree',
'ideal_mate',
'income',
'industry',
'industry_sub',
'interest_hobby',
'introduction',
'province',
'city',
'mate_conditon',
'state',
'stature',
'weight',
'sex'
];
foreach ($keys as $key) {
if ($profile->$key) $total_score += 5;
if ($profile->$key)
$total_score += 5;
}
$photos = MerchantUserPhoto::where(['m_id' => $m_id, 'user_id' => $user_id])->first();
if ($photos) $total_score += 5;//生活照
if ($photos)
$total_score += 5;//生活照
return $total_score;
} catch (\Exception $e) {
$this->getError($e);
@ -440,25 +576,42 @@ class MakeFriendsController extends Controller
return 0;
}
$total_score = 0;
if (!$user || !$user->user || !$user->user->sass_profileCourtship) return $total_score;
if (!$user || !$user->user || !$user->user->sass_profileCourtship)
return $total_score;
$photos = ProfilePhoto::where('user_id', $user->user_id)->first();
if ($user->user->sass_profileCourtship->belief) $total_score += 5;
if ($user->user->sass_profileCourtship->birthday) $total_score += 5;
if ($user->user->sass_profileCourtship->belief)
$total_score += 5;
if ($user->user->sass_profileCourtship->birthday)
$total_score += 5;
// if($user->user->sass_profileCourtship->city) $totle_score += 5;
if ($user->user->sass_profileCourtship->degree) $total_score += 5;
if ($user->user->sass_profileCourtship->ideal_mate) $total_score += 5;
if ($user->user->sass_profileCourtship->income) $total_score += 5;
if ($user->user->industry) $total_score += 5;
if ($user->user->industry_sub) $total_score += 5;
if ($user->user->sass_profileCourtship->interest_hobby) $total_score += 5;
if ($user->user->sass_profileCourtship->introduction) $total_score += 5;
if ($photos) $total_score += 5;//生活照
if ($user->user->sass_profileCourtship->province) $total_score += 5;
if ($user->user->sass_profileCourtship->mate_conditon) $total_score += 25;
if ($user->user->sass_profileCourtship->state) $total_score += 5;
if ($user->user->sass_profileCourtship->stature) $total_score += 5;
if ($user->user->sass_profileCourtship->weight) $total_score += 5;
if ($user->user->sass_profileCourtship->sex) $total_score += 5;
if ($user->user->sass_profileCourtship->degree)
$total_score += 5;
if ($user->user->sass_profileCourtship->ideal_mate)
$total_score += 5;
if ($user->user->sass_profileCourtship->income)
$total_score += 5;
if ($user->user->industry)
$total_score += 5;
if ($user->user->industry_sub)
$total_score += 5;
if ($user->user->sass_profileCourtship->interest_hobby)
$total_score += 5;
if ($user->user->sass_profileCourtship->introduction)
$total_score += 5;
if ($photos)
$total_score += 5;//生活照
if ($user->user->sass_profileCourtship->province)
$total_score += 5;
if ($user->user->sass_profileCourtship->mate_conditon)
$total_score += 25;
if ($user->user->sass_profileCourtship->state)
$total_score += 5;
if ($user->user->sass_profileCourtship->stature)
$total_score += 5;
if ($user->user->sass_profileCourtship->weight)
$total_score += 5;
if ($user->user->sass_profileCourtship->sex)
$total_score += 5;
return $total_score;
} catch (\Exception $e) {
$this->getError($e);

View File

@ -2,6 +2,8 @@
namespace App\Models;
use App\Models\Server\MerchantUserPhoto;
use App\Models\Server\MerchantUserProfile;
use Illuminate\Database\Eloquent\Model;
use Actuallymab\LaravelComment\CanComment;
@ -14,4 +16,16 @@ class MerchantUsers extends Model
{
return $this->belongsTo(User::class);
}
public function profile()
{
return $this->hasOne(MerchantUserProfile::class, "user_id", "id");
}
public function photos()
{
return $this->hasMany(MerchantUserPhoto::class, "user_id", "id");
}
}

View File

@ -265,7 +265,7 @@ Route::middleware('merchant_user')->group(function () {
// 用户详情
Route::get('friend', 'MakeFriendsController@userInfo')->where('id', '[0-9]+');
// 用户列表
Route::get('friend/user/list', 'MakeFriendsController@userList');
Route::get('friend/user/list', 'MakeFriendsController@userListV2');
// 授权同步数据
Route::get('friend/synchronous/data', 'MakeFriendsController@SynchronousData');
// 更新用户资料