人人商城

display.ctrl.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. load()->model('cloud');
  8. load()->func('cron');
  9. $_W['page']['title'] = '计划任务 - 公众号选项';
  10. $dos = array('list', 'post', 'del', 'run', 'status', 'sync');
  11. $do = in_array($do, $dos) ? $do : 'list';
  12. if($do == 'sync') {
  13. $id = intval($_GPC['id']);
  14. $data = pdo_get('core_cron', array('uniacid' => $_W['uniacid'], 'id' => $id));
  15. if(empty($data)) {
  16. message('任务不存在或已经删除', referer(), 'error');
  17. }
  18. $result = cloud_cron_get($data['cloudid']);
  19. if(is_error($result)) {
  20. message($result['message'], referer(), 'error');
  21. }
  22. $cron = $result['message'];
  23. if(!is_array($cron)) {
  24. message('从云服务同步数据出错', referer(), 'error');
  25. }
  26. $cron['id'] = $data['id'];
  27. unset($cron['siteid'], $cron['failed_number'], $cron['extra']);
  28. pdo_update('core_cron', $cron, array('uniacid' => $_W['uniacid'], 'id' => $id));
  29. message('同步计划任务成功', referer(), 'success');
  30. }
  31. if($do == 'post') {
  32. $id = intval($_GPC['id']);
  33. if(!empty($id)) {
  34. $data = pdo_get('core_cron', array('uniacid' => $_W['uniacid'], 'id' => $id));
  35. if(empty($data)) {
  36. message('任务不存在或已经删除', referer(), 'error');
  37. }
  38. $result = cloud_cron_get($data['cloudid']);
  39. if(is_error($result)) {
  40. message("从云服务获取任务失败,详情:{$result['message']}", referer(), 'error');
  41. }
  42. $cron = $result['message'];
  43. $cron['minute'] = str_replace("\t", ',', $cron['minute']);
  44. } else {
  45. $cron = array('weekday' => -1, 'day' => -1, 'hour' => -1, 'type' => 1, 'lastruntime' => TIMESTAMP + 86400, 'minute' => rand(1, 59));
  46. }
  47. if(checksubmit('form')) {
  48. $data['uniacid'] = $_W['uniacid'];
  49. $data['createtime'] = TIMESTAMP;
  50. $data['name'] = trim($_GPC['name']) ? trim($_GPC['name']) : message('请填写任务名称', '', 'error');
  51. $data['filename'] = trim($_GPC['filename']) ? trim($_GPC['filename']) : message('请填写任务脚本文件名称', '', 'error');
  52. $data['type'] = intval($_GPC['type']);
  53. if($data['type'] == 1) {
  54. $data['lastruntime'] = $data['nextruntime'] = strtotime($_GPC['executetime']);
  55. if($data['lastruntime'] <= TIMESTAMP + 3600) {
  56. message('定时任务的执行时间不能早于当前时间', '', 'error');
  57. }
  58. }
  59. $data['status'] = intval($_GPC['status']);
  60. $data['day'] = intval($_GPC['weekday']) == -1 ? intval($_GPC['day']) : -1;
  61. $data['weekday'] = intval($_GPC['weekday']);
  62. $data['hour'] = intval($_GPC['hour']);
  63. $data['module'] = trim($_GPC['module']);
  64. $_GPC['minute'] = str_replace(',', ',', $_GPC['minute']);
  65. if(strpos($_GPC['minute'], ',') !== FALSE) {
  66. $minutenew = explode(',', $_GPC['minute']);
  67. foreach($minutenew as $key => $val) {
  68. $minutenew[$key] = $val = intval($val);
  69. if($val < 0 || $var > 59) {
  70. unset($minutenew[$key]);
  71. }
  72. }
  73. $minutenew = array_slice(array_unique($minutenew), 0, 2);
  74. $minutenew = implode("\t", $minutenew);
  75. } else {
  76. $minutenew = intval($_GPC['minute']);
  77. $minutenew = $minutenew >= 0 && $minutenew < 60 ? $minutenew : '';
  78. }
  79. $data['minute'] = $minutenew;
  80. if($id > 0) {
  81. $data['id'] = $cron['cloudid'];
  82. $status = cloud_cron_update($data);
  83. if(is_error($status)) {
  84. message($status['message'], '', 'error');
  85. }
  86. $data['id'] = $id;
  87. unset($data['cloudid']);
  88. pdo_update('core_cron', $data, array('id' => $id, 'uniacid' => $_W['uniacid']));
  89. message('编辑计划任务成功', url('cron/display/list'), 'success');
  90. } else {
  91. $status = cloud_cron_create($data);
  92. if(is_error($status)) {
  93. message($status['message'], '', 'error');
  94. }
  95. $data['cloudid'] = $status['cron_id'];
  96. pdo_insert('core_cron', $data);
  97. message('添加计划任务成功', url('cron/display/list'), 'success');
  98. }
  99. }
  100. $modules_temp = uni_modules();
  101. $modules['task'] = array('name' => 'task', 'title' => '系统任务');
  102. foreach($modules_temp as $module) {
  103. if(!$module['issystem']) {
  104. $modules[$module['name']] = array('name' => $module['name'], 'title' => $module['title']);
  105. }
  106. }
  107. }
  108. if($do == 'list') {
  109. $pindex = max(1, intval($_GPC['page']));
  110. $psize = 15;
  111. $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('core_cron') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
  112. $crons = pdo_fetchall('SELECT * FROM ' . tablename('core_cron') . ' WHERE uniacid = :uniacid ORDER BY id DESC LIMIT ' . ($pindex - 1) * $psize . ", {$psize}", array(':uniacid' => $_W['uniacid']));
  113. $pager = pagination($total, $pindex, $psize);
  114. if(!empty($crons)) {
  115. foreach($crons as &$cron) {
  116. $id = $cron['id'];
  117. $result = array();
  118. $result = cloud_cron_get($cron['cloudid']);
  119. if(!is_error($result) && is_array($result['message'])) {
  120. $cron = $result['message'];
  121. $cron['id'] = $id;
  122. unset($cron['siteid'], $cron['failed_number'], $cron['extra']);
  123. pdo_update('core_cron', $cron, array('uniacid' => $_W['uniacid'], 'id' => $id));
  124. }
  125. }
  126. }
  127. $weekday_cn = array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
  128. if(!empty($crons)) {
  129. foreach($crons as &$cron) {
  130. $modules[] = $cron['module'];
  131. $cn = '';
  132. if($cron['day'] > 0 && $cron['day'] < 32) {
  133. $cn = '每月' . $cron['day'] . '日';
  134. } elseif($cron['weekday'] >= 0 && $cron['weekday'] < 7) {
  135. $cn = '每' . $weekday_cn[$cron['weekday']];
  136. } elseif($cron['hour'] >= 0 && $cron['hour'] < 24) {
  137. $cn = '每天';
  138. } else{
  139. $cn = '每小时';
  140. }
  141. $cn .= ($cron['hour'] >= 0 && $cron['hour'] < 24) ? sprintf('%02d', $cron['hour']) . '时' : '';
  142. if(!in_array($cron['minute'], array(-1, ''))) {
  143. foreach($cron['minute'] = explode("\t", $cron['minute']) as $k => $v) {
  144. $cron['minute'][$k] = sprintf('%02d', $v);
  145. }
  146. $cron['minute'] = implode(',', $cron['minute']);
  147. $cn .= $cron['minute'] . '分';
  148. } else {
  149. $cn .= '00分';
  150. }
  151. $cron['lastruntime'] = $cron['lastruntime'] ? date('Y-m-d H:i:s', $cron['lastruntime']) : 'N/A';
  152. $cron['nextruntime'] = $cron['nextruntime'] ? date('Y-m-d H:i:s', $cron['nextruntime']) : 'N/A';
  153. $cron['run'] = $cron['status'];
  154. $cron['cn'] = $cn;
  155. }
  156. if(!empty($modules)) {
  157. $modules = "'" . implode($modules, "','") . "'";
  158. $modules = pdo_fetchall('SELECT title,name FROM ' .tablename('modules') . " WHERE name IN ({$modules})", array(), 'name');
  159. $modules['task'] = array('name' => 'task', 'title' => '系统任务');
  160. }
  161. }
  162. }
  163. if($do == 'del') {
  164. $ids = $_GPC['id'];
  165. if(!is_array($ids)) {
  166. $ids = array($ids);
  167. }
  168. if(!empty($ids)) {
  169. foreach($ids as $id) {
  170. $id = intval($id);
  171. if($id > 0) {
  172. $cron = pdo_get('core_cron', array('uniacid' => $_W['uniacid'], 'id' => $id));
  173. if(!empty($cron)) {
  174. $result = cloud_cron_remove($cron['cloudid']);
  175. if(!is_error($result)) {
  176. pdo_delete('core_cron', array('uniacid' => $_W['uniacid'], 'id' => $id));
  177. } else {
  178. message("删除{$cron['title']}失败", url('cron/display/list'), 'error');
  179. }
  180. }
  181. }
  182. }
  183. message('删除计划任务成功', url('cron/display/list'), 'success');
  184. } else {
  185. message('没有选择要删除的任务', referer(), 'error');
  186. }
  187. }
  188. if($do == 'run') {
  189. $id = intval($_GPC['id']);
  190. $status = cron_run($id);
  191. if(is_error($status)) {
  192. message($status['message'], referer(), 'error');
  193. }
  194. message('执行计划任务成功', referer(), 'success');
  195. }
  196. if($do == 'status') {
  197. $id = intval($_GPC['id']);
  198. $status = intval($_GPC['status']);
  199. if(!in_array($status, array(0, 1))) {
  200. exit('状态码错误');
  201. }
  202. $cron = pdo_get('core_cron', array('uniacid' => $_W['uniacid'], 'id' => $id));
  203. if(empty($cron)) {
  204. exit('任务不存在或已删除');
  205. }
  206. $result = cloud_cron_change_status($cron['cloudid'], $status);
  207. if(is_error($result)) {
  208. exit($result['message']);
  209. }
  210. pdo_update('core_cron', array('status' => $status), array('uniacid' => $_W['uniacid'], 'id' => $id));
  211. exit('success');
  212. }
  213. template('cron/display');