# @Date: 2022-04-15 20:22:03 # @Email: fm453@lukegzs.com # @Last modified by: fm453 # @Last modified time: 2024-08-09T09:20:31+08:00 # @Copyright: www.hiluker.cn //DataApi站点用户专入口 namespace api\controllers\client\v1; use Yii; use yii\helpers\Url; use common\models\User; class UserController extends Common { public function beforeAction($action) { if (!$this->pid) { $this->result('您正使用本系统内部接口,禁止非法链接使用!'); } return parent::beforeAction($action); } public function actionIndex() { $apis = [ 'login' => '登陆' ]; $this->result('您正使用DACMS系统用户管理接口!', $apis, 200); } public function actionLogin() { $pid = $this->pid; $post = $this->postdata; $search = $post['search'] ?? NULL; $username = $search['username'] ?? NULL; $username = $post['username'] ?? $username; $passwd = $search['passwd'] ?? NULL; $passwd = $post['passwd'] ?? $passwd; $return = []; if (!$username && !$passwd) { $this->result('登陆参数错误!', NULL, 400); } if (!$username) { $this->result('请输入您的账号!', NULL, 400); } if (!$passwd) { $this->result('请输入您的密码!', NULL, 400); } $username = trim(htmlspecialchars_decode($username)); $passwd = trim(htmlspecialchars_decode($passwd)); if (!$username) { $this->result('输入错误,请检查您的账号!', NULL, 400); } if (!$passwd) { $this->result('输入有误,请检查您的密码!', NULL, 400); } $model = new User(); $user = $model->findOne(['mobile' => $username]); if (!$user) { $this->result('查无此人!', NULL, 404); } if ($user->status != User::STATUS_ACTIVE) { $this->result('账户异常,禁止登陆!', NULL, 403); } //安全校验,登陆密码 $isPwdRight = $user->validatePassword($passwd); if (!$isPwdRight) { $this->result('密码错误,禁止登陆!', NULL, 400); } //用户主信息校验结束 $_user = $this->userLogin($user, TRUE); $this->result('登陆成功!', ['user' => $_user], 200); } private function userLogin($user, $hasExt = FALSE) { // 使用指定用户名获取用户身份实例 $identity = $user; // 登录用户,有效期30d $duration = 3600 * 24 * 30; $_login = Yii::$app->user->login($identity, $duration); if (!$_login) { $this->result('登陆失败!', NULL, 400); } //登陆成功,返回用户信息 $_user = ['id' => $user->id, 'username' => $user->username, 'mobile' => $user->mobile, 'email' => $user->email, 'avatar' => Url::to($user->avatar)]; $_user['employee_id'] = $this->employee_id; //生成token(用户数据,是否强制更新,过期时间 $isLong = $this->postdata['isLong'] ?? FALSE; $expire = $isLong ? '+1 Month' : FALSE; $_user = $this->userToken($_user, $refresh = TRUE, $expire); return $_user; } }