Action.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 = PasAction::all(['menuid'=>$value['id'],"status"=>1]);
  97. $act = Db::name("action")
  98. ->alias("a")
  99. ->leftJoin("action_list l", "a.action_code=l.action_code")
  100. ->field("a.*,action_name")
  101. ->where(['a.menuid' => $value['id'], "a.status" => 1])
  102. ->select()
  103. ->toArray();
  104. // $act_data = Db::name("action_field")
  105. // ->where(['menuid' => $value['id'], "status" => 1])
  106. // ->select()
  107. // ->toArray();
  108. $value['action'] = $act;
  109. $value['action_data'] =[];
  110. if (array_key_exists($value['pid'], $result)) {
  111. $result[$value['pid']]["child"][] = $value;
  112. }
  113. }
  114. return json_show(0, "获取成功", array_values($result));
  115. }
  116. public function ActionList(){
  117. $post =$this->request->post();
  118. $pageid = isset($post['id']) ? intval($post['id']) : "";
  119. if($pageid==""){
  120. return error_show(1001,'页面id不能为空');
  121. }
  122. $condition = ['menuid'=>$pageid];
  123. $data=Db::name('action')
  124. ->alias("a")
  125. ->leftJoin("action_list l","a.action_code=l.action_code")
  126. ->field("a.*,action_name")
  127. ->where($condition)
  128. ->select()
  129. ->toArray();
  130. return app_show(0,"获取成功",$data);
  131. }
  132. /** 菜单下功能信息状态修改
  133. * @return \think\response\Json|void
  134. * @throws \think\exception\DbException
  135. */
  136. public function ActionAdd()
  137. {
  138. $post = $this->request->post();
  139. $pageid = isset($post['menuid']) ? intval($post['menuid']) : "";
  140. if ($pageid == "") {
  141. return error_show(1001, '菜单id不能为空');
  142. }
  143. $code = isset($post['action_code']) ? trim($post['action_code']) : "";
  144. if ($code == "") {
  145. return error_show(1002, '功能code不能为空');
  146. }
  147. $status = isset($post['status']) ? intval($post['status']) : 1;
  148. try {
  149. $where = ['menuid' => $pageid, 'action_code' => $code];
  150. $true = Db::name("action")->field('id')->where($where)->find();
  151. if ($true) return error_show(1003, '此功能已存在');
  152. else {
  153. $data = ['menuid' => $pageid, 'action_code' => $code, 'status' => $status, "updatetime" => date("Y-m-d H:i:s"), "addtime" => date("Y-m-d H:i:s")];
  154. Db::name("action")->insert($data);
  155. return app_show(0, "添加成功");
  156. }
  157. } catch (\Exception $e) {
  158. return error_show(1005, $e->getMessage());
  159. }
  160. }
  161. /** 菜单下功能信息状态修改
  162. * @return \think\response\Json|void
  163. * @throws \think\exception\DbException
  164. */
  165. public function ActionDel()
  166. {
  167. $post = $this->request->filter('trim')->post();
  168. $action_id = isset($post['action_id']) ? intval($post['action_id']) : "";
  169. if ($action_id === "") {
  170. return json_show(1001, '参数action_id不能为空');
  171. }
  172. $action = Db::name("action_list")->where(["id" => $action_id, "is_del" => 0])->find();
  173. if ($action == false) {
  174. return json_show(1004, "未找到功能数据");
  175. }
  176. $upda = ["is_del" => 1, "updatetime" => date("Y-m-d H:i:s")];
  177. Db::startTrans();
  178. try {
  179. $up = Db::name("action_list")->where($action)->update($upda);
  180. if ($up) {
  181. $upall = Db::name("action")->where(["action_code" => $action['action_code'], "is_del" => 0])->update($upda);
  182. Db::commit();
  183. return json_show(0, "删除成功");
  184. }
  185. Db::rollback();
  186. return json_show(1005, "删除失败");
  187. } catch (\Exception $e) {
  188. Db::rollback();
  189. return json_show(1005, $e->getMessage());
  190. }
  191. }
  192. /** 菜单下功能信息状态修改
  193. * @return \think\response\Json|void
  194. * @throws \think\exception\DbException
  195. */
  196. public function ActionCreate()
  197. {
  198. $post = $this->request->param(["action_name"=>"","action_code"=>"","is_del"=>0]);
  199. $valid = Validate::rule([
  200. "action_name|功能名称"=>"require",
  201. "action_code|功能编号"=>"require|unique:action_list,action_code^is_del"]);
  202. if($valid->check($post)==false)return error_show(1004,$valid->getError());
  203. $save=Db::name("action_list")->save($post);
  204. return $save? app_show(0,"创建成功"):error_show(1004,"创建失败");
  205. }
  206. }