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 $response;
  22. protected $device_type;
  23. protected static $CID = 0;
  24. protected static $COMPANY = false;
  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 $p
  55. * @param $re
  56. * @return
  57. */
  58. public function isCan($p = false, $re = false)
  59. {
  60. $admin = login_admin();
  61. if ($admin->isSuperAdmin()) {
  62. return true;
  63. }
  64. $room_p = substr_replace($p, '00', -2);
  65. $ps = [$p, $room_p];
  66. if ($admin->hasAnyPermission($ps)) {
  67. return true;
  68. }
  69. if ($re) return false;
  70. return $this->error('您尚无权限');
  71. }
  72. /**
  73. * 获取验证的数据
  74. * @param $request
  75. * @param array $rules
  76. * @param array $customAttributes
  77. * @param array $otherFields
  78. * @param array $hiddenFields
  79. * @param array $messages
  80. * @return mixed
  81. * @throws \Illuminate\Validation\ValidationException
  82. */
  83. public function validateData($request, array $rules, array $customAttributes = [], array $otherFields = [], array $hiddenFields = [], array $messages = [])
  84. {
  85. $this->validate($request, $rules, $messages, $customAttributes);
  86. $fields = array_keys($rules);
  87. if (count($otherFields)) $fields = array_merge($fields, $otherFields);
  88. if (count($hiddenFields)) $fields = array_diff($fields, $hiddenFields);
  89. return $request->only($fields);
  90. }
  91. /**
  92. * 错误信息
  93. * @param $msg
  94. * @return \Illuminate\Http\JsonResponse
  95. */
  96. public function error($msg)
  97. {
  98. return $this->response->fail($msg, ResponseCodeEnum::SERVICE_OPERATION_ERROR);
  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. }