fans.ctrl.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. set_time_limit(60);
  8. load()->model('mc');
  9. $dos = array('display', 'add_tag', 'del_tag', 'edit_tagname', 'edit_fans_tag', 'batch_edit_fans_tag', 'download_fans', 'sync', 'fans_sync_set', 'register');
  10. $do = in_array($do, $dos) ? $do : 'display';
  11. if ($do == 'display') {
  12. $_W['page']['title'] = '粉丝列表';
  13. $fans_tag = mc_fans_groups(true);
  14. $pageindex = max(1, intval($_GPC['page']));
  15. $search_mod = intval($_GPC['search_mod']) == '' ? 1 : intval($_GPC['search_mod']);
  16. $pagesize = 10;
  17. $param = array(
  18. ':uniacid' => $_W['uniacid'],
  19. ':acid' => $_W['acid']
  20. );
  21. $condition = " WHERE f.`uniacid` = :uniacid AND f.`acid` = :acid";
  22. $tag = intval($_GPC['tag']) ? intval($_GPC['tag']) : 0;
  23. if (!empty($tag)) {
  24. $param[':tagid'] = $tag;
  25. $condition .= " AND m.`tagid` = :tagid";
  26. }
  27. if ($_GPC['type'] == 'bind') {
  28. $condition .= " AND f.`uid` > 0";
  29. $type = 'bind';
  30. }
  31. $nickname = $_GPC['nickname'] ? addslashes(trim($_GPC['nickname'])) : '';
  32. if (!empty($nickname)) {
  33. if ($search_mod == 1) {
  34. $condition .= " AND ((f.`nickname` = :nickname) OR (f.`openid` = :openid))";
  35. $param[':nickname'] = $nickname;
  36. $param[':openid'] = $nickname;
  37. } else {
  38. $condition .= " AND ((f.`nickname` LIKE :nickname) OR (f.`openid` LIKE :openid))";
  39. $param[':nickname'] = "%" . $nickname . "%";
  40. $param[':openid'] = "%" . $nickname . "%";
  41. }
  42. }
  43. $follow = intval($_GPC['follow']) ? intval($_GPC['follow']) : 1;
  44. if ($follow == 1) {
  45. $orderby = " ORDER BY f.`followtime` DESC";
  46. $condition .= " AND f.`follow` = 1";
  47. } elseif ($follow == 2) {
  48. $orderby = " ORDER BY f.`unfollowtime` DESC";
  49. $condition .= " AND f.`follow` = 0";
  50. }
  51. $select_sql = "SELECT %s FROM " .tablename('mc_mapping_fans')." AS f LEFT JOIN ".tablename('mc_fans_tag_mapping')." AS m ON m.`fanid` = f.`fanid` " . $condition ." %s";
  52. $fans_list_sql = sprintf($select_sql, "f.* ", " GROUP BY f.`fanid`" . $orderby . " LIMIT " .($pageindex - 1) * $pagesize.",".$pagesize);
  53. $fans_list = pdo_fetchall($fans_list_sql, $param);
  54. if (!empty($fans_list)) {
  55. foreach ($fans_list as &$v) {
  56. $v['tag_show'] = mc_show_tag($v['groupid']);
  57. $v['tag_show'] = explode(',', $v['tag_show']);
  58. $v['groupid'] = trim($v['groupid'], ',');
  59. if (!empty($v['uid'])) {
  60. $user = mc_fetch($v['uid'], array('realname', 'nickname', 'mobile', 'email', 'avatar'));
  61. }
  62. if (!empty($user)) {
  63. $v['member'] = $user;
  64. }
  65. if (!empty($v['tag']) && is_string($v['tag'])) {
  66. if (is_base64($v['tag'])) {
  67. $v['tag'] = base64_decode($v['tag']);
  68. }
  69. if (is_serialized($v['tag'])) {
  70. $v['tag'] = @iunserializer($v['tag']);
  71. }
  72. if (!empty($v['tag']['headimgurl'])) {
  73. $v['tag']['avatar'] = tomedia($v['tag']['headimgurl']);
  74. }
  75. if (empty($v['nickname']) && !empty($v['tag']['nickname'])) {
  76. $v['nickname'] = strip_emoji($v['tag']['nickname']);
  77. }
  78. }
  79. if (empty($v['tag'])) {
  80. $v['tag'] = array();
  81. }
  82. if (empty($v['user']['nickname']) && !empty($v['tag']['nickname'])) {
  83. $v['user']['nickname'] = strip_emoji($v['tag']['nickname']);
  84. }
  85. if (empty($v['user']['avatar']) && !empty($v['tag']['avatar'])) {
  86. $v['user']['avatar'] = $v['tag']['avatar'];
  87. }
  88. unset($user,$niemmo,$niemmo_effective);
  89. }
  90. unset($v);
  91. }
  92. $total_sql = sprintf($select_sql, "COUNT(DISTINCT f.`fanid`) ", '');
  93. $total = pdo_fetchcolumn($total_sql, $param);
  94. $pager = pagination($total, $pageindex, $pagesize);
  95. $fans['total'] = pdo_getcolumn("mc_mapping_fans", array('uniacid' => $_W['uniacid'], 'acid' => $_W['acid'], 'follow' => 1), 'count(*)');
  96. }
  97. if ($do == 'add_tag') {
  98. $tag_name = trim($_GPC['tag']);
  99. if (empty($tag_name)) {
  100. iajax(1, '请填写标称名称', '');
  101. }
  102. $account_api = WeAccount::create();
  103. $result = $account_api->fansTagAdd($tag_name);
  104. if (is_error($result)) {
  105. iajax(1, $result);
  106. } else {
  107. iajax(0, '');
  108. }
  109. }
  110. if ($do == 'del_tag') {
  111. $tagid = intval($_GPC['tag']);
  112. if (empty($tagid)) {
  113. iajax(1, '标签id为空', '');
  114. }
  115. $account_api = WeAccount::create();
  116. $tags = $account_api->fansTagDelete($tagid);
  117. if (!is_error($tags)) {
  118. $fans_list = pdo_getall('mc_mapping_fans', array('groupid LIKE' => "%,{$tagid},%"));
  119. $count = count($fans_list);
  120. if (!empty($count)) {
  121. $buffSize = ceil($count / 500);
  122. for ($i = 0; $i < $buffSize; $i++) {
  123. $sql = '';
  124. $wechat_fans = array_slice($fans_list, $i * 500, 500);
  125. foreach ($wechat_fans as $fans) {
  126. $tagids = trim(str_replace(','.$tagid.',', ',', $fans['groupid']), ',');
  127. if ($tagids == ',') {
  128. $tagids = '';
  129. }
  130. $sql .= 'UPDATE ' . tablename('mc_mapping_fans') . " SET `groupid`='" . $tagids . "' WHERE `fanid`={$fans['fanid']};";
  131. }
  132. pdo_query($sql); }
  133. }
  134. pdo_delete('mc_fans_tag_mapping', array('tagid' => $tagid));
  135. iajax(0, 'success', '');
  136. } else {
  137. iajax(-1, $tags['message'], '');
  138. }
  139. }
  140. if ($do == 'edit_tagname') {
  141. $tag = intval($_GPC['tag']);
  142. if (empty($tag)) {
  143. iajax(1, '标签id为空', '');
  144. }
  145. $tag_name = trim($_GPC['tag_name']);
  146. if (empty($tag_name)) {
  147. iajax(1, '标签名为空', '');
  148. }
  149. $account_api = WeAccount::create();
  150. $result = $account_api->fansTagEdit($tag, $tag_name);
  151. if (is_error($result)) {
  152. iajax(1, $result);
  153. } else {
  154. iajax(0, '');
  155. }
  156. }
  157. if ($do == 'edit_fans_tag') {
  158. $fanid = intval($_GPC['fanid']);
  159. $tags = $_GPC['tags'];
  160. $openid = pdo_getcolumn('mc_mapping_fans', array('uniacid' => $_W['uniacid'], 'fanid' => $fanid), 'openid');
  161. $account_api = WeAccount::create();
  162. if (empty($tags) || !is_array($tags)) {
  163. $fans_tags =pdo_getall('mc_fans_tag_mapping', array('fanid' => $fanid), array(), 'tagid');
  164. if (!empty($fans_tags)) {
  165. foreach ($fans_tags as $tag) {
  166. $result = $account_api->fansTagBatchUntagging(array($openid), $tag['tagid']);
  167. }
  168. } else {
  169. iajax(0);
  170. }
  171. } else {
  172. $result = $account_api->fansTagTagging($openid, $tags);
  173. }
  174. if (!is_error($result)) {
  175. pdo_delete('mc_fans_tag_mapping', array('fanid' => $fanid));
  176. if (!empty($tags)) {
  177. foreach ($tags as $tag) {
  178. pdo_insert('mc_fans_tag_mapping', array('fanid' => $fanid, 'tagid' => $tag));
  179. }
  180. $tags = implode(',', $tags);
  181. }
  182. pdo_update('mc_mapping_fans', array('groupid' => $tags), array('fanid' => $fanid));
  183. }
  184. iajax(0, $result);
  185. }
  186. if ($do == 'batch_edit_fans_tag') {
  187. $openid_list = $_GPC['openid'];
  188. if (empty($openid_list) || !is_array($openid_list)) {
  189. iajax(1, '请选择粉丝', '');
  190. }
  191. $tags = $_GPC['tag'];
  192. if (empty($tags) || !is_array($tags)) {
  193. iajax(1, '请选择标签', '');
  194. }
  195. $account_api = WeAccount::create();
  196. foreach ($tags as $tag) {
  197. $result = $account_api->fansTagBatchTagging($openid_list, $tag);
  198. if (is_error($result)) {
  199. iajax(-1, $result);
  200. }
  201. foreach ($openid_list as $openid) {
  202. $fan_info = pdo_get('mc_mapping_fans', array('uniacid' => $_W['uniacid'], 'openid' => $openid));
  203. pdo_insert('mc_fans_tag_mapping', array('fanid' => $fan_info['fanid'], 'tagid' => $tag), true);
  204. $groupid = $fan_info['group'].",".$tag;
  205. pdo_update('mc_mapping_fans', array('groupid' => $groupid), array('uniacid' => $_W['uniacid'], 'openid' => $openid));
  206. }
  207. }
  208. iajax(0, '');
  209. }
  210. if ($do == 'download_fans') {
  211. $next_openid = $_GPC['next_openid'];
  212. if (empty($next_openid)) {
  213. pdo_update('mc_mapping_fans', array('follow' => 0), array('uniacid' => $_W['uniacid']));
  214. }
  215. $account_api = WeAccount::create();
  216. $wechat_fans_list = $account_api->fansAll();
  217. $same_account_exist = pdo_getall('account_wechats', array('key' => $_W['account']['key'], 'uniacid <>' => $_W['uniacid']), array(), 'uniacid');
  218. if (!empty($same_account_exist)) {
  219. pdo_update('mc_mapping_fans', array('uniacid' => $_W['uniacid'], 'acid' => $_W['acid']), array('uniacid' => array_keys($same_account_exist)));
  220. }
  221. if (!is_error($wechat_fans_list)) {
  222. $wechat_fans_count = count($wechat_fans_list['fans']);
  223. $total_page = ceil($wechat_fans_count / 500);
  224. for ($i = 0; $i < $total_page; $i++) {
  225. $wechat_fans = array_slice($wechat_fans_list['fans'], $i * 500, 500);
  226. $system_fans = pdo_getall('mc_mapping_fans', array('openid' => $wechat_fans), array(), 'openid');
  227. $add_fans_sql = '';
  228. foreach($wechat_fans as $openid) {
  229. if (empty($system_fans) || empty($system_fans[$openid])) {
  230. $salt = random(8);
  231. $add_fans_sql .= "('{$_W['acid']}', '{$_W['uniacid']}', 0, '{$openid}', '{$salt}', 1, 0, ''),";
  232. }
  233. }
  234. if (!empty($add_fans_sql)) {
  235. $add_fans_sql = rtrim($add_fans_sql, ',');
  236. $add_fans_sql = "INSERT INTO " . tablename('mc_mapping_fans') . " (`acid`, `uniacid`, `uid`, `openid`, `salt`, `follow`, `followtime`, `tag`) VALUES " . $add_fans_sql;
  237. $result = pdo_query($add_fans_sql);
  238. }
  239. pdo_update('mc_mapping_fans', array('follow' => 1, 'uniacid' => $_W['uniacid'], 'acid' => $_W['acid']), array('openid' => $wechat_fans));
  240. }
  241. $return['total'] = $wechat_fans_list['total'];
  242. $return['count'] = !empty($wechat_fans_list['fans']) ? $wechat_fans_count : 0;
  243. $return['next'] = $wechat_fans_list['next'];
  244. iajax(0, $return, '');
  245. } else {
  246. iajax(1, $wechat_fans_list['message']);
  247. }
  248. }
  249. if ($do == 'sync') {
  250. $type = $_GPC['type'] == 'all' ? 'all' : 'check';
  251. $sync_member = safe_gpc_int($_GPC['sync_member']);
  252. $force_init_member = empty($sync_member) ? false : true;
  253. if ($type == 'all') {
  254. $pageindex = $_GPC['pageindex'];
  255. $pageindex++;
  256. $sync_fans = pdo_getslice('mc_mapping_fans', array('uniacid' => $_W['uniacid'], 'acid' => $_W['acid'], 'follow' => '1'), array($pageindex, 100), $total, array(), 'openid', 'fanid DESC');
  257. $total = ceil($total/100);
  258. $start = time();
  259. if (!empty($sync_fans)) {
  260. mc_init_fans_info(array_keys($sync_fans));
  261. }
  262. if ($total == $pageindex) {
  263. setcookie(cache_system_key('sync_fans_pindex:' . $_W['uniacid']), '', -1);
  264. } else {
  265. setcookie(cache_system_key('sync_fans_pindex:' . $_W['uniacid']), $pageindex);
  266. }
  267. iajax(0, array('pageindex' => $pageindex, 'total' => $total), '');
  268. }
  269. if ($type == 'check') {
  270. $openids = $_GPC['openids'];
  271. if (empty($openids) || !is_array($openids)) {
  272. iajax(1, '请选择粉丝', '');
  273. }
  274. $sync_fans = pdo_getall('mc_mapping_fans', array('openid' => $openids));
  275. if (!empty($sync_fans)) {
  276. foreach ($sync_fans as $fans) {
  277. mc_init_fans_info($fans['openid'], $force_init_member);
  278. }
  279. }
  280. iajax(0, 'success', '');
  281. }
  282. }
  283. if ($do == 'fans_sync_set') {
  284. $_W['page']['title'] = '更新粉丝信息 - 公众号选项';
  285. $operate = $_GPC['operate'];
  286. if ($operate == 'save_setting') {
  287. uni_setting_save('sync', intval($_GPC['setting']));
  288. iajax(0, '');
  289. }
  290. $setting = uni_setting($_W['uniacid'], array('sync'));
  291. $sync_setting = $setting['sync'];
  292. }
  293. if ($do == 'register') {
  294. $open_id = trim($_GPC['openid']);
  295. $password = trim($_GPC['password']);
  296. $repassword = trim($_GPC['repassword']);
  297. if (empty($open_id) || empty($password) || empty($repassword)) {
  298. iajax('-1', '参数错误', url('mc/fans/display'));
  299. }
  300. if ($password != $repassword) {
  301. iajax('-1', '密码不一致', url('mc/fans/display'));
  302. }
  303. $member_info = mc_init_fans_info($open_id, true);
  304. $member_salt = pdo_getcolumn('mc_members', array('uid' => $member_info['uid']), 'salt');
  305. $password = md5($password . $member_salt . $_W['config']['setting']['authkey']);
  306. pdo_update('mc_members', array('password' => $password), array('uid' => $uid));
  307. iajax('0', '注册成功', url('mc/member/base_information', array('uid' => $member_info['uid'])));
  308. }
  309. template('mc/fans');