Role.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\App;
  5. use think\facade\Db;
  6. use app\admin\model\ActionLog;
  7. use think\facade\Validate;
  8. use app\admin\model\ActionProcess as APModel;
  9. use app\admin\model\Process as PModel;
  10. //角色
  11. class Role extends BaseController
  12. {
  13. public function __construct(App $app)
  14. {
  15. parent::__construct($app);
  16. $post =$this->request->post();
  17. $token = isset($post['token']) ? trim($post['token']) : "";
  18. if($token==""){
  19. return error_show(101,'token不能为空');
  20. }
  21. $effetc = VerifyTokens($token);
  22. if(!empty($effetc) && $effetc['code']!=0){
  23. return error_show($effetc['code'],$effetc['message']);
  24. }
  25. }
  26. /**
  27. * @param status
  28. * @param role_name
  29. * @param level
  30. * @param page
  31. * @param size
  32. * @return \think\response\Json
  33. * @throws \think\exception\DbException
  34. */
  35. public function RoleList(){
  36. $post =$this->request->post();
  37. $condition=[];
  38. isset($post['status'])&&$post['status']!=="" ? $condition[]=['a.status',"=",$post['status']]:"";
  39. isset($post['role_name'])&&$post['role_name']!=="" ? $condition[]=['a.role_name',"like","%".$post['role_name']."%"]:"";
  40. isset($post['item'])&&$post['item']!=="" ? $condition[]=["a.item","=",$post['item']]:"";
  41. isset($post['createrId'])&&$post['createrId']!=="" ? $condition[]=["a.createrId","=",$post['createrId']]:"";
  42. $roleList = Db::name("role");
  43. $count =$roleList->alias("a")->where($condition)->count();
  44. $page = isset($post['page'])&& $post['page']!=''? intval($post['page']) : 1;
  45. $size = isset($post['size'])&& $post['size']!=''? intval($post['size']) : 10;
  46. $page >=ceil($count/$size) ? $page=ceil($count/$size) :"";
  47. $list=$roleList->alias("a")->leftJoin("role_action t","a.id=t.role_id")->leftJoin("company_item i","a
  48. .item=i.id")->field("a.*,i.name as item_name,t.action_conllect as action,t.action_data")->where($condition)
  49. ->page($page,$size)->select();
  50. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  51. }
  52. /**
  53. * @param role_name
  54. * @param level
  55. * @param action
  56. * @param action_data
  57. * @param role_name
  58. * @return \think\response\Json
  59. * @throws \think\exception\DbException
  60. */
  61. public function RoleAdd(){
  62. $post =$this->request->post();
  63. $token = isset($post['token']) ? trim($post['token']) : "";
  64. if($token==""){
  65. return error_show(101,'token不能为空');
  66. }
  67. $effetc = VerifyTokens($token);
  68. if(!empty($effetc) && $effetc['code']!=0){
  69. return error_show($effetc['code'],$effetc['message']);
  70. }
  71. $creater= GetUserInfo($post['token']);
  72. if(isset($creater['code'])&&$creater['code']==0){
  73. $uid= isset($userinfo['data']['id']) ? $userinfo['data']['id']: "0";
  74. $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: "";
  75. }else{
  76. $username = '';
  77. $uid= 0;
  78. }
  79. $rolename = isset($post['role_name']) ? trim($post['role_name']) : "";
  80. if($rolename==""){
  81. return error_show(1002,"角色名称不能为空");
  82. }
  83. $isT=Db::name("role")->where(['role_name'=>$rolename])->find();
  84. if($isT){
  85. return error_show(1002,"角色名称已存在");
  86. }
  87. $role_code= isset($post['role_code'])&&$post['role_code']!=''?trim($post['role_code']) :"";
  88. // if($role_code==""){
  89. // return error_show(1002,"参数role_code 不能为空");
  90. // }
  91. // $isCode=Db::name("role")->where(['role_code'=>$role_code])->find();
  92. // if($isCode){
  93. // return error_show(1002,"角色编号已存在");
  94. // }
  95. // $item = isset($post['item']) ? trim($post['item']) : "";
  96. // if($item==""){
  97. // return error_show(1003,"参数item 不能为空");
  98. // }
  99. // $iteminfo = Db::name('company_item')->where('id',"=",$item)->find();
  100. // if(empty($iteminfo) || $iteminfo['is_del']==1){
  101. // return error_show(1003,"该部门不存在");
  102. // }
  103. $work_company = isset($post['work_company']) && $post['work_company'] !==""? intval($post['work_company']):"";
  104. // if($work_company==""){
  105. // return error_show(1002,"参数work_company不能为空");
  106. // }
  107. $level = isset($post['level']) ? intval($post['level']) : 0;
  108. $remark = isset($post['remark']) ? trim($post['remark']) : '';
  109. // if($level==""){
  110. // return error_show(1003,"参数不能为空");
  111. // }
  112. $action = isset($post['action'])&&is_array($post['action']) ? implode(",",$post['action']) : "";
  113. if($action==""){
  114. return error_show(1004,"功能权限不能为空");
  115. }
  116. $data = isset($post['action_data']) &&is_array($post['action_data'])?implode(",",$post['action_data']): "";
  117. $private_data = isset($post['private_data']) &&is_array($post['private_data'])?implode(",",$post['private_data']): "";
  118. $private_field = isset($post['private_field']) &&is_array($post['private_field'])?implode(",", $post['private_field']): "";
  119. // if($data==""){
  120. // return error_show(1004,"字段权限不能为空");
  121. // }
  122. Db::startTrans();
  123. try {
  124. $list = [
  125. "role_name"=>$rolename,
  126. "role_code"=>$role_code,
  127. "work_company"=>$work_company,
  128. "status"=>1,
  129. 'creater'=>$username,
  130. 'createrId'=>$uid,
  131. "addtime"=>date("Y-m-d H:i:s"),
  132. "updatetime"=>date("Y-m-d H:i:s"),
  133. "level"=>$level,
  134. "desc"=>$remark,
  135. "item"=>"",
  136. ];
  137. $role= Db::name("role")->insert($list,true);
  138. $st = ["order_code"=>$role_code,"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  139. ActionLog::logAdd($token,$st,"role",1,$st);
  140. if($role>0){
  141. $role_action = [
  142. "role_id"=>$role,
  143. "action_conllect"=>$action,
  144. "action_data"=>$data,
  145. "private_data"=>$private_data,
  146. "private_field"=>$private_field,
  147. "status"=>1,
  148. "addtime"=>date("Y-m-d H:i:s"),
  149. "updatetime"=>date("Y-m-d H:i:s"),
  150. ];
  151. $roleaction= Db::name("role_action")->insert($role_action,true);
  152. if($roleaction){
  153. $sty = ["order_code"=>$role,"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  154. ActionLog::logAdd($token,$sty,"role_action",1,$sty);
  155. Db::commit();
  156. return app_show(0,"新建成功");
  157. }else{
  158. Db::rollback();
  159. return error_show(1005,"权限录入失败");
  160. }
  161. }else{
  162. Db::rollback();
  163. return error_show(1006,"角色新建失败");
  164. }
  165. }catch (\Exception $e){
  166. Db::rollback();
  167. return error_show(1008,$e->getMessage());
  168. }
  169. }
  170. /**
  171. * @param roleid
  172. * @return \think\response\Json
  173. * @throws \think\exception\DbException
  174. */
  175. public function RoleInfo(){
  176. $post =$this->request->post();
  177. $roleid= isset($post['roleid']) ? trim($post['roleid']) : "";
  178. if($roleid==""){
  179. return error_show(1001,'roleid不能为空');
  180. }
  181. $info = $list=Db::name("role")->alias("a")->leftJoin("role_action t","a.id=t.role_id")->field("a.*,t.action_conllect,t.action_data,t.private_data,t.private_field")->where("a.id","=",$roleid)->find();
  182. if(!$info){
  183. return error_show(1002,"未找到对应的数据");
  184. }
  185. $info['item_name'] ='';
  186. if($info['item']!=''){
  187. $iteminfo = Db::name('company_item')->where('id',"=",$info['item'])->find();
  188. $info['item_name'] = isset($iteminfo['name'])? $iteminfo['name']:"";
  189. }
  190. $info['action'] =explode(",",$info['action_conllect']);
  191. $info['action_data'] =$info['action_data']!=""?explode(",",$info['action_data']):"";
  192. $info['private_data'] =$info['private_data']!=""?explode(",",$info['private_data']) :"";
  193. $info['private_field'] =$info['private_field']!=""?explode(",",$info['private_field']) :[];
  194. return app_show(0,"获取成功",$info);
  195. }
  196. /**
  197. * @param role_name
  198. * @param level
  199. * @param action
  200. * @param action_data
  201. * @param roleid
  202. * @return \think\response\Json
  203. * @throws \think\exception\DbException
  204. */
  205. public function RoleSave(){
  206. $post =$this->request->post();
  207. $token = isset($post['token']) ? trim($post['token']) : "";
  208. if($token==""){
  209. return error_show(101,'token不能为空');
  210. }
  211. $effetc = VerifyTokens($token);
  212. if(!empty($effetc) && $effetc['code']!=0){
  213. return error_show($effetc['code'],$effetc['message']);
  214. }
  215. $roleid= isset($post['roleid']) ? intval($post['roleid']) : "";
  216. if($roleid==""){
  217. return error_show(1001,'roleid不能为空');
  218. }
  219. $info =Db::name("role")->where("id","=",$roleid)->find();
  220. if(!$info){
  221. return error_show(1002,"未找到对应的数据");
  222. }
  223. $rolename = isset($post['role_name']) ? trim($post['role_name']) : "";
  224. if($rolename==""){
  225. return error_show(1002,"角色名称不能为空");
  226. }
  227. $vers= Db::name("role")->where("id",'<>',$roleid)->where("role_name","=",$rolename)->find();
  228. if($vers){
  229. return error_show(1002,"角色名已存在");
  230. }
  231. $role_code= isset($post['role_code'])&&$post['role_code']!=''?trim($post['role_code']) :"";
  232. // if($role_code==""){
  233. // return error_show(1002,"参数role_code 不能为空");
  234. // }
  235. // $isCode=Db::name("role")->where(['role_code'=>$role_code])->find();
  236. // if($isCode){
  237. // return error_show(1002,"角色编号已存在");
  238. // }
  239. // $item = isset($post['item']) ? trim($post['item']) : "";
  240. // if($item==""){
  241. // return error_show(1003,"参数item 不能为空");
  242. // }
  243. // $iteminfo = Db::name('company_item')->where('id',"=",$item)->find();
  244. // if($iteminfo['is_del']==1){
  245. // return error_show(1003,"该部门不存在");
  246. // }
  247. $level = isset($post['level']) ? intval($post['level']) : 0;
  248. $remark = isset($post['remark']) ? trim($post['remark']) : '';
  249. $action = isset($post['action'])&&is_array($post['action']) ? implode(",",$post['action']) : "";
  250. if($action==""){
  251. return error_show(1004,"功能权限不能为空");
  252. }
  253. $data = isset($post['action_data']) &&is_array($post['action_data'])?implode(",",$post['action_data']): "";
  254. $private_data = isset($post['private_data']) &&is_array($post['private_data'])?implode(",",$post['private_data']): "";
  255. $private_field = isset($post['private_field']) &&is_array($post['private_field'])?implode(",",$post['private_field']): "";
  256. // if($data==""){
  257. // return error_show(1004,"字段权限不能为空");
  258. // }
  259. Db::startTrans();
  260. try {
  261. $list = [
  262. "role_name"=>$rolename,
  263. "level"=>$level,
  264. 'desc'=>$remark,
  265. 'role_code'=>$role_code,
  266. 'item'=>"",
  267. "updatetime"=>date("Y-m-d H:i:s"),
  268. 'id'=>$roleid
  269. ];
  270. $role= Db::name("role")->save($list);
  271. $stm = ["order_code"=>"YS","status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  272. ActionLog::logAdd($token,$stm,"resign_info",0,$stm);
  273. if($role){
  274. $role_action = [
  275. "role_id"=>$roleid,
  276. "action_conllect"=>$action,
  277. "action_data"=>$data,
  278. "private_data"=>$private_data,
  279. "private_field"=>$private_field,
  280. "updatetime"=>date("Y-m-d H:i:s")
  281. ];
  282. $roleaction= Db::name("role_action")->where("role_id","=",$roleid)->update($role_action);
  283. if($roleaction){
  284. $stl = ["order_code"=>"YS","status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  285. ActionLog::logAdd($token,$stl,"role_action",0,$stl);
  286. Db::commit();
  287. return app_show(0,"更新成功");
  288. }else{
  289. Db::rollback();
  290. return app_show(1005,"权限更新失败");
  291. }
  292. }else{
  293. Db::rollback();
  294. return app_show(1006,"角色更新失败");
  295. }
  296. }catch (\Exception $e){
  297. Db::rollback();
  298. return error_show(1008,$e->getMessage());
  299. }
  300. }
  301. /**
  302. * @return \think\response\Json
  303. * @throws \think\exception\DbException
  304. */
  305. public function RoleStatus(){
  306. $post =$this->request->post();
  307. $token = isset($post['token']) ? trim($post['token']) : "";
  308. if($token==""){
  309. return error_show(101,'token不能为空');
  310. }
  311. $effetc = VerifyTokens($token);
  312. if(!empty($effetc) && $effetc['code']!=0){
  313. return error_show($effetc['code'],$effetc['message']);
  314. }
  315. $roleid= isset($post['roleid']) ? intval($post['roleid']) : "";
  316. if($roleid==""){
  317. return error_show(1001,'roleid不能为空');
  318. }
  319. $status = isset($post['status']) ? intval($post['status']) : "";
  320. if($status===""){
  321. return error_show(1001,'status不能为空');
  322. }
  323. if(!in_array($status,[0,1])){
  324. return error_show(1001,'status参数非法');
  325. }
  326. Db::startTrans();
  327. try {
  328. // $re = PasRoleList::update(['status'=>$status,'update'=>date("Y-m-d H:i:s")],['id'=>$roleid]);
  329. $dat=[
  330. 'status'=>$status,'updatetime'=>date("Y-m-d H:i:s"), 'id'=>$roleid
  331. ];
  332. $re = Db::name("role")->save($dat);
  333. if($re){
  334. $dat2=[
  335. 'status'=>$status,'updatetime'=>date("Y-m-d H:i:s")
  336. ];
  337. $action = Db::name("role_action")->where("role_id","=",$roleid)->update($dat2);
  338. if($action){
  339. $sti = ["order_code"=>$roleid,"status"=>$status,"action_remark"=>'',"action_type"=>"edit"];
  340. ActionLog::logAdd($token,$sti,"role_action",$status,$sti);
  341. Db::commit();
  342. return app_show(0,"状态更新成功");
  343. }
  344. }
  345. Db::rollback();
  346. return error_show(1003,"状态更新失败");
  347. }catch (\Exception $e){
  348. Db::rollback();
  349. return error_show(1002,$e->getMessage());
  350. }
  351. }
  352. /**
  353. * @return \think\response\Json
  354. * @throws \think\exception\DbException
  355. */
  356. public function RoleAll(){
  357. $post =$this->request->post();
  358. $list =Db::name("role")->select();
  359. return app_show(0,"获取成功",$list);
  360. }
  361. //获取角色对应的流程权限id
  362. public function roleProcessInfo()
  363. {
  364. $roleid = $this->request->filter('trim')->post('roleid/d', 0);
  365. $res = Db::name("role")
  366. ->alias("a")
  367. ->leftJoin("role_process b", "a.id=b.role_id AND b.is_del=0")
  368. ->field("a.*,b.action_data")
  369. ->withAttr('action_data', function ($val) {
  370. return explode(',', $val);
  371. })
  372. ->where("a.id", $roleid)
  373. ->findOrEmpty();
  374. return app_show(0, '请求成功', $res);
  375. }
  376. //修改角色对应的流程权限
  377. public function roleProcessSave()
  378. {
  379. $param = $this->request->filter('trim')->only(['token', 'roleid', 'action_data'], 'post');
  380. $val = Validate::rule([
  381. 'token' => 'require',
  382. 'roleid|角色ID' => 'require|number|gt:0',
  383. 'action_data|所选节点id' => 'require|array',
  384. ]);
  385. if (!$val->check($param)) return error_show(1005, $val->getError());
  386. $user = GetUserInfo($param['token']);
  387. $uid = isset($user['data']['id']) ? $user['data']['id'] : 0;
  388. $uname = isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
  389. $date = date('Y-m-d H:i:s');
  390. $res = Db::name('role_process')
  391. ->where(['is_del' => 0, 'role_id' => $param['roleid']])
  392. ->field('id')
  393. ->findOrEmpty();
  394. if (empty($res)) {
  395. $rs = Db::name('role_process')
  396. ->insert([
  397. 'role_id' => $param['roleid'],
  398. 'action_data' => implode(',', $param['action_data']),
  399. 'is_del' => 0,
  400. 'createrid' => $uid,
  401. 'creater' => $uname,
  402. 'addtime' => $date,
  403. 'updaterid' => $uid,
  404. 'updater' => $uname,
  405. 'updatetime' => $date,
  406. ]);
  407. } else {
  408. $rs = Db::name('role_process')
  409. ->where('id', $res['id'])
  410. ->update([
  411. 'action_data' => implode(',', $param['action_data']),
  412. 'updaterid' => $uid,
  413. 'updater' => $uname,
  414. 'updatetime' => $date
  415. ]);
  416. }
  417. return $rs ? app_show(0, '操作成功') : error_show(1005, '操作失败');
  418. }
  419. //获取角色对应的流程权限详情
  420. public function roleProcessDetail()
  421. {
  422. $roleid = $this->request->filter('trim')->post('roleid/d', 0);
  423. $action_data = Db::name("role_process")
  424. ->where('role_id', $roleid)
  425. ->value('action_data', []);
  426. $data = PModel::where(['is_del' => PModel::$is_del_normal, 'status' => PModel::$status_normal])
  427. ->append(['child'])
  428. ->withAttr('child', function () {
  429. return [];
  430. })
  431. ->column('id,process_name,process_type', 'id');
  432. $action = APModel::where([
  433. 'is_del' => APModel::$is_del_normal,
  434. 'status' => APModel::$status_normal,
  435. 'operation_type' => APModel::$operation_type_approval
  436. ])->whereIn('id', $action_data)
  437. ->field('id,process_id,order_process,status_name')
  438. ->cursor();
  439. foreach ($action as $item) {
  440. if (isset($data[$item->process_id])) $data[$item->process_id]['child'][] = $item->toArray();
  441. }
  442. return app_show(0, '请求成功', array_column($data, null, null));
  443. }
  444. }