AdminIndexController.php 8.0 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------

namespace app\user\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;

/**
 * Class AdminIndexController
 * @package app\user\controller
 *
 * @adminMenuRoot(
 *     'name'   =>'用户管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 10,
 *     'icon'   =>'group',
 *     'remark' =>'用户管理'
 * )
 *
 * @adminMenuRoot(
 *     'name'   =>'用户组',
 *     'action' =>'default1',
 *     'parent' =>'user/AdminIndex/default',
 *     'display'=> true,
 *     'order'  => 10000,
 *     'icon'   =>'',
 *     'remark' =>'用户组'
 * )
 */
class AdminIndexController extends AdminBaseController
{

    /**
     * 后台本站用户列表
     * @adminMenu(
     *     'name'   => '本站用户',
     *     'parent' => 'default1',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $content = hook_one('user_admin_index_view');

        if (!empty($content)) {
            return $content;
        }

        $list = Db::name('user')
            ->where('user_type',2)
            ->where(function (Query $query) {
                $data = $this->request->param();
                if (!empty($data['uid'])) {
                    $query->where('id', intval($data['uid']));
                }

                if (!empty($data['keyword'])) {
                    $keyword = $data['keyword'];
                    $query->where('user_login|user_nickname|user_email|mobile', 'like', "%$keyword%");
                }

            })
            ->order("create_time DESC")
            ->paginate(10);
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }

    /**
     * 本站用户拉黑
     * @adminMenu(
     *     'name'   => '本站用户拉黑',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户拉黑',
     *     'param'  => ''
     * )
     */
    public function ban()
    {
        $id = input('param.id', 0, 'intval');
        if ($id) {
            $result = Db::name("user")->where(["id" => $id, "user_type" => 2])->setField('user_status', 0);
            if ($result) {
                $this->success("会员拉黑成功!", "adminIndex/index");
            } else {
                $this->error('会员拉黑失败,会员不存在,或者是管理员!');
            }
        } else {
            $this->error('数据传入失败!');
        }
    }

    /**
     * 本站用户启用
     * @adminMenu(
     *     'name'   => '本站用户启用',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户启用',
     *     'param'  => ''
     * )
     */
    public function cancelBan()
    {
        $id = input('param.id', 0, 'intval');
        if ($id) {
            Db::name("user")->where(["id" => $id, "user_type" => 2])->setField('user_status', 1);
            $this->success("会员启用成功!", '');
        } else {
            $this->error('数据传入失败!');
        }
    }

    //导出excel
    public function export(){//导出Excel
        $ids = $this->request->param('ids');
        if(isset($ids) && !empty($ids)){
            $ids = explode(',',$ids);
            $xlsData = Db::name('user')
                ->whereIn('id',$ids)
                ->field('id,user_nickname,user_status,mobile,create_time,last_login_time')
                ->select()
                ->toArray();
        }else{
            $xlsData = Db::name('user')
                ->where('user_type',2)
                ->field('id,user_nickname,user_status,mobile,create_time,last_login_time')
                ->select()
                ->toArray();
        }
        $xlsName  = "用户表数据";
        $xlsCell  = array(
            array('id','序号'),
            array('user_nickname','昵称'),
            array('mobile','手机号'),
            array('create_time','注册时间'),
            array('last_login_time','最后登录时间'),
            array('user_status','状态'),
        );

        foreach ($xlsData as &$v) {
            $v['user_status'] = $v['user_status'] == 1?'正常':'已拉黑';
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            $v['last_login_time'] = date('Y-m-d H:i:s',$v['last_login_time']);
        }
        $this->exportExcel($xlsName,$xlsCell,$xlsData);
    }

    //导出表格
    public function exportExcel($expTitle,$expCellName,$expTableData){

        $fileName = '用户表';//or $xlsTitle 文件名称可根据自己情况设定
//        $filePath = 'upload/user/'.date('Y-m-d',time()).'.xlsx';
        $topNumber = 1;//表头有几行占用
        $cellKey = array(
            'A','B','C','D','E','F','G','H','I','J','K','L','M',
            'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
            'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM',
            'AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ'
        );
        vendor("PHPExcel");
        
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $sheet->setTitle($expTitle);
//         $spreadsheet->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  Export time:'.date('Y-m-d H:i:s'));
        //处理表头
        foreach ($expCellName as $k=>$v) {
            $sheet->setCellValue($cellKey[$k].$topNumber, $v[1]);//设置表头数据
//            $spreadsheet->getActiveSheet()->freezePane($cellKey[$k].($topNumber+1));//冻结窗口
            $sheet->getStyle($cellKey[$k].$topNumber)->getFont()->setBold(true);//设置是否加粗
        }
        //处理数据
        //设置单元格居中显示
        $styleArray = [
            'alignment' => [
                'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
            ],
        ];
        //表头居中
        foreach ($expCellName as $k2=>$v2) {
            $sheet->getStyle($cellKey[$k2].'1')->applyFromArray($styleArray);
        }
        foreach ($expTableData as $k=>$v) {
            foreach ($expCellName as $k1=>$v1) {
                $sheet->setCellValue($cellKey[$k1].($k+1+$topNumber), $v[$v1[0]]);
                $sheet->getColumnDimension($cellKey[$k1])->setWidth(30);//每列宽度
                $sheet->getStyle($cellKey[$k1].($k+1+$topNumber))->applyFromArray($styleArray);
            }
        }
        ob_end_clean();//清除缓冲区,避免乱码
        header('Content-Type: application/vnd.ms-excel');//告诉浏览器将要输出excel03文件
//        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器数据excel07文件
        header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');
        header('Cache-Control: max-age=0');
//        $writer = new Xlsx($spreadsheet);
        $writer = IOFactory::createWriter($spreadsheet, 'Xls'); //按照指定格式生成Excel文件
        $writer->save('php://output');
        exit;
    }
}