人人商城

mass.ctrl.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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('mc_mass');
  8. $dos = array('default', 'post', 'send', 'ajax', 'news', 'fans', 'page', 'add', 'material');
  9. $_W['page']['title'] = '微信群发-粉丝管理';
  10. $do = in_array($do, $dos) ? $do : 'default';
  11. if($do == 'page') {
  12. template('mc/page');
  13. }
  14. if($do == 'default') {
  15. if($_W['account']['level'] > 2) {
  16. $groups_data = pdo_fetch('SELECT * FROM ' . tablename('mc_fans_groups') . ' WHERE uniacid = :uniacid AND acid = :acid', array(':uniacid' => $_W['uniacid'], ':acid' => $_W['acid']));
  17. if(!empty($groups_data)) {
  18. $groups = iunserializer($groups_data['groups']);
  19. } else {
  20. message('未获取到粉丝分组信息,现在去拉取粉丝分组', url('mc/fangroup'), 'info');
  21. }
  22. }
  23. template('mc/mass');
  24. }
  25. if($do == 'news') {
  26. $condition = ' WHERE uniacid = :uniacid AND status = 1 AND module = :module';
  27. $param = array(':uniacid' => $_W['uniacid'], ':module' => 'news');
  28. if(!empty($_GPC['keyword'])) {
  29. $condition .= ' AND name LIKE :keyword';
  30. $param[':keyword'] = "%{$_GPC['keyword']}%";
  31. }
  32. $psize = 8;
  33. $pindex = max(1, intval($_GPC['page']));
  34. $limit = ' ORDER BY id DESC LIMIT ' . ($pindex - 1) * $psize . ',' . $psize;
  35. $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('rule') . $condition, $param);
  36. $data = pdo_fetchall('SELECT id, name FROM ' . tablename('rule') . $condition . $limit, $param, 'id');
  37. if(!empty($data)) {
  38. foreach($data as &$da) {
  39. $da['replies'] = pdo_fetchall('SELECT id,title,thumb FROM ' . tablename('news_reply') . ' WHERE rid = :rid ORDER BY `displayorder` DESC', array(':rid' => $da['id']));
  40. if(!empty($da['replies'])) {
  41. foreach($da['replies'] as &$li) {
  42. if(!empty($li['thumb'])) $li['thumb'] = tomedia($li['thumb']);
  43. }
  44. }
  45. }
  46. }
  47. $result = array(
  48. 'list' => $data,
  49. 'pager' => pagination($total, $pindex, $psize, '', array('before' => '2', 'after' => '3', 'ajaxcallback'=>'null')),
  50. );
  51. message($result, '', 'ajax');
  52. }
  53. if($do == 'fans') {
  54. $condition = " WHERE uniacid = :uniacid AND acid = :acid AND follow = 1 AND openid != ''";
  55. $param = array(':uniacid' => $_W['uniacid'], ':acid' => $_W['acid']);
  56. if(!empty($_GPC['keyword'])) {
  57. $condition .= ' AND nickname LIKE :keyword';
  58. $param[':keyword'] = "%{$_GPC['keyword']}%";
  59. }
  60. $psize = 10;
  61. $pindex = max(1, intval($_GPC['page']));
  62. $limit = ' ORDER BY followtime DESC LIMIT ' . ($pindex - 1) * $psize . ',' . $psize;
  63. $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('mc_mapping_fans') . $condition, $param);
  64. $data = pdo_fetchall('SELECT fanid,openid,nickname,followtime,tag FROM ' . tablename('mc_mapping_fans') . $condition . $limit, $param, 'fanid');
  65. if(!empty($data)) {
  66. foreach($data as &$da) {
  67. $da['selected'] = 0;
  68. if(empty($da['nickname'])) {
  69. $da['nickname'] = $da['openid'];
  70. }
  71. $da['avatar'] = './resource/images/noavatar_middle.gif';
  72. if (!empty($da['tag']) && is_string($da['tag'])) {
  73. if (is_base64($da['tag'])){
  74. $da['tag'] = base64_decode($da['tag']);
  75. }
  76. if (is_serialized($da['tag'])) {
  77. $da['tag'] = @iunserializer($da['tag']);
  78. }
  79. if(!empty($da['tag']['headimgurl'])) {
  80. $da['avatar'] = tomedia($da['tag']['headimgurl']);
  81. }
  82. unset($da['tag']);
  83. }
  84. $da['followtime'] = date('Y-m-d H:i', $da['followtime']);
  85. }
  86. }
  87. $result = array(
  88. 'list' => $data,
  89. 'pager' => pagination($total, $pindex, $psize, '', array('before' => '2', 'after' => '3', 'ajaxcallback'=>'null')),
  90. );
  91. message($result, '', 'ajax');
  92. }
  93. if($do == 'post') {
  94. set_time_limit(0);
  95. error_reporting(E_ERROR);
  96. $post = $_GPC['__input'];
  97. if($_GPC['send_time'] == 2) {
  98. $time = strtotime($_GPC['time']);
  99. if($time <= TIMESTAMP || $time >= (TIMESTAMP + 86400*3 - 7200)) {
  100. $time = date('Y-m-d H:i', TIMESTAMP + 86400*3 - 7200);
  101. message(error(-1, "定时发送时间不能小于当前时间并且不能超过{$time}"), '', 'ajax');
  102. }
  103. }
  104. $acc = WeAccount::create($_W['acid']);
  105. if($post['msg_type'] == 'mpnews') {
  106. $rid = intval($post['data']);
  107. $rule = pdo_fetch('SELECT * FROM ' . tablename('rule') . ' WHERE uniacid = :uniacid AND id = :id', array(':uniacid' => $_W['uniacid'], ':id' => $rid));
  108. if(empty($rule)) {
  109. message(error(-1, '规则不存在'), '', 'ajax');
  110. }
  111. $replies = pdo_fetchall('SELECT * FROM ' . tablename('news_reply') . ' WHERE rid = :rid ORDER BY `displayorder` DESC', array(':rid' => $rid));
  112. $thumb_message = '';
  113. foreach($replies as &$reply) {
  114. $flag = 1;
  115. if(empty($reply['content'])) {
  116. $thumb_message .= "标题为 '{$reply['title']}' 回复项的内容为空<br>";
  117. }
  118. if(!empty($_W['setting']['remote']['type'])) {
  119. load()->func('file');
  120. $reply['thumb'] = file_fetch(tomedia($reply['thumb']));
  121. if(is_error($reply['thumb'])) {
  122. $flag = 0;
  123. $thumb_message .= "标题为 '{$reply['title']}' 回复项的封面图片获取失败,请重新上传,错误详情:{$reply['thumb']['message']}<br>";
  124. }
  125. }
  126. if($flag) {
  127. $path = ATTACHMENT_ROOT . ltrim($reply['thumb'], '/');
  128. if(!file_exists($path)) {
  129. $thumb_message .= "标题为 '{$reply['title']}' 回复项的封面图片不存在<br>";
  130. } else {
  131. $extension = ltrim(strrchr($reply['thumb'], '.'), '.');
  132. if(!in_array($extension, array('jpg', 'png'))) {
  133. $thumb_message .= "标题为 '{$reply['title']}' 回复项的封面图片格式不对,仅支持jpg,png格式。<br>";
  134. }
  135. if(filesize($path) > 64 * 1024) {
  136. $thumb_message .= "标题为 '{$reply['title']}' 回复项的封面图片大于64K。<br>";
  137. }
  138. }
  139. }
  140. }
  141. if(!empty($thumb_message)) {
  142. message(error(-1, $thumb_message), '', 'ajax');
  143. }
  144. $articles = array(
  145. 'articles' => array()
  146. );
  147. foreach($replies as &$reply) {
  148. $media = $acc->uploadMedia($reply['thumb']);
  149. if(is_error($media)) {
  150. message($media, '', 'ajax');
  151. }
  152. if(!strexists($reply['url'], 'http://') && !strexists($reply['url'], 'https://')) {
  153. $reply['url'] = $_W['siteroot'] . 'app' . ltrim($reply['url'], '.');
  154. }
  155. $str_find = array('../attachment/images');
  156. $str_replace = array($_W['siteroot'] . 'attachment/images');
  157. $reply['content'] = str_replace($str_find, $str_replace, $reply['content']);
  158. $row = array(
  159. 'title' => urlencode($reply['title']),
  160. 'author' => urlencode($reply['author']),
  161. 'digest' => urlencode($reply['description']),
  162. 'content' => urlencode(addslashes(htmlspecialchars_decode($reply['content']))),
  163. 'show_cover_pic' => intval($reply['incontent']),
  164. 'content_source_url' => urlencode($reply['url']),
  165. 'thumb_media_id' => $media['media_id'],
  166. );
  167. $articles['articles'][] = $row;
  168. }
  169. $status = $acc->uploadNews($articles);
  170. if(is_error($status)) {
  171. message($status, '', 'ajax');
  172. }
  173. $data['mpnews'] = array(
  174. 'media_id' => $status['media_id'],
  175. );
  176. $data['msgtype'] = 'mpnews';
  177. }
  178. if($post['msg_type'] == 'text') {
  179. $data['text'] = array(
  180. 'content' => urlencode(trim($post['data'])),
  181. );
  182. $data['msgtype'] = 'text';
  183. }
  184. if($post['msg_type'] == 'image') {
  185. $data['image'] = array(
  186. 'media_id' => urlencode(trim($post['data'])),
  187. );
  188. $data['msgtype'] = 'image';
  189. }
  190. if($post['msg_type'] == 'voice') {
  191. $data['voice'] = array(
  192. 'media_id' => urlencode(trim($post['data'])),
  193. );
  194. $data['msgtype'] = 'voice';
  195. }
  196. if($post['msg_type'] == 'video') {
  197. $video = array(
  198. 'media_id' => $post['data']['media'],
  199. 'title' => urlencode($post['data']['title']),
  200. 'description' => urlencode($post['data']['description']),
  201. );
  202. $status = $acc->uploadVideo($video);
  203. if(is_error($status)) {
  204. message($status, '', 'ajax');
  205. }
  206. if($post['send_type'] == '3') {
  207. $data['video'] = array(
  208. 'media_id' => $status['media_id'],
  209. );
  210. $data['msgtype'] = 'video';
  211. }
  212. if($post['send_type'] == '2') {
  213. $data['mpvideo'] = array(
  214. 'media_id' => $status['media_id'],
  215. );
  216. $data['msgtype'] = 'mpvideo';
  217. }
  218. }
  219. if($post['send_type'] == 1) {
  220. $data['filter'] = array(
  221. 'is_to_all' => true,
  222. 'group_id' => 0,
  223. );
  224. } elseif($post['send_type'] == 2) {
  225. $data['filter'] = array(
  226. 'is_to_all' => false,
  227. 'group_id' => intval($post['send_group']),
  228. );
  229. } elseif($post['send_type'] == 3) {
  230. $data['touser'] = $post['openids'];
  231. }
  232. $record = 0;
  233. if($_GPC['send_time'] == 1) {
  234. $status = $acc->fansSendAll($data);
  235. if(is_error($status)) {
  236. message($status, '', 'ajax');
  237. }
  238. }
  239. if($post['msg_type'] == 'mpnews') {
  240. $post['msg_type'] = 'news';
  241. }
  242. $insert = array(
  243. 'uniacid' => $_W['uniacid'],
  244. 'acid' => $_W['acid'],
  245. 'msgtype' => $post['msg_type'],
  246. 'createtime' => TIMESTAMP,
  247. );
  248. if($post['send_time'] == 1) {
  249. $insert['status'] = 0;
  250. } else {
  251. $insert['sendtime'] = $time;
  252. $insert['status'] = 1;
  253. $insert['data'] = iserializer($data);
  254. }
  255. if($post['send_type'] == 1) {
  256. $insert['groupname'] = '全部用户';
  257. $insert['fansnum'] = '';
  258. } elseif($post['send_type'] == 2) {
  259. $groups_data = pdo_fetch('SELECT * FROM ' . tablename('mc_fans_groups') . ' WHERE uniacid = :uniacid AND acid = :acid', array(':uniacid' => $_W['uniacid'], ':acid' => $_W['acid']));
  260. $groups = iunserializer($groups_data['groups']);
  261. $insert['groupname'] = $groups[$post['send_group']]['name'];
  262. $insert['fansnum'] = $groups[$post['send_group']]['count'];
  263. } elseif($post['send_type'] == 3) {
  264. $insert['groupname'] = '根据粉丝openid群发';
  265. $insert['fansnum'] = count($post['openids']);
  266. }
  267. if(in_array($post['msg_type'], array('text', 'image', 'voice'))) {
  268. $insert['content'] = $post['data'];
  269. } elseif($post['msg_type'] == 'video') {
  270. $insert['content'] = $post['data']['media_id'];
  271. } elseif($post['msg_type'] == 'news') {
  272. $insert['content'] = intval($post['data']);
  273. }
  274. pdo_insert('mc_mass_record', $insert);
  275. message(error(1, ''), '', 'ajax');
  276. }
  277. if($do == 'send') {
  278. $pindex = max(1, intval($_GPC['page']));
  279. $psize = 20;
  280. $condition = ' WHERE `uniacid` = :uniacid AND `acid` = :acid';
  281. $pars = array();
  282. $pars[':uniacid'] = $_W['uniacid'];
  283. $pars[':acid'] = $_W['acid'];
  284. $total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('mc_mass_record').$condition, $pars);
  285. $list = pdo_fetchall("SELECT * FROM ".tablename('mc_mass_record') . $condition ." ORDER BY `id` DESC LIMIT ".($pindex - 1) * $psize.','.$psize, $pars);
  286. $types = array('text' => '文本消息', 'image' => '图片消息', 'voice' => '语音消息', 'video' => '视频消息', 'news' => '图文消息');
  287. if(!empty($list)) {
  288. foreach($list as &$li) {
  289. if($li['msgtype'] == 'news') {
  290. $rid = intval($li['content']);
  291. if($rid > 0) {
  292. $li['rid'] = $rid;
  293. $li['rule_name'] = pdo_fetchcolumn('SELECT name FROM ' . tablename('rule') . ' WHERE id = :id', array(':id' => $rid));
  294. } else {
  295. $li['content'] = iunserializer($li['content']);
  296. $li['content'] = iurldecode($li['content']);
  297. }
  298. } elseif(in_array($li['msgtype'], array('image', 'voice', 'video'))) {
  299. $li['content'] = media2local($li['content']);
  300. }
  301. }
  302. }
  303. $pager = pagination($total, $pindex, $psize);
  304. template('mc/send');
  305. }
  306. function iurldecode($str) {
  307. if(!is_array($str)) {
  308. return urldecode($str);
  309. }
  310. foreach($str as $key => $val) {
  311. $str[$key] = iurldecode($val);
  312. }
  313. return $str;
  314. }