Action.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\admin\controller;
  4. use app\BaseController;
  5. use think\facade\Db;
  6. use think\Request;
  7. use think\App;
  8. class Action extends BaseController
  9. {
  10. /**
  11. * public function __construct(App $app)
  12. * {
  13. * parent::__construct($app);
  14. * $post =$this->request->post();
  15. * $token = isset($post['token']) ? trim($post['token']) : "";
  16. * if($token==""){
  17. * return json_show(101,'token不能为空');
  18. *
  19. * }
  20. * $effetc = VerifyTokens($token);
  21. * if(!empty($effetc) && $effetc['code']!=0){
  22. * return json_show($effetc['code'],$effetc['message']);
  23. *
  24. * }
  25. * }
  26. * public function ActionList(){
  27. * $post =$this->request->post();
  28. * $pageid = isset($post['id']) ? intval($post['id']) : "";
  29. * if($pageid==""){
  30. * return json_show(1001,'页面id不能为空');
  31. * }
  32. * $condition = ['menuid'=>$pageid];
  33. * $data=Db::name('action')->alias("a")->leftJoin("action_list l","a.action_code=l.action_code")->field
  34. * ("a.*,action_name")->where($condition)->select();
  35. * return json_show(0,"获取成功",$data);
  36. * }
  37. *
  38. * public function ActionSave(){
  39. * $post =$this->request->post();
  40. * $actionid = isset($post['id']) ? intval($post['id']) : "";
  41. * if($actionid!=""){
  42. * $isf= Db::name("action_list")->where("id","=",$actionid)->find();
  43. * if($isf==false){
  44. * return json_show(1005,"未找到数据");
  45. * }
  46. * }
  47. * $action = isset($post['action_name']) ? trim($post['action_name']) : "";
  48. * if($action==""){
  49. * return json_show(1003,'功能名称不能为空');
  50. * }
  51. * $desc = isset($post['action_desc']) ? trim($post['action_desc']) : "";
  52. * $status = isset($post['status']) ? intval($post['status']) : 0;
  53. * $data=[
  54. * "action_name"=>$action,
  55. * "action_desc"=>$desc,
  56. * "action_code"=>"",
  57. * ];
  58. *
  59. * $isTrue = Db::name("action_list")->where(["action_name"=>$action])->find();
  60. * if($isTrue){
  61. * if($isTrue['id']!=$actionid || $actionid==""){
  62. * return json_show(1003,'功能名称不能重复');
  63. * }
  64. * }
  65. *
  66. * try{
  67. * $message = "";
  68. * if($actionid==""){
  69. * $data['status']=$status;
  70. * $data['is_show']=1;
  71. * $message = "新建成功";
  72. * }else{
  73. * $data['status']=$status;
  74. * $data['id']=$actionid;
  75. * $message = "更新成功";
  76. * }
  77. * Db::name("action_list")->save($data);
  78. * return json_show(0,$message);
  79. * }catch (\Exception $e){
  80. * return json_show(1005,$e->getMessage());
  81. * }
  82. * }
  83. *
  84. * public function ActionStatus(){
  85. * $post =$this->request->post();
  86. *
  87. * $actid = isset($post['id']) ? intval($post['id']) : "";
  88. * if($actid==""){
  89. * return json_show(1001,'功能id不能为空');
  90. * }
  91. * $status = isset($post['status']) ? intval($post['status']) : 1;
  92. * try{
  93. * $data = ['status'=>$status,"updatetime"=>date("Y-m-d H:i:s")];
  94. * $result=Db::name("action")->where("id","=",$actid)->save($data);
  95. * if($result){
  96. * return json_show(0,"更新成功");
  97. * }else{
  98. * return json_show(1004,"更新失败");
  99. * }
  100. * }catch (\Exception $e){
  101. * return json_show(1003,$e->getMessage());
  102. * }
  103. * }
  104. *
  105. * public function ActionAdd(){
  106. * $post =$this->request->post();
  107. *
  108. * $pageid = isset($post['menuid']) ? intval($post['menuid']) : "";
  109. * if($pageid==""){
  110. * return json_show(1001,'页面id不能为空');
  111. * }
  112. * $code = isset($post['action_code']) ? trim($post['action_code']) : "";
  113. *
  114. * $status = isset($post['status']) ? intval($post['status']) : 1;
  115. * if($code==""){
  116. * return json_show(1002,'功能code不能为空');
  117. * }
  118. * try{
  119. * $where = ['menuid'=>$pageid,'action_code'=>$code];
  120. * $true =Db::name("action")->where($where)->find();
  121. * $data = ['menuid'=>$pageid,'action_code'=>$code,'status'=>$status,"updatetime"=>date("Y-m-d H:i:s"),"addtime"=>date("Y-m-d H:i:s")];
  122. * if($true){
  123. * return json_show(1003,'此功能已存在');
  124. * }else{
  125. * Db::name("action")->insert($data);
  126. * return json_show(0,"添加成功");
  127. * }
  128. * }catch (\Exception $e){
  129. * return json_show(1005,$e->getMessage());
  130. * }
  131. * }
  132. *
  133. * public function index(){
  134. * $post =$this->request->post();
  135. * $data = Db::name("admin_menu")->where(["pid"=>0,"status"=>1,"is_del"=>0])->select();
  136. * $result = [];
  137. * if(empty($data)){
  138. * return json_show(0,"获取成功",$result);
  139. * }
  140. * foreach ($data as $key=>$val){
  141. * $val["child"]=[];
  142. * $result[$val['id']] =$val;
  143. * }
  144. *
  145. * $child =Db::name("admin_menu")->where([["pid","<>",0],['status',"=",1],["is_del","=",0]])->select();
  146. * foreach ($child as $k=>$value){
  147. * // $act = PasAction::all(['menuid'=>$value['id'],"status"=>1]);
  148. * $act =Db::name("action")
  149. * ->alias("a")
  150. * ->leftJoin("action_list l","a.action_code=l.action_code")
  151. * ->field("a.*,action_name")
  152. * ->where(['a.menuid'=>$value['id'],"a.status"=>1])
  153. * ->withAttr('id',function($val){
  154. * return (string)$val;
  155. * })
  156. * ->select()
  157. * ->toArray();
  158. * $act_data = Db::name("action_field")->where(['menuid'=>$value['id'],"status"=>1])->select();
  159. * $value['action'] = $act;
  160. * $value['action_data'] = $act_data;
  161. * if(array_key_exists($value['pid'],$result)){
  162. * $result[$value['pid']]["child"][]=$value;
  163. * }
  164. * }
  165. *
  166. * return json_show(0,"获取成功",array_values($result));
  167. * }
  168. *
  169. * public function ActionInfo(){
  170. * $post =$this->request->post();
  171. * $token = isset($post['token']) ? trim($post['token']) : "";
  172. * if($token==""){
  173. * return json_show(101,'token不能为空');
  174. * }
  175. * $effetc = VerifyTokens($token);
  176. * if(!empty($effetc) && $effetc['code']!=0){
  177. * return json_show($effetc['code'],$effetc['message']);
  178. * }
  179. * $id = isset($post['id'])? intval($post['id']) :"";
  180. * if($id==""){
  181. * return json_show(1002,'功能id不能为空');
  182. * }
  183. * $menu = Db::name("action_list")->where("id","=",$id)->find();
  184. * if(empty($menu)){
  185. * return json_show(1003,"未找到对应的数据");
  186. * }
  187. * return json_show(0,"获取成功!",$menu);
  188. * }
  189. * **/
  190. //11获取素有菜单列表数据
  191. public function index()
  192. {
  193. $post = $this->request->filter('trim')->post();
  194. $where = [["status", "=", 1], ["is_del", "=", 0], ["level", ">=", $post["level"] ?? 1], ['menu_type', "=", 2]];
  195. $data = Db::name("admin_menu")->where($where)->order("weight desc")->column("id,menu_name,menu_img,menu_route,menu_url,pid,level,is_show,is_private,menu_type,status");
  196. $result = [];
  197. if (empty($data)) {
  198. return json_show(0, "获取成功", $result);
  199. }
  200. $list = [];
  201. $menu = [];
  202. foreach ($data as $k => $value) {
  203. $action = Db::name("action")
  204. ->alias("a")
  205. ->leftJoin("action_list b", "a.action_code=b.action_code")
  206. ->where(["menuid" => $value['id'], "a.status" => 1, "a.is_del" => 0, "b.is_del" => 0])
  207. ->column("a.id,a.action_code,b.action_name");
  208. if ($value['menu_type'] == 2) $value['action'] = $action;
  209. $list[] = $value;
  210. }
  211. menuAction($list, $menu);
  212. return json_show(0, "获取成功", array_values($menu));
  213. }
  214. public function ActionList()
  215. {
  216. $post = $this->request->filter('trim')->post();
  217. $page = isset($post['page']) ? intval($post['page']) : 1;
  218. $size = isset($post['size']) ? intval($post['size']) : 10;
  219. $count = Db::name("action_list")->where(["is_del" => 0])->count();
  220. $total = ceil($count / $size) > 1 ? ceil($count / $size) : 1;
  221. $page = $page >= $total ? intval($total) : $page;
  222. $list = Db::name("action_list")->where(["is_del" => 0])->page($page, $size)->select()->toArray();
  223. $data = ['list' => $list, "count" => $count];
  224. return json_show(0, '获取成功', $data);
  225. }
  226. /** 菜单下功能信息状态修改
  227. * @return \think\response\Json|void
  228. * @throws \think\exception\DbException
  229. */
  230. public function ActionAdd()
  231. {
  232. $post = $this->request->filter('trim')->post();
  233. $action_name = isset($post['action_name']) ? trim($post['action_name']) : "";
  234. if ($action_name == "") {
  235. return json_show(1001, '功能名称不能为空');
  236. }
  237. $action_desc = isset($post['action_desc']) ? trim($post['action_desc']) : "";
  238. $code = isset($post['action_code']) ? trim($post['action_code']) : "";
  239. if ($code == "") {
  240. return json_show(1001, '功能代码不能为空');
  241. }
  242. $status = isset($post['status']) ? intval($post['status']) : 1;
  243. try {
  244. $action = [
  245. "action_name" => $action_name,
  246. "action_code" => $code,
  247. "action_desc" => $action_desc,
  248. "status" => $status,
  249. "is_show" => 1,
  250. "addtime" => date("Y-m-d H:i:s"),
  251. "updatetime" => date("Y-m-d H:i:s")
  252. ];
  253. $up = Db::name("action_list")->insert($action);
  254. return $up ? json_show(0, "新建成功") : json_show(1004, "新建失败");
  255. } catch (\Exception $e) {
  256. return json_show(1005, $e->getMessage());
  257. }
  258. }
  259. /** 菜单下功能信息状态修改
  260. * @return \think\response\Json|void
  261. * @throws \think\exception\DbException
  262. */
  263. public function ActionDel()
  264. {
  265. $post = $this->request->filter('trim')->post();
  266. $action_id = isset($post['action_id']) ? intval($post['action_id']) : "";
  267. if ($action_id === "") {
  268. return json_show(1001, '参数action_id不能为空');
  269. }
  270. $action = Db::name("action_list")->where(["id" => $action_id, "is_del" => 0])->find();
  271. if ($action == false) {
  272. return json_show(1004, "未找到功能数据");
  273. }
  274. $upda = ["is_del" => 0, "updatetime" => date("Y-m-d H:i:s")];
  275. Db::startTrans();
  276. try {
  277. $up = Db::name("action_list")->where($action)->update($upda);
  278. if ($up) {
  279. $upall = Db::name("action")->where(["action_code" => $action['action_code'], "is_del" => 0])->update($upda);
  280. Db::commit();
  281. return json_show(0, "删除成功");
  282. }
  283. Db::rollback();
  284. return json_show(1005, "删除失败");
  285. } catch (\Exception $e) {
  286. Db::rollback();
  287. return json_show(1005, $e->getMessage());
  288. }
  289. }
  290. }