MemberController.php 9.5 KB
<?php
/**
 * Created by PhpStorm.
 * User: 29925
 * Date: 2018/8/9
 * Time: 13:54
 */

namespace app\member\controller;

use app\member\model\MemberModel;
use cmf\controller\AdminBaseController;
use think\Db;

class MemberController extends AdminBaseController
{

    protected $table;
    protected $member_model;
    protected $channel;
    public function _initialize()
    {
        $this->table = 'Member';
        $this->member_model = new MemberModel();
//        $this->channel = ['1'=>'站内','2'=>'淘宝','3'=>'京东'];
//        $this->assign('channel',$this->channel);
    }

    /**
     * 注册用户列表
     * @adminMenu(
     *     'name'   => '注册用户列表',
     *     'parent' => 'user/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '注册用户列表',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $param = $this->request->param();
        $where = [
            'delete_time'=>0
        ];
        if(!empty($param['keyword'])) {
            $where['mobile|user_login|baby_name|parent_name'] = ['like','%'.$param['keyword'].'%'];
        }
        if(!empty($param['channel'])) {
            $where['channel_id'] = $param['channel'];
        }
        if(isset($param['is_excel']) && $param['is_excel'] >= 0) {
            $where['is_excel'] = $param['is_excel'];
        }
        $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['create_time'] = ['<= time', $endTime];
            }
        }
        $list = $this->singleData($this->table,$where,2,['create_time'=>'DESC']);
        $list->appends($param);
        $member = $list->items();
        foreach ($member as $k=>$v) {
            if(!empty($v['channel_id'])) {
                $v['channel_name'] = Db::name('Channel')->where(['id'=>$v['channel_id']])->value('name');
            }
            $member[$k] = $v;
        }
        $this->assign('list',$member);
        $this->assign('page',$list->render());
        $this->assign('keyword',isset($param['keyword']) ? $param['keyword'] : '');
        $this->assign('chan',isset($param['channel']) ? $param['channel'] : '');
        $this->assign('start_time',isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time',isset($param['end_time']) ? $param['end_time'] : '');
        $this->assign('is_excel',isset($param['is_excel']) ? $param['is_excel'] : '');
        // 获取渠道列表
        $channel = ['站内'];
        $channelList = $this->singleData('Channel',['delete_time'=>0],1,['create_time'=>'DESC']);
        $this->assign('channel',$channelList);
        return $this->fetch();
    }

    /**
     * 编辑用户
     * @adminMenu(
     *     'name'   => '编辑用户',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 1,
     *     'icon'   => '',
     *     'remark' => '编辑用户',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $id = $this->request->param('id',0,'intval');
        if(!$id) {
            $this->error('参数错误');
        }
        $info = $this->singleData($this->table,['id'=>$id,'delete_time'=>0]);
        if(!empty($info['channel_id'])) {
            $info['channel_name'] = Db::name('Channel')->where(['id'=>$info['channel_id']])->value('name');
        }
        $this->assign($info);
        return $this->fetch();
    }

    /**
     * 编辑用户提交
     * @adminMenu(
     *     'name'   => '编辑用户提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 2,
     *     'icon'   => '',
     *     'remark' => '编辑用户提交',
     *     'param'  => ''
     * )
     */
    public function editPost()
    {
        if($this->request->isPost()) {
            $id = $this->request->param('id',0,'intval');
            if(!$id) {
                $this->error('参数错误');
            }
            $post = $this->request->param();
            $validate = $this->validate($post,$this->table);
            if($validate !== true) {
                $this->error($validate);
            }
            $result = $this->member_model->editMember($post);
            if(!$result) {
                $this->error('修改失败');
            }
            $this->success('修改成功');
        }
    }

    /**
     * 用户数据导出
     * @adminMenu(
     *     'name'   => '用户数据导出',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 2,
     *     'icon'   => '',
     *     'remark' => '用户数据导出',
     *     'param'  => ''
     * )
     */
    public function excel()
    {
        $param = $this->request->param();
        $where = [
            'delete_time'=>0
        ];
        if(!empty($param['ids'])) {
            $where['id'] = ['in',$param['ids']];
        }
        if(!empty($param['keyword'])) {
            $where['mobile|user_login|baby_name|parent_name'] = ['like','%'.$param['keyword'].'%'];
        }
        if(!empty($param['channel'])) {
            $where['channel_id'] = $param['channel'];
        }
        if(isset($param['is_excel']) && $param['is_excel'] >= 0) {
            $where['is_excel'] = $param['is_excel'];
        }
        $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['create_time'] = ['<= time', $endTime];
            }
        }
        $list = $this->singleData($this->table,$where,1,['create_time'=>'DESC']);
        foreach ($list as $k=>$v) {
            if(!empty($v['channel_id'])) {
                $v['channel_name'] = Db::name('Channel')->where(['id'=>$v['channel_id']])->value('name');
            }
            $list[$k] = $v;
        }
        $this->expList($list);
    }

    // 数据导出
    public function expList($array)
    {

        //导出Excel
        $objPHPExcel = new \PHPExcel();

        //Add some data
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'ID')
            ->setCellValue('B1', '账号')
            ->setCellValue('C1', '宝宝姓名')
            ->setCellValue('D1', '宝宝生日')
            ->setCellValue('E1', '家长姓名')
            ->setCellValue('F1', '家长身份')
            ->setCellValue('G1', '是否为巧虎会员')
            ->setCellValue('H1', '注册渠道')
            ->setCellValue('I1', '注册时间')
            ->setCellValue('J1', '最后登录ip')
            ->setCellValue('K1', '最后登录时间');

        $i = 2;

        foreach ($array as $v) {
            $is_member = '否';
            if($v['is_member'] == 1) {
                $is_member = '是';
            }
            if(empty($v['channel_name'])) {
                $v['channel_name'] = '站内';
            }
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A' . $i, $v['id'])
                ->setCellValueExplicit('B' . $i, $v['mobile'],\PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValue('C' . $i, $v['baby_name'])
                ->setCellValue('D' . $i, $v['baby_birth'])
                ->setCellValue('E' . $i, $v['parent_name'])
                ->setCellValue('F' . $i, $v['parent_status'])
                ->setCellValue('G' . $i, $is_member)
                ->setCellValue('H' . $i, $v['channel_name'])
                ->setCellValue('I' . $i, date('Y-m-d',$v['create_time']))
                ->setCellValue('J' . $i, $v['last_login_ip'])
                ->setCellValue('K' . $i, date('Y-m-d',$v['last_login_time']));
            $i++;
            Db::name('Member')->where(['id'=>$v['id']])->update(['is_excel'=>1,'update_time'=>time()]);
        }

        $filename = '用户数据导出'. date('Y-m-d').'.xls';
        ob_end_clean();
        header("Content-type:application/vnd.ms-excel;charset=UTF-8");
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
    }

//    public function delete()
//    {
//        $id = $this->request->param('id',0,'intval');
//        $ids = $this->request->param('ids/a');
//        if(isset($id)) {
//            $result = $this->member_model->where(['id'=>$id])->update(['delete_time'=>time()]);
//        }
//        if(isset($ids)) {
//            $result = $this->member_model->where(['id'=>['in',$ids]])->update(['delete_time'=>time()]);
//        }
//        if(!$result) {
//            $this->error('删除失败');
//        }
//        $this->success('删除成功');
//    }
}