userRepository = $userRepositoryEloquent; } /** * @param Request $request * * @return mixed * @throws \Prettus\Repository\Exceptions\RepositoryException */ public function handleList(Request $request) { $this->userRepository->pushCriteria(new UserCriteria($request)); $this->userRepository->setPresenter(UserPresenter::class); return $this->userRepository->searchUsersByPage(); } /** * @param $id * * @return \Illuminate\Database\Eloquent\Model */ public function handleProfile($id) { $this->userRepository->setPresenter(UserPresenter::class); return $this->userRepository->searchUserBy($id); } /** * @param array $data * * @return mixed * @throws \Prettus\Validator\Exceptions\ValidatorException */ public function handleStore($data) { $user = $this->userRepository->create($data); return $user; } /** * @param array $data * * @return mixed * @throws \Prettus\Validator\Exceptions\ValidatorException */ public function handleUpdate($data) { $user = $this->userRepository->update($data, $data['id']); return $user; } /** * @param Request $request * * @return mixed * @throws \Prettus\Validator\Exceptions\ValidatorException */ public function handleDelete($id) { return $this->userRepository->delete($id); } /** * 账号密码登录 * @param $request * @return bool */ public function handleAccountLogin($credentials, $request) { $credentials['password'] = base64_decode($credentials['password']); $credentials['status'] = ModelStatusEnum::OK; if (!$token = auth()->guard('api')->attempt($credentials)) { return false; } //单机登录限制 $user_id = login_user_id(); SingleLoginLimit::setToken('api', $user_id, $token); $this->userRepository->updateLoginInfo($user_id, $request->ip()); return $token; } /** * 微信登录 * @param $auth * @return mixed * @throws \Prettus\Validator\Exceptions\ValidatorException */ public function handleAuthLogin($auth) { $user_id = $auth->user_id; if (!$user_id) { $user = $this->userRepository->create([ 'nickname' => '用户-' . rand(10000, 99999), 'wechat_auth_id' => $auth->id, 'password' => Hash::make(Str::random(8)), ]); Auth::query()->where('id', $auth->id)->update([ 'user_id' => $user['id'] ]); } else { $user = $this->userRepository->find($user_id); } $token = auth('api')->login($user); //单机登录限制 $user_id = $user['id']; SingleLoginLimit::setToken('api', $user_id, $token); $this->userRepository->updateLoginInfo($user_id, \request()->ip()); return $token; } /** * 绑定微信号 * @param $auth * @return bool */ public function handleBindWechat($auth) { $user_id = login_user_id(); $user = $this->userRepository->find($user_id); $user->wechat_auth_id = $auth->id; $auth->user_id = $user_id; DB::beginTransaction(); try { $auth->save(); $user->save(); DB::commit(); } catch (\Exception $exception) { DB::rollBack(); exception($exception); } return true; } /** * 解绑微信号 * @param $auth * @return bool */ public function handleUnbindWechat() { $user_id = login_user_id(); $user = $this->userRepository->find($user_id); $auth = Auth::query()->find($user->wechat_auth_id); if (!$auth) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '尚未绑定微信号'); DB::beginTransaction(); try { $auth->user_id = 0; $user->wechat_auth_id = 0; $auth->save(); $user->save(); DB::commit(); } catch (\Exception $exception) { DB::rollBack(); exception($exception); } return true; } /** * 获取登录用户信息 * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed */ public function handleMe() { $this->userRepository->setPresenter(\App\Repositories\Presenters\Base\UserPresenter::class); return $this->userRepository->find(login_user_id()); } /** * @param Request $request * @return mixed */ public function handleConfirmPassword(Request $request) { $credentials = $request->only(['id', 'password']); return $this->userRepository->byIdConfirmPassword($credentials['id'], $credentials['password']); } /** * 更换手机号 * @param $user * @param $mobile * @return */ public function handleBindMobile($user_id, $mobile) { $user = $this->userRepository->find($user_id); $user->mobile = $mobile; $user->is_bind_mobile = UserBindMobileEnum::YES; $user->band_mobile_time = time(); $user->save(); return true; } /** * 更新个人数据 * @param $data * @param $user_id * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed * @throws \Prettus\Validator\Exceptions\ValidatorException */ public function handleMeUpdate($data, $user_id = false) { if (!$user_id) $user_id = login_user_id(); return $this->userRepository->update($data, $user_id); } /** * 实名认证 * @return bool */ public function handleRealName($data, $user_id = false) { if (!$user_id) $user_id = login_user_id(); $user = User::query()->find($user_id); DB::beginTransaction(); try { $human = Human::query()->where('card_id', $data['card_id'])->first(); if (!$human) { $human = Human::query()->create([ 'name' => $data['name'], 'card_id' => $data['card_id'], 'nation' => $data['nation'], 'card_start_date' => $data['start_date'], 'card_end_date' => $data['end_date'], 'card_positive_path' => $data['card_positive_path'], 'card_reverse_path' => $data['card_reverse_path'], 'address' => $data['address'], 'native' => $data['native'], 'is_real_name' => 1, 'user_id' => $user_id, ]); $user->humen_id = $human->id; if ($user->is_bind_mobile) { $human->mobile = $user->mobile; $human->band_mobile_time = $user->band_mobile_time; $human->is_band_mobile = 1; } DB::commit(); return true; } $human->fill([ 'name' => $data['name'], 'card_id' => $data['card_id'], 'nation' => $data['nation'], 'card_start_date' => $data['start_date'], 'card_end_date' => $data['end_date'], 'card_positive_path' => $data['card_positive_path'], 'card_reverse_path' => $data['card_reverse_path'], 'address' => $data['address'], 'native' => $data['native'], 'is_real_name' => 1, 'user_id' => $user_id, ]); $old_user = User::query()->where('human_id', $human->id)->where('status', ModelStatusEnum::OK)->first(); $user->human_id = $human->id; if ($user->is_bind_mobile && $user->band_mobile_time > $human->band_mobile_time) { $human->mobile = $user->mobile; $human->band_mobile_time = $user->band_mobile_time; $human->is_band_mobile = 1; } if ($old_user) { Auth::query()->where('user_id', $user_id)->update(['user_id' => $old_user->id]); $user->status = ModelStatusEnum::PAUSE; $user->delete(); } else { $user->save(); Dispatch::query()->where('human_id', $human->id)->where('user_id', 0)->update(['user_id' => $user_id]); Interview::query()->where('human_id', $human->id)->where('user_id', 0)->update(['user_id' => $user_id]); Staff::query()->where('human_id', $human->id)->where('user_id', 0)->update(['user_id' => $user_id]); MoveRecord::query()->where('human_id', $human->id)->where('user_id', 0)->update(['user_id' => $user_id]); } $human->save(); DB::commit(); } catch (\Exception $exception) { DB::rollBack(); exception($exception); return false; } return true; } /** * 手机号登录 * @param $mobile * @return string * @throws \Prettus\Validator\Exceptions\ValidatorException */ public function handleMobileLogin($mobile) { $user = User::query()->where('mobile', $mobile)->first(); if (!$user) { $user = User::query()->create([ 'nickname' => '用户-' . rand(10000, 99999), 'password' => Hash::make(Str::random(8)), 'mobile' => $mobile, 'is_bind_mobile' => 1, ]); } $token = auth('api')->login($user); //单机登录限制 $user_id = $user['id']; SingleLoginLimit::setToken('api', $user_id, $token); $this->userRepository->updateLoginInfo($user_id, \request()->ip()); return $token; } /** * 更新银行卡 * @param $data * @return bool */ public function handleBank($data) { $user = User::query()->find(login_user_id()); if (!$user['human_id']) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '请先实名'); $human = Human::query()->find($user['human_id']); DB::beginTransaction(); try { $human->fill([ 'bank_path' => $data['path'], 'bank_no' => $data['bank_card_number'], 'bank_name' => $data['bank_name'], 'bank_valid_date' => $data['valid_date'], 'bank_type' => $data['bank_card_type'], 'bank_holder_name' => $data['holder_name'], ]); $human->save(); Banks::query()->create([ 'path' => $data['path'], 'no' => $data['bank_card_number'], 'name' => $data['bank_name'], 'valid_date' => $data['valid_date'], 'type' => $data['bank_card_type'], 'holder_name' => $data['holder_name'], ]); DB::commit(); } catch (\Exception $exception) { DB::rollBack(); exception($exception); } return true; } /** * 更新登记注册表 * @param $data * @return bool */ public function handleUpdateRegisterInfo($data) { $user = User::query()->find(login_user_id()); if (!$user['human_id']) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '请先实名'); $human = Human::query()->find($user['human_id']); DB::beginTransaction(); try { $human->fill($data); $human->save(); DB::commit(); } catch (\Exception $exception) { DB::rollBack(); exception($exception); } return true; } /** * 实名信息 * @return array */ public function handleRealNameInfo() { $user = User::query()->find(login_user_id()); if (!$user['human_id']) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '请先实名'); $human = Human::query()->find($user['human_id']); return [ 'name' => $human['name'], 'card_id' => $human['card_id'], 'card_start_date' => $human['card_start_date'], 'card_end_date' => $human['card_end_date'], ]; } /** * 实名信息 * @return array */ public function handleRegisterInfo() { $user = User::query()->find(login_user_id()); if (!$user['human_id']) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '请先实名'); $human = Human::query()->find($user['human_id']); return [ 'nation' => $human['nation'], 'eye' => $human['eye'], 'height' => $human['height'], 'health_status' => $human['health_status'], 'marriage_status' => $human['marriage_status'], 'education_status' => $human['education_status'], 'major' => $human['major'], 'address' => $human['address'], 'contact_name' => $human['contact_name'], 'contact_type' => $human['contact_type'], 'contact_mobile' => $human['contact_mobile'], ]; } /** * 银行卡信息 * @return array */ public function handleBankInfo() { $user = User::query()->find(login_user_id()); if (!$user['human_id']) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '请先实名'); $human = Human::query()->find($user['human_id']); return [ 'bank_no' => $human['bank_no'], 'bank_name' => $human['bank_name'], 'bank_holder_name' => $human['bank_holder_name'], 'bank_is_me' => $human['bank_is_me'], ]; } }