Role.php 16 KB

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