Controller.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\Base\Company;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\Log;
  15. use Jiannei\Response\Laravel\Support\Facades\Response;
  16. use Jiannei\Response\Laravel\Support\Traits\ExceptionTrait;
  17. use Laravel\Lumen\Routing\Controller as BaseController;
  18. abstract class Controller extends BaseController
  19. {
  20. use ExceptionTrait;
  21. protected static $CID = 0;
  22. protected static $COMPANY = false;
  23. protected $response;
  24. protected $device_type;
  25. public function __construct()
  26. {
  27. $this->response = new \Jiannei\Response\Laravel\Response();
  28. $this->device_type = device_type();
  29. //公司
  30. self::$CID = request()->header('company-id', request()->get('company_id', 0));
  31. if (self::$CID) {
  32. self::$COMPANY = Company::byId(self::$CID);
  33. }
  34. }
  35. /**
  36. * 是否存在权限
  37. * @param $arr
  38. * @return bool|\Illuminate\Http\JsonResponse
  39. */
  40. public function isCanAny($arr = [], $re = false)
  41. {
  42. $admin = login_admin();
  43. if ($admin->isSuperAdmin()) {
  44. return true;
  45. }
  46. if ($admin->hasAnyPermission($arr)) {
  47. return true;
  48. }
  49. if ($re) return false;
  50. return $this->error('您尚无权限');
  51. }
  52. /**
  53. * 错误信息
  54. * @param $msg
  55. * @return \Illuminate\Http\JsonResponse
  56. */
  57. public function error($msg)
  58. {
  59. return $this->response->fail($msg, ResponseCodeEnum::SERVICE_OPERATION_ERROR);
  60. }
  61. /**
  62. * 是否有当前权限
  63. * @param $p
  64. * @param $re
  65. * @return
  66. */
  67. public function isCan($p = false, $re = false)
  68. {
  69. $admin = login_admin();
  70. if ($admin->isSuperAdmin()) {
  71. return true;
  72. }
  73. $room_p = substr_replace($p, '00', -2);
  74. $ps = [$p, $room_p];
  75. if ($admin->hasAnyPermission($ps)) {
  76. return true;
  77. }
  78. if ($re) return false;
  79. return $this->error('您尚无权限');
  80. }
  81. /**
  82. * 获取验证的数据
  83. * @param $request
  84. * @param array $rules
  85. * @param array $customAttributes
  86. * @param array $otherFields
  87. * @param array $hiddenFields
  88. * @param array $messages
  89. * @return mixed
  90. * @throws \Illuminate\Validation\ValidationException
  91. */
  92. public function validateData($request, array $rules, array $customAttributes = [], array $otherFields = [], array $hiddenFields = [], array $messages = [])
  93. {
  94. $this->validate($request, $rules, $messages, $customAttributes);
  95. $fields = array_keys($rules);
  96. if (count($otherFields)) $fields = array_merge($fields, $otherFields);
  97. if (count($hiddenFields)) $fields = array_diff($fields, $hiddenFields);
  98. return $request->only($fields);
  99. }
  100. /**
  101. * 处理异常
  102. * @param \Exception $exception
  103. * @param $msg
  104. * @return \Illuminate\Http\JsonResponse
  105. */
  106. public function exception(\Exception $exception, $msg = '操作失败')
  107. {
  108. Log::error($exception);
  109. if (config('app.env') == 'production') return $this->response->fail($msg, ResponseCodeEnum::SERVICE_OPERATION_ERROR);
  110. return $this->response->fail($exception->getMessage(), ResponseCodeEnum::SERVICE_OPERATION_ERROR);
  111. }
  112. }