123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- <?php
- namespace App\Services\Base;
- use App\Http\Middleware\SingleLoginLimit;
- use App\Repositories\Criteria\Base\UserCriteria;
- use App\Repositories\Eloquent\Base\UserRepositoryEloquent;
- use App\Repositories\Enums\Base\UserBindMobileEnum;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Enums\ResponseCodeEnum;
- use App\Repositories\Models\Api\Base\Banks;
- use App\Repositories\Models\Api\Base\User;
- use App\Repositories\Models\Base\Auth;
- use App\Repositories\Models\Human\Dispatch;
- use App\Repositories\Models\Human\Human;
- use App\Repositories\Models\Human\Interview;
- use App\Repositories\Models\Human\MoveRecord;
- use App\Repositories\Models\Human\Staff;
- use App\Repositories\Presenters\Base\UserPresenter;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Str;
- class UserService
- {
- /**
- * @var UserRepositoryEloquent
- */
- private $userRepository;
- /**
- * UserService constructor.
- *
- * @param UserRepositoryEloquent $userRepositoryEloquent
- */
- public function __construct(UserRepositoryEloquent $userRepositoryEloquent)
- {
- $this->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'],
- ];
- }
- }
|