BaseController.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Config;
  4. use Illuminate\Support\Facades\Schema;
  5. class BaseController extends Controller
  6. {
  7. const SUCCESS_MSG = '操作成功';
  8. const ERROR_MSG = '操作失败';
  9. public function __construct()
  10. {
  11. $this->middleware('auth'); // 验证登录
  12. }
  13. // Admin response redirect
  14. protected function response($msg = self::SUCCESS_MSG, $status = 'success', $callback = '', $redirect = '', array $data = []) {
  15. $request = request();
  16. if ($request->wantsJson() || $request->ajax()) {
  17. if ($status == 'success' && empty($redirect)) {
  18. $redirect = '';
  19. }
  20. return response()->json([
  21. 'status' => $status,
  22. 'message' => $msg,
  23. 'redirect' => $redirect,
  24. 'callback' => $callback,
  25. 'data' => $data
  26. ]);
  27. } else {
  28. return redirect()->back()->withErrors($msg)->withInput();
  29. }
  30. }
  31. }