diff --git a/app/Http/Controllers/Mobile/CourseController.php b/app/Http/Controllers/Mobile/CourseController.php index 84b4f25..d632434 100755 --- a/app/Http/Controllers/Mobile/CourseController.php +++ b/app/Http/Controllers/Mobile/CourseController.php @@ -22,7 +22,9 @@ use App\Models\CourseSign; use App\Models\CourseType; use App\Models\Notice; use App\Models\Order; +use App\Http\Resources\Mobile\UserListResource; use App\Models\User; +use App\Services\SchoolmateAccessService; use EasyWeChat\Factory; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Log; @@ -505,17 +507,56 @@ class CourseController extends CommonController if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } - $signModel = CourseSign::find($all['id']); - // 如果已缴费或者已上传凭证,不允许修改 - if ($signModel->fee_status == 1 || !empty($signModel->fee_file_ids)) { + $signModel = CourseSign::where('id', $all['id']) + ->where('user_id', $this->getUserId()) + ->first(); + if (!$signModel) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '报名记录不存在']); + } + + $isFeeUpload = isset($all['fee_file_ids']) && !empty($all['fee_file_ids']); + $isStatusUpdate = isset($all['status']) && in_array((int) $all['status'], [4, 5], true); + + if ($signModel->fee_status == 1) { return $this->fail([ResponseCode::ERROR_BUSINESS, '已缴费或已上传凭证,不允许修改']); } + if ($isFeeUpload) { + if (!empty($signModel->fee_file_ids)) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '已缴费或已上传凭证,不允许修改']); + } + } elseif (!empty($signModel->fee_file_ids)) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '已缴费或已上传凭证,不允许修改']); + } + + $updates = []; + foreach (['data', 'change_data', 'is_change'] as $field) { + if (array_key_exists($field, $all)) { + $updates[$field] = $all[$field]; + } + } + + if ($isFeeUpload) { + $updates['fee_file_ids'] = $all['fee_file_ids']; + if (isset($all['fee_status']) && (int) $all['fee_status'] === 3) { + $updates['fee_status'] = 3; + } + } + + if ($isStatusUpdate) { + $updates['status'] = (int) $all['status']; + if ((int) $all['status'] === 5 && isset($all['giveup_reason'])) { + $updates['giveup_reason'] = $all['giveup_reason']; + } + } + + if (empty($updates)) { + return $this->fail([ResponseCode::ERROR_PARAMETER, '没有可更新的字段']); + } + $original = $signModel->getOriginal(); - $signModel->fill($all); + $signModel->fill($updates); $signModel->save(); if ($original['status'] == 1 && $signModel->status != 1) { - // 审核通过到其他状态 - // 减少预约次数 CourseAppointmentTotal::reduce($signModel->user_id, $signModel->id); } return $this->success(compact('signModel')); @@ -807,6 +848,22 @@ class CourseController extends CommonController if ($validator->fails()) { return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); } + + if ((int) $all['type'] === 1) { + if (!SchoolmateAccessService::canEnterSchoolmate($this->getUserId())) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '无权查看校友库']); + } + } elseif ((int) $all['type'] === 2) { + if (empty($all['course_id'])) { + return $this->fail([ResponseCode::ERROR_PARAMETER, '课程id必填']); + } + if (!SchoolmateAccessService::canViewCourseTxl($this->getUserId(), (int) $all['course_id'])) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '无权查看该课程通讯录']); + } + } else { + return $this->fail([ResponseCode::ERROR_PARAMETER, '类型无效']); + } + $list = User::whereHas('courseSigns', function ($query) use ($all) { if (isset($all['course_id'])) { $query->where('course_id', $all['course_id']); @@ -868,35 +925,27 @@ class CourseController extends CommonController } else { $list = $list->orderBy('letter')->paginate(20); } - // 获取当前用户参与报名 - $userCourseSigns = CourseSign::where('user_id', $this->getUserId())->where('status', 1)->get(); - // 获取当前用户允许的课程体系 - $coursesTypeIds = Course::whereIn('id', $userCourseSigns->pluck('course_id'))->pluck('type')->toArray(); - - foreach ($list as $user) { - $user->open_mobile = false; - // 只对开放的课程体系和本班人员开放手机号 - // 获取本班的课程 - $courseIds = $user->courseSigns->where('status', 1)->pluck('course_id')->toArray(); - // 判断当前用户和$user课程是否存在交集 - if (count(array_intersect($courseIds, $userCourseSigns->pluck('course_id')->toArray())) > 0) { - $user->open_mobile = true; - continue; - } - // 获取用户的课程体系 - // 获取当前用户允许的课程体系,数组 - $open_course_types = explode(',', $user->open_course_types); - // 判断当前用户和$user课程体系是否存在交集 - if (count(array_intersect($coursesTypeIds, $open_course_types)) > 0) { - $user->open_mobile = true; + $course = null; + $show_mobile = 0; + if ((int) $all['type'] === 2) { + $course = Course::find($all['course_id']); + if (!$course) { + return $this->fail([ResponseCode::ERROR_PARAMETER, '课程不存在']); } + $show_mobile = (int) ($course->show_mobile ?? 0); } + + SchoolmateAccessService::applyOpenMobileFlags($list->getCollection(), $this->getUserId(), $course); + + $list->getCollection()->transform(function ($user) { + return (new UserListResource($user))->toArray(request()); + }); + $teacher = []; - if (isset($all['type']) && $all['type'] == 2) { - $course = Course::find($all['course_id']); + if ((int) $all['type'] === 2 && $course) { $teacher = $course['teacher_detail']; } - return $this->success(compact('list', 'teacher')); + return $this->success(compact('list', 'teacher', 'show_mobile')); } /** diff --git a/app/Http/Controllers/Mobile/UserController.php b/app/Http/Controllers/Mobile/UserController.php index 54e8d73..27e9223 100755 --- a/app/Http/Controllers/Mobile/UserController.php +++ b/app/Http/Controllers/Mobile/UserController.php @@ -21,6 +21,7 @@ use App\Models\Sms; use App\Models\User; use App\Repositories\DoorRepository; use App\Repositories\YuanheRepository; +use App\Services\SchoolmateAccessService; use EasyWeChat\Factory; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Cache; @@ -274,18 +275,7 @@ class UserController extends CommonController $course_signs->qrcode = ($qrcode !== false && $qrcode !== null) ? $qrcode : null; } // 是否有资格进入校友库 - $enter_schoolmate = User::where('id', $this->getUserId()) - ->whereHas('courseSigns', function ($query) { - $query->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); - }); - }); - })->count(); + $enter_schoolmate = SchoolmateAccessService::canEnterSchoolmate($this->getUserId()) ? 1 : 0; // 是否生日 $is_birthday = 0; if (User::isBirthdayToday($user->birthday ?? null)) { diff --git a/app/Http/Resources/Mobile/UserListResource.php b/app/Http/Resources/Mobile/UserListResource.php new file mode 100644 index 0000000..6b7213f --- /dev/null +++ b/app/Http/Resources/Mobile/UserListResource.php @@ -0,0 +1,46 @@ + $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(); + } +} diff --git a/app/Services/SchoolmateAccessService.php b/app/Services/SchoolmateAccessService.php new file mode 100644 index 0000000..fe874ef --- /dev/null +++ b/app/Services/SchoolmateAccessService.php @@ -0,0 +1,74 @@ +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; + } + } + } +}