Admin.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. $total = $this->db()->count();
  52. $paginate = $this->db()->where($where)->paginate(config('paginate.list_rows'), $total);
  53. return $paginate->render();
  54. }
  55. /**
  56. * 根据id找到对象
  57. * @param int $id
  58. */
  59. public function getAdminById($id){
  60. return $this->db()->where(['id'=>$id])->find();
  61. }
  62. /**
  63. * 注册方法
  64. */
  65. public function register(){
  66. $this->allowField(true)->save(request()->post());
  67. }
  68. /**
  69. * 添加和更新的方法
  70. */
  71. public function addAndSave(){
  72. $data = request()->post();
  73. return $this->allowField(true)->isUpdate(isset($data['id']) && $data['id']?true:false)->save($data);
  74. }
  75. /**
  76. * 删除管理员
  77. * @param int $id
  78. */
  79. public function remove(){
  80. $request = Request::instance();
  81. if($request->isPost()) $this->data = $request->post();
  82. if($request->isGet()) $this->data = $request->get();
  83. return $this->delete();
  84. }
  85. /**
  86. * 登录方法
  87. * @param string $name
  88. * @param string $password
  89. */
  90. public function login($name, $password){
  91. $res = $this->db()->where(['name'=>strtolower($name), 'password'=>md5($password)])->find();
  92. $res = $res->toArray();
  93. if ($res){
  94. $res['last_time'] = getCurrentTime();
  95. $this->isUpdate(true)->allowField(['last_time', 'id', 'last_ip'])->save($res);
  96. }
  97. return $res;
  98. }
  99. /**
  100. * 重置密码
  101. * @return Ambigous <string, int>
  102. */
  103. public function resetPwd(){
  104. $rule = [
  105. 'password' => 'require|min:6',
  106. 'repwd' => 'require|confirm:password',
  107. ];
  108. $msg = [
  109. 'password.require' => '密码必须',
  110. 'password.min' => '密码最少不能少于6个字符',
  111. 'repwd.require' => '确认密码必须',
  112. 'repwd.confirm' => '输入不一致',
  113. ];
  114. $validate = new Validate($rule, $msg);
  115. $result = $validate->check(request()->post());
  116. if(!$result){
  117. return $validate->getError();
  118. }
  119. return $this->addAndSave();
  120. }
  121. }