Role.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\admin\model\ActionProcess as APModel;
  5. use app\admin\model\Process as PModel;
  6. use think\App;
  7. use think\facade\Db;
  8. use think\facade\Validate;
  9. class Role extends Base{
  10. public function __construct(App $app) {parent::__construct($app);}
  11. /**角色权限列表分页 page size
  12. * @return \think\response\Json|void
  13. * @throws \think\db\exception\DataNotFoundException
  14. * @throws \think\db\exception\DbException
  15. * @throws \think\db\exception\ModelNotFoundException
  16. */
  17. public function roleList()
  18. {
  19. $post = $this->request->only(["status" => "", "role_name" => "", "relaComNo" => "", "companyName" => "", "companyNo" => "", "level" => "", "page" => 1, "size" => 10], "post", "trim");
  20. $condition = [["a.level", "<>", 1]];
  21. isset($post['status']) && $post['status'] !== "" ? $condition[] = ['a.status', "=", $post['status']] : "";
  22. isset($post['role_name']) && $post['role_name'] !== "" ? $condition[] = ['a.role_name', "like", "%" . $post['role_name'] . "%"] : "";
  23. isset($post['level']) && $post['level'] !== "" ? $condition[] = ["a.level", "=", $post['level']] : "";
  24. isset($post['relaComNo']) && $post['relaComNo'] !== "" ? $condition[] = ["a.companyNo", "=", $post['relaComNo']] : "";
  25. isset($post['companyNo']) && $post['companyNo'] !== "" ? $condition[] = ["a.companyNo", "=", $post['companyNo']] : "";
  26. isset($post['companyName']) && $post['companyName'] !== "" ? $condition[] = ["b.name", "like", "%{$post['companyName']}%"] : "";
  27. $count = Db::name("role")->alias("a")
  28. ->leftJoin("supplier_info b", "a.companyNo=b.code")
  29. ->where($condition)->count();
  30. $page = isset($post['page']) ? intval($post['page']) : 1;
  31. $size = isset($post['size']) ? intval($post['size']) : 10;
  32. $page >= ceil($count / $size) ? $page = (int)ceil($count / $size) : "";
  33. $list = Db::name("role")->alias("a")
  34. ->leftJoin("supplier_info b", "a.companyNo=b.code")
  35. // ->leftJoin("role_action t","a.id=t.role_id")
  36. ->field("a.*,b.name as companyName")
  37. ->order("a.addtime desc")
  38. ->where($condition)->page($page, $size)->select();
  39. return app_show(0, "获取成功", ["list" => $list, "count" => $count]);
  40. }
  41. /** 角色新建
  42. * @role_name 角色名称
  43. * @level 角色等级
  44. * @action 功能权限
  45. * @action_data 操作权限
  46. * @private_data 私有权限
  47. * @return \think\response\Json|void
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public function roleAdd(){
  53. $post=$this->request->only(["role_name"=>"","level"=>"","action"=>"","action_data"=>"","private_data"=>"","relaComNo"=>"","companyNo"=>""],"post");
  54. $valid =Validate::rule([
  55. "role_name|角色名称"=>"require|max:255|min:2",
  56. "level|角色级别"=>"require|number|in:1,2,3",
  57. "relaComNo|业务公司编号"=>"max:255",
  58. "companyNo|业务公司编号"=>"max:255",
  59. "action|角色权限"=>"require|array",
  60. ]);
  61. if($valid->check($post)==false)return error_show(1004,$valid->getError());
  62. if ($post['relaComNo'] != "" && $post["level"] == 1) {
  63. return error_show(1002, "超管权限角色不可关联业务公司");
  64. }
  65. if ($post['relaComNo'] != "" && $post["level"] == 3) {
  66. return error_show(1002, "供应商权限角色不可关联业务公司");
  67. }
  68. if($this->level==1){
  69. $companyNo =$post['companyNo']??"";
  70. }else{
  71. $companyNo =$post['relaComNo']??"";
  72. }
  73. $where=[["role_name","=",$post['role_name']]];
  74. if($companyNo!=""){
  75. $where[]=["companyNo","=",$companyNo];
  76. // $company =Db::name("supplier_info")->where(["code"=>$companyNo])->find();
  77. }
  78. $isT=Db::name("role")->where($where)->find();
  79. if($isT){
  80. return error_show(1002,"角色名称已存在");
  81. }
  82. $action = isset($post['action'])&&is_array($post['action']) ? json_encode($post['action']) : "";
  83. $data = isset($post['action_data']) &&is_array($post['action_data'])?json_encode($post['action_data']): "";
  84. $private_data = isset($post['private_data']) &&is_array($post['private_data'])?json_encode($post['private_data']): "";
  85. Db::startTrans();
  86. try {
  87. $list = [
  88. "role_name"=>$post['role_name'],
  89. "companyNo"=>$companyNo,
  90. "status"=>1,
  91. "addtime"=>date("Y-m-d H:i:s"),
  92. "updatetime"=>date("Y-m-d H:i:s"),
  93. "level"=>$post['level'],
  94. ];
  95. $role= Db::name("role")->insert($list,true);
  96. if($role>0){
  97. $role_action = [
  98. "role_id"=>$role,
  99. "action_conllect"=>$action,
  100. "action_data"=>$data,
  101. "private_data"=>$private_data,
  102. "status"=>1,
  103. "addtime"=>date("Y-m-d H:i:s"),
  104. "updatetime"=>date("Y-m-d H:i:s"),
  105. ];
  106. $roleaction= Db::name("role_action")->insert($role_action,true);
  107. if($roleaction){
  108. Db::commit();
  109. return app_show(0,"新建成功");
  110. }else{
  111. Db::rollback();
  112. return app_show(1005,"权限录入失败");
  113. }
  114. }else{
  115. Db::rollback();
  116. return app_show(1006,"角色新建失败");
  117. }
  118. }catch (\Exception $e){
  119. Db::rollback();
  120. return error_show(1008,$e->getMessage());
  121. }
  122. }
  123. /**查询角色信息
  124. * @roleid 角色id
  125. * @return \think\response\Json|void
  126. * @throws \think\db\exception\DataNotFoundException
  127. * @throws \think\db\exception\DbException
  128. * @throws \think\db\exception\ModelNotFoundException
  129. */
  130. public function roleInfo(){
  131. $post=$this->post;
  132. $roleid= isset($post['roleid']) ? intval($post['roleid']) : "";
  133. if($roleid==""){
  134. return error_show(1001,'roleid不能为空');
  135. }
  136. $info =Db::name("role")->alias("a")
  137. ->leftJoin("cfp_role_action t","a.id=t.role_id")
  138. ->field("a.*,t.action_conllect,t.action_data,t.private_data")
  139. ->json(['action_conllect',"action_data","private_data"])
  140. ->where("a.id","=",$roleid)->find();
  141. if(!$info){
  142. return error_show(1002,"未找到对应的数据");
  143. }
  144. $info['action'] = $info['action_conllect'] ;
  145. return app_show(0,"获取成功",$info);
  146. }
  147. /** 角色信息编辑
  148. * @param role_name 角色名称
  149. * @param level 等级
  150. * @param action 功能权限
  151. * @param action_data 操作权限
  152. * @param roleid 角色id
  153. * @param private_data 私有权限
  154. * @return \think\response\Json|void
  155. * @throws \think\db\exception\DataNotFoundException
  156. * @throws \think\db\exception\DbException
  157. * @throws \think\db\exception\ModelNotFoundException
  158. */
  159. public function roleSave(){
  160. $post=$this->request->only(["roleid"=>"","role_name"=>"","level"=>"","relaComNo"=>"","companyNo"=>"","action"=>"","action_data"=>"","private_data"=>""],"post");
  161. $valid=Validate::rule([
  162. "role_name|角色名称"=>"require|max:255|min:2",
  163. "relaComNo|业务公司编号"=>"max:255",
  164. "companyNo|业务公司编号"=>"max:255",
  165. "action|角色权限"=>"require|array",
  166. "level|角色级别"=>"require|in:1,2,3",
  167. ]);
  168. if($valid->check($post)==false)return error_show(1004,$valid->getError());
  169. $info =Db::name("role")->where("id","=",$post['roleid'])->find();
  170. if(!$info){
  171. return error_show(1002,"未找到对应的数据");
  172. }
  173. if ($post['relaComNo'] != "" && $info["level"] == 1) {
  174. return error_show(1002, "超管权限角色不可关联业务公司");
  175. }
  176. if ($post['relaComNo'] != "" && $info["level"] == 3) {
  177. return error_show(1002, "供应商权限角色不可关联业务公司");
  178. }
  179. // if($this->level==1){
  180. // $companyNo =$post['companyNo']??"";
  181. // }else{
  182. // $companyNo =$post['relaComNo']??"";
  183. // }
  184. $vers= Db::name("role")->where([["id",'<>',$post['roleid']],["role_name","=",$post['role_name']], ["companyNo","=",$info['companyNo']]])->find();
  185. if($vers){
  186. return error_show(1002,"角色名已存在");
  187. }
  188. $action = isset($post['action'])&&is_array($post['action']) ?json_encode($post['action']) : "";
  189. if($action==""){
  190. return error_show(1004,"功能权限不能为空");
  191. }
  192. $data = isset($post['action_data']) &&is_array($post['action_data'])?json_encode($post['action_data']): "";
  193. $private_data = isset($post['private_data']) &&is_array($post['private_data'])?json_encode($post['private_data']): "";
  194. Db::startTrans();
  195. try {
  196. $list = [
  197. "role_name"=>$post['role_name'],
  198. "level"=>$post['level'],
  199. "updatetime"=>date("Y-m-d H:i:s"),
  200. 'id'=>$post['roleid']
  201. ];
  202. $role= Db::name("role")->save($list);
  203. if($role){
  204. $role_action = [
  205. "role_id"=>$post['roleid'],
  206. "action_conllect"=>$action,
  207. "action_data"=>$data,
  208. "private_data"=>$private_data,
  209. "updatetime"=>date("Y-m-d H:i:s")
  210. ];
  211. $roleaction= Db::name("role_action")->where("role_id","=",$post['roleid'])->update($role_action);
  212. if($roleaction){
  213. Db::commit();
  214. return app_show(0,"更新成功");
  215. }else{
  216. Db::rollback();
  217. return app_show(1005,"权限更新失败");
  218. }
  219. }else{
  220. Db::rollback();
  221. return app_show(1006,"角色更新失败");
  222. }
  223. }catch (\Exception $e){
  224. Db::rollback();
  225. return error_show(1008,$e->getMessage());
  226. }
  227. }
  228. /**
  229. * 角色状态更新
  230. * @roleid 角色id
  231. * @status 角色状态 0 /1
  232. * @return \think\response\Json
  233. * @throws \think\exception\DbException
  234. */
  235. public function roleStatus(){
  236. $post=$this->request->only(["roleid"=>"","status"=>""],"post","intval");
  237. $valid=Validate::rule([
  238. "roleid|角色id"=>"require|number|gt:0",
  239. "status|状态"=>"require|number|in:0,1"
  240. ]);
  241. if($valid->check($post)==false)return error_show(1004,$valid->getError());
  242. Db::startTrans();
  243. try {
  244. $dat=[
  245. 'status'=>$post['status'],'updatetime'=>date("Y-m-d H:i:s"), 'id'=>$post['roleid']
  246. ];
  247. $re = Db::name("role")->save($dat);
  248. if($re){
  249. $dat2=[
  250. 'status'=>$post['status'],'updatetime'=>date("Y-m-d H:i:s")
  251. ];
  252. $action = Db::name("role_action")->where("role_id","=",$post['roleid'])->update($dat2);
  253. if($action){
  254. Db::commit();
  255. return app_show(0,"状态更新成功");
  256. }
  257. }
  258. Db::rollback();
  259. return error_show(1003,"状态更新失败");
  260. }catch (\Exception $e){
  261. Db::rollback();
  262. return error_show(1002,$e->getMessage());
  263. }
  264. }
  265. /**
  266. * 获取所有角色
  267. * @return \think\response\Json
  268. * @throws \think\exception\DbException
  269. */
  270. public function RoleAll(){
  271. $post=$this->request->only(["relaComNo"=>"","companyNo"=>""],"post","trim");
  272. $where=[["a.level","<>",1]];
  273. if(isset($post['relaComNo']) && $post['relaComNo']!='')$where[]=["companyNo","=",$post['relaComNo']];
  274. if(isset($post['companyNo'])&& $post['companyNo']!='')$where[]=["companyNo","=",$post['companyNo']];
  275. $list =Db::name("role")->alias("a")
  276. ->leftJoin("supplier_info b","a.companyNo=b.code")
  277. ->field("a.*,b.name as companyName")
  278. ->where($where)->select();
  279. return app_show(0,"获取成功",$list);
  280. }
  281. //获取角色对应的流程权限id
  282. public function roleProcessInfo()
  283. {
  284. $roleid = $this->request->filter('trim')->post('roleid/d', 0);
  285. $res = Db::name("role")
  286. ->alias("a")
  287. ->leftJoin("role_process b", "a.id=b.role_id AND b.is_del=0")
  288. ->field("a.*,b.action_data")
  289. ->withAttr('action_data', function ($val) {
  290. return explode(',', $val);
  291. })->where("a.id", $roleid)
  292. ->findOrEmpty();
  293. return app_show(0, '请求成功', $res);
  294. }
  295. //修改角色对应的流程权限
  296. public function roleProcessSave()
  297. {
  298. $param = $this->request->filter('trim')->only(['token', 'roleid', 'action_data'], 'post');
  299. $val = Validate::rule([
  300. 'token' => 'require',
  301. 'roleid|角色ID' => 'require|number|gt:0',
  302. 'action_data|所选节点id' => 'require|array',
  303. ]);
  304. if (!$val->check($param)) return error_show(1005, $val->getError());
  305. $user = GetUserInfo($param['token']);
  306. $uid = isset($user['data']['id']) ? $user['data']['id'] : 0;
  307. $uname = isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
  308. $date = date('Y-m-d H:i:s');
  309. $res = Db::name('role_process')
  310. ->where(['is_del' => 0, 'role_id' => $param['roleid']])
  311. ->field('id')
  312. ->findOrEmpty();
  313. if (empty($res)) {
  314. $rs = Db::name('role_process')
  315. ->insert([
  316. 'role_id' => $param['roleid'],
  317. 'action_data' => implode(',', $param['action_data']),
  318. 'is_del' => 0,
  319. 'createrid' => $uid,
  320. 'creater' => $uname,
  321. 'addtime' => $date,
  322. 'updaterid' => $uid,
  323. 'updater' => $uname,
  324. 'updatetime' => $date,
  325. ]);
  326. } else {
  327. $rs = Db::name('role_process')
  328. ->where('id', $res['id'])
  329. ->update([
  330. 'action_data' => implode(',', $param['action_data']),
  331. 'updaterid' => $uid,
  332. 'updater' => $uname,
  333. 'updatetime' => $date
  334. ]);
  335. }
  336. return $rs ? app_show(0, '操作成功') : error_show(1005, '操作失败');
  337. }
  338. //获取角色对应的流程权限详情
  339. public function roleProcessDetail()
  340. {
  341. $roleid = $this->request->filter('trim')->post('roleid/d', 0);
  342. $action_data = Db::name("role_process")
  343. ->where('role_id', $roleid)
  344. ->value('action_data');
  345. $data = PModel::where(['is_del' => PModel::$is_del_normal, 'status' => PModel::$status_normal])
  346. ->append(['child'])
  347. ->withAttr('child', function () {
  348. return [];
  349. })
  350. ->column('id,process_name,process_type', 'id');
  351. $action = APModel::where([
  352. 'is_del' => APModel::$is_del_normal,
  353. 'status' => APModel::$status_normal,
  354. 'operation_type' => APModel::$operation_type_approval
  355. ])->whereIn('id', $action_data)
  356. ->field('id,process_id,order_process,status_name')
  357. ->cursor();
  358. foreach ($action as $item) {
  359. if (isset($data[$item->process_id])) $data[$item->process_id]['child'][] = $item->toArray();
  360. }
  361. return app_show(0, '请求成功', array_column($data, null, null));
  362. }
  363. }