Controller.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 Illuminate\Support\Facades\Log;
  13. use Jiannei\Response\Laravel\Support\Facades\Response;
  14. use Jiannei\Response\Laravel\Support\Traits\ExceptionTrait;
  15. use Laravel\Lumen\Routing\Controller as BaseController;
  16. abstract class Controller extends BaseController
  17. {
  18. use ExceptionTrait;
  19. const PAGE_NUM = 15;
  20. public static $TERM_ID = 0;
  21. public static $TERM = [];
  22. public $response = null;
  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. $this->response = new \Jiannei\Response\Laravel\Response();
  30. }
  31. public function notFound()
  32. {
  33. Response::fail('The resource could not be found.', ResponseCodeEnum::SERVICE_NOT_FIND_DATA_ERROR);
  34. }
  35. public function errorStore($exception)
  36. {
  37. Log::error($exception);
  38. if (config('app.env') == 'production') {
  39. Response::fail(T('An unknown error was encountered.'), ResponseCodeEnum::SYSTEM_ERROR);
  40. } else {
  41. Response::fail($exception->getMessage(), ResponseCodeEnum::SYSTEM_ERROR);
  42. }
  43. }
  44. public function errorFail()
  45. {
  46. return Response::fail(T('The operation failure.'), ResponseCodeEnum::SERVICE_UPDATE_DATA_ERROR);
  47. }
  48. public function error($msg)
  49. {
  50. return Response::fail($msg, ResponseCodeEnum::SERVICE_UPDATE_DATA_ERROR);
  51. }
  52. /**
  53. * 是否存在权限
  54. * @param $arr
  55. * @return bool|\Illuminate\Http\JsonResponse
  56. */
  57. public function isCanAny($arr = [], $re = false)
  58. {
  59. $admin = login_admin();
  60. if ($admin->isSuperAdmin()) {
  61. return true;
  62. }
  63. if ($admin->hasAnyPermission($arr)) {
  64. return true;
  65. }
  66. if ($re) return false;
  67. return $this->error('您尚无权限');
  68. }
  69. /**
  70. * 是否有当前权限
  71. * @param $p
  72. * @param $re
  73. * @return
  74. */
  75. public function isCan($p = false, $re = false)
  76. {
  77. $admin = login_admin();
  78. if ($admin->isSuperAdmin()) {
  79. return true;
  80. }
  81. $room_p = substr_replace($p, '00', -2);
  82. $ps = [$p, $room_p];
  83. if ($admin->hasAnyPermission($ps)) {
  84. return true;
  85. }
  86. if ($re) return false;
  87. return $this->error('您尚无权限');
  88. }
  89. /**
  90. * 获取验证的数据
  91. * @param $request
  92. * @param array $rules
  93. * @param array $customAttributes
  94. * @param array $otherFields
  95. * @param array $hiddenFields
  96. * @param array $messages
  97. * @return mixed
  98. * @throws \Illuminate\Validation\ValidationException
  99. */
  100. public function validateData($request, array $rules, array $customAttributes = [], array $otherFields = [], array $hiddenFields = [], array $messages = [])
  101. {
  102. $this->validate($request, $rules, $messages, $customAttributes);
  103. $fields = array_keys($rules);
  104. if (count($otherFields)) $fields = array_merge($fields, $otherFields);
  105. if (count($hiddenFields)) $fields = array_diff($fields, $hiddenFields);
  106. return $request->only($fields);
  107. }
  108. /**
  109. * 处理异常
  110. * @param \Exception $exception
  111. * @param $msg
  112. * @return \Illuminate\Http\JsonResponse
  113. */
  114. public function exception(\Exception $exception, $msg = '操作失败')
  115. {
  116. Log::error($exception);
  117. if (config('app.env') == 'production') return $this->response->fail($msg, ResponseCodeEnum::SERVICE_OPERATION_ERROR);
  118. return $this->response->fail($exception->getMessage(), ResponseCodeEnum::SERVICE_OPERATION_ERROR);
  119. }
  120. }