Action.php 11 KB

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