LoginForm.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace merchant\forms;
  3. use Yii;
  4. use common\helpers\StringHelper;
  5. use common\models\member\Member;
  6. use common\enums\AppEnum;
  7. use common\enums\AuditStatusEnum;
  8. use common\models\merchant\Merchant;
  9. use common\enums\MemberTypeEnum;
  10. use common\helpers\ArrayHelper;
  11. use common\enums\SubscriptionActionEnum;
  12. /**
  13. * Class LoginForm
  14. * @package merchant\forms
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class LoginForm extends \common\forms\LoginForm
  18. {
  19. public $verifyCode;
  20. /**
  21. * 默认登录失败3次显示验证码
  22. *
  23. * @var int
  24. */
  25. public $attempts = 3;
  26. /**
  27. * @var bool
  28. */
  29. public $rememberMe = true;
  30. /**
  31. * @inheritdoc
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['username', 'password'], 'required'],
  37. ['rememberMe', 'boolean'],
  38. ['password', 'validatePassword'],
  39. ['password', 'validateIp'],
  40. ['password', 'validateMerchant'],
  41. ['verifyCode', 'captcha', 'on' => 'captchaRequired'],
  42. ];
  43. }
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'username' => '用户名',
  48. 'rememberMe' => '记住我',
  49. 'password' => '密码',
  50. 'verifyCode' => '验证码',
  51. ];
  52. }
  53. /**
  54. * 验证ip地址是否正确
  55. *
  56. * @param $attribute
  57. * @throws \yii\base\InvalidConfigException
  58. */
  59. public function validateIp($attribute)
  60. {
  61. $allowIp = Yii::$app->services->config->backendConfig('sys_allow_ip');
  62. if (!empty($allowIp)) {
  63. $ipList = StringHelper::parseAttr($allowIp);
  64. if (!ArrayHelper::ipInArray(Yii::$app->request->userIP, $ipList)) {
  65. // 记录行为日志
  66. Yii::$app->services->actionLog->create('login', '限制IP登录', 0, [], false);
  67. $this->addError($attribute, '登录失败,请联系管理员');
  68. }
  69. }
  70. }
  71. /**
  72. * @param $attribute
  73. */
  74. public function validateMerchant($attribute)
  75. {
  76. /** @var Member $user */
  77. if ($user = $this->getUser()) {
  78. if (!($merchant = Merchant::findOne($user->merchant_id))) {
  79. $this->addError($attribute, '无法登陆请联系管理员');
  80. return false;
  81. }
  82. if ($merchant->audit_status == AuditStatusEnum::DISABLED) {
  83. $this->addError($attribute, '商户正在审核中,请等待');
  84. return false;
  85. }
  86. if (!Yii::$app->services->rbacAuthAssignment->findRoleIdSByUserIdAndAppId($user->id, AppEnum::MERCHANT)) {
  87. $this->addError($attribute, '未授权, 请联系管理员授权');
  88. return false;
  89. }
  90. }
  91. }
  92. /**
  93. * @return mixed|null|static
  94. */
  95. public function getUser()
  96. {
  97. if ($this->_user === null) {
  98. $this->_user = Yii::$app->services->member->findByCondition([
  99. 'username' => $this->username,
  100. 'type' => MemberTypeEnum::MERCHANT,
  101. ]);
  102. }
  103. return $this->_user;
  104. }
  105. /**
  106. * 验证码显示判断
  107. */
  108. public function loginCaptchaRequired()
  109. {
  110. if (Yii::$app->session->get('loginMerchantCaptchaRequired') >= $this->attempts) {
  111. $this->setScenario("captchaRequired");
  112. // 提醒
  113. Yii::$app->services->notify->sendRemind(0, SubscriptionActionEnum::ABNORMAL_LOGIN, 0, [
  114. 'username' => $this->username,
  115. 'attempts' => Yii::$app->session->get('loginMerchantCaptchaRequired')
  116. ]);
  117. }
  118. }
  119. /**
  120. * 登录
  121. *
  122. * @return bool
  123. * @throws \yii\base\InvalidConfigException
  124. */
  125. public function login()
  126. {
  127. if ($this->validate() && Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0)) {
  128. Yii::$app->session->remove('loginMerchantCaptchaRequired');
  129. return true;
  130. }
  131. $counter = Yii::$app->session->get('loginMerchantCaptchaRequired') + 1;
  132. Yii::$app->session->set('loginMerchantCaptchaRequired', $counter);
  133. return false;
  134. }
  135. }
粤ICP备19079148号