心理咨询网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RoleController.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2017年4月3日
  7. * 角色控制器
  8. */
  9. namespace app\admin\controller\system;
  10. use core\basic\Controller;
  11. use app\admin\model\system\RoleModel;
  12. class RoleController extends Controller
  13. {
  14. private $blank;
  15. private $model;
  16. public function __construct()
  17. {
  18. $this->model = new RoleModel();
  19. }
  20. // 角色列表
  21. public function index()
  22. {
  23. $this->assign('list', true);
  24. $this->assign('roles', $this->model->getList());
  25. // 数据区域选择
  26. $area_model = model('admin.system.Area');
  27. $area_tree = $area_model->getSelect();
  28. $area_checkbox = $this->makeAreaCheckbox($area_tree);
  29. $this->assign('area_checkbox', $area_checkbox);
  30. // 菜单权限表
  31. $menu_model = model('admin.system.Menu');
  32. $menu_level = $menu_model->getMenuLevel();
  33. $menus = $menu_model->getSelect();
  34. $menu_list = $this->makeLevelList($menus, $menu_level);
  35. $this->assign('menu_list', $menu_list);
  36. $this->display('system/role.html');
  37. }
  38. // 角色增加
  39. public function add()
  40. {
  41. if ($_POST) {
  42. // 获取数据
  43. $rcode = get_auto_code($this->model->getLastCode()); // 自动编码
  44. $name = post('name');
  45. $description = post('description');
  46. $acodes = post('acodes', 'array', false, '角色数据区域', array()); // 区域
  47. $levels = post('levels', 'array', false, '角色权限', array()); // 权限
  48. if (! $rcode) {
  49. alert_back('编码不能为空!');
  50. }
  51. if (! $name) {
  52. alert_back('角色名不能为空!');
  53. }
  54. // 检查编码
  55. if ($this->model->checkRole("rcode='$rcode'")) {
  56. alert_back('该角色编号已经存在,不能再使用!');
  57. }
  58. // 构建数据
  59. $data = array(
  60. 'rcode' => $rcode,
  61. 'name' => $name,
  62. 'description' => $description,
  63. 'create_user' => session('username'),
  64. 'update_user' => session('username')
  65. );
  66. // 执行添加
  67. if ($this->model->addRole($data, $acodes, $levels)) {
  68. $this->log('修改角色' . $rcode . '成功!');
  69. if (! ! $backurl = get('backurl')) {
  70. success('新增成功!', base64_decode($backurl));
  71. } else {
  72. success('新增成功!', url('admin/Role/index'));
  73. }
  74. } else {
  75. $this->log('修改角色' . $rcode . '失败!');
  76. error('新增失败!', - 1);
  77. }
  78. }
  79. }
  80. // 生成区域选择,无限制
  81. private function makeAreaCheckbox($tree, $checkeds = array())
  82. {
  83. $list_html = '';
  84. foreach ($tree as $values) {
  85. if (in_array($values->acode, $checkeds)) {
  86. $checked = 'checked="checked"';
  87. } else {
  88. $checked = '';
  89. }
  90. if (! $values->son) { // 没有子类才显示选择框
  91. $list_html .= "<input type='checkbox' $checked name='acodes[]' value='{$values->acode}' title='{$values->acode}-{$values->name}'>";
  92. } else {
  93. $list_html .= $this->makeAreaCheckbox($values->son, $checkeds);
  94. }
  95. }
  96. return $list_html;
  97. }
  98. // 生成无限级菜单权限列表
  99. private function makeLevelList($menus, $menu_level, $checkeds = array())
  100. {
  101. $menu_html = '';
  102. foreach ($menus as $value) {
  103. $string = '';
  104. // 根据是否有子栏目生成图标
  105. if ($value->son) {
  106. $ico = "<i class='fa fa-folder-open-o' aria-hidden='true'></i>";
  107. } else {
  108. $ico = "<i class='fa fa-folder-o' aria-hidden='true'></i>";
  109. }
  110. // 选中状态
  111. if (in_array($value->url, $checkeds)) {
  112. $checked = 'checked="checked"';
  113. } else {
  114. $checked = '';
  115. }
  116. // 获取模块及控制器路径
  117. if ($value->url) {
  118. $pre_url = substr($value->url, 0, get_strpos($value->url, '/', 3) + 1);
  119. } else {
  120. error('"' . $value->name . '"菜单地址为空,请核对!');
  121. }
  122. $string = "<input type='checkbox' $checked class='checkbox' lay-skin='primary' name='levels[]' value='" . $value->url . "' title='浏览'>";
  123. $mcode = $value->mcode;
  124. if (array_key_exists($mcode, $menu_level)) {
  125. foreach ($menu_level[$mcode] as $key2 => $value2) {
  126. $url = $pre_url . $value2->value;
  127. if (in_array($url, $checkeds)) {
  128. $checked = 'checked="checked"';
  129. } else {
  130. $checked = '';
  131. }
  132. $string .= "<input type='checkbox' $checked class='checkbox'lay-skin='primary' name='levels[]' value='$url' title='{$value2->item}'>";
  133. }
  134. }
  135. // 生成菜单html
  136. $menu_html .= "<div class='layui-row'><div class='layui-col-md3 layui-col-lg2' style='margin-top:10px;'>{$this->blank} $ico {$value->name}</div><div class='layui-col-md9'>$string</div></div>";
  137. // 子菜单处理
  138. if ($value->son) {
  139. $this->blank .= '  ';
  140. $menu_html .= $this->makeLevelList($value->son, $menu_level, $checkeds);
  141. }
  142. }
  143. // 循环完后回归缩进位置
  144. $this->blank = substr($this->blank, 0, - 6);
  145. return $menu_html;
  146. }
  147. // 角色删除
  148. public function del()
  149. {
  150. if (! $rcode = get('rcode', 'var')) {
  151. error('传递的参数值错误!', - 1);
  152. }
  153. if ($this->model->delRole($rcode)) {
  154. $this->log('删除角色' . $rcode . '成功!');
  155. success('删除成功!', - 1);
  156. } else {
  157. $this->log('删除角色' . $rcode . '失败!');
  158. error('删除失败!', - 1);
  159. }
  160. }
  161. // 角色修改
  162. public function mod()
  163. {
  164. if (! $rcode = get('rcode', 'var')) {
  165. error('传递的参数值错误!', - 1);
  166. }
  167. // 修改操作
  168. if ($_POST) {
  169. // 获取数据
  170. $name = post('name');
  171. $description = post('description');
  172. $acodes = post('acodes', 'array', false, '角色数据区域', array()); // 区域
  173. $levels = post('levels', 'array', false, '角色权限', array()); // 权限
  174. if (! $name) {
  175. alert_back('角色名不能为空!');
  176. }
  177. // 构建数据
  178. $data = array(
  179. 'name' => $name,
  180. 'description' => $description,
  181. 'update_user' => session('username')
  182. );
  183. // 执行修改
  184. if ($this->model->modRole($rcode, $data, $acodes, $levels)) {
  185. $this->log('修改角色' . $rcode . '成功!');
  186. if (! ! $backurl = get('backurl')) {
  187. success('修改成功!', base64_decode($backurl));
  188. } else {
  189. success('修改成功!', url('admin/Role/index'));
  190. }
  191. } else {
  192. location(- 1);
  193. }
  194. } else {
  195. $this->assign('mod', true);
  196. // 调取修改内容
  197. $result = $this->model->getRole($rcode);
  198. if (! $result) {
  199. error('编辑的内容已经不存在!', - 1);
  200. }
  201. $this->assign('role', $result);
  202. // 数据区域选择
  203. $area_model = model('admin.system.Area');
  204. $area_tree = $area_model->getSelect();
  205. $area_checkbox = $this->makeAreaCheckbox($area_tree, $result->acodes);
  206. $this->assign('area_checkbox', $area_checkbox);
  207. // 菜单权限表
  208. $menu_model = model('admin.system.Menu');
  209. $menu_level = $menu_model->getMenuLevel();
  210. $menus = $menu_model->getSelect();
  211. $menu_list = $this->makeLevelList($menus, $menu_level, $result->levels);
  212. $this->assign('menu_list', $menu_list);
  213. $this->display('system/role.html');
  214. }
  215. }
  216. }