121 lines
3.8 KiB
PHP
121 lines
3.8 KiB
PHP
<?php
|
||
namespace app\models\common;
|
||
use think\response\Redirect;
|
||
use Yii;
|
||
use yii\base\Model;
|
||
use yii\db\ActiveRecord;
|
||
class Users extends ActiveRecord{
|
||
public $username;
|
||
public $password;
|
||
public $rememberMe;
|
||
//重命名表名 %为前缀名
|
||
public static function tableName(){
|
||
return '{{%users}}';
|
||
}
|
||
|
||
public function rules(){
|
||
return [
|
||
[['username','password'],'required','message'=>'必填项'],
|
||
['rememberMe','boolean'],
|
||
];
|
||
}
|
||
|
||
public function attributeLabels(){
|
||
return [
|
||
'uid' => 'ID',//id为数据表中的字段名,ID 为表单显示的描述
|
||
'username' => '用户名',
|
||
'password' => '密码',
|
||
'salt' => '这是用户密码加盐值',
|
||
'lastip' => 'lastip',
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @return bool
|
||
* 设置登录方法
|
||
*/
|
||
public function login(){
|
||
$user=Users::find()->where(['username'=>$this->username])->asarray()->one();//判断用户名是否正确
|
||
if(is_null($user)) {
|
||
Yii::$app->session->setFlash('错误提示','用户名输入有误');
|
||
// echo $this->addError('username','用户名输入有误');
|
||
return false;
|
||
}else{
|
||
//判断密码是否正确
|
||
if(getPassword($this->password)==$user['password']){
|
||
$session = \Yii::$app->session;
|
||
if(!$session->get('we7_user')){
|
||
$session->set('we7_user',$user);
|
||
}
|
||
return true;
|
||
}else{
|
||
// Yii::$app->session->setFlash('错误提示','密码输入有误');
|
||
//// echo $this->addError('password','密码输入有误');
|
||
echo "<script>alert('用户密码错误');window.history.back(-1);</script>";die;
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
|
||
//获取所有用户的信息
|
||
public function getAlls(){
|
||
$res=Users::find()->asarray()->all(); //此方法返回所有数据;
|
||
return $res;
|
||
}
|
||
|
||
//根据用户id返回用户的数据
|
||
public function getById($id){
|
||
$re=Users::find()->where(['uid'=>$id])->one();
|
||
return $re;
|
||
}
|
||
|
||
//根据用户名返回用户的数据
|
||
public function getByName($userName){
|
||
$re=Users::find()->where(['username'=>$userName]);
|
||
// 输出SQL语句
|
||
//echo $re->createCommand()->getRawSql();die;
|
||
$users = $re->asarray()->one();
|
||
// var_dump($users);die;
|
||
return $users;
|
||
}
|
||
//根据用户名返回用户的数据
|
||
public function getUser($userName,$passWord){
|
||
$passWord=getPassword($passWord);
|
||
$re=Users::find()->where(['username'=>$userName,'password'=>$passWord]);
|
||
// 输出SQL语句
|
||
//echo $re->createCommand()->getRawSql();die;
|
||
$users = $re->asarray()->one();
|
||
// var_dump($users);die;
|
||
return $users;
|
||
}
|
||
|
||
public function userSave($id,$userName,$passWord,$portrait,$phone,$type=2){
|
||
$data=array(
|
||
'userName'=>$userName,
|
||
'phone'=>$phone,
|
||
'portrait'=>$portrait,
|
||
'type'=>$type
|
||
);
|
||
if($passWord){
|
||
$salt=randoms(8);
|
||
$auth_key=Yii::$app->params['authkey'];
|
||
$data['salt']=$salt;
|
||
$data['passWord']=checkPassWord($salt,$auth_key,$passWord);
|
||
}
|
||
$data=filter_array($data);
|
||
if($id){
|
||
if($type==0){
|
||
$re=(new \yii\db\Query())->from('{{%ybwm_admin}}')->where(['id'=>$id])->one();
|
||
$res=(new \yii\db\Query())->from('{{%ybwm_admin}}')
|
||
->where(['id'=>$id])->one();
|
||
}else{
|
||
$re=(new \yii\db\Query())->from('{{%ybwm_users}}')->where(['id'=>$id])->one();
|
||
|
||
}
|
||
|
||
}else{
|
||
|
||
|
||
}
|
||
}
|
||
} |