Admin.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace app\user\model;
  3. use think\Model;
  4. use think\Db;
  5. use think\Validate;
  6. use think\Request;
  7. /**
  8. * 管理员类
  9. * @author Superbee
  10. *
  11. */
  12. class Admin extends Model{
  13. protected $auto = ['last_ip']; // 自动填充
  14. protected $autoWriteTimestamp = 'datetime'; // 开启自动写入时间戳字段
  15. protected $insert = ['password', 'last_time', 'name']; // 插入是填充
  16. protected $createTime = 'create_time'; // 创建时间
  17. protected $updateTime = 'update_time'; // 每次更新时间
  18. public function initialize(){
  19. parent::initialize();
  20. }
  21. /* 自动填充设置 begin */
  22. protected function setNameAttr($value) {
  23. return strtolower($value);
  24. }
  25. protected function setLastIpAttr() {
  26. return request()->ip();
  27. }
  28. protected function setPasswordAttr($value) {
  29. return md5($value);
  30. }
  31. protected function setLastTimeAttr(){
  32. return getCurrentTime();
  33. }
  34. /* 自动填充设置 end */
  35. /**
  36. * 获取数据信息
  37. * @param int $page 页码
  38. * @param array $where 查询条件
  39. */
  40. public function getInfo($page = 0, $where = array()){
  41. return $this->db()
  42. ->where($where)
  43. ->page($page, config('paginate.list_rows'))
  44. ->select();
  45. }
  46. /**
  47. * 获取分页信息
  48. * @param array $where 查询条件
  49. */
  50. public function getPage($where = array()){
  51. $query = getPaginatiorQuery();
  52. $total = $this->db()->where($where)->count();
  53. // 查询条件
  54. $config = ['query'=>$query];
  55. $paginate = $this->db()->where($where)->paginate(config('paginate.list_rows'), $total, $config);
  56. return $paginate->render();
  57. }
  58. /**
  59. * 根据id找到对象
  60. * @param int $id
  61. */
  62. public function getAdminById($id){
  63. return $this->db()->where(['id'=>$id])->find();
  64. }
  65. /**
  66. * 注册方法
  67. */
  68. public function register(){
  69. $this->allowField(true)->save(request()->post());
  70. }
  71. /**
  72. * 添加和更新的方法
  73. */
  74. public function addAndSave(){
  75. $data = request()->post();
  76. return $this->allowField(true)->isUpdate(isset($data['id']) && $data['id']?true:false)->save($data);
  77. }
  78. /**
  79. * 删除管理员
  80. * @param int $id
  81. */
  82. public function remove(){
  83. $request = Request::instance();
  84. if($request->isPost()) $this->data = $request->post();
  85. if($request->isGet()) $this->data = $request->get();
  86. return $this->delete();
  87. }
  88. /**
  89. * 登录方法
  90. * @param string $name
  91. * @param string $password
  92. */
  93. public function login($name, $password){
  94. $res = $this->db()->where(['name'=>strtolower($name), 'password'=>md5($password)])->find();
  95. if ($res){
  96. $res = $res->toArray();
  97. $res['last_time'] = getCurrentTime();
  98. $this->isUpdate(true)->allowField(['last_time', 'id', 'last_ip'])->save($res);
  99. }
  100. return $res;
  101. }
  102. /**
  103. * 重置密码
  104. * @return object Ambigous <string, int>
  105. */
  106. public function resetPwd(){
  107. $rule = [
  108. 'password' => 'require|min:6',
  109. 'repwd' => 'require|confirm:password',
  110. ];
  111. $msg = [
  112. 'password.require' => '密码必须',
  113. 'password.min' => '密码最少不能少于6个字符',
  114. 'repwd.require' => '确认密码必须',
  115. 'repwd.confirm' => '输入不一致',
  116. ];
  117. $validate = new Validate($rule, $msg);
  118. $result = $validate->check(request()->post());
  119. if(!$result){
  120. return $validate->getError();
  121. }
  122. return $this->addAndSave();
  123. }
  124. }