adminRepository = $adminRepositoryEloquent; } /** * @param Request $request * * @return mixed * @throws \Prettus\Repository\Exceptions\RepositoryException */ public function handleList(Request $request) { $this->adminRepository->pushCriteria(new AdminCriteria($request)); $this->adminRepository->setPresenter(AdminPresenter::class); return $this->adminRepository->searchAdminsByPage(); } /** * @param $id * * @return \Illuminate\Database\Eloquent\Model */ public function handleProfile($id) { $this->adminRepository->setPresenter(AdminPresenter::class); return $this->adminRepository->searchAdminBy($id); } /** * @param array $data * * @return mixed * @throws \Prettus\Validator\Exceptions\ValidatorException */ public function handleStore($data) { $data['password'] = Hash::make($data['password']); $admin = Admin::query()->where('mobile', $data['mobile'])->first(); if (!$admin) { $admin = $this->adminRepository->create($data); } else { abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '该手机号已经存在'); $admin->fill($data); $admin->save(); } if (array_key_exists('role_ids', $data) && is_array($data['role_ids'])) $admin->syncRoles($data['role_ids']); return $admin; } /** * @param array $data * * @return mixed * @throws \Prettus\Validator\Exceptions\ValidatorException */ public function handleUpdate($data) { if (array_key_exists('password', $data)) unset($data['password']); $admin = $this->adminRepository->update($data, $data['id']); if (array_key_exists('role_ids', $data) && is_array($data['role_ids'])) $admin->syncRoles($data['role_ids']); return $admin; } /** * 删除用户 * @param $id * @return int */ public function handleDelete($id) { $admin = $this->adminRepository->find($id); // $admin->type = AdminTypeEnum::OTHER; return $admin->delete(); } /** * 批量删除 * @param $ids * @return int */ public function handleBatchDelete($ids = []) { // return $this->adminRepository->deleteWhere([['id', 'in', $ids]]); $admins = Admin::query()->whereIn('id', $ids)->get(); foreach ($admins as $admin) { // $admin->type = AdminTypeEnum::OTHER; $admin->delete(); } return true; } /** * 重置密码 * @param Request $request * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed * @throws \Prettus\Validator\Exceptions\ValidatorException */ public function handleResetPassword(Request $request) { return $this->adminRepository->update([ 'password' => Hash::make($request->get('password', '123456')), ], $request->get('id')); } /** * 重置密码 * @param Request $request * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed * @throws \Prettus\Validator\Exceptions\ValidatorException */ public function handleMeResetPassword($password, $admin_id = false) { if (!$admin_id) $admin_id = login_admin_id(); return $this->adminRepository->update([ 'password' => Hash::make($password), ], $admin_id); } /** * 选项数据 * @param Request $request * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed * @throws \Prettus\Repository\Exceptions\RepositoryException */ public function handleSelectOptions(Request $request) { $this->adminRepository->pushCriteria(new AdminCriteria($request)); // return $this->adminRepository->paginate(request('per_page', 15), ['id', 'name', 'mobile', 'username', 'sex']); return $this->adminRepository->all(['id', 'name', 'mobile', 'username', 'sex']); } /** * 账号密码登录 * @param $request * @return bool */ public function handleAccountLogin($credentials, $request) { $credentials['password'] = base64_decode($credentials['password']); $credentials['status'] = ModelStatusEnum::OK; if (!$token = auth()->guard('admins')->attempt($credentials)) { return false; } //单机登录限制 $admin_id = login_admin_id(); SingleLoginLimit::setToken('admins', $admin_id, $token); $this->adminRepository->updateLoginInfo($admin_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) abort(ResponseCodeEnum::SERVICE_NO_WECHAT, '该微信尚未绑定账号'); $admin = $this->adminRepository->find($user_id); if ($admin['wechat_auth_id'] === 0) { abort(ResponseCodeEnum::SERVICE_NO_WECHAT, '该微信尚未绑定账号'); } if ($admin['wechat_auth_id'] != $auth->id) { abort(ResponseCodeEnum::SERVICE_NO_WECHAT, '该微信尚未绑定账号'); } $token = auth('admins')->login($admin); //单机登录限制 $admin_id = $admin['id']; SingleLoginLimit::setToken('admins', $admin_id, $token); $this->adminRepository->updateLoginInfo($admin_id, \request()->ip()); return $token; } /** * 微信登录 * @param $auth * @return mixed * @throws \Prettus\Validator\Exceptions\ValidatorException */ public function handleAuthMobileLogin($auth, $mobile) { $user_id = $auth->user_id; if (!$user_id) { $user = User::query()->updateOrCreate([ 'mobile' => $mobile, ], [ // 'username' => $mobile, // 'name' => $mobile, 'wechat_auth_id' => $auth->id, 'type' => AdminTypeEnum::STUDENT ]); $auth->user_id = $user->id; $auth->save(); $user_id = $user->id; } else { $user = User::query()->where('id', $user_id)->first(); if ($user->mobile != $mobile) { // $user->username = $mobile; $user->mobile = $mobile; $user->save(); } } $admin = $this->adminRepository->find($user_id); if ($admin['wechat_auth_id'] === 0) { abort(ResponseCodeEnum::SERVICE_NO_WECHAT, '该微信尚未绑定账号'); } if ($admin['wechat_auth_id'] != $auth->id) { abort(ResponseCodeEnum::SERVICE_NO_WECHAT, '该微信尚未绑定账号'); } $token = auth('admins')->login($admin); //单机登录限制 $admin_id = $admin['id']; SingleLoginLimit::setToken('admins', $admin_id, $token); $this->adminRepository->updateLoginInfo($admin_id, \request()->ip()); return $token; } /** * 绑定微信号 * @param $auth * @return bool */ public function handleBindWechat($auth) { $admin_id = login_admin_id(); $admin = $this->adminRepository->find($admin_id); $admin->wechat_auth_id = $auth->id; $auth->user_id = $admin_id; DB::beginTransaction(); try { $auth->save(); $admin->save(); DB::commit(); } catch (\Exception $exception) { DB::rollBack(); exception($exception); } return true; } /** * 解绑微信号 * @param $auth * @return bool */ public function handleUnbindWechat() { $admin_id = login_admin_id(); $admin = $this->adminRepository->find($admin_id); $auth = Auth::query()->find($admin->wechat_auth_id); if (!$auth) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '尚未绑定微信号'); DB::beginTransaction(); try { $auth->user_id = 0; $admin->wechat_auth_id = 0; $auth->save(); $admin->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->adminRepository->setPresenter(AdminPresenter::class); return $this->adminRepository->find(login_admin_id()); } /** * @param Request $request * @return mixed */ public function handleConfirmPassword(Request $request) { $credentials = $request->only(['id', 'password']); return $this->adminRepository->byIdConfirmPassword($credentials['id'], $credentials['password']); } /** * 更换手机号 * @param $admin * @param $mobile * @return */ public function handleBindMobile($admin_id, $mobile) { $admin = $this->adminRepository->find($admin_id); $type = $admin['type']; $admin->mobile = $mobile; switch ($type) { case AdminTypeEnum::SUPPLIER: $supplier = $admin->suppliers()->where('type', SupplierTypeEnum::PERSON)->first(); if ($supplier) { $supplier->contact_mobile = $mobile; $supplier->save(); } break; } $admin->save(); return true; } /** * 更新个人数据 * @param $data * @param $admin_id * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed * @throws \Prettus\Validator\Exceptions\ValidatorException */ public function handleMeUpdate($data, $admin_id = false) { if (!$admin_id) $admin_id = login_admin_id(); return $this->adminRepository->update($data, $admin_id); } }