<?php
namespace app\store\controller;
use app\store\service\Auth;
use app\store\service\Menus;
use app\store\model\Setting;
use app\common\exception\BaseException;
use app\store\model\addons\Modules as ModulesModel;
use app\store\model\Wxapp as WxappModel;
use app\common\model\wxapp\WxappModules as WxappModulesModel;
use think\Request;
use think\Session;
use think\console\Input;
class Controller extends \think\Controller
{
protected $store;
protected $controller = '';
protected $action = '';
protected $routeUri = '';
protected $group = '';
protected $allowAllAction = [
'passport/login',
];
protected $notLayoutAction = [
'passport/login',
];
public function _initialize()
{
global $_GPC;
$this->store = Session::get('tpwe_admin');
$this->getRouteinfo();
$this->checkLogin();
$this->checkPrivilege();
$this->layout();
$_GPC = input();
}
private function checkPrivilege()
{
if ($this->routeUri === 'index/index') {
return true;
}
if (!Auth::getInstance()->checkPrivilege($this->routeUri)) {
throw new BaseException(['msg' => '很抱歉,没有访问权限']);
}
return true;
}
private function layout()
{
global $_W;
if (!in_array($this->routeUri, $this->notLayoutAction)) {
$setting = Setting::getAll() ?: null;
$module_route = array('addons/index','addons/run');
if(in_array($this->routeUri,$module_route)){
$model = new ModulesModel;
$modulename = input('modules');
if(empty($modulename)){
$modulename = session('modulename');
}
$module = $model->getModuleDetail($modulename);
$setting['ismodule'] = true;
$setting['store']['values']['name'] =$module['name'];
}elseif ($this->routeUri=='wxapp/home'){
$WxappModel = new WxappModel;
$wxapp = $WxappModel->getWxappDetail(input('uniacid'));
$setting['iswxapp'] = true;
$setting['store']['values']['name'] =$wxapp['name'];
}
$_W['siteroot']=$_SESSION['siteroot'] = base_url();
$_W['admin'] = $this->store;
$_W['menus'] = $this->menus();
$this->assign([
'base_url' => base_url(), 'store_url' => url('/store'), 'group' => $this->group, 'menus' => $this->menus(), 'store' => $this->store, 'setting' => $setting, 'request' => Request::instance(), 'version' => get_version(), ]);
}
}
protected function getRouteinfo()
{
$this->controller = toUnderScore($this->request->controller());
$this->action = $this->request->action();
$groupstr = strstr($this->controller, '.', true);
$this->group = $groupstr !== false ? $groupstr : $this->controller;
$this->routeUri = $this->controller . '/' . $this->action;
}
protected function menus()
{
static $menus = [];
if (empty($menus)) {
$module_route = array('addons/index','addons/run');
if(in_array($this->routeUri,$module_route)){
$model = new ModulesModel;
$modulename = input("modules");
if(empty($modulename)){
$modulename = session('modulename');
}
$menus = $model->getMenus($modulename);
}elseif ($this->routeUri=='wxapp/home'){
$WxappModel = new WxappModulesModel;
$modulename = session('modulename');
$menus = $WxappModel->getWaxppMenus(input('uniacid'));
}else{
$menus = Menus::getInstance()->getMenus($this->routeUri, $this->group);
}
}
return $menus;
}
private function checkLogin()
{
if (in_array($this->routeUri, $this->allowAllAction)) {
return true;
}
if (empty($this->store)
|| (int)$this->store['is_login'] !== 1
|| !isset($this->store['wxapp'])
|| empty($this->store['wxapp'])
) {
$this->redirect('passport/login');
return false;
}
return true;
}
protected function getWxappId()
{
return $this->store['wxapp']['wxapp_id'];
}
protected function renderJson($code = 1, $msg = '', $url = '', $data = [])
{
return compact('code', 'msg', 'url', 'data');
}
protected function renderSuccess($msg = 'success', $url = '', $data = [])
{
if ($this->request->isAjax()) {
return $this->renderJson(1, $msg, $url, $data);
}
$this->success($msg,$url);
return false;
}
protected function renderError($msg = 'error', $url = '', $data = [])
{
if ($this->request->isAjax()) {
return $this->renderJson(0, $msg, $url, $data);
}
$this->error($msg);
return false;
}
protected function postData($key = null)
{
return $this->request->post(is_null($key) ? '' : $key . '/a');
}
protected function getData($key = null)
{
return $this->request->get(is_null($key) ? '' : $key);
}
}