Action.php 7.1 KB

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