User.php 3.6 KB

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