bztang-admin/app/common/components/BaseController.php

172 lines
4.7 KiB
PHP

<?php
namespace app\common\components;
use app\common\exceptions\AppException;
use app\common\exceptions\ShopException;
use app\common\helpers\Client;
use app\common\helpers\Url;
use app\common\middleware\BasicInformation;
use app\common\middleware\SingleLogin;
use app\common\services\Check;
use app\common\services\PermissionService;
use app\common\services\Session;
use app\common\services\Utils;
use app\common\traits\JsonTrait;
use app\common\traits\MessageTrait;
use app\common\traits\PermissionTrait;
use app\common\traits\TemplateTrait;
use app\platform\modules\system\models\SystemSetting;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use app\frontend\modules\member\services\factory\MemberFactory;
/**
* controller基类
*
* Author:
* Date: 21/02/2017
* Time: 21:20
*/
class BaseController extends Controller
{
use DispatchesJobs, MessageTrait, ValidatesRequests, TemplateTrait, PermissionTrait, JsonTrait;
/**
* controller中执行报错需要回滚的action数组
* @var array
*/
public $transactionActions = [];
public $apiErrMsg = [];
public $apiData = [];
protected $isPublic = false;
public function __construct()
{
// request()->offsetSet('is_shop_pos',1);
Session::factory(\YunShop::app()->uniacid);
}
/**
* 前置action
*/
public function preAction()
{
}
public function callAction($method, $parameters)
{
if (method_exists($this,'preAction')) {
call_user_func_array([$this, 'preAction'],$parameters);
}
// action设置了需要回滚
if (method_exists($this, 'needTransaction') && $this->needTransaction($method)) {
return DB::transaction(function() use ($method,$parameters) {
return parent::callAction($method, $parameters);
});
}
$content = parent::callAction($method, $parameters);
return $content;// TODO: Change the autogenerated stub
}
/**
* url参数验证
*
* @param array $rules
* @param Request|null $request
* @param array $messages
* @param array $customAttributes
*
* @throws AppException
*/
public function validate(array $rules, Request $request = null, array $messages = [], array $customAttributes = [])
{
if (!isset($request)) {
$request = request();
}
$validator = $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes);
if ($validator->fails()) {
throw new AppException($validator->errors()->first());
}
}
/**
* 需要回滚
*
* @param $action
*
* @return bool
*/
private function needTransaction($action)
{
return in_array($action, $this->transactionActions) || in_array('*',
$this->transactionActions) || $this->transactionActions == '*';
}
public function dataIntegrated($data, $flag)
{
if ($this->apiErrMsg) {
return $this->successJson($this->apiErrMsg[0]);
}
if (0 == $data['status']) {
$this->apiErrMsg[] = $data['json'];
return;
}
$this->apiData[$flag] = $data['json'];
}
public function getIsPublic(): bool
{
return $this->isPublic;
}
/**
* @param $type
* @param $mid
* @return bool|\Illuminate\Http\JsonResponse
*/
protected function jumpUrl($type, $mid)
{
if (empty($type) || $type == 'undefined') {
$type = Client::getType();
}
$scope = request()->input('scope', '');
$queryString = ['type' => $type, 'i' => \YunShop::app()->uniacid, 'mid' => $mid, 'scope' => $scope];
if (in_array($type, [MemberFactory::LOGIN_MINI_APP, MemberFactory::LOGIN_DOUYIN, MemberFactory::LOGIN_MINI_APP_FACE])) {
return $this->errorJson('请登录', ['login_status' => 0, 'login_url' => Url::absoluteApi('member.login.index', $queryString)]);
}
if (($scope == 'home' && !$mid) || $scope == 'pass') {
return true;
}
if (in_array($type, [MemberFactory::LOGIN_MOBILE, MemberFactory::LOGIN_APP_YDB, MemberFactory::LOGIN_Native, MemberFactory::LOGIN_APP_ANCHOR, MemberFactory::LOGIN_APP_LSP_WALLET])) {
return $this->errorJson('请登录', ['login_status' => 1, 'login_url' => '', 'type' => $type, 'i' => \YunShop::app()->uniacid, 'mid' => $mid, 'scope' => $scope]);
}
return $this->errorJson('请登录', ['login_status' => 0, 'login_url' => Url::absoluteApi('member.login.index', $queryString)]);
}
}