AdminBaseController.php 6.4 KB
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace cmf\controller;

use think\Db;

class AdminBaseController extends BaseController
{

    public function _initialize()
    {
        // 监听admin_init
        hook('admin_init');
        parent::_initialize();
        $session_admin_id = session('ADMIN_ID');
        if (!empty($session_admin_id)) {
            $user = Db::name('user')->where(['id' => $session_admin_id])->find();

            if (!$this->checkAccess($session_admin_id)) {
                $this->error("您没有访问权限!");
            }
            $this->assign("admin", $user);
        } else {
            if ($this->request->isPost()) {
                $this->error("您还没有登录!", url("admin/public/login"));
            } else {
                header("Location:" . url("admin/public/login"));
                exit();
            }
        }
        $siteInfo = cmf_get_site_info();
        $this->assign('site_info', $siteInfo);
    }

    public function _initializeView()
    {
        $cmfAdminThemePath    = config('cmf_admin_theme_path');
        $cmfAdminDefaultTheme = config('cmf_admin_default_theme');

        $themePath = "{$cmfAdminThemePath}{$cmfAdminDefaultTheme}";

        $root = cmf_get_root();

        //使cdn设置生效
        $cdnSettings = cmf_get_option('cdn_settings');
        if (empty($cdnSettings['cdn_static_root'])) {
            $viewReplaceStr = [
                '__ROOT__'     => $root,
                '__TMPL__'     => "{$root}/{$themePath}",
                '__STATIC__'   => "{$root}/static",
                '__WEB_ROOT__' => $root
            ];
        } else {
            $cdnStaticRoot  = rtrim($cdnSettings['cdn_static_root'], '/');
            $viewReplaceStr = [
                '__ROOT__'     => $root,
                '__TMPL__'     => "{$cdnStaticRoot}/{$themePath}",
                '__STATIC__'   => "{$cdnStaticRoot}/static",
                '__WEB_ROOT__' => $cdnStaticRoot
            ];
        }

        $viewReplaceStr = array_merge(config('view_replace_str'), $viewReplaceStr);
        config('template.view_base', "$themePath/");
        config('view_replace_str', $viewReplaceStr);
    }

    /**
     * 初始化后台菜单
     */
    public function initMenu()
    {
    }

    /**
     *  检查后台用户访问权限
     * @param int $userId 后台用户id
     * @return boolean 检查通过返回true
     */
    private function checkAccess($userId)
    {
        // 如果用户id是1,则无需判断
        if ($userId == 1) {
            return true;
        }

        $module     = $this->request->module();
        $controller = $this->request->controller();
        $action     = $this->request->action();
        $rule       = $module . $controller . $action;

        $notRequire = ["adminIndexindex", "adminMainindex"];
        if (!in_array($rule, $notRequire)) {
            return cmf_auth_check($userId);
        } else {
            return true;
        }
    }



//    后台列表
//  $data   post/get 获取到的值

    public function adminIndex($data){
        $arr = array();
        $find = array();
        if($data){
            $startTime = empty($data['start_time']) ? 0 : strtotime($data['start_time']);
            $endTime   = empty($data['end_time']) ? 0 : strtotime($data['end_time']);
            if ($startTime && $endTime) {
                $arr['start_time'] = $data['start_time'];
                $arr['end_time'] = $data['end_time'];
                $find['create_time'] = array('between',"$startTime,$endTime");
                $this->assign('start_time', $data['start_time']);
                $this->assign('end_time', $data['end_time']);
            }else{
                if($startTime){
                    $arr['start_time'] = $data['start_time'];
                    $find['create_time'] = array('egt',$startTime);
                    $this->assign('start_time', $data['start_time']);
                }
                if($endTime){
                    $arr['end_time'] = $data['end_time'];
                    $find['create_time'] = array('elt',$endTime);
                    $this->assign('end_time', $data['end_time']);
                }
            }
        }
        $return['page_arr'] = $arr;
        $return['where_arr'] = $find;
        return $return;
    }





//  删除
    public function adminDel($model){
        $ids = $this->request->post('ids');
        $id = $this->request->param('id');
        if($ids){
            $add_del['id'] = array('in',$ids);
        }else if($id){
            $add_del['id'] = $this->request->param('id');
        }else{
            $this->error('删除失败');
        }
        $add_del['status'] = 9;
        $del = $model->isUpdate(true)->allowField(true)->save($add_del);
        if($del){
            $this->success('删除成功',url('index'));
        }else{
            $this->error('删除失败');
        }
    }


    /*
* 长度处理
* */
    public function changeLen($text,$number,$numberNext){
        $change_html = strip_tags(htmlspecialchars_decode($text));
        $sub_text_first = mb_substr($change_html,0,$number,'utf-8');

        if(strlen($change_html) >= $numberNext){
            $sub_text = $sub_text_first.'...';
        }else{
            $sub_text = $sub_text_first;
        }
        return $sub_text;
    }

    /**
     *  API返回信息格式函数 ;0失败,1成功,-1需要登录
     * @param string $code
     * @param string $message
     * @param array $data
     */
    public function apiResponse($code = '0', $message = '',$data = array(),$nums =0){
        header('Access-Control-Allow-Origin: *');
        header('Content-Type:application/json; charset=utf-8');
        $result = array(
            'code'=>$code,
            'message'=>$message,
            'data'=>$data,
            'nums'=>''.$nums
        );
        die(json_encode($result,JSON_UNESCAPED_UNICODE));
    }

}