User.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/2/26/026
  6. * Time: 11:13
  7. */
  8. namespace common\models;
  9. use Yii;
  10. //use yii\db\ActiveRecord;
  11. //use yii\behaviors\TimestampBehavior;
  12. //use yii\captcha\Captcha;
  13. class User extends Common
  14. {
  15. public $order = 'status ASC';
  16. public $repassword; //确认密码
  17. public $verifycode;
  18. // public function behaviors()
  19. // {
  20. // return [
  21. // [
  22. // 'class' => TimestampBehavior::className(),
  23. // 'attributes' => [
  24. // # 创建之前
  25. // ActiveRecord::EVENT_BEFORE_INSERT => ['create_at', 'update_at'],
  26. // # 修改之前
  27. // ActiveRecord::EVENT_BEFORE_UPDATE => ['update_at']
  28. // ],
  29. // #设置默认值
  30. // 'value' => $_SERVER['REQUEST_TIME']
  31. // ]
  32. // ];
  33. // }
  34. public function rules()
  35. {
  36. return [
  37. ['user','required','message'=>'不能为空'],
  38. ['password','required','message'=>'密码不能为空'],
  39. ['password','match','pattern'=>Yii::$app->params['match']['password'],'message'=>'请输入正确的密码'],
  40. ['repassword', 'compare', 'compareAttribute' => 'password','on'=>['Signup','savepwd']],
  41. ['user', 'unique', 'targetClass' => 'common\models\User','on'=>['Signup']],
  42. ['repassword','required','message'=>'确认密码不能为空','on'=>['Signup']],
  43. ['name','required','message'=>'姓名不能为空','on'=>['Signup']],
  44. ['verifycode', 'captcha','captchaAction'=>'/public/captcha','on'=>['login'],'message'=>'请输入正确的验证码!'],
  45. ['name','string','max'=>6],
  46. ['status','in','range'=>[1,2]],
  47. ];
  48. }
  49. public function Authenticator($input)
  50. {
  51. $this->load($input,'');
  52. if(!$this->validate()) return $this->errors;
  53. return $this;
  54. }
  55. public function findById($id)
  56. {
  57. return self::findOne($id);
  58. }
  59. public function findUserName()
  60. {
  61. $query = self::find();
  62. $query = $this->Setonditions($query);
  63. return $query->one();
  64. }
  65. public function getList($input,$field = null )
  66. {
  67. $query = self::find();
  68. $query->select($field);
  69. $query = $this->Setonditions($query);
  70. $query->orderBy($this->order);
  71. if(Yii::$app->session['user_info']['user'] != 'admin'){
  72. $query->andFilterWhere(['not',"user = 'admin'"]);
  73. }
  74. if(!empty($input))
  75. {
  76. $query->offset = ($input['page'] -1) * $input['limit'];
  77. $query->limit = $input['limit'];
  78. }
  79. return $query->orderBy(['status'=>SORT_ASC])->asArray()->all();
  80. }
  81. public function Setonditions($query)
  82. {
  83. $query->andFilterWhere(['user'=>$this->user]);
  84. $query->andFilterWhere(['uid'=>$this->uid]);
  85. return $query;
  86. }
  87. /*
  88. * 返回数据条数
  89. * */
  90. public function Total()
  91. {
  92. $query = self::find();
  93. $query = $this->Setonditions($query);
  94. return $query->count();
  95. }
  96. public function setPassword($pwd)
  97. {
  98. return md5(md5($pwd));
  99. }
  100. public function getNews()
  101. {
  102. return $this->hasMany(News::className(),['author'=>'name']);
  103. }
  104. }