Action.php 7.7 KB

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