人人商城

mass.ctrl.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. uni_user_permission_check('material_mass');
  8. $_W['page']['title'] = '定时群发-微信素材';
  9. $dos = array('list', 'post', 'cron', 'send', 'del');
  10. $do = in_array($do, $dos) ? $do : 'list';
  11. if($do == 'list') {
  12. set_time_limit(0);
  13. load()->model('cloud');
  14. $cloud = cloud_prepare();
  15. $cloud_error = 0;
  16. if(is_error($cloud)) {
  17. $cloud_error = 1;
  18. }
  19. $groups = pdo_fetch('SELECT * FROM ' . tablename('mc_fans_groups') . ' WHERE uniacid = :uniacid AND acid = :acid', array(':uniacid' => $_W['uniacid'], ':acid' => $_W['acid']));
  20. $groups = iunserializer($groups['groups']);
  21. $time = strtotime(date('Y-m-d'));
  22. $record = pdo_fetchall('SELECT * FROM ' . tablename('mc_mass_record') . ' WHERE uniacid = :uniacid AND sendtime >= :time ORDER BY sendtime ASC LIMIT 7', array(':uniacid' => $_W['uniacid'], ':time' => $time), 'sendtime');
  23. for($i = 0; $i < 7; $i++) {
  24. $time_key = date('Y-m-d', strtotime("+{$i} days", $time));
  25. $mass_old[$time_key] = array(
  26. 'msgtype' => 'news',
  27. 'group' => -1,
  28. 'time' => $time_key,
  29. 'status' => 1,
  30. 'clock' => '20:00',
  31. 'media' => array(
  32. 'items' => array(
  33. array(
  34. 'title' => '请选择素材'
  35. )
  36. )
  37. ),
  38. );
  39. }
  40. $mass_new = array();
  41. if(!empty($record)) {
  42. foreach($record as &$li) {
  43. $time_key = date('Y-m-d', $li['sendtime']);
  44. $li['time'] = $time_key;
  45. $li['clock'] = date('H:i', $li['sendtime']);
  46. $li['media'] = pdo_get('wechat_attachment', array('id' => $li['attach_id']));
  47. $li['media']['attach'] = tomedia($li['media']['attachment']);
  48. if($li['msgtype'] == 'video') {
  49. $li['media']['attach']['tag'] = iunserializer($li['media']['tag']);
  50. } elseif($li['msgtype'] == 'news') {
  51. $li['media']['items'] = pdo_getall('wechat_news', array('attach_id' => $li['attach_id']));
  52. foreach($li['media']['items'] as &$row) {
  53. $row['thumb_url'] = url('utility/wxcode/image', array('attach' => $row['thumb_url']));
  54. }
  55. } elseif($li['msgtype'] == 'wxcard') {
  56. $li['media'] = pdo_get('coupon', array('id' => $li['attach_id']));
  57. $li['media']['media_id'] = $li['media']['card_id'];
  58. $li['media']['logo_url'] = url('utility/wxcode/image', array('attach' => $li['media']['logo_url']));
  59. $li['media']['type'] = 'wxcard';
  60. }
  61. $li['media']['createtime_cn'] = date('Y-m-d H:i', $li['media']['createtime']);
  62. $li['media_id'] = $li['media']['media_id'];
  63. $mass_new[$time_key] = $li;
  64. }
  65. unset($record);
  66. }
  67. $mass = array_values((array_merge($mass_old, $mass_new)));
  68. template('material/mass');
  69. }
  70. if($do == 'del') {
  71. load()->func('cron');
  72. load()->model('cloud');
  73. $post = $_GPC['__input'];
  74. $mass = pdo_get('mc_mass_record', array('uniacid' => $_W['uniacid'], 'id' => intval($post['id'])));
  75. if(!empty($mass) && $mass['cron_id'] > 0) {
  76. $status = cron_delete(array($mass['cron_id']));
  77. if(is_error($status)) {
  78. message($status, '', 'ajax');
  79. }
  80. }
  81. pdo_delete('mc_mass_record', array('uniacid' => $_W['uniacid'], 'id' => intval($post['id'])));
  82. message(error(0, ''), '', 'ajax');
  83. }
  84. if($do == 'post') {
  85. load()->func('cron');
  86. load()->model('cloud');
  87. $cloud = cloud_prepare();
  88. if(is_error($cloud)) {
  89. message($cloud, '', 'ajax');
  90. }
  91. set_time_limit(0);
  92. $records = pdo_fetchall('SELECT id, cron_id FROM ' . tablename('mc_mass_record') . ' WHERE uniacid = :uniacid AND sendtime >= :time AND status = 1 ORDER BY sendtime ASC LIMIT 8', array(':uniacid' => $_W['uniacid'], ':time' => strtotime(date('Y-m-d'))), 'id');
  93. if(!empty($records)) {
  94. foreach($records as $re) {
  95. if(!$re['cron_id']) {
  96. continue;
  97. }
  98. $corn_ids[] = $re['cron_id'];
  99. }
  100. if(!empty($corn_ids)) {
  101. $status = cron_delete($corn_ids);
  102. if(is_error($status)) {
  103. message(error(-1, '删除群发错误,请重新提交'), '', 'ajax');
  104. }
  105. }
  106. $ids = implode(',', array_keys($records));
  107. pdo_query('DELETE FROM ' . tablename('mc_mass_record') . " WHERE uniacid = :uniacid AND id IN ({$ids})", array(':uniacid' => $_W['uniacid']));
  108. }
  109. $groups = pdo_fetch('SELECT * FROM ' . tablename('mc_fans_groups') . ' WHERE uniacid = :uniacid AND acid = :acid', array(':uniacid' => $_W['uniacid'], ':acid' => $_W['acid']));
  110. $groups = iunserializer($groups['groups']);
  111. $groups['-1'] = array('name' => '全部粉丝', 'count' => '');
  112. $post = $_GPC['__input'];
  113. $mass = $post['data'];
  114. $message = '';
  115. $sended = array();
  116. foreach($mass as $key => $row) {
  117. if($row['media_id']) {
  118. if($row['id'] && (!$row['status'] || $row['sendtime'] < TIMESTAMP)) {
  119. $sended[] = $row['id'];
  120. } else {
  121. $row['sendtime'] = strtotime("{$row['time']} {$row['clock']}");
  122. if($row['sendtime'] <= TIMESTAMP) {
  123. $message .= "{$row['time']}的群发时间不合法,必须大于当前时间<br>";
  124. }
  125. }
  126. } else {
  127. unset($mass[$key]);
  128. }
  129. }
  130. if(empty($mass)) {
  131. message(error(-1, '没有设置群发'), '', 'ajax');
  132. }
  133. if(!empty($message)) {
  134. message(error(-1, $message), '', 'ajax');
  135. }
  136. $cron_status = 0;
  137. $message = '';
  138. foreach($mass as $row) {
  139. if(!empty($sended) && in_array($row['id'], $sended)) {
  140. continue;
  141. }
  142. $data = array(
  143. 'uniacid' => $_W['uniacid'],
  144. 'acid' => $_W['acid'],
  145. 'groupname' => $groups[$row['group']]['name'],
  146. 'group' => $row['group'],
  147. 'attach_id' => $row['attach_id'],
  148. 'media_id' => $row['media_id'],
  149. 'fansnum' => $groups[$row['group']]['count'],
  150. 'msgtype' => $row['msgtype'],
  151. 'sendtime' => strtotime($row['time'] . " {$row['clock']}"),
  152. 'createtime' => TIMESTAMP,
  153. 'type' => 1,
  154. 'status' => 1,
  155. 'cron_id' => 0,
  156. );
  157. pdo_insert('mc_mass_record', $data);
  158. $insert_id = pdo_insertid();
  159. $cron = array(
  160. 'uniacid' => $_W['uniacid'],
  161. 'name' => $row['time'] . "微信群发任务",
  162. 'filename' => 'mass',
  163. 'type' => 1,
  164. 'lastruntime' => $row['sendtime'],
  165. 'extra' => $insert_id,
  166. 'module' => 'task',
  167. 'status' => 1,
  168. );
  169. $status = cron_add($cron);
  170. if(is_error($status)) {
  171. $message .= "{$row['time']}的群发任务同步到云服务失败,请手动同步<br>";
  172. $cron_status = 1;
  173. } else {
  174. pdo_update('mc_mass_record', array('cron_id' => $status), array('id' => $insert_id));
  175. }
  176. }
  177. if($cron_status) {
  178. message(error(-1000, $message), '', 'ajax');
  179. }
  180. message(error(0, 'success'), '', 'ajax');
  181. }
  182. if($do == 'cron') {
  183. $id = intval($_GPC['id']);
  184. $record = pdo_get('mc_mass_record', array('uniacid' => $_W['uniacid'], 'id' => $id));
  185. if(empty($record)) {
  186. message('群发任务不存在或已删除', referer(), 'error');
  187. }
  188. load()->func('cron');
  189. $cron = array(
  190. 'uniacid' => $_W['uniacid'],
  191. 'name' => date('Y-m-d', $record['sendtime']) . "微信群发任务",
  192. 'filename' => 'mass',
  193. 'type' => 1,
  194. 'lastruntime' => $record['sendtime'],
  195. 'extra' => $record['id'],
  196. 'module' => 'task',
  197. 'status' => 1
  198. );
  199. $status = cron_add($cron);
  200. if(is_error($status)) {
  201. message($status['message'], referer(), 'error');
  202. }
  203. pdo_update('mc_mass_record', array('cron_id' => $status), array('uniacid' => $_W['uniacid'], 'id' => $id));
  204. message('同步到云服务成功', referer(), 'success');
  205. }
  206. if($do == 'send') {
  207. $_W['page']['title'] = '群发记录-微信群发';
  208. $pindex = max(1, intval($_GPC['page']));
  209. $psize = 20;
  210. $condition = ' WHERE `uniacid` = :uniacid AND `acid` = :acid';
  211. $pars = array();
  212. $pars[':uniacid'] = $_W['uniacid'];
  213. $pars[':acid'] = $_W['acid'];
  214. $total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('mc_mass_record').$condition, $pars);
  215. $list = pdo_fetchall("SELECT * FROM ".tablename('mc_mass_record') . $condition ." ORDER BY `id` DESC LIMIT ".($pindex - 1) * $psize.','.$psize, $pars);
  216. $types = array('text' => '文本消息', 'image' => '图片消息', 'voice' => '语音消息', 'video' => '视频消息', 'news' => '图文消息', 'wxcard' => '微信卡券');
  217. $pager = pagination($total, $pindex, $psize);
  218. template('material/send');
  219. }