|
| 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\AttachmentStatusEnum; |
| 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\Attachment; |
| 22 | +use App\Models\Log; |
| 23 | +use App\Models\User; |
| 24 | +use App\Repositories\AttachmentRepository; |
| 25 | +use Illuminate\Http\Request; |
| 26 | +use Illuminate\Support\Facades\Storage; |
| 27 | +use Illuminate\Support\Facades\View; |
| 28 | +use Intervention\Image\Facades\Image; |
| 29 | + |
| 30 | +class AttachmentController extends Controller |
| 31 | +{ |
| 32 | + protected $module_name = 'attachment'; |
| 33 | + /** |
| 34 | + * @var AttachmentRepository |
| 35 | + */ |
| 36 | + private $repository; |
| 37 | + |
| 38 | + public function __construct (AttachmentRepository $repository) |
| 39 | + { |
| 40 | + View::share ('MODULE_NAME', $this->module_name);//模块名称 |
| 41 | + $this->repository = $repository; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Display a listing of the resource. |
| 46 | + * |
| 47 | + * @return \Illuminate\Http\Response |
| 48 | + */ |
| 49 | + public function index (Request $request) |
| 50 | + { |
| 51 | + |
| 52 | + $category_id = $request->input ('category_id', 0); |
| 53 | + if (request ()->wantsJson ()) { |
| 54 | + $limit = $request->input ('limit', 15); |
| 55 | + QueryWhere::defaultOrderBy ('attachments.id', 'DESC')->setRequest ($request->all ()); |
| 56 | + $M = $this->repository->makeModel ()->select ('attachments.*'); |
| 57 | + QueryWhere::date ($M, 'attachments.created_at'); |
| 58 | + QueryWhere::like ($M, 'attachments.name'); |
| 59 | + QueryWhere::orderBy ($M); |
| 60 | + |
| 61 | + $M = $M->paginate ($limit); |
| 62 | + $count = $M->total (); |
| 63 | + $data = $M->items (); |
| 64 | + foreach ($data as $key => $item) { |
| 65 | + $wh = '-'; |
| 66 | + $size = '-'; |
| 67 | + $src = ''; |
| 68 | + $path = $item->storage_path; |
| 69 | + $exits = Storage::disk ('public')->exists ($path); |
| 70 | + if ($exits) { |
| 71 | + $src = asset ($item->path); |
| 72 | + $size = Storage::disk ('public')->size ($path); |
| 73 | + $size = format_size ($size); |
| 74 | + $mineType = mime_content_type ($item->path); |
| 75 | + if ($mineType && strstr ($mineType, 'image')) { |
| 76 | + $img = Image::make ($item->path); |
| 77 | + $width = $img->getWidth (); |
| 78 | + $height = $img->getHeight (); |
| 79 | + $wh = $width . '*' . $height; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + $data[ $key ]['_src'] = $src; |
| 84 | + $data[ $key ]['_w_h'] = $wh; |
| 85 | + $data[ $key ]['_size'] = $size; |
| 86 | + $data[ $key ]['user_id'] = User::showName ($item->user_id); |
| 87 | + $data[ $key ]['status'] = AttachmentStatusEnum::toHtml ($item->status); |
| 88 | + } |
| 89 | + $result = [ |
| 90 | + 'count' => $count, |
| 91 | + 'data' => $data |
| 92 | + ]; |
| 93 | + |
| 94 | + return ajax_success_result ('成功', $result); |
| 95 | + |
| 96 | + } else { |
| 97 | + $attachment = $this->repository->makeModel (); |
| 98 | + |
| 99 | + return view ('admin.' . $this->module_name . '.index', compact ('attachment', 'category_id')); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Show the form for creating a new resource. |
| 105 | + * |
| 106 | + * @return \Illuminate\Http\Response |
| 107 | + */ |
| 108 | + public function create (Request $request) |
| 109 | + { |
| 110 | + $attachment = $this->repository->makeModel (); |
| 111 | + $_method = 'POST'; |
| 112 | + $attachment->status = StatusEnum::NORMAL; |
| 113 | + |
| 114 | + return view ('admin.' . $this->module_name . '.add', compact ('attachment', '_method')); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Store a newly created resource in storage. |
| 119 | + * |
| 120 | + * @param \Illuminate\Http\Request $request |
| 121 | + * @return \Illuminate\Http\Response |
| 122 | + */ |
| 123 | + public function store (Request $request) |
| 124 | + { |
| 125 | + $request->validate ([ |
| 126 | + 'Attachment.title' => 'required', |
| 127 | + 'Attachment.name' => 'unique:attachments,name', |
| 128 | + 'Attachment.content' => 'required', |
| 129 | + 'Attachment.status' => 'required', |
| 130 | + ], [], [ |
| 131 | + 'Attachment.title' => '标题', |
| 132 | + 'Attachment.name' => '英文标识', |
| 133 | + 'Attachment.content' => '内容', |
| 134 | + 'Attachment.status' => '状态', |
| 135 | + ]); |
| 136 | + if (!check_admin_auth ($this->module_name . '_edit')) { |
| 137 | + return auth_error_return (); |
| 138 | + } |
| 139 | + $input = $request->input ('Attachment'); |
| 140 | + $input = $this->formatRequestInput (__FUNCTION__, $input); |
| 141 | + try { |
| 142 | + $input['user_id'] = get_login_user_id (); |
| 143 | + $attachment = $this->repository->create ($input); |
| 144 | + if ($attachment) { |
| 145 | + $log_title = '添加附件记录'; |
| 146 | + Log::createLog (Log::ADD_TYPE, $log_title, '', $attachment->id, Attachment::class); |
| 147 | + |
| 148 | + return ajax_success_result ('添加成功'); |
| 149 | + } else { |
| 150 | + return ajax_success_result ('添加失败'); |
| 151 | + } |
| 152 | + |
| 153 | + } catch (BusinessException $e) { |
| 154 | + return ajax_error_result ($e->getMessage ()); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private function formatRequestInput (string $__FUNCTION__, $input) |
| 159 | + { |
| 160 | + return $input; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Display the specified resource. |
| 165 | + * |
| 166 | + * @param \App\Models\Attachment $attachment |
| 167 | + * @return \Illuminate\Http\Response |
| 168 | + */ |
| 169 | + public function show (Attachment $attachment) |
| 170 | + { |
| 171 | + // |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Show the form for editing the specified resource. |
| 176 | + * |
| 177 | + * @param \App\Models\Attachment $attachment |
| 178 | + * @return \Illuminate\Http\Response |
| 179 | + */ |
| 180 | + public function edit (Attachment $attachment) |
| 181 | + { |
| 182 | + $_method = 'PUT'; |
| 183 | + |
| 184 | + return view ('admin.' . $this->module_name . '.add', compact ('attachment', '_method')); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Update the specified resource in storage. |
| 189 | + * |
| 190 | + * @param \Illuminate\Http\Request $request |
| 191 | + * @param \App\Models\Attachment $attachment |
| 192 | + * @return \Illuminate\Http\Response |
| 193 | + */ |
| 194 | + public function update (Request $request, Attachment $attachment) |
| 195 | + { |
| 196 | + $request->validate ([ |
| 197 | + 'Attachment.title' => 'required', |
| 198 | + 'Attachment.name' => 'unique:attachments,name,' . $attachment->id, |
| 199 | + 'Attachment.content' => 'required', |
| 200 | + 'Attachment.status' => 'required', |
| 201 | + ], [], [ |
| 202 | + 'Attachment.title' => '标题', |
| 203 | + 'Attachment.name' => '英文标识', |
| 204 | + 'Attachment.content' => '内容', |
| 205 | + 'Attachment.status' => '状态', |
| 206 | + ]); |
| 207 | + $input = $request->input ('Attachment'); |
| 208 | + $input = $this->formatRequestInput (__FUNCTION__, $input); |
| 209 | + try { |
| 210 | + $input['user_id'] = get_login_user_id (); |
| 211 | + $attachment = $this->repository->update ($input, $attachment->id); |
| 212 | + if ($attachment) { |
| 213 | + $content = $attachment->toArray () ?? ''; |
| 214 | + $log_title = '修改附件记录'; |
| 215 | + Log::createLog (Log::EDIT_TYPE, $log_title, $content, $attachment->id, Attachment::class); |
| 216 | + |
| 217 | + return ajax_success_result ('修改成功'); |
| 218 | + } else { |
| 219 | + return ajax_success_result ('修改失败'); |
| 220 | + } |
| 221 | + |
| 222 | + } catch (BusinessException $e) { |
| 223 | + return ajax_error_result ($e->getMessage ()); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Remove the specified resource from storage. |
| 229 | + * |
| 230 | + * @return \Illuminate\Http\Response |
| 231 | + * @throws BusinessException |
| 232 | + */ |
| 233 | + public function destroy ($id, Request $request) |
| 234 | + { |
| 235 | + $ids = $request->input ('ids', []); |
| 236 | + if (empty($ids)) { |
| 237 | + $ids[] = $id; |
| 238 | + } |
| 239 | + $ids = (array)$ids; |
| 240 | + $M = $this->repository->makeModel (); |
| 241 | + $lists = $M->whereIn ('id', $ids)->get (); |
| 242 | + $num = 0; |
| 243 | + foreach ($lists as $item) { |
| 244 | + try { |
| 245 | + $this->repository->checkAuth ($item); |
| 246 | + } catch (BusinessException $e) { |
| 247 | + return ajax_error_result ($e->getMessage ()); |
| 248 | + } |
| 249 | + $log_title = '删除附件[' . ($item->category->title ?? '') . '->' . $item->title . ']记录'; |
| 250 | + $check = $this->repository->allowDelete ($item->id); |
| 251 | + if ($check) { |
| 252 | + $ret = $this->repository->delete ($item->id); |
| 253 | + if ($ret) { |
| 254 | + Log::createLog (Log::DELETE_TYPE, $log_title, $item, $item->id, Attachment::class); |
| 255 | + $num++; |
| 256 | + } |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + return ajax_success_result ('成功删除' . $num . '条记录'); |
| 261 | + } |
| 262 | +} |
0 commit comments