Pro/application/agent/controller/Sub.php
2026-02-25 01:50:31 +08:00

275 lines
12 KiB
PHP

<?php
namespace app\agent\controller;
use \think\Controller;
use think\Lang;
use \think\Session;
use \think\Request;
use \think\Db;
class Sub Extends Common{
public function index(){
$langType = cookie('think_var');
$lang = Lang::get('agent');
$jsonlang = json_encode($lang);
$this->assign('langType',$langType);
$this->assign('lang',$lang);
$this->assign('jsonlang',$jsonlang);
// 用于分页和搜索查询的数据
$get = Request::instance()->get();
$query = http_build_query($get);
$this->assign('get',$get);
$this->assign('query',$query);
// 获取管理员信息
$user_info = Session::get('user_info');
$account_type = $user_info['account_type'];
$user_info = Db::name('user')->where('id',$user_info['id'])->find();
$user_info['account_type'] = $account_type;
if($user_info['account_type'] == 1 ||$user_info['account_type'] == 2 || $user_info['account_type'] == 3){
return $lang['illegal_request'];
}
$sub_list = Db::name('sub_account')->where('parent_id',$user_info['id'])->where('is_delete',0)->order('id asc')->paginate(15,false,['query'=>$get]);
foreach($sub_list as $k => $v){
$v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
if($v['account_type'] == 1){
$v['account_type'] = $lang['account_management'];
}else if($v['account_type'] == 2){
$v['account_type'] = $lang['supply_and_drainage_management'];
}else if($v['account_type'] == 3){
$v['account_type'] = $lang['report_management'];
}
$sub_list[$k] = $v;
}
// 渲染参数和模板
$this->assign('user_info',$user_info);
$this->assign('sub_list',$sub_list);
return $this->fetch();
}
// 添加子账户
public function add_sub(){
$langType = cookie('think_var');
$lang = Lang::get('agent');
$jsonlang = json_encode($lang);
$this->assign('langType',$langType);
$this->assign('lang',$lang);
$this->assign('jsonlang',$jsonlang);
// 获取管理员信息
$user_info = Session::get('user_info');
$account_type = $user_info['account_type'];
$user_info = Db::name('user')->where('id',$user_info['id'])->find();
$user_info['account_type'] = $account_type;
if($user_info['account_type'] == 1 ||$user_info['account_type'] == 2 || $user_info['account_type'] == 3){
return $lang['illegal_request'];
}
//渲染参数和模板
$this->assign('user_info',$user_info);
return $this->fetch();
}
// 处理添加代理
public function do_add(){
$langType = cookie('think_var');
$lang = Lang::get('agent');
$jsonlang = json_encode($lang);
$this->assign('langType',$langType);
$this->assign('lang',$lang);
$this->assign('jsonlang',$jsonlang);
$user_info = Session::get('user_info');
$account_type = $user_info['account_type'];
$user_info = Db::name('user')->where('id',$user_info['id'])->find();
$user_info['account_type'] = $account_type;
if($user_info['account_type'] == 1 ||$user_info['account_type'] == 2 || $user_info['account_type'] == 3){
die(json_encode(['code'=>0,'msg'=>$lang['illegal_request']]));
}
if(Request::instance()->post()){
// 接收参数
$username = Request::instance()->post('username');
$nickname = Request::instance()->post('nickname');
$password = Request::instance()->post('password');
$repassword = Request::instance()->post('repassword');
$account_type = Request::instance()->post('account_type');
// 验证数据
if(!$username){
die(json_encode(['code'=>0,'msg'=>$lang['empty_username']]));
}
$user = Db::name('user')->where('username',$username)->find();
if($user){
die(json_encode(['code'=>0,'msg'=>$lang['already_exist_username']]));
}
if(!$nickname){
die(json_encode(['code'=>0,'msg'=>$lang['empty_nickname']]));
}
if(!$password){
die(json_encode(['code'=>0,'msg'=>$lang['empty_password']]));
}
if(strlen($password) < 6){
die(json_encode(['code'=>0,'msg'=>$lang['new_pass_limit']]));
}
if($password != $repassword){
die(json_encode(['code'=>0,'msg'=>$lang['pass_diff']]));
}
if($account_type > 3 && $account_type <= 0){
die(json_encode(['code'=>0,'msg'=>$lang['account_function_error']]));
}
// 拼装数据创建代理
$user_data = array();
$user_data['username'] = $username;
$user_data['nickname'] = $nickname;
$user_data['parent_id'] = $user_info['id'];
$user_data['password'] = think_ucenter_md5($password, UC_AUTH_KEY);
$user_data['account_type'] = $account_type;
$user_data['create_time'] = time();
$insertId = Db::name('sub_account')->insertGetId($user_data);
if($insertId){
// 写入管理员日志
insertAgentLog('添加子账户','添加子账户:| ID: '.$insertId.' | 账号: '.$username);
die(json_encode(['code'=>1,'msg'=>$lang['added_successfully']]));
}else{
die(json_encode(['code'=>0,'msg'=>$lang['add_failed']]));
}
}else{
die(json_encode(['code'=>0,'msg'=>$lang['operation_error']]));
}
}
// 编辑子账户页面
public function edit_sub(){
$langType = cookie('think_var');
$lang = Lang::get('agent');
$jsonlang = json_encode($lang);
$this->assign('langType',$langType);
$this->assign('lang',$lang);
$this->assign('jsonlang',$jsonlang);
$user_info = Session::get('user_info');
$account_type = $user_info['account_type'];
$user_info = Db::name('user')->where('id',$user_info['id'])->find();
$user_info['account_type'] = $account_type;
$id = Request::instance()->get('id');
$sub_info = Db::name('sub_account')->where('id',$id)->where('parent_id',$user_info['id'])->find();
//渲染参数和模板
$this->assign('user_info',$user_info);
$this->assign('sub_info',$sub_info);
return $this->fetch();
}
// 处理编辑子账户
public function do_edit(){
$langType = cookie('think_var');
$lang = Lang::get('agent');
$user_info = Session::get('user_info');
$account_type = $user_info['account_type'];
$user_info = Db::name('user')->where('id',$user_info['id'])->find();
$user_info['account_type'] = $account_type;
if($user_info['account_type'] == 1 ||$user_info['account_type'] == 2 || $user_info['account_type'] == 3){
die(json_encode(['code'=>0,'msg'=>$lang['illegal_request']]));
}
if(Request::instance()->post()){
// 接收参数
$id = Request::instance()->post('sub_id');
$nickname = Request::instance()->post('nickname');
$password = trim(Request::instance()->post('password'));
$repassword = Request::instance()->post('repassword');
$account_type = Request::instance()->post('account_type');
// 拼装数据创建代理
$user_data = array();
// 验证数据
if(!$nickname){
die(json_encode(['code'=>0,'msg'=>$lang['empty_nickname']]));
}else{
$user_data['nickname'] = $nickname;
}
if(!$account_type){
die(json_encode(['code'=>0,'msg'=>$lang['select_account_function']]));
}else{
$user_data['account_type'] = $account_type;
}
if($password){
if(strlen($password) < 6){
die(json_encode(['code'=>0,'msg'=>$lang['new_pass_limit']]));
}
if($password != $repassword){
die(json_encode(['code'=>0,'msg'=>$lang['pass_diff']]));
}
$user_data['password'] = think_ucenter_md5($password, UC_AUTH_KEY);
}
$result = Db::name('sub_account')->where('id',$id)->update($user_data);
if($result){
// 写入管理员日志
$user = Db::name('sub_account')->where('id',$id)->find();
insertAgentLog('编辑子账户','编辑子账户:| ID: '.$user['id'].' | 账号: '.$user['username']);
}
die(json_encode(['code'=>1,'msg'=>$lang['edit_successfully']]));
}else{
die(json_encode(['code'=>0,'msg'=>$lang['edit_failed']]));
}
}
//删除子账户
public function do_del(){
$langType = cookie('think_var');
$lang = Lang::get('agent');
$user_info = Session::get('user_info');
$account_type = $user_info['account_type'];
$user_info = Db::name('user')->where('id',$user_info['id'])->find();
$user_info['account_type'] = $account_type;
if($user_info['account_type'] == 1 ||$user_info['account_type'] == 2 || $user_info['account_type'] == 3){
die(json_encode(['code'=>0,'msg'=>$lang['illegal_request']]));
}
if(Request::instance()->post()){
// 接收参数
$id = Request::instance()->post('sub_id');
$result = Db::name('sub_account')->where('id',$id)->update(['is_delete' => 1]);
if($result){
// 写入管理员日志
$user = Db::name('sub_account')->where('id',$id)->find();
insertAgentLog('删除子账户','删除子账户| ID: '.$user['id'].' | 账号: '.$user['username']);
}
die(json_encode(['code'=>1,'msg'=>$lang['deleted_successfully']]));
}else{
die(json_encode(['code'=>0,'msg'=>$lang['operation_error']]));
}
}
public function delete(){
$langType = cookie('think_var');
$lang = Lang::get('agent');
$jsonlang = json_encode($lang);
$this->assign('langType',$langType);
$this->assign('lang',$lang);
$this->assign('jsonlang',$jsonlang);
// 获取管理员信息
$user_info = Session::get('user_info');
$account_type = $user_info['account_type'];
$user_info = Db::name('user')->where('id',$user_info['id'])->find();
$user_info['account_type'] = $account_type;
if($user_info['account_type'] == 1 ||$user_info['account_type'] == 2 || $user_info['account_type'] == 3){
return $lang['illegal_request'];
}
$sub_list = Db::name('sub_account')->where('parent_id',$user_info['id'])->where('is_delete',1)->order('id asc')->select();
foreach($sub_list as $k => $v){
$v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
if($v['account_type'] == 1){
$v['account_type'] = $lang['account_management'];
}else if($v['account_type'] == 2){
$v['account_type'] = $lang['supply_and_drainage_management'];
}else if($v['account_type'] == 3){
$v['account_type'] = $lang['report_management'];
}
$sub_list[$k] = $v;
}
// 渲染参数和模板
$this->assign('user_info',$user_info);
$this->assign('sub_list',$sub_list);
return $this->fetch();
}
}