<?php
namespace Admin\Controller;
use Think\Controller;
class CommonController extends Controller {
protected $_permission = array(); //管理员权限
protected $_success = NULL; //是否成功操作
protected $_run_action_end = true; // 是否自动运行_action_end方法
/**
* 后台日志记录方法
* @author hdh
* @access protected
* @param $bool 是否成功执行
* @return void
*/
protected function recordLog($bool) {
$logs = C('log');
if (isset($logs[MODULE_NAME][CONTROLLER_NAME][ACTION_NAME])) {
//判断是否是存在以post方式访问的操作,并记录下来
if ($logs[MODULE_NAME][CONTROLLER_NAME][ACTION_NAME]['isRecord'] === IS_POST) {
$id = I('get.id', '') != '' ? I('get.id') : I('post.id');
$log = D('Log')->addLog($id);
$opstate = $bool;
if(is_bool($this->_success)) {
if ($this->_success != true)
$opstate = false;
}
$log->result = $opstate;
$log->add();
}
}
$this->_run_action_end = false;
}
/**
* 操作错误跳转的快捷方法
* @access protected
* @param string $message 错误信息
* @param string $jumpUrl 页面跳转地址
* @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
* @return void
*/
protected function error($message='',$jumpUrl='',$ajax=false) {
$this->_success = false;
// 原因明白了,就是如果在error调用recordLog的话,会导致_action_end多执行一次
// 和是否有没有执行parent::error没关系,所以就是说,有没有die不重要
$this->recordLog(false);
parent::error($message, $jumpUrl, $ajax);
}
/**
* 操作成功跳转的快捷方法
* @access protected
* @param string $message 提示信息
* @param string $jumpUrl 页面跳转地址
* @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
* @return void
*/
protected function success($message='',$jumpUrl='',$ajax=false) {
$this->_success = true;
parent::success($message, $jumpUrl, $ajax);
}
//自行运行方法
protected function _initialize() {
//检查是否拥有操作权限
$this->hasPermission();
// 当前的控制器和操作
$this->assign('controllerName', CONTROLLER_NAME);
$this->assign('actionName', ACTION_NAME);
$this->assign('menus', C('MENUS'));
$this->assign('site', C('site')); // 站点信息
$this->assign('adminPermission', $this->_permission);
$this->loginCheck();
}
// 每一个操作结束时都会执行的代码
protected function _action_end() {
// 每调用一个次(不管是不是URL请求)都会执行控制器的析构方法
if ($this->_run_action_end) {
if (session('?isLogin')) {
if (session('isLogin') == '1') {
$this->recordLog(true);
}
}
}
}
public function __destruct() {
// 在没修改框架和使用HOOK的情况下,扩展了一下框架
// 调用父类的析构函数
parent::__destruct();
if(method_exists($this,'_action_end'))
$this->_action_end();
}
protected function getOrderWhere($orderCode) {
// 根据md5码生成对应的排序字段条件
// 请在这里配置排序时用到的字段名称
$order_field = array('created_time', 'updated_time');
foreach($order_field as $v) {
if (md5("$v desc") == $orderCode) {
return "$v desc";
}elseif(md5("$v asc") == $orderCode) {
return "$v asc";
}
}
}
protected function loginCheck() {
session('last_access', time());
if(session('?isLogin')) {
if (session('isLogin') == '1') {
$this->_isLogin = true;
//只记录登陆之后的操作
}else {
$this->_isLogin = false;
}
}else {
$this->_isLogin = false;
}
if (!$this->_isLogin) {
if (strtolower(CONTROLLER_NAME) != 'login') {
//C('TMPL_ACTION_ERROR', 'Public:outcpmsg');
//$this->error('对不起,请登录', _url('Login/index'), 1);
//直接跳转到登录界面
$this->redirect('Login/index');
}
}
}
/**
* 判断是否有权限
*/
protected function hasPermission(){
$this->_permission = D('Admin')->getPermissions();
$permissions = $this->_permission;
$action = strtolower(CONTROLLER_NAME . '/' . ACTION_NAME);
if (!in_array($action, $permissions)){
if (IS_AJAX){
$this->ajaxReturn(array(
'status' => 'ERROR',
'message' => '您暂无权限',
));
}else{
$this->redirect('Index/nopermission');
}
}
}
// 获取分页对象
protected function getPage($count, $listRows = 20, $parameter=array()) {
return new \Think\Page($count, $listRows, $parameter);
}
} 最佳答案