123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace app\user\model;
- use think\Model;
- use think\Db;
- use think\Validate;
- use think\Request;
- /**
- * 管理员类
- * @author Superbee
- *
- */
- class Admin extends Model{
-
- protected $auto = ['last_ip']; // 自动填充
- protected $autoWriteTimestamp = 'datetime'; // 开启自动写入时间戳字段
- protected $insert = ['password', 'last_time', 'name']; // 插入是填充
- protected $createTime = 'create_time'; // 创建时间
- protected $updateTime = 'update_time'; // 每次更新时间
-
- public function initialize(){
- parent::initialize();
- }
-
- /* 自动填充设置 begin */
- protected function setNameAttr($value) {
- return strtolower($value);
- }
-
- protected function setLastIpAttr() {
- return request()->ip();
- }
-
- protected function setPasswordAttr($value) {
- return md5($value);
- }
-
- protected function setLastTimeAttr(){
- return getCurrentTime();
- }
- /* 自动填充设置 end */
-
- /**
- * 获取数据信息
- * @param int $page 页码
- * @param array $where 查询条件
- */
- public function getInfo($page = 0, $where = array()){
- return $this->db()
- ->where($where)
- ->page($page, config('paginate.list_rows'))
- ->select();
- }
-
- /**
- * 获取分页信息
- * @param array $where 查询条件
- */
- public function getPage($where = array()){
- $total = $this->db()->count();
- $paginate = $this->db()->where($where)->paginate(config('paginate.list_rows'), $total);
-
- return $paginate->render();
- }
-
- /**
- * 根据id找到对象
- * @param int $id
- */
- public function getAdminById($id){
- return $this->db()->where(['id'=>$id])->find();
- }
-
- /**
- * 注册方法
- */
- public function register(){
- $this->allowField(true)->save(request()->post());
- }
-
- /**
- * 添加和更新的方法
- */
- public function addAndSave(){
- $data = request()->post();
- return $this->allowField(true)->isUpdate(isset($data['id']) && $data['id']?true:false)->save($data);
- }
-
- /**
- * 删除管理员
- * @param int $id
- */
- public function remove(){
- $request = Request::instance();
-
- if($request->isPost()) $this->data = $request->post();
- if($request->isGet()) $this->data = $request->get();
-
- return $this->delete();
- }
-
- /**
- * 登录方法
- * @param string $name
- * @param string $password
- */
- public function login($name, $password){
- $res = $this->db()->where(['name'=>strtolower($name), 'password'=>md5($password)])->find();
- $res = $res->toArray();
-
- if ($res){
- $res['last_time'] = getCurrentTime();
- $this->isUpdate(true)->allowField(['last_time', 'id', 'last_ip'])->save($res);
- }
- return $res;
- }
-
- /**
- * 重置密码
- * @return Ambigous <string, int>
- */
- public function resetPwd(){
- $rule = [
- 'password' => 'require|min:6',
- 'repwd' => 'require|confirm:password',
- ];
-
- $msg = [
- 'password.require' => '密码必须',
- 'password.min' => '密码最少不能少于6个字符',
- 'repwd.require' => '确认密码必须',
- 'repwd.confirm' => '输入不一致',
- ];
-
- $validate = new Validate($rule, $msg);
- $result = $validate->check(request()->post());
-
- if(!$result){
- return $validate->getError();
- }
-
- return $this->addAndSave();
- }
-
- }
|