Action.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. public function __construct(App $app)
  11. {
  12. parent::__construct($app);
  13. $post =$this->request->post();
  14. $token = isset($post['token']) ? trim($post['token']) : "";
  15. // if($token==""){
  16. // return error_show(101,'token不能为空');
  17. //
  18. // }
  19. // $effetc = VerifyTokens($token);
  20. // if(!empty($effetc) && $effetc['code']!=0){
  21. // return error_show($effetc['code'],$effetc['message']);
  22. //
  23. // }
  24. }
  25. /**
  26. * 显示资源列表
  27. *
  28. * @return \think\Response
  29. */
  30. public function ActionList(){
  31. $post =$this->request->post();
  32. $pageid = isset($post['id']) ? intval($post['id']) : "";
  33. if($pageid==""){
  34. return error_show(1001,'页面id不能为空');
  35. }
  36. $condition = ['menuid'=>$pageid];
  37. $data=Db::name('action')->alias("a")->leftJoin("action_list l","a.action_code=l.action_code")->field
  38. ("a.*,action_name")->where($condition)->select();
  39. return app_show(0,"获取成功",$data);
  40. }
  41. public function ActionSave(){
  42. $post =$this->request->post();
  43. $actionid = isset($post['id']) ? intval($post['id']) : "";
  44. if($actionid!=""){
  45. $isf= Db::name("action_list")->where("id","=",$actionid)->find();
  46. if($isf==false){
  47. return error_show(1005,"未找到数据");
  48. }
  49. }
  50. $action = isset($post['action_name']) ? trim($post['action_name']) : "";
  51. if($action==""){
  52. return error_show(1003,'功能名称不能为空');
  53. }
  54. $desc = isset($post['action_desc']) ? trim($post['action_desc']) : "";
  55. $status = isset($post['status']) ? intval($post['status']) : 0;
  56. $data=[
  57. "action_name"=>$action,
  58. "action_desc"=>$desc,
  59. "action_code"=>"",
  60. ];
  61. $isTrue = Db::name("action_list")->where(["action_name"=>$action])->find();
  62. if($isTrue){
  63. if($isTrue['id']!=$actionid || $actionid==""){
  64. return error_show(1003,'功能名称不能重复');
  65. }
  66. }
  67. try{
  68. $message = "";
  69. if($actionid==""){
  70. $data['status']=$status;
  71. $data['is_show']=1;
  72. $message = "新建成功";
  73. }else{
  74. $data['status']=$status;
  75. $data['id']=$actionid;
  76. $message = "更新成功";
  77. }
  78. Db::name("action_list")->save($data);
  79. return app_show(0,$message);
  80. }catch (\Exception $e){
  81. return error_show(1005,$e->getMessage());
  82. }
  83. }
  84. public function ActionStatus(){
  85. $post =$this->request->post();
  86. $actid = isset($post['id']) ? intval($post['id']) : "";
  87. if($actid==""){
  88. return error_show(1001,'功能id不能为空');
  89. }
  90. $status = isset($post['status']) ? intval($post['status']) : 1;
  91. try{
  92. $data = ['status'=>$status,"updatetime"=>date("Y-m-d H:i:s")];
  93. $result=Db::name("action")->where("id","=",$actid)->save($data);
  94. if($result){
  95. return app_show(0,"更新成功");
  96. }else{
  97. return error_show(1004,"更新失败");
  98. }
  99. }catch (\Exception $e){
  100. return error_show(1003,$e->getMessage());
  101. }
  102. }
  103. /**
  104. * @return \think\response\Json|void
  105. * @throws \think\exception\DbException
  106. */
  107. public function ActionAdd(){
  108. $post =$this->request->post();
  109. $pageid = isset($post['menuid']) ? intval($post['menuid']) : "";
  110. if($pageid==""){
  111. return error_show(1001,'页面id不能为空');
  112. }
  113. $code = isset($post['action_code']) ? trim($post['action_code']) : "";
  114. $status = isset($post['status']) ? intval($post['status']) : 1;
  115. if($code==""){
  116. return error_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 error_show(1003,'此功能已存在');
  124. }else{
  125. Db::name("action")->insert($data);
  126. return app_show(0,"添加成功");
  127. }
  128. }catch (\Exception $e){
  129. return error_show(1005,$e->getMessage());
  130. }
  131. }
  132. /**
  133. * @return \think\response\Json
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. * @throws \think\exception\DbException
  138. */
  139. public function index(){
  140. $post =$this->request->post();
  141. $data = Db::name("admin_menu")->where(["pid"=>0,"status"=>1])->select();
  142. $result = [];
  143. if(empty($data)){
  144. return app_show(0,"获取成功",$result);
  145. }
  146. foreach ($data as $key=>$val){
  147. $val["child"]=[];
  148. $result[$val['id']] =$val;
  149. }
  150. $child =Db::name("admin_menu")->where("pid","<>",0)->where('status',"=",1)->select();
  151. foreach ($child as $k=>$value){
  152. // $act = PasAction::all(['menuid'=>$value['id'],"status"=>1]);
  153. $act =Db::name("action")->alias("a")->leftJoin("action_list l","a.action_code=l.action_code")->field
  154. ("a.*,action_name")->where(['a.menuid'=>$value['id'],"a.status"=>1])->select();
  155. $act_data = Db::name("action_field")->where(['menuid'=>$value['id'],"status"=>1])->select();
  156. $value['action'] = $act;
  157. $value['action_data'] = $act_data;
  158. if(array_key_exists($value['pid'],$result)){
  159. $result[$value['pid']]["child"][]=$value;
  160. }
  161. }
  162. return app_show(0,"获取成功",array_values($result));
  163. }
  164. /**@param id menu 主键id
  165. * @return \think\response\Json
  166. * @throws \think\exception\DbException
  167. */
  168. public function ActionInfo(){
  169. $post =$this->request->post();
  170. $token = isset($post['token']) ? trim($post['token']) : "";
  171. if($token==""){
  172. return error_show(101,'token不能为空');
  173. }
  174. $effetc = VerifyTokens($token);
  175. if(!empty($effetc) && $effetc['code']!=0){
  176. return error_show($effetc['code'],$effetc['message']);
  177. }
  178. $id = isset($post['id'])? intval($post['id']) :"";
  179. if($id==""){
  180. return error_show(1002,'功能id不能为空');
  181. }
  182. $menu = Db::name("action_list")->where("id","=",$id)->find();
  183. if(empty($menu)){
  184. return error_show(1003,"未找到对应的数据");
  185. }
  186. return app_show(0,"获取成功!",$menu);
  187. }
  188. }