|
| 1 | +<?php |
| 2 | +/* |
| 3 | +|----------------------------------------------------------------------------------------------------------- |
| 4 | +| laravel-admin-cms [ 简单高效的开发插件系统 ] |
| 5 | +|----------------------------------------------------------------------------------------------------------- |
| 6 | +| Licensed ( MIT ) |
| 7 | +| ---------------------------------------------------------------------------------------------------------- |
| 8 | +| Copyright (c) 2020-2021 https://gitee.com/liaodeiy/laravel-admin-cms All rights reserved. |
| 9 | +| ---------------------------------------------------------------------------------------------------------- |
| 10 | +| Author: 廖春贵 < liaodeity@gmail.com > |
| 11 | +|----------------------------------------------------------------------------------------------------------- |
| 12 | +*/ |
| 13 | + |
| 14 | +namespace App\Http\Controllers\Admin; |
| 15 | + |
| 16 | +use App\Enums\SexEnum; |
| 17 | +use App\Enums\StatusEnum; |
| 18 | +use App\Exceptions\BusinessException; |
| 19 | +use App\Http\Controllers\Controller; |
| 20 | +use App\Libs\QueryWhere; |
| 21 | +use App\Models\Log; |
| 22 | +use App\Models\Page; |
| 23 | +use App\Repositories\PageRepository; |
| 24 | +use Illuminate\Http\Request; |
| 25 | +use Illuminate\Support\Facades\View; |
| 26 | + |
| 27 | +class PageController extends Controller |
| 28 | +{ |
| 29 | + protected $module_name = 'page'; |
| 30 | + /** |
| 31 | + * @var PageRepository |
| 32 | + */ |
| 33 | + private $repository; |
| 34 | + |
| 35 | + public function __construct (PageRepository $repository) |
| 36 | + { |
| 37 | + SexEnum::attrs (); |
| 38 | + View::share ('MODULE_NAME', $this->module_name);//模块名称 |
| 39 | + $this->repository = $repository; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Display a listing of the resource. |
| 44 | + * |
| 45 | + * @return \Illuminate\Http\Response |
| 46 | + */ |
| 47 | + public function index (Request $request) |
| 48 | + { |
| 49 | + |
| 50 | + $category_id = $request->input ('category_id', 0); |
| 51 | + if (request ()->wantsJson ()) { |
| 52 | + $limit = $request->input ('limit', 15); |
| 53 | + QueryWhere::defaultOrderBy ('pages.id', 'DESC')->setRequest ($request->all ()); |
| 54 | + $M = $this->repository->makeModel ()->select ('pages.*'); |
| 55 | + QueryWhere::eq ($M, 'pages.category_id', $category_id); |
| 56 | + QueryWhere::eq ($M, 'pages.status'); |
| 57 | + QueryWhere::eq ($M, 'pages.istop'); |
| 58 | + QueryWhere::like ($M, 'pages.title'); |
| 59 | + QueryWhere::like ($M, 'pages.link_label'); |
| 60 | + QueryWhere::orderBy ($M); |
| 61 | + |
| 62 | + $M = $M->paginate ($limit); |
| 63 | + $count = $M->total (); |
| 64 | + $data = $M->items (); |
| 65 | + foreach ($data as $key => $item) { |
| 66 | + $data[ $key ]['_url'] = url ('page/detail/' . $item->id); |
| 67 | + $data[ $key ]['status'] = StatusEnum::toLabel ($item->status); |
| 68 | + } |
| 69 | + $result = [ |
| 70 | + 'count' => $count, |
| 71 | + 'data' => $data |
| 72 | + ]; |
| 73 | + |
| 74 | + return ajax_success_result ('成功', $result); |
| 75 | + |
| 76 | + } else { |
| 77 | + $page = $this->repository->makeModel (); |
| 78 | + |
| 79 | + return view ('admin.' . $this->module_name . '.index', compact ('page', 'category_id')); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Show the form for creating a new resource. |
| 85 | + * |
| 86 | + * @return \Illuminate\Http\Response |
| 87 | + */ |
| 88 | + public function create (Request $request) |
| 89 | + { |
| 90 | + $page = $this->repository->makeModel (); |
| 91 | + $_method = 'POST'; |
| 92 | + $page->status = StatusEnum::NORMAL; |
| 93 | + |
| 94 | + return view ('admin.' . $this->module_name . '.add', compact ('page', '_method')); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Store a newly created resource in storage. |
| 99 | + * |
| 100 | + * @param \Illuminate\Http\Request $request |
| 101 | + * @return \Illuminate\Http\Response |
| 102 | + */ |
| 103 | + public function store (Request $request) |
| 104 | + { |
| 105 | + $request->validate ([ |
| 106 | + 'Page.title' => 'required', |
| 107 | + 'Page.name' => 'unique:pages,name', |
| 108 | + 'Page.content' => 'required', |
| 109 | + 'Page.status' => 'required', |
| 110 | + ], [], [ |
| 111 | + 'Page.title' => '标题', |
| 112 | + 'Page.name' => '英文标识', |
| 113 | + 'Page.content' => '内容', |
| 114 | + 'Page.status' => '状态', |
| 115 | + ]); |
| 116 | + if (!check_admin_auth ($this->module_name . '_edit')) { |
| 117 | + return auth_error_return (); |
| 118 | + } |
| 119 | + $input = $request->input ('Page'); |
| 120 | + $input = $this->formatRequestInput (__FUNCTION__, $input); |
| 121 | + try { |
| 122 | + $input['user_id'] = get_login_user_id (); |
| 123 | + $page = $this->repository->create ($input); |
| 124 | + if ($page) { |
| 125 | + $log_title = '添加单页面记录'; |
| 126 | + Log::createLog (Log::ADD_TYPE, $log_title, '', $page->id, Page::class); |
| 127 | + |
| 128 | + return ajax_success_result ('添加成功'); |
| 129 | + } else { |
| 130 | + return ajax_success_result ('添加失败'); |
| 131 | + } |
| 132 | + |
| 133 | + } catch (BusinessException $e) { |
| 134 | + return ajax_error_result ($e->getMessage ()); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private function formatRequestInput (string $__FUNCTION__, $input) |
| 139 | + { |
| 140 | + return $input; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Display the specified resource. |
| 145 | + * |
| 146 | + * @param \App\Models\Page $page |
| 147 | + * @return \Illuminate\Http\Response |
| 148 | + */ |
| 149 | + public function show (Page $page) |
| 150 | + { |
| 151 | + // |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Show the form for editing the specified resource. |
| 156 | + * |
| 157 | + * @param \App\Models\Page $page |
| 158 | + * @return \Illuminate\Http\Response |
| 159 | + */ |
| 160 | + public function edit (Page $page) |
| 161 | + { |
| 162 | + $_method = 'PUT'; |
| 163 | + |
| 164 | + return view ('admin.' . $this->module_name . '.add', compact ('page', '_method')); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Update the specified resource in storage. |
| 169 | + * |
| 170 | + * @param \Illuminate\Http\Request $request |
| 171 | + * @param \App\Models\Page $page |
| 172 | + * @return \Illuminate\Http\Response |
| 173 | + */ |
| 174 | + public function update (Request $request, Page $page) |
| 175 | + { |
| 176 | + $request->validate ([ |
| 177 | + 'Page.title' => 'required', |
| 178 | + 'Page.name' => 'unique:pages,name,' . $page->id, |
| 179 | + 'Page.content' => 'required', |
| 180 | + 'Page.status' => 'required', |
| 181 | + ], [], [ |
| 182 | + 'Page.title' => '标题', |
| 183 | + 'Page.name' => '英文标识', |
| 184 | + 'Page.content' => '内容', |
| 185 | + 'Page.status' => '状态', |
| 186 | + ]); |
| 187 | + $input = $request->input ('Page'); |
| 188 | + $input = $this->formatRequestInput (__FUNCTION__, $input); |
| 189 | + try { |
| 190 | + $input['user_id'] = get_login_user_id (); |
| 191 | + $page = $this->repository->update ($input, $page->id); |
| 192 | + if ($page) { |
| 193 | + $content = $page->toArray () ?? ''; |
| 194 | + $log_title = '修改单页面记录'; |
| 195 | + Log::createLog (Log::EDIT_TYPE, $log_title, $content, $page->id, Page::class); |
| 196 | + |
| 197 | + return ajax_success_result ('修改成功'); |
| 198 | + } else { |
| 199 | + return ajax_success_result ('修改失败'); |
| 200 | + } |
| 201 | + |
| 202 | + } catch (BusinessException $e) { |
| 203 | + return ajax_error_result ($e->getMessage ()); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Remove the specified resource from storage. |
| 209 | + * |
| 210 | + * @return \Illuminate\Http\Response |
| 211 | + * @throws BusinessException |
| 212 | + */ |
| 213 | + public function destroy ($id, Request $request) |
| 214 | + { |
| 215 | + $ids = $request->input ('ids', []); |
| 216 | + if (empty($ids)) { |
| 217 | + $ids[] = $id; |
| 218 | + } |
| 219 | + $ids = (array)$ids; |
| 220 | + $M = $this->repository->makeModel (); |
| 221 | + $lists = $M->whereIn ('id', $ids)->get (); |
| 222 | + $num = 0; |
| 223 | + foreach ($lists as $item) { |
| 224 | + try { |
| 225 | + $this->repository->checkAuth ($item); |
| 226 | + } catch (BusinessException $e) { |
| 227 | + return ajax_error_result ($e->getMessage ()); |
| 228 | + } |
| 229 | + $log_title = '删除单页面[' . ($item->category->title ?? '') . '->' . $item->title . ']记录'; |
| 230 | + $check = $this->repository->allowDelete ($item->id); |
| 231 | + if ($check) { |
| 232 | + $ret = $this->repository->delete ($item->id); |
| 233 | + if ($ret) { |
| 234 | + Log::createLog (Log::DELETE_TYPE, $log_title, $item, $item->id, Page::class); |
| 235 | + $num++; |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + return ajax_success_result ('成功删除' . $num . '条记录'); |
| 241 | + } |
| 242 | +} |
0 commit comments