288 lines
7.8 KiB
PHP
288 lines
7.8 KiB
PHP
<?php
|
|
namespace app\admin\controller;
|
|
use app\admin\controller\Base;
|
|
use think\facade\Db;
|
|
use think\facade\View;
|
|
use think\facade\Request;
|
|
|
|
class Yunzeradmin extends Base{
|
|
// 角色列表
|
|
public function groupinfo(){
|
|
$group = Db::table('yz_admin_user_group')->select();
|
|
View::assign([
|
|
'group' => $group
|
|
]);
|
|
return View::fetch();
|
|
}
|
|
|
|
// 角色添加
|
|
public function groupadd(){
|
|
if(Request::isPost()){
|
|
$data['group_name'] = trim(input('post.group_name'));
|
|
if(!$data['group_name']){
|
|
$this->returnCode(1, '角色名称不能为空');
|
|
}
|
|
$data['status'] = (int)trim(input('post.status'));
|
|
$data['create_time'] = time();
|
|
$menus = input('post.menu/a');
|
|
if($menus){
|
|
$data['rights'] = json_encode(array_keys($menus));
|
|
}
|
|
$res = Db::table('yz_admin_user_group')->insert($data);
|
|
if(!$res){
|
|
$this->returnCode(1, '添加角色失败');
|
|
}
|
|
$this->returnCode(0);
|
|
}else{
|
|
$menus = Db::table('yz_admin_sys_menu')->order('type,sort desc')->where('status','=',1)->select();
|
|
$menu = [];
|
|
|
|
// 先处理所有父菜单
|
|
foreach($menus as $menus_v){
|
|
if($menus_v['parent_id'] == 0){
|
|
$menu[$menus_v['smid']] = $menus_v;
|
|
$menu[$menus_v['smid']]['children'] = []; // 初始化 children 数组
|
|
}
|
|
}
|
|
|
|
// 再处理子菜单
|
|
foreach($menus as $menus_v){
|
|
if($menus_v['parent_id'] != 0 && isset($menu[$menus_v['parent_id']])){
|
|
$menu[$menus_v['parent_id']]['children'][] = $menus_v;
|
|
}
|
|
}
|
|
|
|
View::assign([
|
|
'menus' => $menu
|
|
]);
|
|
return View::fetch();
|
|
}
|
|
}
|
|
|
|
// 角色编辑
|
|
public function groupedit(){
|
|
if(Request::isPost()){
|
|
$group_id = (int)trim(input('post.group_id'));
|
|
$data['group_name'] = trim(input('post.group_name'));
|
|
if(!$data['group_name']){
|
|
$this->returnCode(1, '角色名称不能为空');
|
|
}
|
|
$data['status'] = (int)trim(input('post.status'));
|
|
$menus = input('post.menu/a');
|
|
if($menus){
|
|
$data['rights'] = json_encode(array_keys($menus));
|
|
}else{
|
|
$data['rights'] = '';
|
|
}
|
|
$res = Db::table('yz_admin_user_group')->where('group_id',$group_id)->update($data);
|
|
if(!$res){
|
|
$this->returnCode(1, '更新角色失败');
|
|
}
|
|
$this->returnCode(0);
|
|
}else{
|
|
$group_id = (int)input('get.group_id');
|
|
$group = Db::table('yz_admin_user_group')->where('group_id',$group_id)->find();
|
|
if($group && $group['rights']){
|
|
$group['rights'] = json_decode($group['rights']);
|
|
}
|
|
|
|
$menus = Db::table('yz_admin_sys_menu')->order('type,sort desc')->where('status','=',1)->select();
|
|
$menu = [];
|
|
|
|
// 先处理所有父菜单
|
|
foreach($menus as $menus_v){
|
|
if($menus_v['parent_id'] == 0){
|
|
$menu[$menus_v['smid']] = $menus_v;
|
|
$menu[$menus_v['smid']]['children'] = []; // 初始化 children 数组
|
|
}
|
|
}
|
|
|
|
// 再处理子菜单
|
|
foreach($menus as $menus_v){
|
|
if($menus_v['parent_id'] != 0 && isset($menu[$menus_v['parent_id']])){
|
|
$menu[$menus_v['parent_id']]['children'][] = $menus_v;
|
|
}
|
|
}
|
|
|
|
View::assign([
|
|
'group' => $group,
|
|
'menus' => $menu
|
|
]);
|
|
return View::fetch();
|
|
}
|
|
}
|
|
|
|
// 角色删除
|
|
public function groupdel(){
|
|
$group_id = (int)input('post.group_id');
|
|
$res = Db::table('yz_admin_user_group')->where('group_id',$group_id)->delete();
|
|
if(empty($res)){
|
|
$this->returnCode(1, '删除角色失败');
|
|
}
|
|
$this->returnCode(0);
|
|
}
|
|
|
|
// 管理员列表
|
|
public function userinfo(){
|
|
$lists = Db::table('yz_admin_user')->select();
|
|
$group = [];
|
|
$groups = Db::table('yz_admin_user_group')->select();
|
|
foreach ($groups as $key => $value) {
|
|
$group[$value['group_id']] = $value;
|
|
}
|
|
View::assign([
|
|
'lists' => $lists,
|
|
'group' => $group
|
|
]);
|
|
return View::fetch();
|
|
}
|
|
|
|
// 管理员添加
|
|
public function useradd(){
|
|
if(Request::isPost()){
|
|
$data['account'] = trim(input('post.account'));
|
|
if(empty($data['account'])){
|
|
$this->returnCode(1, '账号不能为空');
|
|
}
|
|
$pattern = "/^([0-9A-Za-z-_.]+)@([0-9a-z]+.[a-z]{2,3}(.[a-z]{2})?)$/i";
|
|
if(!preg_match($pattern,$data['account'])){
|
|
$this->returnCode(1, '邮箱格式不正确');
|
|
}
|
|
$item = Db::table('yz_admin_user')->where('account',$data['account'])->find();
|
|
if($item){
|
|
$this->returnCode(1, '该账号已存在');
|
|
}
|
|
$data['name'] = trim(input('post.name'));
|
|
$data['phone'] = trim(input('post.phone'));
|
|
$data['qq'] = (int)trim(input('post.qq'));
|
|
$data['group_id'] = (int)input('post.group_id');
|
|
$data['sex'] = (int)(input('post.sex'));
|
|
$data['status'] = (int)(input('post.status'));
|
|
$password = trim(input('post.password'));
|
|
if(empty($data['name'])){
|
|
$this->returnCode(1, '姓名不能为空');
|
|
}
|
|
if(empty($data['phone'])){
|
|
$this->returnCode(1, '手机号不能为空');
|
|
}
|
|
if(empty($data['group_id'])){
|
|
$this->returnCode(1, '请选择角色');
|
|
}
|
|
if(empty($password)){
|
|
$this->returnCode(1, '密码不能为空');
|
|
}else{
|
|
$data['password'] = md5($password);
|
|
}
|
|
$data['create_time'] = time();
|
|
$data['update_time'] = time();
|
|
$res = Db::table('yz_admin_user')->insert($data);
|
|
if(!$res){
|
|
$this->returnCode(1, '添加管理员失败');
|
|
}
|
|
$this->returnCode(0);
|
|
}else{
|
|
$group = [];
|
|
$groups = Db::table('yz_admin_user_group')->select();
|
|
foreach ($groups as $key => $value) {
|
|
$group[$value['group_id']] = $value;
|
|
}
|
|
View::assign([
|
|
'group' => $group
|
|
]);
|
|
return View::fetch();
|
|
}
|
|
}
|
|
|
|
// 管理员编辑
|
|
public function useredit(){
|
|
if(Request::isPost()){
|
|
$uid = (int)trim(input('post.uid'));
|
|
$data['name'] = trim(input('post.name'));
|
|
$data['phone'] = trim(input('post.phone'));
|
|
$data['qq'] = (int)trim(input('post.qq'));
|
|
$data['group_id'] = (int)input('post.group_id');
|
|
$data['sex'] = (int)(input('post.sex'));
|
|
$data['status'] = (int)(input('post.status'));
|
|
if(empty($data['name'])){
|
|
$this->returnCode(1, '姓名不能为空');
|
|
}
|
|
if(empty($data['phone'])){
|
|
$this->returnCode(1, '手机号不能为空');
|
|
}
|
|
if(empty($data['group_id'])){
|
|
$this->returnCode(1, '请选择角色');
|
|
}
|
|
// 保存用户
|
|
$res = Db::table('yz_admin_user')->where('uid',$uid)->update($data);
|
|
if(!$res){
|
|
$this->returnCode(1, '更新管理员信息失败');
|
|
}
|
|
$this->returnCode(0);
|
|
}else{
|
|
$uid = (int)input('get.uid');
|
|
// 加载管理员
|
|
$lists = Db::table('yz_admin_user')->where('uid',$uid)->find();
|
|
// 加载角色
|
|
$group = [];
|
|
$groups = Db::table('yz_admin_user_group')->select();
|
|
foreach ($groups as $key => $value) {
|
|
$group[$value['group_id']] = $value;
|
|
}
|
|
View::assign([
|
|
'lists' => $lists,
|
|
'group' => $group
|
|
]);
|
|
return View::fetch();
|
|
}
|
|
}
|
|
|
|
// 管理员删除
|
|
public function userdel(){
|
|
$uid = (int)input('post.uid');
|
|
$res = Db::table('yz_admin_user')->where('uid',$uid)->delete();
|
|
if(empty($res)){
|
|
$this->returnCode(1, '删除管理员失败');
|
|
}
|
|
$this->returnCode(0);
|
|
}
|
|
|
|
// 管理员信息
|
|
public function admininfo(){
|
|
if(Request::isPost()){
|
|
$find = Db::table('yz_admin_user')->where('uid',$this->adminId)->find();
|
|
if(empty($find)){
|
|
$this->returnCode(1,'当前账户不存在');
|
|
}
|
|
$data['name'] = trim(input('post.name'));
|
|
$data['phone'] = trim(input('post.phone'));
|
|
$data['qq'] = (int)trim(input('post.qq'));
|
|
$data['sex'] = (int)(input('post.sex'));
|
|
if(empty($data['name'])){
|
|
$this->returnCode(1, '姓名不能为空');
|
|
}
|
|
if(empty($data['phone'])){
|
|
$this->returnCode(1, '手机号不能为空');
|
|
}
|
|
|
|
// 处理密码修改
|
|
$old_pw = trim(input('post.old_pw'));
|
|
$new_pw = trim(input('post.new_pw'));
|
|
if(!empty($old_pw) && !empty($new_pw)){
|
|
if(md5($old_pw) != $find['password']){
|
|
$this->returnCode(1, '原密码错误');
|
|
}
|
|
$data['password'] = md5($new_pw);
|
|
}
|
|
|
|
// 保存用户
|
|
$res = Db::table('yz_admin_user')->where('uid',$this->adminId)->update($data);
|
|
if(!$res){
|
|
$this->returnCode(1, '更新管理员信息失败');
|
|
}
|
|
$this->returnCode(0);
|
|
}else{
|
|
return View::fetch();
|
|
}
|
|
}
|
|
|
|
} |