Action.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\admin\controller;
  4. use app\BaseController;
  5. use think\facade\Db;
  6. use think\facade\Validate;
  7. use think\Request;
  8. use think\App;
  9. class Action extends BaseController
  10. {
  11. /**
  12. * public function __construct(App $app)
  13. * {
  14. * parent::__construct($app);
  15. * $post =$this->request->post();
  16. * $token = isset($post['token']) ? trim($post['token']) : "";
  17. * if($token==""){
  18. * return json_show(101,'token不能为空');
  19. *
  20. * }
  21. * $effetc = VerifyTokens($token);
  22. * if(!empty($effetc) && $effetc['code']!=0){
  23. * return json_show($effetc['code'],$effetc['message']);
  24. *
  25. * }
  26. * }
  27. * public function ActionList(){
  28. * $post =$this->request->post();
  29. * $pageid = isset($post['id']) ? intval($post['id']) : "";
  30. * if($pageid==""){
  31. * return json_show(1001,'页面id不能为空');
  32. * }
  33. * $condition = ['menuid'=>$pageid];
  34. * $data=Db::name('action')->alias("a")->leftJoin("action_list l","a.action_code=l.action_code")->field
  35. * ("a.*,action_name")->where($condition)->select();
  36. * return json_show(0,"获取成功",$data);
  37. * }
  38. */
  39. public function ActionSave()
  40. {
  41. $post = $this->request->only(['id', 'action_code'], 'post', 'trim');
  42. $val = Validate::rule([
  43. 'id|ID' => 'require|number|gt:0',
  44. 'action_code' => 'require'
  45. ]);
  46. if ($val->check($post) == false) return error_show(1004, $val->getError());
  47. $rs = Db::name("action")
  48. ->where(['is_del' => 0, 'id' => $post['id']])
  49. ->update([
  50. 'action_code' => $post['action_code'],
  51. 'updatetime' => date("Y-m-d H:i:s")
  52. ]);
  53. return $rs ? json_show(0, "修改成功") : json_show(0, "修改失败");
  54. // $post = $this->request->post();
  55. // $actionid = isset($post['id']) ? intval($post['id']) : "";
  56. // if ($actionid != "") {
  57. // $isf = Db::name("action_list")->where("id", "=", $actionid)->find();
  58. // if ($isf == false) {
  59. // return json_show(1005, "未找到数据");
  60. // }
  61. // }
  62. // $action = isset($post['action_name']) ? trim($post['action_name']) : "";
  63. // if ($action == "") {
  64. // return json_show(1003, '功能名称不能为空');
  65. // }
  66. // $desc = isset($post['action_desc']) ? trim($post['action_desc']) : "";
  67. // $status = isset($post['status']) ? intval($post['status']) : 0;
  68. // $data = [
  69. // "action_name" => $action,
  70. // "action_desc" => $desc,
  71. // "action_code" => "",
  72. // ];
  73. //
  74. // $isTrue = Db::name("action_list")->where(["action_name" => $action])->find();
  75. // if ($isTrue) {
  76. // if ($isTrue['id'] != $actionid || $actionid == "") {
  77. // return json_show(1003, '功能名称不能重复');
  78. // }
  79. // }
  80. //
  81. // try {
  82. // $message = "";
  83. // if ($actionid == "") {
  84. // $data['status'] = $status;
  85. // $data['is_show'] = 1;
  86. // $message = "新建成功";
  87. // } else {
  88. // $data['status'] = $status;
  89. // $data['id'] = $actionid;
  90. // $message = "更新成功";
  91. // }
  92. // Db::name("action_list")->save($data);
  93. // return json_show(0, $message);
  94. // } catch (\Exception $e) {
  95. // return json_show(1005, $e->getMessage());
  96. // }
  97. }
  98. public function ActionStatus()
  99. {
  100. $post = $this->request->post();
  101. $actid = isset($post['id']) ? intval($post['id']) : "";
  102. if ($actid == "") {
  103. return json_show(1001, '功能id不能为空');
  104. }
  105. $status = isset($post['status']) ? intval($post['status']) : 1;
  106. try {
  107. $data = ['status' => $status, "updatetime" => date("Y-m-d H:i:s")];
  108. $result = Db::name("action")->where("id", "=", $actid)->save($data);
  109. return $result ? json_show(0, "更新成功") : json_show(1004, "更新失败");
  110. // if ($result) {
  111. // return json_show(0, "更新成功");
  112. // } else {
  113. // return json_show(1004, "更新失败");
  114. // }
  115. } catch (\Exception $e) {
  116. return json_show(1003, $e->getMessage());
  117. }
  118. }
  119. /*
  120. *
  121. * public function ActionAdd(){
  122. * $post =$this->request->post();
  123. *
  124. * $pageid = isset($post['menuid']) ? intval($post['menuid']) : "";
  125. * if($pageid==""){
  126. * return json_show(1001,'页面id不能为空');
  127. * }
  128. * $code = isset($post['action_code']) ? trim($post['action_code']) : "";
  129. *
  130. * $status = isset($post['status']) ? intval($post['status']) : 1;
  131. * if($code==""){
  132. * return json_show(1002,'功能code不能为空');
  133. * }
  134. * try{
  135. * $where = ['menuid'=>$pageid,'action_code'=>$code];
  136. * $true =Db::name("action")->where($where)->find();
  137. * $data = ['menuid'=>$pageid,'action_code'=>$code,'status'=>$status,"updatetime"=>date("Y-m-d H:i:s"),"addtime"=>date("Y-m-d H:i:s")];
  138. * if($true){
  139. * return json_show(1003,'此功能已存在');
  140. * }else{
  141. * Db::name("action")->insert($data);
  142. * return json_show(0,"添加成功");
  143. * }
  144. * }catch (\Exception $e){
  145. * return json_show(1005,$e->getMessage());
  146. * }
  147. * }
  148. *
  149. * public function index(){
  150. * $post =$this->request->post();
  151. * $data = Db::name("admin_menu")->where(["pid"=>0,"status"=>1,"is_del"=>0])->select();
  152. * $result = [];
  153. * if(empty($data)){
  154. * return json_show(0,"获取成功",$result);
  155. * }
  156. * foreach ($data as $key=>$val){
  157. * $val["child"]=[];
  158. * $result[$val['id']] =$val;
  159. * }
  160. *
  161. * $child =Db::name("admin_menu")->where([["pid","<>",0],['status',"=",1],["is_del","=",0]])->select();
  162. * foreach ($child as $k=>$value){
  163. * // $act = PasAction::all(['menuid'=>$value['id'],"status"=>1]);
  164. * $act =Db::name("action")
  165. * ->alias("a")
  166. * ->leftJoin("action_list l","a.action_code=l.action_code")
  167. * ->field("a.*,action_name")
  168. * ->where(['a.menuid'=>$value['id'],"a.status"=>1])
  169. * ->withAttr('id',function($val){
  170. * return (string)$val;
  171. * })
  172. * ->select()
  173. * ->toArray();
  174. * $act_data = Db::name("action_field")->where(['menuid'=>$value['id'],"status"=>1])->select();
  175. * $value['action'] = $act;
  176. * $value['action_data'] = $act_data;
  177. * if(array_key_exists($value['pid'],$result)){
  178. * $result[$value['pid']]["child"][]=$value;
  179. * }
  180. * }
  181. *
  182. * return json_show(0,"获取成功",array_values($result));
  183. * }
  184. *
  185. * public function ActionInfo(){
  186. * $post =$this->request->post();
  187. * $token = isset($post['token']) ? trim($post['token']) : "";
  188. * if($token==""){
  189. * return json_show(101,'token不能为空');
  190. * }
  191. * $effetc = VerifyTokens($token);
  192. * if(!empty($effetc) && $effetc['code']!=0){
  193. * return json_show($effetc['code'],$effetc['message']);
  194. * }
  195. * $id = isset($post['id'])? intval($post['id']) :"";
  196. * if($id==""){
  197. * return json_show(1002,'功能id不能为空');
  198. * }
  199. * $menu = Db::name("action_list")->where("id","=",$id)->find();
  200. * if(empty($menu)){
  201. * return json_show(1003,"未找到对应的数据");
  202. * }
  203. * return json_show(0,"获取成功!",$menu);
  204. * }
  205. * **/
  206. //11获取所有菜单列表数据
  207. public function index()
  208. {
  209. $post = $this->request->post();
  210. $data = Db::name("admin_menu")
  211. ->where(["pid" => 0, "status" => 1, "is_del" => 0])
  212. ->order(['weight'=>'desc','id'=>'desc'])
  213. ->select()
  214. ->toArray();
  215. $result = [];
  216. if (empty($data)) {
  217. return app_show(0, "获取成功", $result);
  218. }
  219. foreach ($data as $key => $val) {
  220. $val["child"] = [];
  221. $result[$val['id']] = $val;
  222. }
  223. $child_where = [["pid", "<>", 0], ['status', "=", 1], ["is_del", "=", 0]];
  224. if (isset($post['level']) && $post['level'] !== '') $child_where[] = ['level', 'in', $post['level']];
  225. $child = Db::name("admin_menu")
  226. ->where($child_where)
  227. ->order(['weight'=>'desc','id'=>'desc'])
  228. ->select()
  229. ->toArray();
  230. foreach ($child as $k => $value) {
  231. // $act = PasAction::all(['menuid'=>$value['id'],"status"=>1]);
  232. $act = Db::name("action")
  233. ->alias("a")
  234. ->leftJoin("action_list l", "a.action_code=l.action_code")
  235. ->field("a.*,action_name")
  236. ->where(['a.menuid' => $value['id'], "a.status" => 1])
  237. ->select()
  238. ->toArray();
  239. $act_data = Db::name("action_field")
  240. ->where(['menuid' => $value['id'], "status" => 1])
  241. ->select()
  242. ->toArray();
  243. $value['action'] = $act;
  244. $value['action_data'] = $act_data;
  245. if (array_key_exists($value['pid'], $result)) {
  246. $result[$value['pid']]["child"][] = $value;
  247. }
  248. }
  249. return json_show(0, "获取成功", array_values($result));
  250. }
  251. public function ActionList(){
  252. $post =$this->request->post();
  253. $pageid = isset($post['id']) ? intval($post['id']) : "";
  254. if($pageid==""){
  255. return error_show(1001,'页面id不能为空');
  256. }
  257. $condition = ['menuid'=>$pageid];
  258. $data=Db::name('action')
  259. ->alias("a")
  260. ->leftJoin("action_list l","a.action_code=l.action_code")
  261. ->field("a.*,action_name")
  262. ->where($condition)
  263. ->select()
  264. ->toArray();
  265. return app_show(0,"获取成功",$data);
  266. }
  267. /** 菜单下功能信息状态修改
  268. * @return \think\response\Json|void
  269. * @throws \think\exception\DbException
  270. */
  271. public function ActionAdd()
  272. {
  273. $post = $this->request->post();
  274. $pageid = isset($post['menuid']) ? intval($post['menuid']) : "";
  275. if ($pageid == "") {
  276. return error_show(1001, '菜单id不能为空');
  277. }
  278. $code = isset($post['action_code']) ? trim($post['action_code']) : "";
  279. if ($code == "") {
  280. return error_show(1002, '功能code不能为空');
  281. }
  282. $status = isset($post['status']) ? intval($post['status']) : 1;
  283. try {
  284. $where = ['menuid' => $pageid, 'action_code' => $code];
  285. $true = Db::name("action")->field('id')->where($where)->find();
  286. if ($true) return error_show(1003, '此功能已存在');
  287. else {
  288. $data = ['menuid' => $pageid, 'action_code' => $code, 'status' => $status, "updatetime" => date("Y-m-d H:i:s"), "addtime" => date("Y-m-d H:i:s")];
  289. Db::name("action")->insert($data);
  290. return app_show(0, "添加成功");
  291. }
  292. } catch (\Exception $e) {
  293. return error_show(1005, $e->getMessage());
  294. }
  295. }
  296. /** 菜单下功能信息状态修改
  297. * @return \think\response\Json|void
  298. * @throws \think\exception\DbException
  299. */
  300. public function ActionDel()
  301. {
  302. $post = $this->request->filter('trim')->post();
  303. $action_id = isset($post['action_id']) ? intval($post['action_id']) : "";
  304. if ($action_id === "") {
  305. return json_show(1001, '参数action_id不能为空');
  306. }
  307. $action = Db::name("action_list")->where(["id" => $action_id, "is_del" => 0])->find();
  308. if ($action == false) {
  309. return json_show(1004, "未找到功能数据");
  310. }
  311. $upda = ["is_del" => 0, "updatetime" => date("Y-m-d H:i:s")];
  312. Db::startTrans();
  313. try {
  314. $up = Db::name("action_list")->where($action)->update($upda);
  315. if ($up) {
  316. $upall = Db::name("action")->where(["action_code" => $action['action_code'], "is_del" => 0])->update($upda);
  317. Db::commit();
  318. return json_show(0, "删除成功");
  319. }
  320. Db::rollback();
  321. return json_show(1005, "删除失败");
  322. } catch (\Exception $e) {
  323. Db::rollback();
  324. return json_show(1005, $e->getMessage());
  325. }
  326. }
  327. }