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 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;
- const PAGE_NUM = 15;
- public static $TERM_ID = 0;
- public static $TERM = [];
- public $response = null;
- public function __construct()
- {
- // self::$TERM_ID = request()->header('term-id', request()->get('term_id', 0));
- // if (self::$TERM_ID) {
- // self::$TERM = Term::byId(self::$TERM_ID);
- // }
- $this->response = new \Jiannei\Response\Laravel\Response();
- }
- public function notFound()
- {
- Response::fail('The resource could not be found.', ResponseCodeEnum::SERVICE_NOT_FIND_DATA_ERROR);
- }
- public function errorStore($exception)
- {
- Log::error($exception);
- if (config('app.env') == 'production') {
- Response::fail(T('An unknown error was encountered.'), ResponseCodeEnum::SYSTEM_ERROR);
- } else {
- Response::fail($exception->getMessage(), ResponseCodeEnum::SYSTEM_ERROR);
- }
- }
- public function errorFail()
- {
- return Response::fail(T('The operation failure.'), ResponseCodeEnum::SERVICE_UPDATE_DATA_ERROR);
- }
- public function error($msg)
- {
- return Response::fail($msg, ResponseCodeEnum::SERVICE_UPDATE_DATA_ERROR);
- }
- /**
- * 是否存在权限
- * @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 \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);
- }
- }
|