BaseController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\common\components;
  3. use app\common\exceptions\AppException;
  4. use app\common\exceptions\ShopException;
  5. use app\common\helpers\Client;
  6. use app\common\helpers\Url;
  7. use app\common\middleware\BasicInformation;
  8. use app\common\middleware\SingleLogin;
  9. use app\common\services\Check;
  10. use app\common\services\PermissionService;
  11. use app\common\services\Session;
  12. use app\common\services\Utils;
  13. use app\common\traits\JsonTrait;
  14. use app\common\traits\MessageTrait;
  15. use app\common\traits\PermissionTrait;
  16. use app\common\traits\TemplateTrait;
  17. use app\platform\modules\system\models\SystemSetting;
  18. use Illuminate\Foundation\Bus\DispatchesJobs;
  19. use Illuminate\Foundation\Validation\ValidatesRequests;
  20. use Illuminate\Http\Request;
  21. use Illuminate\Routing\Controller;
  22. use Illuminate\Support\Facades\Cookie;
  23. use Illuminate\Support\Facades\DB;
  24. use Illuminate\Support\Facades\Validator;
  25. use app\frontend\modules\member\services\factory\MemberFactory;
  26. /**
  27. * controller基类
  28. *
  29. * Author: 芸众商城 www.yunzshop.com
  30. * Date: 21/02/2017
  31. * Time: 21:20
  32. */
  33. class BaseController extends Controller
  34. {
  35. use DispatchesJobs, MessageTrait, ValidatesRequests, TemplateTrait, PermissionTrait, JsonTrait;
  36. /**
  37. * controller中执行报错需要回滚的action数组
  38. * @var array
  39. */
  40. public $transactionActions = [];
  41. public $apiErrMsg = [];
  42. public $apiData = [];
  43. protected $isPublic = false;
  44. public function __construct()
  45. {
  46. // request()->offsetSet('is_shop_pos',1);
  47. Session::factory(\YunShop::app()->uniacid);
  48. }
  49. /**
  50. * 前置action
  51. */
  52. public function preAction()
  53. {
  54. }
  55. public function callAction($method, $parameters)
  56. {
  57. if (method_exists($this,'preAction')) {
  58. call_user_func_array([$this, 'preAction'],$parameters);
  59. }
  60. // action设置了需要回滚
  61. if (method_exists($this, 'needTransaction') && $this->needTransaction($method)) {
  62. return DB::transaction(function() use ($method,$parameters) {
  63. return parent::callAction($method, $parameters);
  64. });
  65. }
  66. $content = parent::callAction($method, $parameters);
  67. return $content;// TODO: Change the autogenerated stub
  68. }
  69. /**
  70. * url参数验证
  71. *
  72. * @param array $rules
  73. * @param Request|null $request
  74. * @param array $messages
  75. * @param array $customAttributes
  76. *
  77. * @throws AppException
  78. */
  79. public function validate(array $rules, Request $request = null, array $messages = [], array $customAttributes = [])
  80. {
  81. if (!isset($request)) {
  82. $request = request();
  83. }
  84. $validator = $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes);
  85. if ($validator->fails()) {
  86. throw new AppException($validator->errors()->first());
  87. }
  88. }
  89. /**
  90. * 需要回滚
  91. *
  92. * @param $action
  93. *
  94. * @return bool
  95. */
  96. private function needTransaction($action)
  97. {
  98. return in_array($action, $this->transactionActions) || in_array('*',
  99. $this->transactionActions) || $this->transactionActions == '*';
  100. }
  101. public function dataIntegrated($data, $flag)
  102. {
  103. if ($this->apiErrMsg) {
  104. return $this->successJson($this->apiErrMsg[0]);
  105. }
  106. if (0 == $data['status']) {
  107. $this->apiErrMsg[] = $data['json'];
  108. return;
  109. }
  110. $this->apiData[$flag] = $data['json'];
  111. }
  112. public function getIsPublic(): bool
  113. {
  114. return $this->isPublic;
  115. }
  116. /**
  117. * @param $type
  118. * @param $mid
  119. * @return bool|\Illuminate\Http\JsonResponse
  120. */
  121. protected function jumpUrl($type, $mid)
  122. {
  123. if (empty($type) || $type == 'undefined') {
  124. $type = Client::getType();
  125. }
  126. $scope = request()->input('scope', '');
  127. $queryString = ['type' => $type, 'i' => \YunShop::app()->uniacid, 'mid' => $mid, 'scope' => $scope];
  128. if (in_array($type, [MemberFactory::LOGIN_MINI_APP, MemberFactory::LOGIN_DOUYIN, MemberFactory::LOGIN_MINI_APP_FACE])) {
  129. return $this->errorJson('请登录', ['login_status' => 0, 'login_url' => Url::absoluteApi('member.login.index', $queryString)]);
  130. }
  131. if (($scope == 'home' && !$mid) || $scope == 'pass') {
  132. return true;
  133. }
  134. if (in_array($type, [MemberFactory::LOGIN_MOBILE, MemberFactory::LOGIN_APP_YDB, MemberFactory::LOGIN_Native, MemberFactory::LOGIN_APP_ANCHOR, MemberFactory::LOGIN_APP_LSP_WALLET])) {
  135. return $this->errorJson('请登录', ['login_status' => 1, 'login_url' => '', 'type' => $type, 'i' => \YunShop::app()->uniacid, 'mid' => $mid, 'scope' => $scope]);
  136. }
  137. return $this->errorJson('请登录', ['login_status' => 0, 'login_url' => Url::absoluteApi('member.login.index', $queryString)]);
  138. }
  139. }