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(); } }