人人商城

mass.ctrl.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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()->func('cron');
  8. load()->model('cloud');
  9. load()->model('module');
  10. load()->model('mc');
  11. load()->model('material');
  12. $dos = array('list', 'post', 'cron', 'send', 'del', 'preview');
  13. $do = in_array($do, $dos) ? $do : 'post';
  14. $_W['page']['title'] = '定时群发-微信素材';
  15. if ($do == 'list') {
  16. $time = strtotime(date('Y-m-d'));
  17. $record = pdo_getall('mc_mass_record', array('uniacid' => $_W['uniacid'], 'sendtime >=' => $time), array(), 'sendtime', 'sendtime ASC', array(1,7));
  18. $days = array();
  19. for ($i = 0; $i < 8; $i++) {
  20. $day_info = array();
  21. $day_info['day'] = date('Y-m-d', strtotime("+{$i} days", $time));
  22. $starttime = strtotime("+{$i} days", $time);
  23. $endtime = $i+1;
  24. $endtime = strtotime("+{$endtime} days", $time);
  25. $massdata = pdo_fetch('SELECT id, `groupname`, `msgtype`, `group`, `attach_id`, `media_id`, `sendtime` FROM '. tablename('mc_mass_record') . ' WHERE uniacid = :uniacid AND sendtime BETWEEN :starttime AND :endtime AND status = 1', array(':uniacid' => $_W['uniacid'], ':starttime' => $starttime, ':endtime' => $endtime));
  26. if (!empty($massdata)) {
  27. $massdata['media'] = pdo_get('wechat_attachment', array('id' => $massdata['attach_id']));
  28. $massdata['media']['attach'] = tomedia($massdata['media']['attachment']);
  29. $massdata['media']['createtime_cn'] = date('Y-m-d H:i', $massdata['media']['createtime']);
  30. switch ($massdata['msgtype']) {
  31. case 'news':
  32. $massdata['msgtype_zh'] = '图文';
  33. $massdata['media']['items'] = pdo_getall('wechat_news', array('attach_id' => $massdata['attach_id']));
  34. foreach ($massdata['media']['items'] as &$news_val) {
  35. $news_val['thumb_url'] = tomedia($news_val['thumb_url']);
  36. }
  37. unset($news_val);
  38. break;
  39. case 'image':
  40. $massdata['msgtype_zh'] = '图片';
  41. break;
  42. case 'voice':
  43. $massdata['msgtype_zh'] = '语音';
  44. break;
  45. case 'video':
  46. $massdata['msgtype_zh'] = '视频';
  47. $massdata['media']['attach']['tag'] = iunserializer($massdata['media']['tag']);
  48. break;
  49. }
  50. $massdata['clock'] = date('H:m', $massdata['sendtime']);
  51. }
  52. $day_info['info'] = $massdata;
  53. $days[] = $day_info;
  54. }
  55. template('platform/mass-display');
  56. }
  57. if ($do == 'del') {
  58. $mass = pdo_get('mc_mass_record', array('uniacid' => $_W['uniacid'], 'id' => intval($_GPC['id'])));
  59. if (!empty($mass) && $mass['cron_id'] > 0) {
  60. $status = cron_delete(array($mass['cron_id']));
  61. if (is_error($status)) {
  62. iajax(0, $status, '');
  63. }
  64. }
  65. pdo_delete('mc_mass_record', array('uniacid' => $_W['uniacid'], 'id' => intval($_GPC['id'])));
  66. itoast('删除成功', '', 'info');
  67. }
  68. if ($do == 'post') {
  69. permission_check_account_user('platform_masstask_post');
  70. $id = intval($_GPC['id']);
  71. $mass_info = pdo_get('mc_mass_record', array('id' => $id));
  72. $groups = mc_fans_groups();
  73. $account_api = WeAccount::createByUniacid();
  74. $supports = $account_api->getMaterialSupport();
  75. $show_post_content = $supports['mass'];
  76. if (checksubmit('submit')) {
  77. $type = in_array(intval($_GPC['type']), array(0, 1)) ? intval($_GPC['type']) : 0;
  78. $group = json_decode(htmlspecialchars_decode($_GPC['group']), true);
  79. if (empty($_GPC['reply'])) {
  80. itoast('请选择要群发的素材', '', 'error');
  81. }
  82. $mass_record = array(
  83. 'uniacid' => $_W['uniacid'],
  84. 'acid' => $_W['acid'],
  85. 'groupname' => $group['name'],
  86. 'fansnum' => $group['count'],
  87. 'msgtype' => '',
  88. 'group' => $group['id'],
  89. 'attach_id' => '',
  90. 'media_id' => '',
  91. 'status' => 1,
  92. 'type' => $type,
  93. 'sendtime' => TIMESTAMP,
  94. 'createtime' => TIMESTAMP,
  95. 'cron_id' => 0,
  96. );
  97. foreach ($_GPC['reply'] as $material_type => $material) {
  98. if (empty($material)) {
  99. continue;
  100. }
  101. list($temp, $msgtype) = explode('_', $material_type);
  102. $attachment = material_get($material);
  103. if ($attachment['model'] == 'local') {
  104. itoast('图文素材请选择微信素材', '', 'error');
  105. }
  106. $mass_record['media_id'] = $material;
  107. $mass_record['attach_id'] = $attachment['id'];
  108. $mass_record['msgtype'] = $msgtype;
  109. break;
  110. }
  111. if ($type == 1) {
  112. $cloud = cloud_prepare();
  113. if (is_error($cloud)) {
  114. itoast($cloud['message'], '', 'error');
  115. }
  116. set_time_limit(0);
  117. $starttime = strtotime(date('Y-m-d', strtotime($_GPC['sendtime'])));
  118. $endtime = strtotime(date('Y-m-d', strtotime($_GPC['sendtime']))) + 86400;
  119. $cron_title = date('Y-m-d', strtotime($_GPC['sendtime'])) . '微信群发任务';
  120. $mass_record['sendtime'] = strtotime($_GPC['sendtime']);
  121. $records = pdo_fetchall("SELECT id, cron_id FROM " . tablename('mc_mass_record') . ' WHERE uniacid = :uniacid AND sendtime BETWEEN :starttime AND :endtime AND status = 1 ORDER BY sendtime ASC LIMIT 8', array(':uniacid' => $_W['uniacid'], ':starttime' => $starttime, ':endtime' => $endtime), 'id');
  122. if (!empty($records)) {
  123. foreach ($records as $record) {
  124. if (!$record['cron_id']) {
  125. continue;
  126. }
  127. $corn_ids[] = $record['cron_id'];
  128. }
  129. if (!empty($corn_ids)) {
  130. $status = cron_delete($corn_ids);
  131. if (is_error($status)) {
  132. itoast('删除群发错误,请重新提交', referer());
  133. }
  134. }
  135. $ids = implode(',', array_keys($records));
  136. pdo_delete('mc_mass_record', array('uniacid' => $_W['uniacid'], 'id' => array($ids)));
  137. }
  138. pdo_insert('mc_mass_record', $mass_record);
  139. $mass_record_id = pdo_insertid();
  140. $cron_data = array(
  141. 'uniacid' => $_W['uniacid'],
  142. 'name' => $cron_title,
  143. 'filename' => 'mass',
  144. 'type' => 1,
  145. 'lastruntime' => $mass_record['sendtime'],
  146. 'extra' => $mass_record_id,
  147. 'module' => 'task',
  148. 'status' => 1,
  149. );
  150. $status = cron_add($cron_data);
  151. if (is_error($status)) {
  152. $message = "{$cron_title}同步到云服务失败,请手动同步<br>";
  153. itoast($message, url('platform/mass/send'), 'info');
  154. }
  155. pdo_update('mc_mass_record', array('cron_id' => $status), array('id' => $mass_record_id));
  156. itoast('定时群发设置成功', url('platform/mass/send'), 'success');
  157. } else {
  158. $account_api = WeAccount::createByUniacid();
  159. $result = $account_api->fansSendAll($group['id'], $msgtype, $mass_record['media_id']);
  160. if (is_error($result)) {
  161. itoast($result['message'], url('platform/mass'), 'info');
  162. }
  163. if ($msgtype == 'news') {
  164. $mass_record['msg_id'] = $result['msg_id'];
  165. $mass_record['msg_data_id'] = $result['msg_data_id'];
  166. }
  167. $mass_record['status'] = 0;
  168. pdo_insert('mc_mass_record', $mass_record);
  169. itoast('立即发送设置成功', url('platform/mass/send'), 'success');
  170. }
  171. }
  172. template('platform/mass-post');
  173. }
  174. if ($do == 'cron') {
  175. $id = intval($_GPC['id']);
  176. $record = pdo_get('mc_mass_record', array('uniacid' => $_W['uniacid'], 'id' => $id));
  177. if (empty($record)) {
  178. itoast('群发任务不存在或已删除', referer(), 'error');
  179. }
  180. $cron = array(
  181. 'uniacid' => $_W['uniacid'],
  182. 'name' => date('Y-m-d', $record['sendtime']) . "微信群发任务",
  183. 'filename' => 'mass',
  184. 'type' => 1,
  185. 'lastruntime' => $record['sendtime'],
  186. 'extra' => $record['id'],
  187. 'module' => 'task',
  188. 'status' => 1
  189. );
  190. $status = cron_add($cron);
  191. if (is_error($status)) {
  192. itoast($status['message'], referer(), 'error');
  193. }
  194. pdo_update('mc_mass_record', array('cron_id' => $status), array('uniacid' => $_W['uniacid'], 'id' => $id));
  195. itoast('同步到云服务成功', referer(), 'success');
  196. }
  197. if ($do == 'preview') {
  198. $wxname = trim($_GPC['wxname']);
  199. if (empty($wxname)) {
  200. iajax(1, '微信号不能为空', '');
  201. }
  202. $type = trim($_GPC['type']);
  203. $media_id = trim($_GPC['media_id']);
  204. $account_api = WeAccount::createByUniacid();
  205. $data = $account_api->fansSendPreview($wxname, $media_id, $type);
  206. if (is_error($data)) {
  207. iajax(-1, $data['message'], '');
  208. }
  209. iajax(0, 'success', '');
  210. }
  211. if ($do == 'send') {
  212. $_W['page']['title'] = '定时群发记录-定时群发';
  213. permission_check_account_user('platform_masstask_send');
  214. $pindex = max(1, intval($_GPC['page']));
  215. $psize = 20;
  216. $condition = ' WHERE `uniacid` = :uniacid AND `acid` = :acid';
  217. $params = array();
  218. $params[':uniacid'] = $_W['uniacid'];
  219. $params[':acid'] = $_W['acid'];
  220. $total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('mc_mass_record') . $condition, $params);
  221. $lists = pdo_getall('mc_mass_record', array('uniacid' => $_W['uniacid'], 'acid' => $_W['acid']), array(), '', 'id DESC', 'LIMIT '.($pindex-1)* $psize.','.$psize);
  222. $types = array('text' => '文本消息', 'image' => '图片消息', 'voice' => '语音消息', 'video' => '视频消息', 'news' => '图文消息', 'wxcard' => '微信卡券');
  223. $pager = pagination($total, $pindex, $psize);
  224. template('platform/mass-send');
  225. }