chengkun
2025-09-13 36fcee83fd60816d65f7c06840f5b8f92c01484a
app/admin/util/Opadmin.php
@@ -13,13 +13,13 @@
class Opadmin {
    
    public string $user_name; //用户名
    private string $password; //密码
    private mixed $session_prefix; //SESSION前缀
    public string  $user_name;      //用户名
    private string $password;       //密码
    private mixed  $session_prefix; //SESSION前缀
    
    private string $kinfo = 'admininfo';
    public mixed $info;
    public string $commfield = 'id,user_name,real_name,ban_access,initialize,start_time,end_time';
    private string $kinfo     = 'admininfo';
    public mixed   $info;
    public string  $commfield = 'id,user_name,real_name,ban_access,initialize,start_time,end_time';
    
    /**
     * +----------------------------------------------------------
@@ -27,7 +27,7 @@
     * +----------------------------------------------------------
     * @param string $username 用户名
     * @param string $password 密码
     * +----------------------------------------------------------
     *                         +----------------------------------------------------------
     */
    public function __construct(string $username = '', string $password = '') {
        $this->session_prefix = Config::get('app.session_admin_prefix');
@@ -35,7 +35,7 @@
        $this->kinfo = $this->session_prefix . $this->kinfo;
        //用于登陆的时候初始化变量
        $this->user_name = $username;
        $this->password = joinmd5($password);
        $this->password  = joinmd5($password);
        //判断session是否存在,存在就赋值
        if (session('?' . $this->kinfo)) {
            $this->info = session($this->kinfo);
@@ -57,13 +57,13 @@
//        $subwhere['password'] = $this->password;
        $tempinfo = Db::name('administrators')->field('id,password,ban_access,login_lock_time,login_try_num')->where($subwhere)->find();
        if (!$tempinfo) {
            $d['code'] = 400;
            $d['code']    = 400;
            $d['message'] = '账号或密码错误、请重试';
            return $d;
        }
        
        if ($tempinfo['login_lock_time'] != '' && time() - $tempinfo['login_lock_time'] < 600) {
            $d['code'] = 400;
            $d['code']    = 400;
            $d['message'] = '该账号已被锁定、请10分钟后重试';
            return $d;
        }
@@ -72,14 +72,14 @@
            // 次数 小于等于 1 -> 锁定登录操作
            if ($tempinfo['login_try_num'] <= 1) {
                $upd_data['login_lock_time'] = time();
                $upd_data['login_try_num'] = 5;
                $upd_data['login_try_num']   = 5;
                Db::name('administrators')->where($upd_where)->save($upd_data);
                $d['code'] = 400;
                $d['code']    = 400;
                $d['message'] = '该账号已被锁定、请10分钟后重试';
            } else {
                // 次数 小于2 次数-1 -> 账号密码错误
                Db::name('administrators')->where($upd_where)->dec('login_try_num')->update();
                $d['code'] = 400;
                $d['code']    = 400;
                $d['message'] = '账号或密码错误、剩余' . ($tempinfo['login_try_num'] - 1) . '次';
            }
            return $d;
@@ -87,21 +87,28 @@
        /////以下密码正确,成功登陆///////////
        Db::name('administrators')->where($upd_where)->save(['login_try_num' => 5]);
        if ($tempinfo['ban_access'] == 0) {
            $msg['code'] = 400;
            $msg['code']    = 400;
            $msg['message'] = '该账号已被禁止登录';
            return $msg;
        }
        return $this->getlogininfo($tempinfo['id']);
    }
    
    //////获取用户登录信息///////////
    /**
     * 获取用户登录信息
     * @param $id
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getlogininfo($id = ''): array {
        $subwhere['id'] = $id;
        $info = Db::name('administrators')->field($this->commfield)->where($subwhere)->find();
        $info           = Db::name('administrators')->field($this->commfield)->where($subwhere)->find();
        if ($info) {
            if ($info['initialize'] == 0 && ($info['start_time'] > time() || $info['end_time'] < time())) {
                $this->loginout();
                $msg['code'] = 400;
                $msg['code']    = 400;
                $msg['message'] = '账号已过期,请联系管理员!';
                return $msg;
            }
@@ -111,44 +118,49 @@
            $this->writelogs();
            //更新登陆信息
            if ($this->updateInfo()) {
                $msg['code'] = 200;
                $msg['code']    = 200;
                $msg['message'] = '登录成功';
            } else {
                $msg['code'] = 400;
                $msg['code']    = 400;
                $msg['message'] = '登录失败';
            }
        } else {
            $msg['code'] = 400;
            $msg['code']    = 400;
            $msg['message'] = '用户名或密码不正确';
        }
        return $msg;
    }
    
    /////账号登录信息///////////////
    /**
     * 更新用户信息
     * @return bool
     */
    private function updateInfo(): bool {
        $temp['login_time'] = time();
        $temp['login_ip'] = getIP();
        $where['id'] = $this->info['id'];
        $count = Db::name('administrators')->where($where)->save($temp);
        $temp['login_ip']   = getIP();
        $where['id']        = $this->info['id'];
        $count              = Db::name('administrators')->where($where)->save($temp);
        if ($count > 0)
            return TRUE;
        else
            return FALSE;
    }
    
    /**
     * 写入登陆日志
     * @return void
     */
    private function writelogs(): void {
        //////登陆记录//////////
        $d['login_ip'] = getIP();
        $d['login_ip']   = getIP();
        $d['login_time'] = time();
        $d['admin_id'] = $this->info['id'];
        $d['admin_id']   = $this->info['id'];
        Db::name('admin_login_logs')->insert($d);
    }
    
    /**
     * +----------------------------------------------------------
     * 保存session
     * +----------------------------------------------------------
     * +----------------------------------------------------------
     * @return void
     */
    public function saveSession(): void {
        session($this->kinfo, $this->info);
@@ -156,11 +168,8 @@
    }
    
    /**
     * +----------------------------------------------------------
     * 判断用户是否登陆
     * @return bool
     * +----------------------------------------------------------
     * +----------------------------------------------------------
     */
    public function islogin(): bool {
        if (isset($this->info['id']) && $this->info['id'] != '')
@@ -170,11 +179,8 @@
    }
    
    /**
     * +----------------------------------------------------------
     * 用户退出
     * @return bool
     * +----------------------------------------------------------
     * +----------------------------------------------------------
     */
    public function loginout(): bool {
        $this->info = "";
@@ -185,12 +191,16 @@
        return TRUE;
    }
    
    /**
     * 获取菜单
     * @return array
     */
    public function menu(): array {
        $condition['show_menu'] = 1;
        $order = 'order_id asc,id asc';
        $list = Db::name('admin_menu')
        $order                  = 'order_id asc,id asc';
        $list                   = Db::name('admin_menu')
            ->field("id,title,menu_index,menu_icon,show_menu,menu_url,father_id")
            ->cacheAlways(TRUE, 0, 'admin_menu')
            ->cache(60)
            ->where($condition)
            ->order($order)
            ->withAttr('menu_index', function ($value) {
@@ -203,7 +213,7 @@
    
    /**
     * 获取菜单列表
     * @param $result
     * @param        $result
     * @param string $one_field
     * @return array
     */
@@ -215,7 +225,7 @@
            }
            return $result_arr;
        } else {
            return array();
            return [];
        }
    }