Controller.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Http\Controllers;
  11. use App\Repositories\Enums\ResponseCodeEnum;
  12. use App\Repositories\Models\School\Term;
  13. use Illuminate\Support\Facades\Log;
  14. use Jiannei\Response\Laravel\Support\Facades\Response;
  15. use Jiannei\Response\Laravel\Support\Traits\ExceptionTrait;
  16. use Laravel\Lumen\Routing\Controller as BaseController;
  17. abstract class Controller extends BaseController
  18. {
  19. use ExceptionTrait;
  20. const PAGE_NUM = 15;
  21. public static $TERM_ID = 0;
  22. public static $TERM = [];
  23. public function __construct()
  24. {
  25. self::$TERM_ID = request()->header('term-id', request()->get('term_id', 0));
  26. if (self::$TERM_ID) {
  27. self::$TERM = Term::byId(self::$TERM_ID);
  28. }
  29. }
  30. public function exception(\Exception $exception)
  31. {
  32. Log::error($exception);
  33. if (config('app.env') == 'production') {
  34. Response::fail(T('An unknown error was encountered.'), ResponseCodeEnum::SYSTEM_ERROR);
  35. } else {
  36. Response::fail($exception->getMessage(), ResponseCodeEnum::SYSTEM_ERROR);
  37. }
  38. }
  39. public function notFound()
  40. {
  41. Response::fail('The resource could not be found.', ResponseCodeEnum::SERVICE_NOT_FIND_DATA_ERROR);
  42. }
  43. public function errorStore($exception)
  44. {
  45. Log::error($exception);
  46. if (config('app.env') == 'production') {
  47. Response::fail(T('An unknown error was encountered.'), ResponseCodeEnum::SYSTEM_ERROR);
  48. } else {
  49. Response::fail($exception->getMessage(), ResponseCodeEnum::SYSTEM_ERROR);
  50. }
  51. }
  52. public function errorFail()
  53. {
  54. return Response::fail(T('The operation failure.'), ResponseCodeEnum::SERVICE_UPDATE_DATA_ERROR);
  55. }
  56. public function error($msg)
  57. {
  58. return Response::fail($msg, ResponseCodeEnum::SERVICE_UPDATE_DATA_ERROR);
  59. }
  60. /**
  61. * 是否存在权限
  62. * @param $arr
  63. * @return bool|\Illuminate\Http\JsonResponse
  64. */
  65. public function isCanAny($arr = [], $re = false, $guard = 'admin')
  66. {
  67. $admin = login_admin();
  68. if ($admin->hasAnyPermission($arr)) {
  69. return true;
  70. }
  71. if ($re) return false;
  72. return $this->error('您尚无权限');
  73. }
  74. public function isCan($p, $re = false, $guard = 'admin')
  75. {
  76. $admin = login_admin();
  77. if ($admin->can($p)) {
  78. return true;
  79. }
  80. if ($re) return false;
  81. return $this->error('您尚无权限');
  82. }
  83. }