Role.php 18 KB

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