AuthController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace addons\Mch\client\controllers;
  3. use addons\Mch\common\models\MchAdmin;
  4. use backend\controllers\AdminController;
  5. use common\enums\StatusEnum;
  6. use common\helpers\sms\Sms;
  7. use common\models\backend\Admin;
  8. use common\models\mall\Mall;
  9. use common\traits\BaseAction;
  10. use common\traits\BaseControllerTrait;
  11. use common\traits\ErrorTrait;
  12. use Yii;
  13. use yii\filters\AccessControl;
  14. use yii\filters\VerbFilter;
  15. use yii\web\Controller;
  16. /**
  17. * 管理员
  18. * @Author bing
  19. * @DateTime 2021-08-11 17:22:39
  20. * @copyright: Copyright (c) 2020 广东七件事集团
  21. */
  22. class AuthController extends Controller
  23. {
  24. use ErrorTrait;
  25. use BaseControllerTrait;
  26. public $mall = null;
  27. public $mall_id = 0;
  28. public $mch_id = 0;
  29. public $admin = null;
  30. public $enableCsrfValidation = false;
  31. public $layout = false;
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function behaviors()
  36. {
  37. return [
  38. 'access' => [
  39. 'class' => AccessControl::class,
  40. 'rules' => [
  41. [
  42. 'actions' => ['login','logout', 'error', 'captcha','register','register-sms-code'],
  43. 'allow' => true,
  44. ],
  45. ],
  46. ],
  47. ];
  48. }
  49. public function actions()
  50. {
  51. return [
  52. 'error' => [
  53. 'class' => 'yii\web\ErrorAction',
  54. ],
  55. 'captcha' => [
  56. 'class' => 'common\components\CaptchaAction',
  57. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  58. 'maxLength' => 5, // 最大显示个数
  59. 'minLength' => 5, // 最少显示个数
  60. 'padding' => 5, // 间距
  61. 'height' => 32, // 高度
  62. 'width' => 100, // 宽度
  63. 'offset' => 4, // 设置字符偏移量
  64. 'backColor' => 0xffffff, // 背景颜色
  65. 'foreColor' => 0x2F4F4F, // 字体颜色
  66. ]
  67. ];
  68. }
  69. /**
  70. * 登录
  71. * @Author bing
  72. * @DateTime 2021-08-11 18:45:46
  73. * @copyright: Copyright (c) 2020 广东七件事集团
  74. * @return void
  75. */
  76. public function actionLogin()
  77. {
  78. if (!Yii::$app->mch_user->isGuest) {
  79. // 记录行为日志
  80. // Yii::$app->services->actionLog->create('login', '自动登录', false);
  81. $sign = Yii::$app->request->get('sign');
  82. $route = 'mch/client/index/index';
  83. if(!empty($sign)){
  84. $redirect_url = \Yii::$app->urlManager->createAbsoluteUrl([$route,'sign'=>$sign]);
  85. }else{
  86. $redirect_url = \Yii::$app->urlManager->createAbsoluteUrl($route);
  87. }
  88. return $this->redirect($redirect_url);
  89. }
  90. if(Yii::$app->request->isPost){
  91. $post = Yii::$app->request->post();
  92. $res = self::checkRequireParam($post,['captcha','username','admin_type','sign','is_remember']);
  93. if($res === false) return $this->error(self::getStaticError());
  94. if (YII_ENV != 'dev'){
  95. if (!$this->createAction('captcha')->validate($post['captcha'], false)) return $this->error('验证码错误');
  96. }
  97. $mall = Mall::findOne(['mall_sign'=>$post['sign']]);
  98. if(empty($mall)) $this->error('门店商城不存在');
  99. $post['mall_id'] = $mall['id'];
  100. $data = MchAdmin::login($post);
  101. if($data === false) return $this->error(Admin::getStaticError());
  102. return $this->success('登录成功',$data);
  103. }
  104. $system = \Yii::$app->services->setting->platform->get('system',true);
  105. return $this->render($this->action->id,compact('system'));
  106. }
  107. /**
  108. * 退出登录
  109. * @Author bing
  110. * @DateTime 2021-08-11 18:45:57
  111. * @copyright: Copyright (c) 2020 广东七件事集团
  112. * @return void
  113. */
  114. public function actionLogout()
  115. {
  116. $mall_id = Yii::$app->mch_user->identity->mall_id;
  117. $mall = Mall::findOne(['id'=>$mall_id]);
  118. Yii::$app->mch_user->logout();
  119. Yii::$app->session->set('mall',null);
  120. $redirect_url = \Yii::$app->urlManager->createAbsoluteUrl(['mch/client/auth/login','sign'=>$mall['mall_sign'] ]);
  121. return $this->success('操作成功',['url'=>$redirect_url]);
  122. }
  123. }