search_url = strtolower($this->request->controller()).'/index_search'; $this->title = '会员信息管理'; $this->level_arr = UserLevel::column('name','id'); $where = []; $where[] = ['m.is_deleted','=',0]; if($name = input('get.name')) $where[] = ['m.name','like','%'.$name.'%']; if($phone = input('get.phone')) $where[] = ['m.phone','like','%'.$phone.'%']; if($level_id = input('get.level_id')) $where[] = ['m.level_id','=',$level_id]; $field="m.id,m.openid,m.name,m.headimg,m.level_exp,m.phone,m.status,m.create_at,w.growth,w.integral,w.money"; $query = $this->_query($this->table) ->field($field) ->alias('m') ->leftJoin('UserWallet w','m.id =w.user_id') ->where($where) ->dateBetween('create_at')->order('id desc')->page(); } /** * 数据列表处理 * @auth true * @menu true * @param array $data * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ protected function _index_page_filter(&$data) { $level_arr = UserLevel::column('name','id'); foreach ($data as $k=>&$v){ $level_rank= UserLevelRank::where([['user_id','=',$v['id']],['end_time','>',time()]])->order('level_id desc')->find(); $v['level_id'] = $level_rank ? $level_rank->level_id : 0 ; $v['level_name'] = $level_rank ? $level_arr[$level_rank->level_id] : '--'; $v['end_date'] = $level_rank ? $level_rank->end_date:'--'; } } /** * 删除 * @auth true * @menu true * @param array $data * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function remove() { $this->_save($this->table, ['is_deleted' => '1','phone'=>'','email'=>'']); } /** * 禁用 * @auth true * @menu true * @param array $data * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function forbid() { $this->_save($this->table, ['status' => '0']); } /** * 启用 * @auth true * @menu true * @param array $data * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function resume() { $this->_save($this->table, ['status' => '1']); } /** * 添加 * @auth true * @menu true * @param array $data * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function add(){ $this->title = '添加'; $this->level_arr = UserLevel::column('name','id'); if($this->request->isPost()) { list($data) = [$this->request->post()]; $check_where =[]; $check_where[] = $data['account_type'] == 1 ? ['email','=',$data['account']] : ['phone','=',$data['account']]; $user_info = User::where($check_where)->find(); if($user_info) $this->error('用户已存在'); $user_add= []; $data['account_type'] == 1 ? $user_add['email'] = $data['account'] : $user_add['phone'] = $data['account']; $user_add['name'] = $data['name']; $user_add['account_type'] = $data['account_type']; $new_user = User::create($user_add); if($data['level_id'] && intval($data['days']) > 0) { UserLevelRank::create(['user_id'=>$new_user->id,'level_id'=>$data['level_id'],'start_time'=>time(),'end_time'=>time()+86400* intval($data['days']),'end_date'=>date('Y-m-d H:i:s',time()+86400*intval($data['days']))]); } $this->success('添加成功'); } $this->_form($this->table, 'add'); } /** * * 编辑 * @auth true * @menu true * @param array $data * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function edit() { $this->title = '编辑'; $this->level_arr = UserLevel::column('name','id'); $this->_form($this->table, 'form'); } /** * 钱包管理 * @auth true * @menu true * @param array $data * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function wallet() { $this->title = '钱包'; $this->_form($this->table, 'wallet'); } protected function _form_filter(&$data){ if($this->request->isGet() && $this->request->action() =='edit') { $data['rank_id'] = UserLevelRank::getUserVip($data['id']); } } public function export(){ $get_data = $this->request->get(); $time = explode(' - ',$get_data['create_at']); $phone = $get_data['phone']; $name = $get_data['name']; $where = []; $where[] = ['status',1]; $where[] = ['is_deleted',0]; $where_str = ' status = 1 AND is_deleted = 0'; if($name) $where_str .=' AND name like '."'%".$name."%'"; if($phone) $where_str .=' AND phone like '."'%".$phone."%'"; if($get_data['create_at']) $where_str.=" AND create_at > '".$time[0]."'AND create_at <'".$time[1]."'"; $data = Db::query("SELECT name,headimg,email,phone,create_at,account_type FROM dd_store_member WHERE".$where_str.' ORDER BY id DESC'); if(empty($data)) $this->error('暂无可以导出的数据'); foreach ($data as $k=>&$v) { if(!$v) $v = '--'; } $field=array( 'A' => array('name', '昵称'), 'B' => array('email', '邮箱'), 'C' => array('phone', '电话'), 'E' => array('create_at', '注册时间'), ); $this->phpExcelList($field,$data,'会员列表'); } public function phpExcelList($field=[],$list=[],$title='文件'){ $PHPExcel=new \PHPExcel(); $PHPSheet=$PHPExcel->getActiveSheet(); $PHPSheet->setTitle('demo'); //给当前活动sheet设置名称 foreach($list as $key=>$value) { foreach($field as $k=>$v){ if($key == 0){ $PHPSheet= $PHPExcel->getActiveSheet()->setCellValue($k.'1',$v[1]); } $i=$key+2; $PHPExcel->getActiveSheet()->setCellValue($k . $i, $value[$v[0]]); } } $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007'); //按照指定格式生成Excel文件, header('Content-Type: application/vnd.ms-excel'); // 告诉浏览器生成一个excel05版的表格 header("Content-Disposition: attachment;filename={$title}.xls"); //告诉浏览器输出文件的名称 header('Cache-Control: max-age=0'); //禁止缓存 $PHPWriter->save("php://output"); //输出到浏览器 } /** * 导入 * @auth true * @menu true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function import() { $file = request()->file('file'); $file_size = $_FILES['file']['size']; if ($file_size > 5 * 1024 * 1024) $this->error('文件大小不能超过5M'); //限制上传表格类型 $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1); if ($fileExtendName != 'xls' && $fileExtendName != 'xlsx') $this->error('必须为excel表格,且必须为xls格式!'); $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/upload'; if (!file_exists($dir)) mkdir($dir, 0777, true); $info = $file->move($dir); $fileName = $info->getSaveName(); $filePath = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . "/public/upload/{$fileName}"; /* $reader = \PHPExcel_IOFactory::createReader('Excel2007'); if(!$reader->canRead($filePath)) $reader = \PHPExcel_IOFactory::createReader('Excel2015'); */ $objPHPExcelReader = \PHPExcel_IOFactory::load($filePath); $sheet = $objPHPExcelReader->getSheet(0); // 读取第一个工作表(编号从 0 开始) $highestRow = $sheet->getHighestRow(); // 取得总行数 $arr = array('A','B','C','D','E'); // 一次读取一列 $res_arr = []; for ($row = 2; $row <= $highestRow; $row++) { $row_arr = array(); for ($column = 0 ;$column < count($arr) ; $column++) { $val = $sheet->getCellByColumnAndRow($column, $row)->getValue(); $row_arr[] = $val; } $res_arr[] = $row_arr; } $success_num = 0; // name account account_type level_id days foreach ($res_arr as $new_user) { $check_where[] = $new_user['2'] == 1 ? ['email','=',trim($new_user['1'])]: ['phone','=',trim($new_user['1'])]; $ck_res = User::where($check_where)->value('id'); if($ck_res){ Data::save('UserLevelRank',[ 'user_id'=>$ck_res, 'level_id'=>intval($new_user['3']), 'start_time'=>time(), 'end_time'=>time()+86400* intval($new_user['4']), 'end_date'=>date('Y-m-d H:i:s',time()+86400*intval($new_user['4']))],'user_id',['user_id'=>$ck_res]); $success_num++; User::where('id',$ck_res)->update(['level_id'=>intval($new_user['3']),'level_exp'=>time()+86400* intval($new_user['4'])]); continue; } $user_add= []; $new_user['2'] == 1 ? $user_add['email'] = trim($new_user['1']) : $user_add['phone'] = trim($new_user['1']); $user_add['name'] = $new_user['0']; $user_add['account_type'] = $new_user['2']; $add_res = User::create($user_add); if($new_user['3'] > 0 && intval($new_user[4]) > 0) { UserLevelRank::create(['user_id'=>$add_res->id,'level_id'=>$new_user['3'],'start_time'=>time(),'end_time'=>time()+86400* intval($new_user['4']),'end_date'=>date('Y-m-d H:i:s',time()+86400*intval($new_user['4']))]); $success_num++; } } $this->success('成功导入会员:'.$success_num); } }