123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?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 Carbon\Carbon;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- use Jiannei\Response\Laravel\Support\Traits\ExceptionTrait;
- use Laravel\Lumen\Routing\Controller as BaseController;
- abstract class Controller extends BaseController
- {
- use ExceptionTrait;
- protected $response;
- protected $device_type;
- protected static $CID = 0;
- protected static $COMPANY = false;
- public function __construct()
- {
- $this->response = new \Jiannei\Response\Laravel\Response();
- $this->device_type = device_type();
- }
- /**
- * 是否存在权限
- * @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 $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 $msg
- * @return \Illuminate\Http\JsonResponse
- */
- public function error($msg)
- {
- return $this->response->fail($msg, ResponseCodeEnum::SERVICE_OPERATION_ERROR);
- }
- /**
- * 处理异常
- * @param \Exception $exception
- * @param $msg
- * @return \Illuminate\Http\JsonResponse
- */
- public function exception(\Exception $exception, $msg = '操作失败')
- {
- Log::error($exception);
- if (!config('app.debug')) return $this->response->fail($msg, ResponseCodeEnum::SERVICE_OPERATION_ERROR);
- return $this->response->fail($exception->getMessage(), ResponseCodeEnum::SERVICE_OPERATION_ERROR);
- }
- /**
- * 是否重复提交
- * @param $key
- * @param $sec
- * @return bool
- */
- public function isRepeat(string $tag, $key, $sec = 10, $isMsg = true)
- {
- if (is_array($key)) $key = md5(serialize($key));
- $key = "{$tag}:{$key}";
- if (Cache::has($key)) {
- if ($isMsg) {
- abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '操作太频繁了');
- }
- return false;
- }
- Cache::put($key, 1, Carbon::now()->addSeconds($sec));
- return true;
- }
- }
|