Role.php 17 KB

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