297 lines
11 KiB
PHP
297 lines
11 KiB
PHP
<?php
|
||
|
||
namespace app\admin\controller;
|
||
|
||
use think\Db;
|
||
use think\Request;
|
||
|
||
class Admin extends Common{
|
||
public function index(){
|
||
// 接收分页的条件
|
||
$get = Request::instance()->get();
|
||
$query = http_build_query($get);
|
||
$this->assign('get',$get);
|
||
$this->assign('query',$query);
|
||
|
||
// 接收搜索的条件信息
|
||
$username = Request::instance()->get('username');
|
||
$status = Request::instance()->get('status');
|
||
$startDate = Request::instance()->get('startDate');
|
||
$endDate = Request::instance()->get('endDate');
|
||
$export = Request::instance()->get('export');
|
||
$startTime = 0;
|
||
$endTime = time();
|
||
|
||
// 拼装搜索条件
|
||
$where = array();
|
||
if(!empty($username)) $where['admin'] = array('like',"%".$username."%");
|
||
if($status > 0){
|
||
// 锁定不能用0作为判断,用2代替
|
||
if($status == 2){
|
||
$where['status'] = 0;
|
||
}else{
|
||
$where['status'] = $status;
|
||
}
|
||
}
|
||
if($startDate) $startTime = strtotime($startDate);
|
||
if($endDate) $endTime = strtotime($endDate);
|
||
$where['create_time'] = array('between',[$startTime,$endTime]);
|
||
$where['is_delete'] = 0;
|
||
|
||
if($export == 1){
|
||
$admin_list = Db::name('admin')->where($where)->order('create_time desc')->select();
|
||
}else{
|
||
// 所有总台管理员列表
|
||
$admin_list = Db::name('admin')->where($where)->order('create_time desc')->paginate(10,false,array('query'=>$get));
|
||
}
|
||
foreach($admin_list as $k => $v){
|
||
$v['last_login_time'] = date('Y-m-d H:i:s',$v['last_login_time']);
|
||
$admin_list[$k] = $v;
|
||
}
|
||
|
||
//导出excel列表
|
||
if($export == 1){
|
||
if($admin_list){
|
||
//重新组合
|
||
$excelData = array();
|
||
foreach($admin_list AS $k => $v){
|
||
$excelData[$k][0] = $v['admin'];
|
||
$excelData[$k][1] = $v['mobile'];
|
||
$excelData[$k][2] = $v['email'];
|
||
$excelData[$k][3] = $v['last_login_ip'];
|
||
$excelData[$k][4] = $v['last_login_time'];
|
||
if($v['status'] == 1){
|
||
$excelData[$k][5] = '正常';
|
||
}else{
|
||
$excelData[$k][5] = '锁定中';
|
||
}
|
||
}
|
||
$title = array('用户名','电话','邮箱','最后登录IP','最后登录时间','状态');
|
||
if($startDate && $endDate){
|
||
$this->exportExcelCore($excelData, '用户列表-'.$startDate."至".$endDate, $title);
|
||
}else{
|
||
$this->exportExcelCore($excelData, '用户列表', $title);
|
||
}
|
||
exit('已导出支持列表,请不要重复刷新该页面!');
|
||
}else{
|
||
exit('没有可以导出的列表!');
|
||
}
|
||
}
|
||
|
||
// 渲染参数和模板
|
||
$this->assign('admin_list',$admin_list);
|
||
return $this->fetch();
|
||
}
|
||
|
||
// 添加总台管理员页面
|
||
public function admin_add(){
|
||
return $this->fetch('admin-add');
|
||
}
|
||
|
||
// 处理添加总台管理员
|
||
public function do_admin_add(){
|
||
// 接收提交过来的数据
|
||
$username = Request::instance()->post('username');
|
||
$pass = Request::instance()->post('pass');
|
||
$repass = Request::instance()->post('repass');
|
||
$mobile = Request::instance()->post('mobile');
|
||
$email = Request::instance()->post('email');
|
||
$status = Request::instance()->post('status');
|
||
$remarks = Request::instance()->post('remarks');
|
||
|
||
// 数据验证
|
||
if( !isset($username) && empty($username) ){
|
||
die(json_encode(['code'=>0,'msg'=>'用户名不能为空!']));
|
||
}
|
||
if( !isset($pass) && empty($pass) ){
|
||
die(json_encode(['code'=>0,'msg'=>'密码不能为空!']));
|
||
}
|
||
if( !isset($repass) && empty($repass) ){
|
||
die(json_encode(['code'=>0,'msg'=>'确认密码不能为空!']));
|
||
}
|
||
if( $repass != $pass ){
|
||
die(json_encode(['code'=>0,'msg'=>'两次密码不一致!']));
|
||
}
|
||
|
||
// 检测用户名是否已经被注册
|
||
$user = Db::name('admin')->where('admin',$username)->find();
|
||
if($user){
|
||
die(json_encode(['code'=>0,'msg'=>'用户已存在!']));
|
||
}
|
||
|
||
// 拼装数据
|
||
$data = array();
|
||
$data['admin'] = $username;
|
||
$data['password'] = think_ucenter_md5($pass, UC_AUTH_KEY);
|
||
$data['mobile'] = $mobile;
|
||
$data['email'] = $email;
|
||
$data['status'] = $status;
|
||
$data['remark'] = $remarks;
|
||
$data['is_delete'] = 0;
|
||
$data['create_time'] = time();
|
||
$insert_id = Db::name('admin')->insertGetId($data);
|
||
if($insert_id){
|
||
// 写入管理员日志
|
||
insertAdminLog('添加管理员','添加管理员:| ID: '.$insert_id.' | 账号: '.$username);
|
||
die(json_encode(['code'=>1,'msg'=>'添加成功!']));
|
||
}else{
|
||
die(json_encode(['code'=>0,'msg'=>'添加失败!']));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 编辑管理员页面
|
||
*/
|
||
public function admin_edit()
|
||
{
|
||
// 接收代理ID,查询代理信息
|
||
$id = Request::instance()->get('id');
|
||
$admin = Db::name('admin')->find($id);
|
||
|
||
// 渲染参数和模板
|
||
$this->assign('admin',$admin);
|
||
return $this->fetch('/admin/admin-edit');
|
||
}
|
||
|
||
/**
|
||
* 处理修改管理员信息
|
||
*/
|
||
public function do_admin_edit()
|
||
{
|
||
if(Request::instance()->post()){
|
||
// 接收传过来的数据
|
||
$admin_id = Request::instance()->post('admin_id');
|
||
$username = Request::instance()->post('username');
|
||
$pass = Request::instance()->post('pass');
|
||
$repass = Request::instance()->post('repass');
|
||
$mobile = Request::instance()->post('mobile');
|
||
$email = Request::instance()->post('email');
|
||
$remarks = Request::instance()->post('remarks');
|
||
|
||
//数据验证
|
||
if(empty($username)){
|
||
die(json_encode(['code'=>0,'msg'=>'账号不能为空!']));
|
||
}
|
||
if(!empty($pass) && strlen($pass) < 6){
|
||
die(json_encode(['code'=>0,'msg'=>'密码长度不能少于6位!']));
|
||
}
|
||
if(!empty($pass) && $pass != $repass){
|
||
die(json_encode(['code'=>0,'msg'=>'两次密码输入不一致!']));
|
||
}
|
||
if( !isset($mobile) && empty($mobile) ){
|
||
die(json_encode(['code'=>0,'msg'=>'手机号码不能为空!']));
|
||
}
|
||
|
||
// 拼装数据
|
||
$data = array();
|
||
$data['admin'] = $username;
|
||
if(!empty($pass)) $data['password'] = think_ucenter_md5($pass, UC_AUTH_KEY);
|
||
$data['mobile'] = $mobile;
|
||
$data['email'] = $email;
|
||
$data['remark'] = $remarks;
|
||
$data['update_time'] = time();
|
||
|
||
// 修改管理员资料
|
||
$result = Db::name('admin')->where('id',$admin_id)->update($data);
|
||
if($result){
|
||
insertAdminLog('修改代理','修改代理:| ID: '.$admin_id.' | 账号:'.$username);
|
||
die(json_encode(['code'=>1,'msg'=>'修改成功!']));
|
||
}else{
|
||
die(json_encode(['code'=>0,'msg'=>'修改失败!']));
|
||
}
|
||
}else{
|
||
die(json_encode(['code'=>0,'msg'=>'操作错误!']));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 修改用户状态(锁定和解锁用户)
|
||
*/
|
||
public function change_status()
|
||
{
|
||
if(Request::instance()->post()){
|
||
// 接收传过来的数据
|
||
$user_id = Request::instance()->post('user_id');
|
||
$status = Request::instance()->post('status');
|
||
|
||
// 数据验证
|
||
if( !isset($user_id) && empty($user_id) ){
|
||
die(json_encode(['code'=>0,'msg'=>'用户不能为空!']));
|
||
}
|
||
if( !isset($status) && empty($status) ){
|
||
die(json_encode(['code'=>0,'msg'=>'状态不能为空!']));
|
||
}
|
||
|
||
// 修改用户状态
|
||
$result = Db::name('admin')->where('id',$user_id)->update(['status'=>$status]);
|
||
if($status == 1) $msg = "解锁";
|
||
if($status == 0) $msg = "锁定";
|
||
if($result){
|
||
insertAdminLog($msg."代理",$msg."代理: | ID: ".$user_id);
|
||
die(json_encode(['code'=>1,'msg'=>$msg.'成功!']));
|
||
}else{
|
||
die(json_encode(['code'=>0,'msg'=>$msg.'失败!']));
|
||
}
|
||
}else{
|
||
die(json_encode(['code'=>0,'msg'=>'操作错误!']));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除管理员
|
||
*/
|
||
public function admin_del()
|
||
{
|
||
if(Request::instance()->post()){
|
||
// 接收数据
|
||
$user_id = Request::instance()->post('user_id');
|
||
|
||
// 数据验证
|
||
if(!$user_id){
|
||
die(json_encode(['code'=>0,'msg'=>'用户不存在']));
|
||
}
|
||
|
||
// 删除用户
|
||
$result = Db::name('admin')->where('id',$user_id)->update(['is_delete'=>1]);
|
||
if($result){
|
||
insertAdminLog('删除代理',"删除代理: | ID: ".$user_id);
|
||
die(json_encode(['code'=>1,'msg'=>'删除成功!']));
|
||
}else{
|
||
die(json_encode(['code'=>1,'msg'=>'删除失败!']));
|
||
}
|
||
}else{
|
||
die(json_encode(['code'=>0,'msg'=>'操作错误!']));
|
||
}
|
||
}
|
||
public function admin_print(){
|
||
$get = Request::instance()->get();
|
||
$query = http_build_query($get);
|
||
$username = Request::instance()->get('username');
|
||
$status = Request::instance()->get('status');
|
||
$startDate = Request::instance()->get('startDate');
|
||
$endDate = Request::instance()->get('endDate');
|
||
$startTime = 0;
|
||
$endTime = time();
|
||
$where = array();
|
||
if(!empty($username)) $where['admin'] = array('like',"%".$username."%");
|
||
if($status > 0){
|
||
// 锁定不能用0作为判断,用2代替
|
||
if($status == 2){
|
||
$where['status'] = 0;
|
||
}else{
|
||
$where['status'] = $status;
|
||
}
|
||
}
|
||
if($startDate) $startTime = strtotime($startDate);
|
||
if($endDate) $endTime = strtotime($endDate);
|
||
$where['create_time'] = array('between',[$startTime,$endTime]);
|
||
$where['is_delete'] = 0;
|
||
$admin_list = Db::name('admin')->where($where)->order('create_time desc')->select();
|
||
foreach($admin_list as $k => $v){
|
||
$v['last_login_time'] = date('Y-m-d H:i:s',$v['last_login_time']);
|
||
$admin_list[$k] = $v;
|
||
}
|
||
$this->assign('admin_list',$admin_list);
|
||
return $this->fetch();
|
||
}
|
||
} |