Action.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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->post();
  194. $data = Db::name("admin_menu")
  195. ->where(["pid" => 0, "status" => 1, "is_del" => 0])
  196. ->select()
  197. ->toArray();
  198. $result = [];
  199. if (empty($data)) {
  200. return app_show(0, "获取成功", $result);
  201. }
  202. foreach ($data as $key => $val) {
  203. $val["child"] = [];
  204. $result[$val['id']] = $val;
  205. }
  206. $child_where = [["pid", "<>", 0], ['status', "=", 1], ["is_del", "=", 0]];
  207. if (isset($post['level']) && $post['level'] !== '') $child_where[] = ['level', '=', $post['level']];
  208. $child = Db::name("admin_menu")
  209. ->where($child_where)
  210. ->select()
  211. ->toArray();
  212. foreach ($child as $k => $value) {
  213. // $act = PasAction::all(['menuid'=>$value['id'],"status"=>1]);
  214. $act = Db::name("action")
  215. ->alias("a")
  216. ->leftJoin("action_list l", "a.action_code=l.action_code")
  217. ->field("a.*,action_name")
  218. ->where(['a.menuid' => $value['id'], "a.status" => 1])
  219. ->select()
  220. ->toArray();
  221. $act_data = Db::name("action_field")
  222. ->where(['menuid' => $value['id'], "status" => 1])
  223. ->select()
  224. ->toArray();
  225. $value['action'] = $act;
  226. $value['action_data'] = $act_data;
  227. if (array_key_exists($value['pid'], $result)) {
  228. $result[$value['pid']]["child"][] = $value;
  229. }
  230. }
  231. return json_show(0, "获取成功", array_values($result));
  232. }
  233. public function ActionList(){
  234. $post =$this->request->post();
  235. $pageid = isset($post['id']) ? intval($post['id']) : "";
  236. if($pageid==""){
  237. return error_show(1001,'页面id不能为空');
  238. }
  239. $condition = ['menuid'=>$pageid];
  240. $data=Db::name('action')
  241. ->alias("a")
  242. ->leftJoin("action_list l","a.action_code=l.action_code")
  243. ->field("a.*,action_name")
  244. ->where($condition)
  245. ->select()
  246. ->toArray();
  247. return app_show(0,"获取成功",$data);
  248. }
  249. /** 菜单下功能信息状态修改
  250. * @return \think\response\Json|void
  251. * @throws \think\exception\DbException
  252. */
  253. public function ActionAdd()
  254. {
  255. $post = $this->request->post();
  256. $pageid = isset($post['menuid']) ? intval($post['menuid']) : "";
  257. if ($pageid == "") {
  258. return error_show(1001, '菜单id不能为空');
  259. }
  260. $code = isset($post['action_code']) ? trim($post['action_code']) : "";
  261. if ($code == "") {
  262. return error_show(1002, '功能code不能为空');
  263. }
  264. $status = isset($post['status']) ? intval($post['status']) : 1;
  265. try {
  266. $where = ['menuid' => $pageid, 'action_code' => $code];
  267. $true = Db::name("action")->field('id')->where($where)->find();
  268. if ($true) return error_show(1003, '此功能已存在');
  269. else {
  270. $data = ['menuid' => $pageid, 'action_code' => $code, 'status' => $status, "updatetime" => date("Y-m-d H:i:s"), "addtime" => date("Y-m-d H:i:s")];
  271. Db::name("action")->insert($data);
  272. return app_show(0, "添加成功");
  273. }
  274. } catch (\Exception $e) {
  275. return error_show(1005, $e->getMessage());
  276. }
  277. }
  278. /** 菜单下功能信息状态修改
  279. * @return \think\response\Json|void
  280. * @throws \think\exception\DbException
  281. */
  282. public function ActionDel()
  283. {
  284. $post = $this->request->filter('trim')->post();
  285. $action_id = isset($post['action_id']) ? intval($post['action_id']) : "";
  286. if ($action_id === "") {
  287. return json_show(1001, '参数action_id不能为空');
  288. }
  289. $action = Db::name("action_list")->where(["id" => $action_id, "is_del" => 0])->find();
  290. if ($action == false) {
  291. return json_show(1004, "未找到功能数据");
  292. }
  293. $upda = ["is_del" => 0, "updatetime" => date("Y-m-d H:i:s")];
  294. Db::startTrans();
  295. try {
  296. $up = Db::name("action_list")->where($action)->update($upda);
  297. if ($up) {
  298. $upall = Db::name("action")->where(["action_code" => $action['action_code'], "is_del" => 0])->update($upda);
  299. Db::commit();
  300. return json_show(0, "删除成功");
  301. }
  302. Db::rollback();
  303. return json_show(1005, "删除失败");
  304. } catch (\Exception $e) {
  305. Db::rollback();
  306. return json_show(1005, $e->getMessage());
  307. }
  308. }
  309. }