123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- <?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\ModelStatusEnum;
- use App\Repositories\Enums\ResponseCodeEnum;
- use App\Repositories\Models\Base\Auth;
- use App\Repositories\Presenters\Base\UserPresenter;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Crypt;
- use Illuminate\Support\Facades\DB;
- use App\Repositories\Models\Base\User;
- use Illuminate\Support\Str;
- class UserService
- {
- /**
- * @var UserRepositoryEloquent
- */
- private $repository;
- /**
- * UserService constructor.
- *
- * @param UserRepositoryEloquent $userRepositoryEloquent
- */
- public function __construct(UserRepositoryEloquent $userRepositoryEloquent)
- {
- $this->repository = $userRepositoryEloquent;
- }
- /**
- * @param Request $request
- *
- * @return mixed
- * @throws \Prettus\Repository\Exceptions\RepositoryException
- */
- public function handleList(Request $request)
- {
- $this->repository->pushCriteria(new UserCriteria($request));
- $this->repository->setPresenter(UserPresenter::class);
- return $this->repository->searchUsersByPage();
- }
- /**
- * @param $id
- *
- * @return \Illuminate\Database\Eloquent\Model
- */
- public function handleProfile($id)
- {
- $this->repository->setPresenter(UserPresenter::class);
- return $this->repository->searchUserBy($id);
- }
- /**
- * @param array $data
- *
- * @return mixed
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleStore($data)
- {
- $data['mobile_encryption'] = Crypt::encryptString($data['mobile']);
- $data['mobile'] = mobile_hidden($data['mobile']);
- $user = $this->repository->create($data);
- return $user;
- }
- /**
- * @param array $data
- *
- * @return mixed
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleUpdate($data)
- {
- if (array_key_exists('mobile', $data) && strpos($data['mobile'], '*') === false) {
- $data['mobile_encryption'] = Crypt::encryptString($data['mobile']);
- $data['mobile'] = mobile_hidden($data['mobile']);
- }
- $user = $this->repository->update($data, $data['id']);
- return $user;
- }
- /**
- * @param Request $request
- *
- * @return mixed
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleDelete($id)
- {
- Auth::query()->where('user_id', $id)->update(['user_id' => 0]);
- return $this->repository->delete($id);
- }
- /**
- * 解绑微信号
- * @param $auth
- * @return bool
- */
- public function handleUnbindWechat($user_id = 0)
- {
- if (!$user_id) $user_id = login_user_id();
- $user = $this->repository->find($user_id);
- if (!$user->wechat_auth_id) return false;
- $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;
- //todo:是否解绑之后还要收取信息
- // $user->credential = null;
- $auth->save();
- $user->save();
- DB::commit();
- } catch (\Exception $exception) {
- DB::rollBack();
- exception($exception);
- }
- return true;
- }
- /**
- * 绑定微信
- * @param $auth_id
- * @param $user
- * @return bool
- */
- public function handleBindWechat($auth_id, $user)
- {
- $auth = Auth::query()->where('id', $auth_id)->first();
- if (!$auth) return false;
- $user = User::query()->where('id', $user['id'])->first();
- if (!$user) return false;
- $userInfo = Cache::get("cache:service:auth:userinfo:api:" . $auth['id'], false);
- DB::transaction(function () use ($user, $auth, $userInfo) {
- $auth->user_id = $user->id;
- $user->wechat_auth_id = $auth->id;
- $user->credential = $auth->credential;
- // if (isset($userInfo['name'])) $user->name = $userInfo['name'];
- if (isset($userInfo['nickname'])) $user->nickname = $userInfo['nickname'];
- if (isset($userInfo['headimg'])) {
- $filename = 'user-imgs/' . Str::random() . '.png';
- $filename = saveImageFromUrl($userInfo['headimg'], $filename);
- $user->headimg = $filename;
- }
- if (empty($user->last_update_time)) $user->last_update_time = Carbon::now()->toDateTimeString();
- $auth->save();
- $user->save();
- Auth::query()->where('user_id', $user->id)->where('id', '<>', $auth->id)->update(['user_id' => 0]);
- });
- return true;
- }
- /**
- * 获取登录用户信息
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
- */
- public function handleMe()
- {
- $this->repository->setPresenter(UserPresenter::class);
- return $this->repository->find(login_user_id());
- }
- /**
- * 更新个人数据
- * @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->repository->update($data, $user_id);
- }
- /**
- * 手机号登录
- * @param $mobile
- * @return array
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleMobileLogin($mobile, $area_code = "+86")
- {
- $miMobile = mobile_hidden($mobile);
- $users = User::query()->where('mobile_code', $area_code)->where('mobile', $miMobile)->select(['id', 'mobile', 'mobile_encryption'])->where('status', ModelStatusEnum::OK)->get();
- $userNums = count($users);
- if (!$userNums) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '找不到该用户');
- $user_id = 0;
- foreach ($users as $user) {
- if (Crypt::decryptString($user['mobile_encryption']) === $mobile) {
- $user_id = $user['id'];
- break;
- }
- }
- if (!$user_id) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '找不到该用户');
- $user = User::query()->where('id', $user_id)->first();
- if (empty($user->last_update_time)) {
- $user->last_update_time = Carbon::now()->toDateTimeString();
- $user->save();
- }
- $token = auth('api')->login($user);
- //单机登录限制
- $user_id = $user['id'];
- SingleLoginLimit::setToken('api', $user_id, $token);
- $this->repository->updateLoginInfo($user_id, getClientIp());
- return [$token, $user];
- }
- /**
- * 手机号登录
- * @param $mobile
- * @return bool
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleCheckMobileIsExists($mobile, $area_code = "+86")
- {
- $miMobile = mobile_hidden($mobile);
- $users = User::query()->where('mobile_code', $area_code)->where('mobile', $miMobile)->select(['id', 'mobile', 'mobile_encryption'])->where('status', ModelStatusEnum::OK)->get();
- $userNums = count($users);
- if (!$userNums) return false;
- $user_id = 0;
- foreach ($users as $user) {
- if (Crypt::decryptString($user['mobile_encryption']) === $mobile) {
- $user_id = $user['id'];
- break;
- }
- }
- if (!$user_id) return false;
- $user = User::query()->where('id', $user_id)->where('status', ModelStatusEnum::OK)->exists();
- if (!$user) return false;
- return true;
- }
- /**
- * auth登录
- * @param $auth
- * @return array
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function handleAuthLogin($auth)
- {
- $user_id = $auth['user_id'];
- $user = User::query()->where('id', $user_id)->first();
- if (!$user) {
- // abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '找不到该用户');
- return [false, false];
- }
- Cache::remember("sercer:user:handleAuthLogin:{$user_id}", Carbon::now()->addDay(), function () use ($user, $auth) {
- $userInfo = Cache::get("cache:service:auth:userinfo:api:" . $auth['id'], false);
- // if (isset($userInfo['name'])) $user->name = $userInfo['name'];
- if (isset($userInfo['nickname'])) $user->nickname = $userInfo['nickname'];
- if (isset($userInfo['headimg'])) {
- $filename = 'user-imgs/' . Str::random() . '.png';
- $filename = saveImageFromUrl($userInfo['headimg'], $filename);
- $user->headimg = $filename;
- }
- $user->save();
- });
- $token = auth('api')->login($user);
- //单机登录限制
- $user_id = $user['id'];
- SingleLoginLimit::setToken('api', $user_id, $token);
- $this->repository->updateLoginInfo($user_id, getClientIp());
- return [$token, $user];
- }
- /**
- * 选项
- * @param Request $request
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
- * @throws \Prettus\Repository\Exceptions\RepositoryException
- */
- public function handleSelectOptions(Request $request)
- {
- $this->repository->pushCriteria(new UserCriteria($request));
- return $this->repository->all(['id', 'nickname', 'mobile']);
- }
- /**
- * 读信
- * @param $user_id
- * @return void
- */
- public function handleReadXin($user_id)
- {
- $user = User::query()->where('id', $user_id)->first();
- $user->is_read_xin = ModelStatusEnum::OK;
- $user->save();
- }
- }
|