BaseAction.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace common\traits;
  3. use Yii;
  4. use yii\base\Model;
  5. use yii\web\Response;
  6. use yii\web\UnprocessableEntityHttpException;
  7. use common\enums\AppEnum;
  8. /**
  9. * trait BaseAction
  10. * @package common\traits
  11. * @author jianyan74 <751393839@qq.com>
  12. */
  13. trait BaseAction
  14. {
  15. /**
  16. * 默认分页
  17. *
  18. * @var int
  19. */
  20. protected $pageSize = 10;
  21. /**
  22. * 可跳转的方法 ID
  23. *
  24. * @var string[]
  25. */
  26. protected $referrerActionIds = ['edit', 'delete', 'destroy'];
  27. /**
  28. * 商户id
  29. *
  30. * @return int
  31. */
  32. protected function getMerchantId()
  33. {
  34. if (in_array(Yii::$app->id, [AppEnum::CONSOLE, AppEnum::BACKEND])) {
  35. return '';
  36. }
  37. return Yii::$app->services->merchant->getId();
  38. }
  39. /**
  40. * 店铺id
  41. *
  42. * @return int
  43. */
  44. protected function getStoreId()
  45. {
  46. if (in_array(Yii::$app->id, [AppEnum::CONSOLE, AppEnum::BACKEND])) {
  47. return '';
  48. }
  49. return Yii::$app->services->store->getId();
  50. }
  51. /**
  52. * @param Model $model
  53. * @throws UnprocessableEntityHttpException
  54. */
  55. protected function error(Model $model)
  56. {
  57. throw new UnprocessableEntityHttpException($this->getError($model));
  58. }
  59. /**
  60. * @param Model $model
  61. * @return string
  62. */
  63. protected function getError(Model $model)
  64. {
  65. return $this->analyErr($model->getFirstErrors());
  66. }
  67. /**
  68. * 解析错误
  69. *
  70. * @param $fistErrors
  71. * @return string
  72. */
  73. protected function analyErr($firstErrors)
  74. {
  75. return Yii::$app->services->base->analysisErr($firstErrors);
  76. }
  77. /**
  78. * @param $model \yii\db\ActiveRecord|Model
  79. * @throws \yii\base\ExitException
  80. */
  81. protected function activeFormValidate($model)
  82. {
  83. if (Yii::$app->request->isAjax && !Yii::$app->request->isPjax) {
  84. if ($model->load(Yii::$app->request->post())) {
  85. Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  86. Yii::$app->response->data = \yii\widgets\ActiveForm::validate($model);
  87. Yii::$app->end();
  88. }
  89. }
  90. }
  91. /**
  92. * 错误提示信息
  93. *
  94. * @param string $msgText 错误内容
  95. * @param string|array|Response $skipUrl 跳转链接
  96. * @param string $msgType 提示类型 [success/error/info/warning]
  97. * @return mixed
  98. */
  99. protected function message($msgText, $skipUrl, $msgType = null)
  100. {
  101. if (!$msgType || !in_array($msgType, ['success', 'error', 'info', 'warning'])) {
  102. $msgType = 'success';
  103. }
  104. Yii::$app->getSession()->setFlash($msgType, $msgText);
  105. return $skipUrl;
  106. }
  107. /**
  108. * 记录上一页地址
  109. *
  110. * @param $actionId
  111. */
  112. protected function setReferrer($actionId)
  113. {
  114. if (in_array($actionId, $this->referrerActionIds)) {
  115. $route = Yii::$app->controller->route;
  116. if (!Yii::$app->session->get($route)) {
  117. Yii::$app->session->set($route, Yii::$app->request->referrer);
  118. }
  119. }
  120. }
  121. /**
  122. * 跳转到之前的页面
  123. *
  124. * @return mixed
  125. */
  126. protected function referrer()
  127. {
  128. $key = Yii::$app->controller->route;
  129. $url = Yii::$app->session->get($key);
  130. Yii::$app->session->remove($key);
  131. if ($url) {
  132. return $this->redirect($url);
  133. }
  134. return $this->redirect(['index']);
  135. }
  136. }
粤ICP备19079148号