Commit a4d4a964 authored by shajiaiming's avatar shajiaiming

user login api

parent 67534fea
......@@ -20,30 +20,18 @@ class UserController extends BaseController
{
$model = new LoginForm();
$model->setScenario(LoginForm::SCENARIOS_LOGIN);
#var_dump(Yii::$app->request->post());exit;
$model->load(Yii::$app->request->post(),'');
if ($model->login()) {
$response = [
'success' => true,
'msg' => 'Login Successful'
'access_token' => $model->login(),
];
} else {
$error = implode(", ", \yii\helpers\ArrayHelper::getColumn($model->errors, 0, false)); // Model's Errors string
$response = [
'success' => false,
'msg' => $error
];
}
return $response;
/* if ($model->login()) {
var_dump([]);exit;
return [
'access_token' => $model->login(),
];
} else {
var_dump($model->getFirstErrors(),[]);exit;
return $model->getFirstErrors();
}*/
}
/**
......
......@@ -13,6 +13,7 @@ use yii\web\IdentityInterface;
* @property string $uid
* @property string $username
* @property string $password
* @property string $access_token
* @property string $reg_time
* @property string $reg_ip
* @property string $last_login_time
......@@ -157,4 +158,14 @@ class Admin extends \common\modelsgii\Admin implements IdentityInterface
$this->password = null;
}
/**
* Generate accessToken string
* @return string
* @throws \yii\base\Exception
*/
public function generateAccessToken()
{
$this->access_token = Yii::$app->security->generateRandomString();
return $this->access_token;
}
}
......@@ -64,10 +64,13 @@ class LoginForm extends Model
*/
public function login()
{
#echo 'aaaaa';exit;
#var_dump($this->validate());exit;
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
if ($this->getUser()) {
$access_token = $this->_user->generateAccessToken();
$this->_user->access_token = $access_token ;
$this->_user->save();
return $access_token;
}
}
return false;
......
......@@ -10,6 +10,7 @@ use yii\helpers\HtmlPurifier;
* @property string $uid
* @property string $username
* @property string $password
* @property string $access_token
* @property string $salt
* @property string $reg_time
* @property string $reg_ip
......@@ -47,6 +48,7 @@ class Admin extends \common\core\BaseActiveRecord
[['username'], 'string', 'max' => 32],
[['password'], 'string', 'min' => 6, 'max' => 60],
[['salt'], 'string', 'max' => 32],
['access_token', 'safe']
];
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment