RoleModel.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. $param['create_timestamp'] = $param['update_timestamp'] = date('Y-m-d H:i:s');
  60. $result = $this->save($param);
  61. if(false === $result){
  62. // 验证失败 输出错误信息
  63. return msg(-1, '', $this->getError());
  64. }else{
  65. return msg(1, '', '添加角色成功');
  66. }
  67. }catch(PDOException $e){
  68. return msg(-2, '', $e->getMessage());
  69. }
  70. }
  71. /**
  72. * 删除角色
  73. * @param $id
  74. */
  75. public function delRole($id)
  76. {
  77. try{
  78. $this->where('id', $id)->delete();
  79. return msg(1, '', '删除角色成功');
  80. }catch(PDOException $e){
  81. return msg(-1, '', $e->getMessage());
  82. }
  83. }
  84. // 获取所有的角色信息
  85. public function getRole()
  86. {
  87. return $this->select();
  88. }
  89. }