RoleModel.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace app\index\model;
  3. use think\Model;
  4. use app\index\model\NodeModel;
  5. class RoleModel extends Model{
  6. // 确定链接表名
  7. protected $name = 'role';
  8. /**
  9. * 获取角色信息
  10. * @param $id
  11. */
  12. public function getRoleInfo($id)
  13. {
  14. $result = $this->where('id', $id)->find()->toArray();
  15. // 超级管理员权限是 *
  16. if(empty($result['rule'])){
  17. $result['action'] = '';
  18. return $result;
  19. }else if('*' == $result['rule']){
  20. $where = '';
  21. }else{
  22. $where = 'id in(' . $result['rule'] . ')';
  23. }
  24. // 查询权限节点
  25. $nodeModel = new NodeModel();
  26. $res = $nodeModel->getActions($where);
  27. foreach($res as $key=>$vo){
  28. if('#' != $vo['action_name']){
  29. $result['action'][] = $vo['control_name'] . '/' . $vo['action_name'];
  30. }
  31. }
  32. return $result;
  33. }
  34. /**
  35. * 根据搜索条件获取角色列表信息
  36. * @param $where
  37. * @param $offset
  38. * @param $limit
  39. */
  40. public function getRoleByWhere($where, $offset, $limit)
  41. {
  42. return $this->where($where)->limit($offset, $limit)->order('id desc')->select();
  43. }
  44. /**
  45. * 根据搜索条件获取所有的角色数量
  46. * @param $where
  47. */
  48. public function getAllRole($where)
  49. {
  50. return $this->where($where)->count();
  51. }
  52. /**
  53. * 插入角色信息
  54. * @param $param
  55. */
  56. public function insertRole($param)
  57. {
  58. try{
  59. $result = $this->validate('RoleValidate')->save($param);
  60. if(false === $result){
  61. // 验证失败 输出错误信息
  62. return msg(-1, '', $this->getError());
  63. }else{
  64. return msg(1, url('role/index'), '添加角色成功');
  65. }
  66. }catch(PDOException $e){
  67. return msg(-2, '', $e->getMessage());
  68. }
  69. }
  70. /**
  71. * 删除角色
  72. * @param $id
  73. */
  74. public function delRole($id)
  75. {
  76. try{
  77. $this->where('id', $id)->delete();
  78. return msg(1, '', '删除角色成功');
  79. }catch(PDOException $e){
  80. return msg(-1, '', $e->getMessage());
  81. }
  82. }
  83. // 获取所有的角色信息
  84. public function getRole()
  85. {
  86. return $this->select();
  87. }
  88. }