WorkLogic.php 3.3KB

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