Exec.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\facade\Db;
  5. use think\facade\Validate;
  6. use think\Request;
  7. use think\facade\Cache;
  8. class Exec extends Base
  9. {
  10. public function list()
  11. {
  12. $post = $this->post;
  13. $token = isset($post['token']) ? trim($post['token']) : "";
  14. if ($token == "") {
  15. return error_show(101, 'token不能为空');
  16. }
  17. $where = ['type' => 0];
  18. $page = isset($post['page']) ? intval($post['page']) : 1;
  19. $size = isset($post['size']) ? intval($post['size']) : 10;
  20. $count = Db::name("exec")->where($where)->count();
  21. $total = ceil($count / $size) > 1 ? ceil($count / $size) : 1;
  22. $page = $page >= $total ? intval($total) : $page;
  23. $list = Db::name("exec")->where($where)->page($page, $size)->select();
  24. return app_show(0, "获取成功", ["list" => $list, "count" => $count]);
  25. }
  26. public function nowlist()
  27. {
  28. $param = $this->request->only(['token', 'page' => 1, 'size' => 15, 'type' => 1], 'post', 'trim');
  29. $val = Validate::rule([
  30. 'token' => 'require',
  31. 'page' => 'require|number',
  32. 'size' => 'require|number',
  33. 'type' => 'require|number',
  34. ]);
  35. if (!$val->check($param)) return error_show(1005, $val->getError());
  36. $count = Db::name("exec")
  37. ->where('type', $param['type'])
  38. ->count('id');
  39. $list = Db::name("exec")
  40. ->where('type', $param['type'])
  41. ->page($param['page'], $param['size'])
  42. ->order('id')
  43. ->select()
  44. ->toArray();
  45. return app_show(0, "获取成功", ["list" => $list, "count" => $count]);
  46. }
  47. public function save()
  48. {
  49. $post = $this->post;
  50. $token = isset($post['token']) ? trim($post['token']) : "";
  51. if ($token == "") {
  52. return error_show(101, 'token不能为空');
  53. }
  54. $userinfo = GetUserInfo($token);
  55. if (!isset($userinfo['code']) || $userinfo['code'] != 0) {
  56. return error_show(101, '未能获取用户信息');
  57. }
  58. $start = isset($post['start']) && $post['start'] != "" ? $post['start'] . " 00:00:00" : '';
  59. $end = isset($post['end']) && $post['end'] != "" ? $post['end'] . " 23:59:59" : '';
  60. $id = isset($post['id']) && $post['id'] !== '' ? intval($post['id']) : "";
  61. if ($id == "") {
  62. return error_show(1004, "参数id不能为空");
  63. }
  64. $info = Db::name("exec")->where(["id" => $id])->find();
  65. if (empty($info)) {
  66. return error_show(1004, "未找到下载数据");
  67. }
  68. if($info['type']==1){
  69. $num = Db::name("exec")->where(["type" =>1,"status"=>1])->where("id","<>",$id)->count();
  70. if($num>0){
  71. return error_show(1004, "已有脚本在运行");
  72. }
  73. }
  74. if ($info['start'] == $start && $info['end'] == $end) {
  75. return error_show(1004, "数据已提交");
  76. }
  77. if ($start != "") {
  78. $info['start'] = $start;
  79. }
  80. if ($end != "") {
  81. $info['end'] = $end;
  82. }
  83. if (is_file(root_path() . 'public' . $info['down_url'])) {
  84. @unlink(root_path() . "public" . $info['down_url']);
  85. }
  86. $info['status'] = 1;
  87. $info['apply_id'] = $userinfo['data']['id'];
  88. $info['apply_name'] = $userinfo['data']['nickname'];
  89. $info['down_url'] = '';
  90. $info['remark'] = '';
  91. $info['updatetime'] = date("Y-m-d H:i:s");
  92. $info['addtime'] = date("Y-m-d H:i:s");
  93. $info['expiretime'] = date("Y-m-d H:i:s", time() + 7 * 24 * 3600);
  94. $up = Db::name("exec")->save($info);
  95. $redis = Cache::store("redis")->handler()->lPush("cxreport", json_encode($info));
  96. return $up ? app_show(0, "编辑成功") : error_show(1004, "编辑失败");
  97. }
  98. public function down()
  99. {
  100. $post = $this->post;
  101. $token = isset($post['token']) ? trim($post['token']) : "";
  102. if ($token == "") {
  103. return error_show(101, 'token不能为空');
  104. }
  105. $id = isset($post['id']) && $post['id'] !== '' ? intval($post['id']) : "";
  106. if ($id == "") {
  107. return error_show(1004, "参数id不能为空");
  108. }
  109. $info = Db::name("exec")->where(["id" => $id])->find();
  110. if (empty($info)) {
  111. return error_show(1004, "未找到下载数据");
  112. }
  113. if($info['type']==1){
  114. $num = Db::name("exec")->where(["type" =>1,"status"=>1])->count();
  115. if($num>0){
  116. return error_show(1004, "已有脚本在运行");
  117. }
  118. }
  119. // $effetc = VerifyTokens($token);
  120. // if(!empty($effetc) && $effetc['code']!=0){
  121. // return error_show($effetc['code'],$effetc['message']);
  122. // }
  123. $userinfo = GetUserInfo($token);
  124. if (!isset($userinfo['code']) || $userinfo['code'] != 0) {
  125. return error_show(101, '未能获取用户信息');
  126. }
  127. $info['status'] = 1;
  128. $info['down_url'] = '';
  129. $info['remark'] = '';
  130. $info['apply_id'] = $userinfo['data']['id'];
  131. $info['apply_name'] = $userinfo['data']['nickname'];
  132. $info['expiretime'] = date("Y-m-d H:i:s", time() + 7 * 24 * 3600);
  133. $info['updatetime'] = date("Y-m-d H:i:s");
  134. $info['addtime'] = date("Y-m-d H:i:s");
  135. $up = Db::name("exec")->save($info);
  136. $redis = Cache::store("redis")->handler()->lPush("cxreport", json_encode($info));
  137. return $up ? app_show(0, "编辑成功") : error_show(1004, "编辑失败");
  138. }
  139. }