Action.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. $data=[
  55. "action_name"=>$action,
  56. "action_desc"=>$desc,
  57. "action_code"=>"",
  58. ];
  59. $isTrue = Db::name("action_list")->where(["action_name"=>$action])->find();
  60. if($isTrue){
  61. if($isTrue['id']!=$actionid || $actionid==""){
  62. return error_show(1003,'功能名称不能重复');
  63. }
  64. }
  65. try{
  66. $message = "";
  67. if($actionid==""){
  68. $data['status']=$status;
  69. $data['is_show']=1;
  70. $message = "新建成功";
  71. }else{
  72. $data['status']=$status;
  73. $data['id']=$actionid;
  74. $message = "更新成功";
  75. }
  76. Db::name("action_list")->save($data);
  77. return app_show(0,$message);
  78. }catch (\Exception $e){
  79. return error_show(1005,$e->getMessage());
  80. }
  81. }
  82. public function ActionStatus(){
  83. $post =$this->request->post();
  84. $actid = isset($post['id']) ? intval($post['id']) : "";
  85. if($actid==""){
  86. return error_show(1001,'功能id不能为空');
  87. }
  88. $status = isset($post['status']) ? intval($post['status']) : 1;
  89. try{
  90. $data = ['status'=>$status,"updatetime"=>date("Y-m-d H:i:s")];
  91. $result=Db::name("action")->where("id","=",$actid)->save($data);
  92. if($result){
  93. return app_show(0,"更新成功");
  94. }else{
  95. return error_show(1004,"更新失败");
  96. }
  97. }catch (\Exception $e){
  98. return error_show(1003,$e->getMessage());
  99. }
  100. }
  101. /**
  102. * @return \think\response\Json|void
  103. * @throws \think\exception\DbException
  104. */
  105. public function ActionAdd(){
  106. $post =$this->request->post();
  107. $pageid = isset($post['menuid']) ? intval($post['menuid']) : "";
  108. if($pageid==""){
  109. return error_show(1001,'页面id不能为空');
  110. }
  111. $code = isset($post['action_code']) ? trim($post['action_code']) : "";
  112. $status = isset($post['status']) ? intval($post['status']) : 1;
  113. if($code==""){
  114. return error_show(1002,'功能code不能为空');
  115. }
  116. try{
  117. $where = ['menuid'=>$pageid,'action_code'=>$code];
  118. $true =Db::name("action")->where($where)->find();
  119. $data = ['menuid'=>$pageid,'action_code'=>$code,'status'=>$status,"updatetime"=>date("Y-m-d H:i:s"),"addtime"=>date("Y-m-d H:i:s")];
  120. if($true){
  121. return error_show(1003,'此功能已存在');
  122. }else{
  123. Db::name("action")->insert($data);
  124. return app_show(0,"添加成功");
  125. }
  126. }catch (\Exception $e){
  127. return error_show(1005,$e->getMessage());
  128. }
  129. }
  130. /**
  131. * @return \think\response\Json
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\DbException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. * @throws \think\exception\DbException
  136. */
  137. public function index(){
  138. $post =$this->request->post();
  139. $data = Db::name("admin_menu")->where(["pid"=>0,"status"=>1,"is_del"=>0])->select();
  140. $result = [];
  141. if(empty($data)){
  142. return app_show(0,"获取成功",$result);
  143. }
  144. foreach ($data as $key=>$val){
  145. $val["child"]=[];
  146. $result[$val['id']] =$val;
  147. }
  148. $child =Db::name("admin_menu")->where([["pid","<>",0],['status',"=",1],["is_del","=",0]])->select();
  149. foreach ($child as $k=>$value){
  150. // $act = PasAction::all(['menuid'=>$value['id'],"status"=>1]);
  151. $act =Db::name("action")
  152. ->alias("a")
  153. ->leftJoin("action_list l","a.action_code=l.action_code")
  154. ->field("a.*,action_name")
  155. ->where(['a.menuid'=>$value['id'],"a.status"=>1])
  156. ->withAttr('id',function($val){
  157. return (string)$val;
  158. })
  159. ->select()
  160. ->toArray();
  161. $act_data = Db::name("action_field")->where(['menuid'=>$value['id'],"status"=>1])->select();
  162. $value['action'] = $act;
  163. $value['action_data'] = $act_data;
  164. if(array_key_exists($value['pid'],$result)){
  165. $result[$value['pid']]["child"][]=$value;
  166. }
  167. }
  168. return app_show(0,"获取成功",array_values($result));
  169. }
  170. /**@param id menu 主键id
  171. * @return \think\response\Json
  172. * @throws \think\exception\DbException
  173. */
  174. public function ActionInfo(){
  175. $post =$this->request->post();
  176. $token = isset($post['token']) ? trim($post['token']) : "";
  177. if($token==""){
  178. return error_show(101,'token不能为空');
  179. }
  180. $effetc = VerifyTokens($token);
  181. if(!empty($effetc) && $effetc['code']!=0){
  182. return error_show($effetc['code'],$effetc['message']);
  183. }
  184. $id = isset($post['id'])? intval($post['id']) :"";
  185. if($id==""){
  186. return error_show(1002,'功能id不能为空');
  187. }
  188. $menu = Db::name("action_list")->where("id","=",$id)->find();
  189. if(empty($menu)){
  190. return error_show(1003,"未找到对应的数据");
  191. }
  192. return app_show(0,"获取成功!",$menu);
  193. }
  194. }