123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524 |
- <?php
- namespace App\Services\Client;
- use App\Enums\TechnicianAuthStatus;
- use App\Enums\TechnicianStatus;
- use App\Models\CoachInfoRecord;
- use App\Models\CoachUser;
- use App\Models\MemberUser;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- use SimpleSoftwareIO\QrCode\Facades\QrCode;
- class UserService
- {
- protected $marketDistTeamService;
- public function __construct(MarketDistTeamService $marketDistTeamService)
- {
- $this->marketDistTeamService = $marketDistTeamService;
- }
-
- public function getUserInfo(): MemberUser
- {
- try {
-
- $user = Auth::user();
-
- $user->load('coach:id,user_id,state');
- return $user;
- } catch (\Exception $e) {
-
- Log::error('获取用户信息失败', ['error' => $e->getMessage()]);
- throw $e;
- }
- }
-
- public function register(string $mobile, string $code, ?string $invite_code = null, ?int $invite_id = null, ?string $invite_role = null): array
- {
- try {
- DB::beginTransaction();
-
- abort_if(MemberUser::where('mobile', $mobile)->exists(), 422, '该手机号已注册');
-
- abort_if(! $this->verifySmsCode($mobile, $code), 422, '验证码错或已过期');
-
- $user = MemberUser::create([
- 'mobile' => $mobile,
- 'password' => bcrypt(substr($mobile, -6)),
- 'nickname' => substr_replace($mobile, '****', 3, 4),
- ]);
-
- if ($invite_code && $invite_role && $invite_id) {
- $this->marketDistTeamService->createInviteRelation($user, $invite_code, $invite_id, $invite_role);
- }
- DB::commit();
- return [
- 'user_id' => $user->id,
- 'mobile' => $mobile,
- 'invite_code' => $user->invite_code,
- ];
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error('用户注册', ['error' => $e->getMessage(), 'mobile' => $mobile]);
- throw $e;
- }
- }
-
- private function verifySmsCode(string $mobile, string $code): bool
- {
- try {
- $cacheKey = "sms_code:{$mobile}";
- $cacheCode = Redis::get($cacheKey);
- if (! $cacheCode || $cacheCode !== $code) {
- return false;
- }
- Redis::del($cacheKey);
- return true;
- } catch (\Exception $e) {
- Log::error('验证码验证失败', ['error' => $e->getMessage(), 'mobile' => $mobile]);
- return false;
- }
- }
-
- public function updateUserInfo(array $data): MemberUser
- {
- try {
- DB::beginTransaction();
-
- $user = Auth::user();
- $user->update($data);
- DB::commit();
- return $user->fresh();
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error('更新用户信息失败', ['error' => $e->getMessage(), 'data' => $data]);
- throw $e;
- }
- }
-
- public function feedback(string $content, array $images = [], ?string $contact = null): void //Feedback
- {
- try {
- DB::beginTransaction();
-
-
-
-
-
-
-
- DB::commit();
-
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error('提交反馈失败', ['error' => $e->getMessage(), 'content' => $content]);
- throw $e;
- }
- }
-
- private function checkCoachApplicationEligibility(): void
- {
-
- $user = Auth::user();
-
- abort_if(
- $user->coach && $user->coach->state != TechnicianStatus::PENDING->value,
- 422,
- '您已是技师,无需重复申请'
- );
-
- $hasActiveApplication = CoachUser::where('user_id', $user->id)
- ->whereHas('infoRecords', function ($query) {
- $query->where('state', TechnicianAuthStatus::AUDITING->value);
- })
- ->exists();
- abort_if(
- $hasActiveApplication,
- 422,
- '您有正在审核的申请,请耐心等待'
- );
- }
-
- public function applyCoach(
- int $age,
- string $mobile,
- int $gender,
- string $work_years,
- string $intention_city,
- array $portrait_images,
- ?string $introduction = null
- ): CoachInfoRecord {
- try {
- DB::beginTransaction();
-
- $this->checkCoachApplicationEligibility();
-
- $user = Auth::user();
-
- $coach = $user->coach;
-
- if(!$coach){
-
- $coach = CoachUser::create([
- 'user_id' => $user->id,
- 'state' => TechnicianStatus::PENDING->value,
- ]);
- }
-
- $infoRecord = CoachInfoRecord::create([
- 'coach_id' => $coach->id,
- 'age' => $age,
- 'mobile' => $mobile,
- 'gender' => $gender,
- 'work_years' => $work_years,
- 'intention_city' => $intention_city,
- 'portrait_images' => $portrait_images,
- 'introduction' => $introduction,
- 'state' => TechnicianAuthStatus::AUDITING->value,
- ]);
-
- $coach->update(['info_record_id' => $infoRecord->id]);
- DB::commit();
- return $infoRecord;
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error('申请成为技师失败', [
- 'error' => $e->getMessage(),
- 'user_id' => Auth::id(),
- 'mobile' => $mobile,
- ]);
- throw $e;
- }
- }
-
- public function generateInviteCode(string $type = 'user'): array
- {
- try {
-
-
- $user = Auth::user();
-
- $inviteInfo = $this->getInviteInfo($user, $type);
- if (! $inviteInfo) {
- throw new \Exception('无法生邀请码,用户类型不匹配');
- }
-
- $inviteCode = $this->generateInviteCodeByType(
- $type,
- $inviteInfo['id']
- );
-
- $inviteUrl = $this->generateInviteUrl($inviteCode);
- $qrCode = $this->generateQrCode($inviteUrl);
-
- Log::info('生成邀请码', [
- 'user_id' => $user->id,
- 'type' => $type,
- 'invite_id' => $inviteInfo['id'],
- 'invite_code' => $inviteCode,
- 'invite_url' => $inviteUrl,
- ]);
- return [
- 'invite_code' => $inviteCode,
- 'invite_url' => $inviteUrl,
- 'qr_code' => $qrCode,
- ];
- } catch (\Exception $e) {
- Log::error('生成邀请码失', [
- 'error' => $e->getMessage(),
- 'user_id' => Auth::id(),
- 'type' => $type,
- ]);
- throw $e;
- }
- }
-
- private function getInviteInfo(MemberUser $user, string $type): ?array
- {
- switch ($type) {
- case 'user':
- return [
- 'id' => $user->id,
- 'role' => 'user',
- ];
- case 'coach':
- $coach = $user->coach;
- if ($coach && $coach->state === TechnicianAuthStatus::PASSED) {
- return [
- 'id' => $coach->id,
- 'role' => 'coach',
- ];
- }
- return null;
- default:
- return null;
- }
- }
-
- private function generateInviteCodeByType(string $type, int $id): string
- {
- return sprintf('%s_%d', $type, $id);
- }
-
- private function generateInviteUrl(string $inviteCode): string
- {
- return config('app.url') . '/invite?' . http_build_query(['invite_code' => $inviteCode]);
- }
-
- private function generateQrCode(string $content): string
- {
- $qrImage = QrCode::format('svg')
- ->size(200)
- ->margin(2)
- ->encoding('UTF-8')
- ->generate($content);
- return 'data:image/svg+xml;base64,' . base64_encode($qrImage);
- }
-
- public function getCoachApplication(): ?CoachInfoRecord
- {
- try {
-
- $user = Auth::user();
-
- abort_if(! $user, 401, '用户未登录');
- $coach = $user->coach;
-
- if (! $coach) {
- return null;
- }
-
- return $coach->info;
- } catch (\Exception $e) {
- Log::error('获取技师申请记录失败', [
- 'error' => $e->getMessage(),
- 'user_id' => Auth::id(),
- ]);
- throw $e;
- }
- }
- }
|