Useradmin.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * This is the model class for table "{{useradmin}}".
  4. *
  5. * The followings are the available columns in table '{{useradmin}}':
  6. * @property integer $id
  7. * @property string $username
  8. * @property string $password
  9. * @property string $email
  10. * @property string $status
  11. * @property string $role_id
  12. * @property string $avatar
  13. * @property string $descr
  14. * @property string $create_date
  15. * @property string $update_date
  16. */
  17. class Useradmin extends CActiveRecord
  18. {
  19. public $verifypassword; //注册中的重复密码
  20. public $verifyCode; //注册中的验证码
  21. public $phoneCode; //手机验证码
  22. /**
  23. * @return string the associated database table name
  24. */
  25. public function tableName()
  26. {
  27. return '{{useradmin}}';
  28. }
  29. /**
  30. * @return array validation rules for model attributes.
  31. */
  32. public function rules()
  33. {
  34. // NOTE: you should only define rules for those attributes that
  35. // will receive user inputs.
  36. return array(
  37. array('username, password, phone', 'required'),
  38. array('username', 'length', 'max'=>20),
  39. array('username','unique','message'=>'用户名已被占用'),
  40. array('password', 'length', 'max'=>50),
  41. array('verifypassword', 'length', 'max'=>50),
  42. array('verifypassword', 'compare', 'compareAttribute'=>'password', 'message'=>'请再输入确认密码', 'on'=>'register'),
  43. array('auth, status', 'safe'),
  44. // The following rule is used by search().
  45. // @todo Please remove those attributes that should not be searched.
  46. array('id, username, password, auth, status, verifypassword, phone,email', 'safe', 'on'=>'search'),
  47. //找回密码相关
  48. // array('username', 'safe', 'on'=>'findpassword'),
  49. array('username', 'exist', 'on'=>'findpassword', 'message'=>'{attribute}{value}不存在!'),
  50. array('password', 'required', 'on'=>'changepassword'),
  51. array('verifypassword', 'compare', 'compareAttribute'=>'password', 'on'=>'changepassword'),
  52. // 检查手机验证码
  53. array('phoneCode', 'required', 'on'=>'register'),
  54. array('phoneCode', 'regVerifyCodeCheck', 'on'=>'register'),
  55. array('phone', 'checkPhoneRegNum', 'on'=>'register'),
  56. );
  57. }
  58. public function usernameCheck($attribute,$params)
  59. {
  60. if(!$this->hasErrors())
  61. {
  62. if (strpos($this->username, ':') || strpos($this->username, ':'))
  63. {
  64. $this->addError('username', '用户名不能包含冒号!');
  65. }
  66. }
  67. }
  68. /**
  69. * 检查手机验证码
  70. * @param unknown $attribute
  71. * @param unknown $params
  72. */
  73. public function regVerifyCodeCheck($attribute,$params)
  74. {
  75. if(!$this->hasErrors())
  76. {
  77. $result = CheckVerifyCode($this->phoneCode);
  78. if(isset($result['errmsg']))
  79. {
  80. $this->addError("phoneCode", $result['errmsg']);
  81. return false;
  82. }
  83. }
  84. }
  85. /**
  86. * 检查手机验证码
  87. * @param unknown $attribute
  88. * @param unknown $params
  89. */
  90. public function checkPhoneRegNum($attribute,$params)
  91. {
  92. if(!$this->hasErrors())
  93. {
  94. $result = self::getPhoneRegNum($this->phone);
  95. if($result >= 3)
  96. {
  97. $this->addError("phone", '一个手机号码最多只能注册3个乐外卖账号!');
  98. return false;
  99. }
  100. }
  101. }
  102. /**
  103. * @return array relational rules.
  104. */
  105. public function relations()
  106. {
  107. // NOTE: you may need to adjust the relation name and the related
  108. // class name for the relations automatically generated below.
  109. return array(
  110. );
  111. }
  112. /**
  113. * @return array customized attribute labels (name=>label)
  114. */
  115. public function attributeLabels()
  116. {
  117. return array(
  118. 'id' => 'ID',
  119. 'username' => '账号',
  120. 'password' => '密码',
  121. 'verifypassword' => '确认密码',
  122. 'phone' =>'手机号码',
  123. 'email' =>'邮箱地址',
  124. );
  125. }
  126. /**
  127. * Retrieves a list of models based on the current search/filter conditions.
  128. *
  129. * Typical usecase:
  130. * - Initialize the model fields with values from filter form.
  131. * - Execute this method to get CActiveDataProvider instance which will filter
  132. * models according to data in model fields.
  133. * - Pass data provider to CGridView, CListView or any similar widget.
  134. *
  135. * @return CActiveDataProvider the data provider that can return the models
  136. * based on the search/filter conditions.
  137. */
  138. public function search()
  139. {
  140. // @todo Please modify the following code to remove attributes that should not be searched.
  141. $criteria=new CDbCriteria;
  142. $criteria->compare('id',$this->id);
  143. $criteria->compare('username',$this->username,true);
  144. $criteria->compare('password',$this->password,true);
  145. $criteria->compare('verifypassword',$this->verifypassword,true);
  146. $criteria->compare('phone',$this->phone,true);
  147. $criteria->compare('email',$this->email,true);
  148. return new CActiveDataProvider($this, array(
  149. 'criteria'=>$criteria,
  150. ));
  151. }
  152. /**
  153. * Returns the static model of the specified AR class.
  154. * Please note that you should have this exact method in all your CActiveRecord descendants!
  155. * @param string $className active record class name.
  156. * @return Useradmin the static model class
  157. */
  158. public static function model($className=__CLASS__)
  159. {
  160. return parent::model($className);
  161. }
  162. public function searchusing()
  163. {
  164. $criteria=new CDbCriteria;
  165. $criteria->addCondition('status=1');
  166. return new CActiveDataProvider($this, array(
  167. 'criteria'=>$criteria,
  168. 'pagination'=>['pageSize'=>20]
  169. ));
  170. }
  171. public function extendDueDate($days)
  172. {
  173. $oldDueDate = $this->due_date > date("Y-m-d H:i:s") ? $this->due_date : date("Y-m-d H:i:s");
  174. $year = (int) substr($oldDueDate, 0, 4);
  175. $mon = (int) substr($oldDueDate, 5, 2);
  176. $mday = (int) substr($oldDueDate, 8, 2);
  177. $hour = (int) substr($oldDueDate, 11, 2);
  178. $minute = (int) substr($oldDueDate, 14, 2);
  179. $second = (int) substr($oldDueDate, 17, 2);
  180. $this->due_date = date("Y-m-d H:i:s", mktime($hour, $minute, $second, $mon, $mday+$days, $year));
  181. }
  182. /**
  183. * 取出手机号码的注册次数
  184. * @param unknown $phone
  185. */
  186. public static function getPhoneRegNum($phone)
  187. {
  188. if(empty($phone))
  189. {
  190. return false;
  191. }
  192. $result = self::model()->count('phone=:phone', array(':phone'=>$phone));
  193. return intval($result);
  194. }
  195. }