123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- /*
- * This file is part of the Jiannei/lumen-api-starter.
- *
- * (c) Jiannei <longjian.huang@foxmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace App\Http\Controllers;
- use App\Repositories\Enums\ResponseCodeEnum;
- use App\Repositories\Models\Base\Company;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- use Jiannei\Response\Laravel\Support\Facades\Response;
- use Jiannei\Response\Laravel\Support\Traits\ExceptionTrait;
- use Laravel\Lumen\Routing\Controller as BaseController;
- abstract class Controller extends BaseController
- {
- use ExceptionTrait;
- protected static $CID = 0;
- protected static $COMPANY = false;
- protected $response;
- protected $device_type;
- public function __construct()
- {
- $this->response = new \Jiannei\Response\Laravel\Response();
- $this->device_type = device_type();
- //公司
- self::$CID = request()->header('company-id', request()->get('company_id', 0));
- if (self::$CID) {
- self::$COMPANY = Company::byId(self::$CID);
- }
- }
- /**
- * 是否存在权限
- * @param $arr
- * @return bool|\Illuminate\Http\JsonResponse
- */
- public function isCanAny($arr = [], $re = false)
- {
- $admin = login_admin();
- if ($admin->isSuperAdmin()) {
- return true;
- }
- if ($admin->hasAnyPermission($arr)) {
- return true;
- }
- if ($re) return false;
- return $this->error('您尚无权限');
- }
- /**
- * 错误信息
- * @param $msg
- * @return \Illuminate\Http\JsonResponse
- */
- public function error($msg)
- {
- return $this->response->fail($msg, ResponseCodeEnum::SERVICE_OPERATION_ERROR);
- }
- /**
- * 是否有当前权限
- * @param $p
- * @param $re
- * @return
- */
- public function isCan($p = false, $re = false)
- {
- $admin = login_admin();
- if ($admin->isSuperAdmin()) {
- return true;
- }
- $room_p = substr_replace($p, '00', -2);
- $ps = [$p, $room_p];
- if ($admin->hasAnyPermission($ps)) {
- return true;
- }
- if ($re) return false;
- return $this->error('您尚无权限');
- }
- /**
- * 获取验证的数据
- * @param $request
- * @param array $rules
- * @param array $customAttributes
- * @param array $otherFields
- * @param array $hiddenFields
- * @param array $messages
- * @return mixed
- * @throws \Illuminate\Validation\ValidationException
- */
- public function validateData($request, array $rules, array $customAttributes = [], array $otherFields = [], array $hiddenFields = [], array $messages = [])
- {
- $this->validate($request, $rules, $messages, $customAttributes);
- $fields = array_keys($rules);
- if (count($otherFields)) $fields = array_merge($fields, $otherFields);
- if (count($hiddenFields)) $fields = array_diff($fields, $hiddenFields);
- return $request->only($fields);
- }
- /**
- * 处理异常
- * @param \Exception $exception
- * @param $msg
- * @return \Illuminate\Http\JsonResponse
- */
- public function exception(\Exception $exception, $msg = '操作失败')
- {
- Log::error($exception);
- if (config('app.env') == 'production') return $this->response->fail($msg, ResponseCodeEnum::SERVICE_OPERATION_ERROR);
- return $this->response->fail($exception->getMessage(), ResponseCodeEnum::SERVICE_OPERATION_ERROR);
- }
- }
|