BaseAction.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace common\traits;
  3. use Yii;
  4. use yii\base\Model;
  5. use common\components\Init;
  6. use common\enums\AppEnum;
  7. /**
  8. * trait BaseAction
  9. * @package common\traits
  10. * @author qimall
  11. */
  12. trait BaseAction
  13. {
  14. protected $merchant_id;
  15. /**
  16. * 默认分页
  17. *
  18. * @var int
  19. */
  20. protected $pageSize = 10;
  21. /**
  22. * 商户id
  23. *
  24. * @return int
  25. */
  26. public function getMerchantId()
  27. {
  28. if (!$this->merchant_id) {
  29. $this->merchant_id = Yii::$app->services->merchant->getId();
  30. }
  31. if(in_array(Yii::$app->id, [AppEnum::CONSOLE, AppEnum::BACKEND])){
  32. return '';
  33. }
  34. return $this->merchant_id;
  35. }
  36. /**
  37. * @param Model $model
  38. * @return string
  39. */
  40. public function getError(Model $model)
  41. {
  42. return $this->analyErr($model->getFirstErrors());
  43. }
  44. /**
  45. * 重载配置
  46. *
  47. * @param $merchant_id
  48. * @throws \yii\base\InvalidConfigException
  49. * @throws \yii\web\UnauthorizedHttpException
  50. */
  51. public function afreshLoad($merchant_id)
  52. {
  53. // 微信配置 具体可参考EasyWechat
  54. Yii::$app->params['wechatConfig'] = [];
  55. // 微信支付配置 具体可参考EasyWechat
  56. Yii::$app->params['wechatPaymentConfig'] = [];
  57. // 微信小程序配置 具体可参考EasyWechat
  58. Yii::$app->params['wechatMiniProgramConfig'] = [];
  59. // 微信开放平台第三方平台配置 具体可参考EasyWechat
  60. Yii::$app->params['wechatOpenPlatformConfig'] = [];
  61. // 微信企业微信配置 具体可参考EasyWechat
  62. Yii::$app->params['wechatWorkConfig'] = [];
  63. // 微信企业微信开放平台 具体可参考EasyWechat
  64. Yii::$app->params['wechatOpenWorkConfig'] = [];
  65. (new Init())->afreshLoad($merchant_id);
  66. }
  67. /**
  68. * 解析错误
  69. *
  70. * @param $fistErrors
  71. * @return string
  72. */
  73. protected function analyErr($firstErrors)
  74. {
  75. return Yii::$app->debris->analyErr($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 $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, ['edit', 'delete', 'destroy'])) {
  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. }