<?php
namespace Tools;
use Tools\Qiniu;
use Think\Controller;

class AdminController extends Controller {

    public function _initialize()
    {
        $this->assign('menu', $this->getMenu());
    }
   public function __construct()
   {
       parent::__construct();
       $admin_id = session('admin_id');
       $admin_name = session('admin_name');
       $admin_info = M('Manager')->where(array('id' => $admin_id))->select();
       if (!$admin_info && ACTION_NAME!= 'signin') {
           $this->redirect('home/signin');
       }
   }
    public function getMenu()
    {
        $auths = [
            [
                'auth_id' =>'1',
                'auth_name' =>'统计信息',
                'auth_pid' =>'0',
                'auth_c' =>'Report',
                'auth_a' =>'statistic',
                'auth_path' =>'1',
                'auth_level' =>'1',
                'auth_src' => '0'
            ],
            [
                'auth_id' =>'2',
                'auth_name' =>'中奖信息',
                'auth_pid' =>'0',
                'auth_c' =>'Prize',
                'auth_a' =>'index',
                'auth_path' =>'1',
                'auth_level' =>'1',
                'auth_src' => '0'
            ]
        ];
        $menu = [];
        foreach ($auths as $item) {
            if ($item['auth_pid'] == 0) {
                if (CONTROLLER_NAME == $item['auth_c']) {
                    $item['active_class'] = 'active';
                } else {
                    $item['active_class'] = '';
                }
                $menu[] = $item;
            } else {
                if (CONTROLLER_NAME == $item['auth_c'] && ACTION_NAME == $item['auth_a']) {
                    $item['active_class'] = 'active';
                } else {
                    $item['active_class'] = '';
                }
                $menu[$item['auth_pid'] -1]['children'][] = $item;
            }
        }
        return $menu;
    }

    public function getToken(){
        $qiniu = new Qiniu(C('UPLOAD_SITEIMG_QINIU')['accessKey'], C('UPLOAD_SITEIMG_QINIU')['secrectKey'],C('UPLOAD_SITEIMG_QINIU')['domain'],  C('UPLOAD_SITEIMG_QINIU')['bucket'] );
        $uploadToken = $qiniu->uploadToken(array('scope' => 'tofax-salesman'));
        $this->ajaxReturn([
            'uptoken' => $uploadToken
        ]);
    }
}