123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * 项目管理逻辑类
  4. *
  5. * @author qifan
  6. * @date 2020-12-17
  7. */
  8. namespace app\admin\logic;
  9. use app\admin\controller\ApiCommon;
  10. use think\Db;
  11. class WorkLogic
  12. {
  13. /**
  14. * 规则列表
  15. *
  16. * @return bool|\PDOStatement|string|\think\Collection
  17. * @throws \think\db\exception\DataNotFoundException
  18. * @throws \think\db\exception\ModelNotFoundException
  19. * @throws \think\exception\DbException
  20. */
  21. public function getRules()
  22. {
  23. return Db::name('admin_rule')->field(['id', 'title', 'name'])->where(['types' => 3, 'level' => 4, 'status' => 0])->select();
  24. }
  25. /**
  26. * 获取角色
  27. *
  28. * @return bool|\PDOStatement|string|\think\Collection
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. * @throws \think\exception\DbException
  32. */
  33. public function getRoles()
  34. {
  35. $data = Db::name('admin_group')->field(['id', 'title', 'rules', 'remark', 'system'])->where(['pid' => 5, 'types' => 7, 'status' => 1])->select();
  36. foreach ($data AS $key => $value) {
  37. $data[$key]['rules'] = explode(',', trim($value['rules'], ','));
  38. }
  39. return $data;
  40. }
  41. /**
  42. * 创建角色
  43. *
  44. * @param $param
  45. * @return int|string
  46. */
  47. public function saveRole($param)
  48. {
  49. # 设置参数
  50. $param['pid'] = 5;
  51. $param['status'] = 1;
  52. $param['type'] = 0;
  53. $param['types'] = 7;
  54. $param['system'] = 0;
  55. $data=Db::name('admin_group')->insertGetId($param);
  56. return $data;
  57. }
  58. /**
  59. * 权限角色详情
  60. *
  61. * @param $id
  62. * @return array|bool|\PDOStatement|string|\think\Model|null
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. * @throws \think\exception\DbException
  66. */
  67. public function readRole($id)
  68. {
  69. $data = Db::name('admin_group')->field(['id', 'title', 'rules', 'remark'])->where('id', $id)->find();
  70. $data['rules'] = trim($data['rules'], ',');
  71. return $data;
  72. }
  73. /**
  74. * 编辑权限角色
  75. *
  76. * @param $param
  77. * @return int|string
  78. * @throws \think\Exception
  79. * @throws \think\exception\PDOException
  80. */
  81. public function updateRole($param)
  82. {
  83. $res=Db::name('admin_group')->where('id',$param['id'])->find();
  84. $userInd=$param['user_id'];
  85. unset($param['user_id']);
  86. if(!$res){
  87. return false;
  88. }else{
  89. $data=Db::name('admin_group')->update($param);
  90. # 添加系统操作日志
  91. SystemActionLog($userInd, 'admin_group','project', $param['id'], 'update',$res['title'] , '', '','编辑了项目管理权限:'.$res['title']);
  92. return $data;
  93. }
  94. }
  95. /**
  96. * 删除权限角色
  97. *
  98. * @param $id
  99. * @return array|bool[]
  100. * @throws \think\Exception
  101. * @throws \think\exception\PDOException
  102. */
  103. public function deleteRole($id,$userId)
  104. {
  105. $system = Db::name('admin_group')->where('id', $id)->find();
  106. if (!empty($system['system'])) return ['status' => false, 'error' => '不允许删除系统默认角色!'];
  107. if (!Db::name('admin_group')->where('id', $id)->delete()) return ['status' => false, 'error' => '操作失败!'];
  108. # 将项目权限变更为只读权限
  109. $readOnlyId = db('admin_group')->where(['title' => '只读', 'types' => 7, 'system' => 1])->value('id');
  110. if (!empty($readOnlyId)) {
  111. db('work')->where('group_id', $id)->update(['group_id' => $readOnlyId]); # 处理公开项目的权限
  112. db('work_user')->where('group_id', $id)->update(['group_id' => $readOnlyId]); # 处理私有项目的权限
  113. }
  114. # 添加系统操作日志
  115. SystemActionLog($userId, 'admin_group','project', $id, 'update',$system['title'] , '', '','删除了项目管理权限:'.$system['title']);
  116. return ['status' => true];
  117. }
  118. }