LoginForm.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: NODELOG
  5. * DateTime: 2017/3/8 17:46
  6. * Description:
  7. */
  8. namespace api\modules\v1\models;
  9. use api\common\models\User;
  10. use Yii;
  11. use yii\base\Model;
  12. use yii\web\ServerErrorHttpException;
  13. class LoginForm extends Model
  14. {
  15. //user
  16. public $id;
  17. public $companyname;
  18. public $mobile;
  19. public $email;
  20. public $type;
  21. //profile
  22. public $address;
  23. public $contact;
  24. public $logo;
  25. public $rememberMe = true;
  26. /* @var $_user \common\modules\user\models\User */
  27. private $_user;
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['id'], 'required'],
  35. [['address', 'companyname', 'contact', 'email', 'logo'], 'string'],
  36. [['id', 'type'], 'integer'],
  37. [['mobile'], 'safe'],
  38. ];
  39. }
  40. public function attributeLabels()
  41. {
  42. return [
  43. 'id' => 'ID',
  44. 'companyname' => '公司名称',
  45. 'contact' => '联系人',
  46. 'address' => '地址',
  47. 'email' => '邮箱',
  48. 'mobile' => '手机号',
  49. 'logo' => 'LOGO',
  50. 'type' => '类型',
  51. ];
  52. }
  53. /**
  54. * Logs in a user using the provided openid and password.
  55. *
  56. * @return bool whether the user is logged in successfully
  57. * @throws \yii\base\InvalidConfigException
  58. * @author nodelog
  59. */
  60. public function login()
  61. {
  62. if ($this->validate()) {
  63. return Yii::$app->user->login($this->getUser(), 3600 * 2);//2小时
  64. } else {
  65. return false;
  66. }
  67. }
  68. /**
  69. * @return User|array|\common\modules\user\models\User|\yii\db\ActiveRecord|null
  70. * @throws ServerErrorHttpException
  71. * @throws \yii\base\InvalidConfigException
  72. * @author nodelog
  73. */
  74. protected function getUser()
  75. {
  76. //查询用户
  77. if ($this->_user === null) {
  78. $this->_user = User::find()->where(['jucai_id' => $this->id, 'type' => $this->type])->one();
  79. if ($this->_user && $this->_user->getIsBlocked()) {
  80. throw new ServerErrorHttpException('用户已被禁用');
  81. }
  82. }
  83. //注册用户
  84. if ($this->_user === null) {
  85. /** @var User $model */
  86. $model = \Yii::createObject([
  87. 'class' => User::className(),
  88. 'scenario' => 'create',
  89. ]);
  90. $model->jucai_id = $this->id;
  91. $model->nickname = $this->companyname;
  92. $model->tel = $this->mobile;
  93. $model->email = $this->email;
  94. $model->type = $this->type;
  95. $model->username = Yii::$app->security->generateRandomString();
  96. if ($model->create()) {
  97. $this->_user = $model;
  98. } else {
  99. throw new ServerErrorHttpException(current($model->getErrors())[0]);
  100. }
  101. } else {
  102. $this->_user->updateAttributes(['nickname' => $this->companyname, 'tel' => $this->mobile, 'email' => $this->email]);
  103. }
  104. if ($this->_user != null) {
  105. $this->_user->profile->updateAttributes([
  106. 'avatar' => $this->logo,
  107. 'address' => $this->address,
  108. 'contact' => $this->contact,
  109. ]);
  110. } else {
  111. throw new ServerErrorHttpException('登录失败');
  112. }
  113. return $this->_user;
  114. }
  115. }