Controller.php 3.8 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 Carbon\Carbon;
  13. use Illuminate\Support\Facades\Cache;
  14. use Illuminate\Support\Facades\Log;
  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. protected $response;
  21. protected $device_type;
  22. protected static $CID = 0;
  23. protected static $COMPANY = false;
  24. public function __construct()
  25. {
  26. $this->response = new \Jiannei\Response\Laravel\Response();
  27. $this->device_type = device_type();
  28. }
  29. /**
  30. * 是否存在权限
  31. * @param $arr
  32. * @return bool|\Illuminate\Http\JsonResponse
  33. */
  34. public function isCanAny($arr = [], $re = false)
  35. {
  36. $admin = login_admin();
  37. if ($admin->isSuperAdmin()) {
  38. return true;
  39. }
  40. if ($admin->hasAnyPermission($arr)) {
  41. return true;
  42. }
  43. if ($re) return false;
  44. return $this->error('您尚无权限');
  45. }
  46. /**
  47. * 是否有当前权限
  48. * @param $p
  49. * @param $re
  50. * @return
  51. */
  52. public function isCan($p = false, $re = false)
  53. {
  54. $admin = login_admin();
  55. if ($admin->isSuperAdmin()) {
  56. return true;
  57. }
  58. $room_p = substr_replace($p, '00', -2);
  59. $ps = [$p, $room_p];
  60. if ($admin->hasAnyPermission($ps)) {
  61. return true;
  62. }
  63. if ($re) return false;
  64. return $this->error('您尚无权限');
  65. }
  66. /**
  67. * 获取验证的数据
  68. * @param $request
  69. * @param array $rules
  70. * @param array $customAttributes
  71. * @param array $otherFields
  72. * @param array $hiddenFields
  73. * @param array $messages
  74. * @return mixed
  75. * @throws \Illuminate\Validation\ValidationException
  76. */
  77. public function validateData($request, array $rules, array $customAttributes = [], array $otherFields = [], array $hiddenFields = [], array $messages = [])
  78. {
  79. $this->validate($request, $rules, $messages, $customAttributes);
  80. $fields = array_keys($rules);
  81. if (count($otherFields)) $fields = array_merge($fields, $otherFields);
  82. if (count($hiddenFields)) $fields = array_diff($fields, $hiddenFields);
  83. return $request->only($fields);
  84. }
  85. /**
  86. * 错误信息
  87. * @param $msg
  88. * @return \Illuminate\Http\JsonResponse
  89. */
  90. public function error($msg)
  91. {
  92. return $this->response->fail($msg, ResponseCodeEnum::SERVICE_OPERATION_ERROR);
  93. }
  94. /**
  95. * 处理异常
  96. * @param \Exception $exception
  97. * @param $msg
  98. * @return \Illuminate\Http\JsonResponse
  99. */
  100. public function exception(\Exception $exception, $msg = '操作失败')
  101. {
  102. Log::error($exception);
  103. if (!config('app.debug')) return $this->response->fail($msg, ResponseCodeEnum::SERVICE_OPERATION_ERROR);
  104. return $this->response->fail($exception->getMessage(), ResponseCodeEnum::SERVICE_OPERATION_ERROR);
  105. }
  106. /**
  107. * 是否重复提交
  108. * @param $key
  109. * @param $sec
  110. * @return bool
  111. */
  112. public function isRepeat(string $tag, $key, $sec = 10, $isMsg = true)
  113. {
  114. if (is_array($key)) $key = md5(serialize($key));
  115. $key = "{$tag}:{$key}";
  116. if (Cache::has($key)) {
  117. if ($isMsg) {
  118. abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '操作太频繁了');
  119. }
  120. return false;
  121. }
  122. Cache::put($key, 1, Carbon::now()->addSeconds($sec));
  123. return true;
  124. }
  125. }