master
parent
16f48fad50
commit
5b462c4fbd
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Mobile;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class UserListResource extends JsonResource
|
||||
{
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'letter' => $this->letter,
|
||||
'company_name' => $this->company_name,
|
||||
'company_position' => $this->company_position,
|
||||
'company_product' => $this->company_product,
|
||||
'open_mobile' => (bool) ($this->open_mobile ?? false),
|
||||
'mobile' => ($this->open_mobile ?? false) ? $this->mobile : null,
|
||||
'course_signs' => $this->formatCourseSigns(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function formatCourseSigns(): array
|
||||
{
|
||||
if (!$this->relationLoaded('courseSigns')) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->courseSigns->map(function ($sign) {
|
||||
$course = $sign->course;
|
||||
$typeDetail = $course ? $course->typeDetail : null;
|
||||
|
||||
return [
|
||||
'position' => $sign->position ?? null,
|
||||
'course' => $course ? [
|
||||
'year' => $course->year ?? null,
|
||||
'name' => $course->name ?? null,
|
||||
'type_detail' => $typeDetail ? [
|
||||
'name' => $typeDetail->name ?? null,
|
||||
] : null,
|
||||
] : null,
|
||||
];
|
||||
})->values()->all();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,74 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue