AdminModel.class.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace User\Model;
  3. use Think\Model;
  4. /**
  5. * 管理员模型
  6. * @author Superbee
  7. *
  8. */
  9. class AdminModel extends Model{
  10. protected $fields = array('id', 'name', 'password', 'realname', 'last_login' , 'last_ip',
  11. '_pk' => 'id', '_autoinc' => true);
  12. protected $rule = array(
  13. array('username', 'require', '用户名不能为空!', 1),
  14. // 正则验证密码 [需包含字母数字以及@*#中的一种,长度为6-22位]
  15. array('password', '/^([a-zA-Z0-9@*#]{6,22})$/', '密码格式不正确,请重新输入!', 1),
  16. array('verify','require','验证码必须!'), // 都有时间都验证
  17. array('verify','checkVerify','验证码错误',0, 'function'),
  18. );
  19. protected $_auto = array(
  20. array('password','md5',3,'function') , // 对password字段在新增和编辑的时候使md5函数处理
  21. );
  22. public function __construct(){
  23. $this->dbName = 'admin'; // 表名
  24. $this->db = M($this->dbName);
  25. }
  26. public function getAdminList(){
  27. return $this->db->alias('a')->join('jj_admintype as b on(a.type_id=b.type_id)')->select();
  28. }
  29. /**
  30. * 获取页码当前标签
  31. * @param array $where
  32. * @param int $page
  33. * @return \Think\mixed
  34. */
  35. public function getInfo($page=0, $where=array()){
  36. return $this->db->where($where)->page(0, PAGE_ROW)->select();
  37. }
  38. /**
  39. * 获取分页信息
  40. * @param array $where 查询条件
  41. * @return string
  42. */
  43. public function getPages($where=array()){
  44. $p = getPages($this->db, $where, PAGE_ROW);
  45. return $p->show();
  46. }
  47. /**
  48. * 根据id找到人员
  49. * @param int $id
  50. * @return mixed|boolean|NULL|string|unknown|object
  51. */
  52. public function getAdminById($id){
  53. return $this->db->where(array('id'=>$id))->find();
  54. }
  55. /**
  56. * 登录判断
  57. */
  58. public function onLogin(){
  59. $name = I('name');
  60. $password = I('password');
  61. $where = array('username'=>$name, 'password'=>md5($password));
  62. $res = $this->db->where($where)->find();
  63. if($res){
  64. $this->db->save(array('last_login'=>getCurrentTime()));
  65. }
  66. return $res;
  67. }
  68. /**
  69. * 修改密码
  70. * @param int $id 管理员id
  71. * @param string $pwd 新密码
  72. */
  73. public function modifyPwd($id, $pwd){
  74. return $this->db->where(array('id'=>$id))->save(array('password'=>md5($pwd)));
  75. }
  76. public function update(){
  77. $data = $this->db->create();
  78. return $this->db->save($data);
  79. }
  80. public function add(){
  81. $data = $this->db->create();
  82. $data['password']=md5($data['password']);
  83. return $this->db->add($data);
  84. }
  85. }