<?php
namespace app\controller\manager;

use app\controller\BaseController;

/**
 * 控制器基础类
 */
class Base extends BaseController
{
    protected $data = [];
    protected function initialize()
    {   
        $this->middleware = [
            'csrf',
            'auth' => [
                'except' => ['manager.login/index']
            ]
        ];
        $this->setCsrfToken();
        $auth = session('auth');
        $this->data['member'] = $auth;
        $this->data['groupId'] = $auth['groupId'] ?? 0;
    }

    //变量赋值到模板
    protected function view()
    {
        return view()->assign($this->data);
    }

    protected function error($msg = '', string $url = null, $data = '', int $wait = 3)
    {
        if (is_null($url)) {
            $url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);';
        } elseif ($url) {
            $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
        }
        $result = [
            'code' => 0,
            'msg'  => $msg,
            'data' => $data,
            'url'  => $url,
            'wait' => $wait,
        ];

       return $this->redirect(url('manager.error/jump', $result));
    }

    protected function setCsrfToken()
    {
        $this->data['_token'] = session('_token') ?? '';
    }
}