Action.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\admin\controller;
  4. use app\BaseController;
  5. use think\facade\Db;
  6. use think\facade\Validate;
  7. use think\Request;
  8. use think\App;
  9. class Action extends BaseController
  10. {
  11. public function ActionSave()
  12. {
  13. $post = $this->request->only(['id', 'action_code'], 'post', 'trim');
  14. $val = Validate::rule([
  15. 'id|ID' => 'require|number|gt:0',
  16. 'action_code' => 'require'
  17. ]);
  18. if ($val->check($post) == false) return error_show(1004, $val->getError());
  19. $rs = Db::name("action")
  20. ->where(['is_del' => 0, 'id' => $post['id']])
  21. ->update([
  22. 'action_code' => $post['action_code'],
  23. 'updatetime' => date("Y-m-d H:i:s")
  24. ]);
  25. return $rs ? json_show(0, "修改成功") : json_show(0, "修改失败");
  26. }
  27. public function ActionStatus()
  28. {
  29. $post = $this->request->post();
  30. $actid = isset($post['id']) ? intval($post['id']) : "";
  31. if ($actid == "") {
  32. return json_show(1001, '功能id不能为空');
  33. }
  34. $status = isset($post['status']) ? intval($post['status']) : 1;
  35. try {
  36. $data = ['status' => $status, "updatetime" => date("Y-m-d H:i:s")];
  37. $result = Db::name("action")->where("id", "=", $actid)->save($data);
  38. return $result ? json_show(0, "更新成功") : json_show(1004, "更新失败");
  39. } catch (\Exception $e) {
  40. return json_show(1003, $e->getMessage());
  41. }
  42. }
  43. //11获取所有菜单列表数据
  44. public function index()
  45. {
  46. $post = $this->request->post();
  47. $data = Db::name("admin_menu")
  48. ->where(["pid" => 0, "status" => 1, "is_del" => 0])
  49. ->order(['weight'=>'desc','id'=>'desc'])
  50. ->select()
  51. ->toArray();
  52. $result = [];
  53. if (empty($data)) {
  54. return app_show(0, "获取成功", $result);
  55. }
  56. foreach ($data as $key => $val) {
  57. $val["child"] = [];
  58. $result[$val['id']] = $val;
  59. }
  60. $child_where = [["pid", "<>", 0], ['status', "=", 1], ["is_del", "=", 0]];
  61. if (isset($post['level']) && $post['level'] !== '') $child_where[] = ['level', 'in', $post['level']];
  62. $child = Db::name("admin_menu")
  63. ->where($child_where)
  64. ->order(['weight'=>'desc','id'=>'desc'])
  65. ->select()
  66. ->toArray();
  67. foreach ($child as $k => $value) {
  68. $act = Db::name("action")
  69. ->alias("a")
  70. ->leftJoin("action_list l", "a.action_code=l.action_code")
  71. ->field("a.*,action_name")
  72. ->where(['a.menuid' => $value['id'], "a.status" => 1, 'a.is_del' =>0])
  73. ->select()
  74. ->toArray();
  75. $value['action'] = $act;
  76. $value['action_data'] =[];
  77. if (array_key_exists($value['pid'], $result)) {
  78. $result[$value['pid']]["child"][] = $value;
  79. }
  80. }
  81. return json_show(0, "获取成功", array_values($result));
  82. }
  83. public function ActionList(){
  84. $post =$this->request->post();
  85. $pageid = isset($post['id']) ? intval($post['id']) : "";
  86. if($pageid==""){
  87. return error_show(1001,'页面id不能为空');
  88. }
  89. $condition = ['menuid'=>$pageid,'a.is_del' =>0];
  90. $data=Db::name('action')
  91. ->alias("a")
  92. ->leftJoin("action_list l","a.action_code=l.action_code")
  93. ->field("a.*,action_name")
  94. ->where($condition)
  95. ->select()
  96. ->toArray();
  97. return app_show(0,"获取成功",$data);
  98. }
  99. /** 菜单下功能信息状态修改
  100. * @return \think\response\Json|void
  101. * @throws \think\exception\DbException
  102. */
  103. public function ActionAdd()
  104. {
  105. $post = $this->request->post();
  106. $pageid = isset($post['menuid']) ? intval($post['menuid']) : "";
  107. if ($pageid == "") {
  108. return error_show(1001, '菜单id不能为空');
  109. }
  110. $code = isset($post['action_code']) ? trim($post['action_code']) : "";
  111. if ($code == "") {
  112. return error_show(1002, '功能code不能为空');
  113. }
  114. $status = isset($post['status']) ? intval($post['status']) : 1;
  115. try {
  116. $where = ['menuid' => $pageid, 'action_code' => $code];
  117. $true = Db::name("action")->field('id')->where($where)->find();
  118. if ($true) return error_show(1003, '此功能已存在');
  119. else {
  120. $data = ['menuid' => $pageid, 'action_code' => $code, 'status' => $status, "updatetime" => date("Y-m-d H:i:s"), "addtime" => date("Y-m-d H:i:s")];
  121. Db::name("action")->insert($data);
  122. return app_show(0, "添加成功");
  123. }
  124. } catch (\Exception $e) {
  125. return error_show(1005, $e->getMessage());
  126. }
  127. }
  128. /** 菜单下功能信息状态修改
  129. * @return \think\response\Json|void
  130. * @throws \think\exception\DbException
  131. */
  132. public function ActionDel()
  133. {
  134. $post = $this->request->filter('trim')->post();
  135. $action_id = isset($post['action_id']) ? intval($post['action_id']) : "";
  136. if ($action_id === "") {
  137. return json_show(1001, '参数action_id不能为空');
  138. }
  139. $action = Db::name("action_list")->where(["id" => $action_id, "is_del" => 0])->find();
  140. if ($action == false) {
  141. return json_show(1004, "未找到功能数据");
  142. }
  143. $upda = ["is_del" => 1, "updatetime" => date("Y-m-d H:i:s")];
  144. Db::startTrans();
  145. try {
  146. $up = Db::name("action_list")->where($action)->update($upda);
  147. if ($up) {
  148. $upall = Db::name("action")->where(["action_code" => $action['action_code'], "is_del" => 0])->update($upda);
  149. Db::commit();
  150. return json_show(0, "删除成功");
  151. }
  152. Db::rollback();
  153. return json_show(1005, "删除失败");
  154. } catch (\Exception $e) {
  155. Db::rollback();
  156. return json_show(1005, $e->getMessage());
  157. }
  158. }
  159. /** 菜单下功能信息状态修改
  160. * @return \think\response\Json|void
  161. * @throws \think\exception\DbException
  162. */
  163. public function ActionCreate()
  164. {
  165. $post = $this->request->param(["action_name"=>"","action_code"=>"","is_del"=>0]);
  166. $valid = Validate::rule([
  167. "action_name|功能名称"=>"require",
  168. "action_code|功能编号"=>"require|unique:action_list,action_code^is_del"]);
  169. if($valid->check($post)==false)return error_show(1004,$valid->getError());
  170. $save=Db::name("action_list")->save($post);
  171. return $save? app_show(0,"创建成功"):error_show(1004,"创建失败");
  172. }
  173. }