You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wx.sstbc.com/app/Services/SchoolmateAccessService.php

75 lines
2.5 KiB

<?php
namespace App\Services;
use App\Models\Course;
use App\Models\CourseSign;
use Illuminate\Support\Collection;
class SchoolmateAccessService
{
/**
* 是否有资格进入校友库(与 getUserInfo 中 enter_schoolmate 规则一致)
*/
public static function canEnterSchoolmate(int $userId): bool
{
return self::qualifyingCourseSignsQuery($userId)->exists();
}
/**
* 是否可查看指定课程的本班通讯录
*/
public static function canViewCourseTxl(int $userId, int $courseId): bool
{
return CourseSign::where('user_id', $userId)
->where('course_id', $courseId)
->where('status', 1)
->exists();
}
/**
* 审核通过且满足校友库准入条件的报名查询
*/
public static function qualifyingCourseSignsQuery(int $userId)
{
return CourseSign::where('user_id', $userId)
->where('status', 1)
->where(function ($schoolmateQuery) {
$schoolmateQuery->whereHas('course', function ($courseQuery) {
$courseQuery->where('is_fee', 1);
})->orWhereHas('course', function ($courseQuery) {
$courseQuery->where('is_fee', 0)
->where('free_enter_schoolmate', 1);
});
});
}
/**
* 为列表用户计算 open_mobile 标记
*/
public static function applyOpenMobileFlags(Collection $users, int $viewerUserId, ?Course $course = null): void
{
$userCourseSigns = CourseSign::where('user_id', $viewerUserId)->where('status', 1)->get();
$coursesTypeIds = Course::whereIn('id', $userCourseSigns->pluck('course_id'))->pluck('type')->toArray();
$courseShowMobile = $course ? (int) ($course->show_mobile ?? 0) : null;
foreach ($users as $user) {
$user->open_mobile = false;
$courseIds = $user->courseSigns->where('status', 1)->pluck('course_id')->toArray();
if (count(array_intersect($courseIds, $userCourseSigns->pluck('course_id')->toArray())) > 0) {
$user->open_mobile = true;
} else {
$openCourseTypes = array_filter(explode(',', (string) ($user->open_course_types ?? '')));
if (count(array_intersect($coursesTypeIds, $openCourseTypes)) > 0) {
$user->open_mobile = true;
}
}
if ($courseShowMobile !== null && $courseShowMobile === 0) {
$user->open_mobile = false;
}
}
}
}