Queue.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller;
  15. use library\Controller;
  16. use library\service\ProcessService;
  17. use think\Console;
  18. use think\Db;
  19. use think\exception\HttpResponseException;
  20. use app\service\queue\ImportTable1Queue;
  21. use app\service\queue\ImportTable2Queue;
  22. /**
  23. * 上传管理
  24. * Class Queue
  25. * @package app\admin\controller
  26. */
  27. class Queue extends Controller
  28. {
  29. /**
  30. * 绑定数据表
  31. * @var string
  32. */
  33. protected $table = 'system_queue';
  34. /**
  35. * 上传记录
  36. * @auth true
  37. * @menu true
  38. * @throws \think\Exception
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. * @throws \think\exception\DbException
  42. * @throws \think\exception\PDOException
  43. */
  44. public function index()
  45. {
  46. if (session('user.username') === 'admin') try {
  47. $this->message = Console::call('xtask:state')->fetch();
  48. $this->command = ProcessService::instance()->think('xtask:start');
  49. $this->listen = preg_match('/process.*?\d+.*?running/', $this->message, $attr);
  50. } catch (\Exception $exception) {
  51. $this->listen = false;
  52. $this->message = $exception->getMessage();
  53. }
  54. $this->title = '上传记录';
  55. sysoplog('上传记录', '访问上传记录面');
  56. $this->iswin = ProcessService::instance()->iswin();
  57. $query = $this->_query($this->table)->dateBetween('create_at,start_at,end_at');
  58. $query->like('title,preload')->equal('status,type,is_undo')->order('id desc')->page();
  59. }
  60. /**
  61. * 数据列表处理
  62. * @auth true
  63. * @menu true
  64. * @param array $data
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. * @throws \think\exception\DbException
  68. */
  69. protected function _index_page_filter(&$data)
  70. {
  71. }
  72. /**
  73. * 导入表一数据
  74. * @auth true
  75. * @menu true
  76. * @throws \think\Exception
  77. * @throws \think\exception\PDOException
  78. */
  79. public function import_table1()
  80. {
  81. // $query = "SHOW FULL COLUMNS FROM system_courier";
  82. // $table = Db::query($query); //获取表所有字段
  83. // $field = array_column($table,'Field'); //提取表字段,放到一维数组中
  84. $username = $this->app->session->get('user')['username'];
  85. $get = $this->request->get();
  86. $import_path = $_SERVER['DOCUMENT_ROOT'].'/'.$get['path'];
  87. $url = $get['url'];
  88. $ordfilename = $get['ordfilename'];
  89. if (getConfigValue('storage_type')=='oss'){
  90. $save_dir = "./upload/excle/"; // 服务资源目录
  91. $filename = date('Ymd').time().".xlsx"; // 自定义名称
  92. $res = getFile($url,$save_dir,$filename,1);
  93. $import_path = $_SERVER['DOCUMENT_ROOT'].'/upload/excle/'.$res;
  94. }
  95. try {
  96. $result = sysqueue($ordfilename, ImportTable1Queue::URI, 0, ['type' => 1,'local_url'=>$import_path,'import_name'=>$username], 0);
  97. if ($result['status']){
  98. sysoplog('上传记录', '导入表一数据成功');
  99. $this->success($result['info']);
  100. }else{
  101. sysoplog('上传记录', '导入表一数据失败');
  102. $this->error($result['info'],[],3);
  103. }
  104. } catch (\think\exception\ValidateException $e) {
  105. $this->error($e->getMessage());
  106. }
  107. }
  108. /**
  109. * 导入表二数据
  110. * @auth true
  111. * @menu true
  112. * @throws \think\Exception
  113. * @throws \think\exception\PDOException
  114. */
  115. public function import_table2()
  116. {
  117. set_time_limit(0);
  118. $username = $this->app->session->get('user')['username'];
  119. $get = $this->request->get();
  120. $import_path = $_SERVER['DOCUMENT_ROOT'].'/'.$get['path'];
  121. $url = $get['url'];
  122. $ordfilename = $get['ordfilename'];
  123. if (getConfigValue('storage_type')=='oss'){
  124. $save_dir = "./upload/excle/"; // 服务资源目录
  125. $filename = date('Ymd').time().".xlsx"; // 自定义名称
  126. $res = getFile($url,$save_dir,$filename,1);
  127. $import_path = $_SERVER['DOCUMENT_ROOT'].'/upload/excle/'.$res;
  128. }
  129. try {
  130. $result = sysqueue($ordfilename, ImportTable2Queue::URI, 0, ['type' => 2,'local_url'=>$import_path,'import_name'=>$username], 0);
  131. if ($result['status']){
  132. sysoplog('上传记录', '导入表二数据成功');
  133. $this->success($result['info']);
  134. }else{
  135. sysoplog('上传记录', '导入表二数据失败');
  136. $this->error($result['info'],[],3);
  137. }
  138. } catch (\think\exception\ValidateException $e) {
  139. $this->error($e->getMessage());
  140. }
  141. }
  142. /**
  143. * 重置任务
  144. * @auth true
  145. * @menu true
  146. * @throws \think\Exception
  147. * @throws \think\exception\PDOException
  148. */
  149. public function redo()
  150. {
  151. $id = $this->request->get('id');
  152. $queue = Db::name($this->table)->where('id',$id)->find();
  153. $info = Db::name($this->table)->where('type',$queue['type'])->whereIn('status','1,2')->count();
  154. if ($info){
  155. $this->error('有待处理或处理中的任务,无法重置');
  156. }
  157. $log = ['action'=>'上传记录','content'=>'重置ID:'.$id.'任务'];
  158. $this->_save($this->table,['status' => '1'],$log);
  159. }
  160. /**
  161. * WIN开始监听任务
  162. * @auth false
  163. */
  164. public function start()
  165. {
  166. try {
  167. $message = nl2br(Console::call('xtask:start')->fetch());
  168. if (preg_match('/process.*?\d+/', $message, $attr)) {
  169. $this->success('任务监听主进程启动成功!');
  170. } else {
  171. $this->error($message);
  172. }
  173. } catch (HttpResponseException $exception) {
  174. throw $exception;
  175. } catch (\Exception $e) {
  176. $this->error($e->getMessage());
  177. }
  178. }
  179. /**
  180. * WIN停止监听任务
  181. * @auth false
  182. */
  183. public function stop()
  184. {
  185. try {
  186. $message = nl2br(Console::call('xtask:stop')->fetch());
  187. if (stripos($message, 'succeeded')) {
  188. $this->success('停止任务监听主进程成功!');
  189. } elseif (stripos($message, 'finish')) {
  190. $this->success('没有找到需要停止的进程!');
  191. } else {
  192. $this->error($message);
  193. }
  194. } catch (HttpResponseException $exception) {
  195. throw $exception;
  196. } catch (\Exception $e) {
  197. $this->error($e->getMessage());
  198. }
  199. }
  200. /**
  201. * 清理3天前的记录
  202. * @auth false
  203. * @throws \think\Exception
  204. * @throws \think\exception\PDOException
  205. */
  206. public function clear()
  207. {
  208. $map = [['time', '<', strtotime('-3days')]];
  209. $result = Db::name($this->table)->where($map)->delete();
  210. if ($result !== false) {
  211. $this->success('成功清理3天前的任务记录!');
  212. } else {
  213. $this->error('清理3天前的任务记录失败!');
  214. }
  215. }
  216. /**
  217. * 删除任务
  218. * @auth true
  219. * @menu true
  220. * @throws \think\Exception
  221. * @throws \think\exception\PDOException
  222. */
  223. public function remove()
  224. {
  225. $log = ['action'=>'上传记录'];
  226. $this->_delete($this->table,$log);
  227. }
  228. /**
  229. * 回撤
  230. * @auth true
  231. * @menu true
  232. * @throws \think\Exception
  233. * @throws \think\exception\PDOException
  234. */
  235. public function undo()
  236. {
  237. $id = $this->request->get('id');
  238. Db::startTrans();
  239. try {
  240. $queue = Db::name('system_queue')->where('id',$id)->find();
  241. $queue_result = Db::name('system_queue')->where('id',$id)->update(
  242. [
  243. 'is_undo'=>2,
  244. 'undo_at'=>date('Y-m-d H:i:s'),
  245. 'undo_name'=>$this->app->session->get('user')['username']
  246. ]
  247. );
  248. if ($queue['type']==1){
  249. $values = Db::name('system_values')->where('import_log_id',$id)->delete();
  250. if ($queue_result && $values){
  251. Db::commit();
  252. sysoplog('上传记录', '回撤ID:'.$id.'成功');
  253. $this->success('回撤成功');
  254. }else{
  255. Db::rollback();
  256. sysoplog('上传记录', '回撤ID:'.$id.'失败');
  257. $this->error('撤销失败');
  258. }
  259. }else{
  260. $table2 = Db::name('system_table2')->where('import_log_id',$id)->delete();
  261. $values2 = Db::name('system_values2')->where('import_log_id',$id)->delete();
  262. $values3 = Db::name('system_values3')->where('import_log_id',$id)->delete();
  263. if ($queue_result && $table2 && $values2 && $values3){
  264. Db::commit();
  265. sysoplog('上传记录', '回撤ID:'.$id.'成功');
  266. $this->success('回撤成功');
  267. }else{
  268. Db::rollback();
  269. sysoplog('上传记录', '回撤ID:'.$id.'失败');
  270. $this->error('撤销失败');
  271. }
  272. }
  273. }catch (\think\exception\ValidateException $e) {
  274. Db::rollback();
  275. $this->error('撤销失败');
  276. }
  277. }
  278. }