<?php

namespace app\user\controller;

use think\Controller;
use think\Lang;

class UserControl extends Controller
{

    /**
     * 管理员资料 name id group
     */
    protected $admin_info;

    public function _initialize()
    {
        if (in_array(cookie('ds_admin_lang'), array('zh-cn', 'en-us'))) {
            config('default_lang', cookie('ds_admin_lang'));
        }
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '.php');
        $this->user_info = $this->systemLogin();
//        if ($this->admin_info['admin_id'] != 1) {
//            // 验证权限
////            $this->checkPermission();
//        }
        $this->setMenuList();
    }

    /**
     * 取得当前管理员信息
     *
     * @param
     * @return 数组类型的返回结果
     */
    protected final function getAdminInfo()
    {
        return $this->user_info;
    }

    /**
     * 系统后台登录验证
     *
     * @param
     * @return array 数组类型的返回结果
     */
    protected final function systemLogin()
    {
        $user_info = array(
            'user_id' => session('user_id'),
            'user_email' => session('user_email'),
        );
        if (empty($user_info['user_id']) || empty($user_info['user_email'])) {
            $this->redirect('User/Login/index');
        }

        return $user_info;
    }

    /**
     * 侧边栏
     */
    public function setMenuList()
    {
        header("content-type:text/html;charset=utf-8");//设置编码
        $menu_list = $this->menuList();
        //权限组单组管理(已有权限)
        $admin_group_model = model('admingroup');
        $group_info = $admin_group_model->getOneAdmingroup(array('group_id' => session('admin_group_id')));
        $hlimit = ds_decrypt($group_info['group_limits'], MD5_KEY . md5($group_info['group_name']));
        $allkzq = $group_info['group_limits'] = explode('|', $hlimit);
        for ($i=0; $i < count($allkzq); $i++) {
            $oallkzq[] = strtolower($allkzq[$i]);
        }

        $this->assign('gupinfo',$oallkzq);
        $this->assign('menu_list', $menu_list);
    }

    //所有权限
    public function qxhans(){
        //权限组单组管理(已有权限)
        $admin_group_model = model('admingroup');
        $group_info = $admin_group_model->getOneAdmingroup(array('group_id' => session('admin_group_id')));
        $hlimit = ds_decrypt($group_info['group_limits'], MD5_KEY . md5($group_info['group_name']));
        $allkzq = $group_info['group_limits'] = explode('|', $hlimit);
        for ($i=0; $i < count($allkzq); $i++) {
            $oallkzq[] = strtolower($allkzq[$i]);
        }

        return $oallkzq;
    }

    /**
     * 当前选中的栏目
     */
    protected function setAdminCurItem($curitem = '')
    {
        $this->assign('admin_item', $this->getAdminItemList());
        $this->assign('curitem', $curitem);
    }

    /**
     * 获取卖家栏目列表,针对控制器下的栏目
     */
    protected function getAdminItemList()
    {
        return array();
    }

    /**
     * 侧边栏列表
     */
    function menuList()
    {
        return array(
            'all' => array(
                'name' => 'all',
                'text' => lang('ds_all'),
                'url' => url('All/index'),
            ),
            'product_sever' => array(
                'name' => 'product_sever',
                'text' => lang('ds_product_sever'),
				'url' => url('Config/index'),
            ),
            'personnel_manage' => array(
                'name' => 'personnel_manage',
                'text' => lang('ds_personnel_manage'),
                'children' => array(
                    'personnel_info' => array(
                        'text' => lang('ds_personnel_info'),
                        'url' => url('Member/index'),
                    ),
                    'personnel_security' => array(
                        'text' => lang('ds_personnel_security'),
                        'url' => url('Accountsafe/index'),
                    ),
                    'real_name_authentication' => array(
                        'text' => lang('ds_real_name_authentication'),
                        'url' => url('Admingroup/index'),
                    ),
                ),
            ),
            'cost_center' => array(
                'name' => 'cost_center',
                'text' => lang('ds_cost_center'),
                'children' => array(
                    'account_recharge' => array(
                        'text' => lang('ds_account_recharge'),
                        'url' => url('Cost/recharge'),
                    ),
                    'order_management' => array(
                        'text' => lang('ds_order_management'),
                        'url' => url('Cost/orderManagement'),
                    ),
                    'transaction_management' => array(
                        'text' => lang('ds_transaction_management'),
                        'url' => url('Cost/transaction'),
                    ),
                ),
            ),
            'renewal_management' => array(
                'name' => 'renewal_management',
                'text' => lang('ds_renewal_management'),
                'url' => url('Renewal/index'),
            ),
            'access_control' => array(
                'name' => 'access_control',
                'text' => lang('ds_access_control'),
                'url' => url('Config/index'),
            ),
            'my_massage' => array(
                'name' => 'my_massage',
                'text' => lang('ds_my_massage'),
                'url' => url('Config/index'),
            ),

        );
    }

    /**
     * 记录系统日志
     *
     * @param $lang 日志语言包
     * @param $state 1成功0失败null不出现成功失败提示
     * @param $admin_name
     * @param $admin_id
     */
    protected final function log($lang = '', $state = 1, $admin_name = '', $admin_id = 0)
    {
        if ($admin_name == '') {
            $admin_name = session('admin_name');
            $admin_id = session('admin_id');
        }
        $data = array();
        if (is_null($state)) {
            $state = null;
        } else {
            $state = $state ? '' : lang('nc_fail');
        }
        $data['content'] = $lang . $state;
        $data['admin_name'] = $admin_name;
        $data['createtime'] = TIMESTAMP;
        $data['admin_id'] = $admin_id;
        $data['ip'] = request()->ip();
        $data['url'] = request()->controller() . '&' . request()->action();
        $data['lang'] = config('default_lang');
        return db('adminlog')->insertGetId($data);
    }
}

?>
