middleware('auth'); // 验证登录 $this->repository = $repository; $this->validator = $validator; $this->modelCopy = $repository->makeModel(); // 重置model $this->tables = \Illuminate\Support\Str::camel($this->modelCopy->getTable()); // 获取指定表名称并转换成驼峰形式 } // Admin response redirect protected function response($msg = self::SUCCESS_MSG, $status = 'success', $callback = '', $redirect = '', array $data = []) { $request = request(); if ($status == 'success') { $this->successMsg ? $msg = $this->successMsg : null; } else { $this->errorMsg ? $msg = $this->errorMsg : null; } if ($request->wantsJson() || $request->ajax()) { if ($status == 'success' && empty($redirect)) { $redirect = ''; } return response()->json([ 'status' => $status, 'code' => $status == 'success'?1:0, 'message' => $msg, 'redirect' => $redirect, 'callback' => $callback, 'data' => $data ]); } else { return redirect()->back()->withErrors($msg)->withInput(); } } /** * @param Request $request * @param ResponseFactory $response * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View * @throws \Prettus\Repository\Exceptions\RepositoryException * */ protected function index(Request $request, ResponseFactory $response) { if ($request->isMethod('post') && $request->ajax()) { // $this->repository->pushCriteria(app(RequestCriteria::class)); // $query = $this->repository; // // if( !is_null($this->indexWith) || (is_array($this->indexWith) && !empty($this->indexWith)) ){ // $query->with($this->indexWith); // } if (method_exists($this, "_indexJoin")) { $query = $this->_indexJoin(); } else { $query = DB::table($this->modelCopy->getTable()); } if (method_exists($this, "_indexSelect")) { $query->select($this->_indexSelect()); } if (method_exists($this, "_indexScopeQuery")) { $query->where($this->_indexScopeQuery()); } if (method_exists($this, "_indexOrderBy")) { $order_by = $this->_indexOrderBy(); foreach ($order_by as $by_info) { $query->orderBy($by_info[0], $by_info[1]); } } elseif ($this->defaultCreatedAtDesc) { $query->orderBy('id', 'DESC'); } if (!is_null($this->indexOrderBy) || (is_array($this->indexOrderBy) && !empty($this->indexOrderBy))) { foreach ($this->indexOrderBy as $key => $value) { $query->orderBy($key, $value); } } if(empty($request->pageSize))$request->pageSize=20; $datum = $query->paginate($request->pageSize); if (method_exists($this, "_indexPost")) { $datum = $this->_indexPost($datum); } if ($request->wantsJson()) { return $response->json([ "total" => $datum->total(), // $datum->total() 返回总行数 "rows" => $datum->items(), // 返回当前查询出来的数据 "data" => $datum, // 返回当前对象 ]); } } $params = array(); if (method_exists($this, "_indexGet")) { $params = $this->_indexGet(); } return view("admins.{$this->tables}.index", (array)$params); } /** * Store a newly created resource in storage. * * @param CreateRequest $request * * @return \Illuminate\Http\Response * * @throws \Prettus\Validator\Exceptions\ValidatorException */ protected function store(CreateRequest $request) { try { if ($request->isMethod('post') && $request->ajax()) { $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE); if (method_exists($this, "_storePost")) { $page = $this->_storePost($request); } else { $page = $this->repository->create($request->all()); } if ($page !== false) { return $this->response(self::SUCCESS_MSG, 'success', '', ''); } else { return $this->response(self::ERROR_MSG, 'error'); } } $params = []; if (method_exists($this, "_storeGet")) { $params = $this->_storeGet($request); } return response()->view("admins.{$this->tables}.create", (array)$params)->header('Content-Type', 'text/html'); } catch (ValidatorException $e) { if ($request->wantsJson()) { $errors = $e->getMessageBag()->getMessages(); $error = array_shift($errors); return response()->json([ 'error' => true, 'message' => $error[0] ]); } return redirect()->back()->withErrors($e->getMessageBag())->withInput(); } } /** * Show the form for editing the specified resource. * * @param int $id * * @return \Illuminate\Http\Response * */ protected function edit($id = null) { if (method_exists($this, "_editGet")) { $params = $this->_editGet($id); } else { $params['model'] = $this->repository->find($id); } return view("admins.{$this->tables}.edit", (array)$params); } /** * Update the specified resource in storage. * * @param UpdateRequest $request * @param string $id * * @return Response * * @throws \Prettus\Validator\Exceptions\ValidatorException * */ protected function update(UpdateRequest $request, $id = null) { try { if (method_exists($this, "_updatePost")) { $page = $this->_updatePost($request, $id); } else { $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE); if (is_null($this->onlyUpdateField) || empty($this->onlyUpdateField) || !is_array($this->onlyUpdateField)) { $update = $request->except($this->exceptUpdateField); } else { $update = $request->only($this->onlyUpdateField); } $page = $this->repository->update($update, $id); } if ($page !== false) { return $this->response(self::SUCCESS_MSG, 'success', '', ''); } else { return $this->response(self::ERROR_MSG, 'error'); } } catch (ValidatorException $e) { if ($request->wantsJson()) { $errors = $e->getMessageBag()->getMessages(); $error = array_shift($errors); return response()->json([ 'error' => true, 'message' => $error[0] ]); } return redirect()->back()->withErrors($e->getMessageBag())->withInput(); } } /** * 删除一行数据 * @param Request $request * @param ResponseFactory $response * @param $id * @return \Illuminate\Http\JsonResponse */ protected function destroy(Request $request, ResponseFactory $response, $id = null) { if ($request->isMethod('post') && $request->ajax()) { if (method_exists($this, "_destroy")) { $deleted = $this->_destroy($id); }else{ $deleted = $this->repository->delete($id); } $deleted === false ? $data = [ 'status' => 1, 'message' =>$this->errorMsg?: self::ERROR_MSG ] : $data = [ 'status' => 0, 'message' => $this->successMsg?: self::SUCCESS_MSG ]; if ($request->wantsJson()) { return $response->json($data); } } } /** * @param Request $request * @param ResponseFactory $response * @return \Illuminate\Http\JsonResponse * @throws \Exception * * 批量删除 banners 数据 */ protected function destroys(Request $request, ResponseFactory $response) { if ($request->isMethod('post') && $request->ajax()) { $ids = $request->params; $ids = explode(';', $ids); if (method_exists($this, "_destroys")) { $res = $this->_destroys($ids); }else{ $res = $this->modelCopy->whereIn('id', $ids)->delete(); } !$res ? $data = [ 'status' => 1, 'message' => self::ERROR_MSG ] : $data = [ 'status' => 0, 'message' => '' ]; if ($request->wantsJson()) { return $response->json($data); } } } /** * 成功提示 * @param $msg * @param string $url * @param string $data * @param string $code * @return string */ protected function success($msg, $url = '', $data = '', $code = '1') { return json_encode(array('msg' => $msg, 'data' => $data, 'url' => $url, 'code' => $code,'status'=>$code*1)); } /** * 错误提示 * @param $msg * @param string $url * @param string $data * @param string $code * @return string */ protected function error($msg, $url = '', $data = '', $code = '0') { return json_encode(array('msg' => $msg, 'data' => $data, 'url' => $url, 'code' => $code,'status'=>$code*1)); } }