<?php
namespace Admin\Controller;
use Think\controller;
use Think\Page;
class UserController extends Controller{
public function userlist(){
$userModel = D('user');
$where = [];
if(I('keywords')){
$keywords = '%'.trim(I('keywords')).'%';
$where['nickname'] = ['like',$keywords];
}
$count = D('user')->where($where)->count();
$page = new Page($count,25);
$show = $page->show();
$userlist = D('user')
->join('user_level on user.level=user_level.level_id')
->where($where)->order(['reg_time'=>'desc'])
->limit($page->firstRow.','.$page->listRows)
->select();
$this->assign('count',$count);
$this->assign('page',$show);
$this->assign('userlist',$userlist);
$this->assign('level',D('user_level')->getField('level_id,level_name'));
$this->display();
}
public function useradd(){
if(IS_POST){
$_POST['reg_time'] = NOW_TIME;
if(!D('user')->create($_POST)){
exit(D('user')->getError());
}
if($userinfo=D('user')->add($_POST)){
$this->success('用户添加成功','userlist',3);
}else{
$this->error('用户添加失败','userlist',3);
}
}else{
$this->display();
}
}
public function useredit(){
if(IS_POST){
$user_id = I('user_id');
$userModel = D('user');
if(!$userModel->create($_POST)){
exit($userModel->getError());
}else{
$userinfo = $userModel->where('user_id='.$user_id)->save($_POST);
if($userinfo){
$this->success('修改成功','userlist');
}else{
$this->error('修改失败');
}
}
}else{
$user=D('user')->where('user_id='.I('user_id'))->find();
$this->assign('user',$user);
$this->display();
}
}
public function userdel(){
$delid=D('user')->where('user_id='.I('user_id'))->delete();
if($delid){
$this->ajaxReturn(["status"=>1,"msg"=>"删除成功"]);
}else{
$this->ajaxReturn(["status"=>0,"msg"=>"删除失败"]);
}
}
public function userdelbatch(){
$delid=implode(',',I('ids'));
$userModel=D('user');
$delinfo=$userModel->where(['user_id'=>$delid])->delete();
if($delinfo){
$this->success('删除成功','userlist');
}else{
$this->error('删除失败','userlist');
}
}
public function userlevellist(){
$level=D("user_level")->select();
$this->assign('level',$level);
$this->display();
}
public function useraddress(){
$address=D('user_address')->where('user_id='.I('user_id'))->select();
$this->assign('address',$address);
$this->display();
}
public function accountlog(){
$account=D('account_log')->where('user_id='.I('user_id'))->select();
$this->assign('account',$account);
$this->display();
}
public function recharge(){
$where = [];
if(IS_POST){
$nickname = '%'.trim(I('nickname')).'%';
$where['nickname'] = ['like',$nickname];
}
$count = D('recharge')->where($where)->count();
$page = new Page($count,20);
$show = $page->show();
$recharge = D('recharge')
->order(['pay_time'=>'asc'])
->where($where)
->limit($page->firstRow.','.$page->listRows)
->select();
$this->assign('recharge',$recharge);
$this->assign('page',$show);
$this->assign('count',$count);
$this->display();
}
public function search_user(){
$search_user = I('search_user');
$where['mobile'] = array('like',"%$search_user%");
$userlist = M('user')->where($where)->select();
foreach ($userlist as $key => $value) {
echo "<option value='{$value['user_id']}'>{$value['mobile']}</option>";
}
}
public function send_mail(){
if(IS_POST){
$user = I('user');
$title = I('title');
$content = I('content');
$user = implode(',',$user);
$userlist = M('user')->where(['user_id'=>['in',$user]])->select();
Vendor('PHPMailer.PHPMailerAutoload');
$mail = new \PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.163.com";
$mail->SMTPAuth = true;
$mail->Username = "liuguoqiang010@163.com";
$mail->Password = "manage3389";
$mail->From = "liuguoqiang010@163.com";
$mail->FromName = "电商测试";
if(is_array($userlist)){
foreach ($userlist as $value) {
$mail->AddAddress($value['email'],$value['nickname']);
}
}
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->CharSet = "utf-8";
$mail->Subject = $title;
$mail->Body = $content;
$info=$mail->send();
if($info){
$this->ajaxReturn(['status'=>1,'msg'=>'邮件发送成功,窗口关闭']);
}else{
$this->ajaxReturn(['status'=>0,'msg'=>'邮件发送失败']);
}
}
$where['user_id']=array('in',I('is_array_user_id'));
$user=M('user')->where($where)->select();
$this->assign('user',$user);
$this->display();
}
public function export_excel(){
vendor("PHPExcel.PHPExcel") $objPHPExcel = new \PHPExcel();
$objPHPExcel->createSheet; $objPHPExcel->setActiveSheetIndex(); $objSheet = $objPHPExcel->getActiveSheet() $objSheet->setTitle('用户列表');
$objSheet->setCellValue("A1","用户ID")
->setCellValue("B1","会员昵称")
->setCellValue("C1","会员等级")
->setCellValue("D1","手机号")
->setCellValue('E1','邮箱')
->setCellValue('F1',"注册时间")
->setCellValue("G1","最后登陆")
->setCellValue("H1","余额")
->setCellValue("J1","积分")
->setCellValue("K1","累计消费");
$data = D('user')->select();
$j = 2;
foreach($data as $key=>$val){
$objSheet->setCellValue("A".$j,$val['user_id'])
->setCellValue("B".$j,$val['nickname'])
->setCellValue("C".$j,$val['level'])
->setCellValue("D".$j,$val['mobile'])
->setCellValue("E".$j,$val['email'])
->setCellValue("F".$j,date('Y-m-d H:m:s',$val['reg_time']))
->setCellValue("G".$j,date('Y-m-d H:m:s',$val['last_login']))
->setCellValue("H".$j,$val['user_money'])
->setCellValue("J".$j,$val['pay_points'])
->setCellValue("K".$j,$val['total_amount']);
$j++;
}
$date=date("Ymd_His");
header('Content-type:application/vnd.ms-excel;charset=utf-8;');
header("Content-Disposition:attachment;filename=user_".$date.".xls") header('Cache-Control: max-age=0') $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
}