人人商城

fans.ctrl.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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', 'sync_member');
  10. $do = in_array($do, $dos) ? $do : 'display';
  11. if ($do == 'display') {
  12. $_W['page']['title'] = '粉丝列表';
  13. permission_check_account_user('mc_fans_display');
  14. $sync_member = uni_setting_load('sync_member');
  15. $sync_member = empty($sync_member['sync_member']) ? 0 : 1;
  16. $fans_tag = mc_fans_groups(true);
  17. $pageindex = max(1, intval($_GPC['page']));
  18. $search_mod = intval($_GPC['search_mod']) == '' ? 1 : intval($_GPC['search_mod']);
  19. $pagesize = 10;
  20. $param = array(
  21. ':uniacid' => $_W['uniacid'],
  22. ':acid' => $_W['acid']
  23. );
  24. $condition = " WHERE f.`uniacid` = :uniacid AND f.`acid` = :acid";
  25. $tag = intval($_GPC['tag']) ? intval($_GPC['tag']) : 0;
  26. if (!empty($tag)) {
  27. $param[':tagid'] = $tag;
  28. $condition .= " AND m.`tagid` = :tagid";
  29. }
  30. if ($_GPC['type'] == 'bind') {
  31. $condition .= " AND f.`uid` > 0";
  32. $type = 'bind';
  33. }
  34. $nickname = $_GPC['nickname'] ? addslashes(trim($_GPC['nickname'])) : '';
  35. if (!empty($nickname)) {
  36. if ($search_mod == 1) {
  37. $condition .= " AND ((f.`nickname` = :nickname) OR (f.`openid` = :openid))";
  38. $param[':nickname'] = $nickname;
  39. $param[':openid'] = $nickname;
  40. } else {
  41. $condition .= " AND ((f.`nickname` LIKE :nickname) OR (f.`openid` LIKE :openid))";
  42. $param[':nickname'] = "%" . $nickname . "%";
  43. $param[':openid'] = "%" . $nickname . "%";
  44. }
  45. }
  46. $follow = intval($_GPC['follow']) ? intval($_GPC['follow']) : 1;
  47. if ($follow == 1) {
  48. $condition .= " AND f.`follow` = 1";
  49. } elseif ($follow == 2) {
  50. $condition .= " AND f.`follow` = 0";
  51. }
  52. $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";
  53. $fans_list_sql = sprintf($select_sql, "f.fanid, f.acid, f.uniacid, f.uid, f.openid, f.nickname, f.groupid, f.follow, f.followtime, f.unfollowtime, f.tag ", " GROUP BY f.`fanid` ORDER BY f.`fanid` DESC LIMIT " .($pageindex - 1) * $pagesize.",".$pagesize);
  54. $fans_list = pdo_fetchall($fans_list_sql, $param);
  55. if (!empty($fans_list)) {
  56. foreach ($fans_list as &$v) {
  57. $v['tag_show'] = mc_show_tag($v['groupid']);
  58. $v['tag_show'] = explode(',', $v['tag_show']);
  59. $v['groupid'] = trim($v['groupid'], ',');
  60. if (!empty($v['uid'])) {
  61. $user = mc_fetch($v['uid'], array('realname', 'nickname', 'mobile', 'email', 'avatar'));
  62. }
  63. if (!empty($user)) {
  64. $v['member'] = $user;
  65. }
  66. if (!empty($v['tag']) && is_string($v['tag'])) {
  67. if (is_base64($v['tag'])) {
  68. $v['tag'] = base64_decode($v['tag']);
  69. }
  70. if (is_serialized($v['tag'])) {
  71. $v['tag'] = @iunserializer($v['tag']);
  72. }
  73. if (!empty($v['tag']['headimgurl'])) {
  74. $v['tag']['avatar'] = tomedia($v['tag']['headimgurl']);
  75. }
  76. if (empty($v['nickname']) && !empty($v['tag']['nickname'])) {
  77. $v['nickname'] = strip_emoji($v['tag']['nickname']);
  78. }
  79. }
  80. if (empty($v['tag'])) {
  81. $v['tag'] = array();
  82. }
  83. if (empty($v['user']['nickname']) && !empty($v['tag']['nickname'])) {
  84. $v['user']['nickname'] = strip_emoji($v['tag']['nickname']);
  85. }
  86. if (empty($v['user']['avatar']) && !empty($v['tag']['avatar'])) {
  87. $v['user']['avatar'] = $v['tag']['avatar'];
  88. }
  89. unset($user,$niemmo,$niemmo_effective);
  90. }
  91. unset($v);
  92. }
  93. $total_sql = sprintf($select_sql, "COUNT(DISTINCT f.`fanid`) ", '');
  94. $total = pdo_fetchcolumn($total_sql, $param);
  95. $pager = pagination($total, $pageindex, $pagesize);
  96. $fans['total'] = pdo_getcolumn("mc_mapping_fans", array('uniacid' => $_W['uniacid'], 'acid' => $_W['acid'], 'follow' => 1), 'count(*)');
  97. }
  98. if ($do == 'add_tag') {
  99. $tag_name = trim($_GPC['tag']);
  100. if (empty($tag_name)) {
  101. iajax(1, '请填写标称名称', '');
  102. }
  103. $account_api = WeAccount::createByUniacid();
  104. $result = $account_api->fansTagAdd($tag_name);
  105. if (is_error($result)) {
  106. iajax(1, $result);
  107. } else {
  108. iajax(0, '');
  109. }
  110. }
  111. if ($do == 'del_tag') {
  112. $tagid = intval($_GPC['tag']);
  113. if (empty($tagid)) {
  114. iajax(1, '标签id为空', '');
  115. }
  116. $account_api = WeAccount::createByUniacid();
  117. $tags = $account_api->fansTagDelete($tagid);
  118. if (!is_error($tags)) {
  119. $fans_list = pdo_getall('mc_mapping_fans', array('groupid LIKE' => "%,{$tagid},%"));
  120. $count = count($fans_list);
  121. if (!empty($count)) {
  122. $buffSize = ceil($count / 500);
  123. for ($i = 0; $i < $buffSize; $i++) {
  124. $sql = '';
  125. $wechat_fans = array_slice($fans_list, $i * 500, 500);
  126. foreach ($wechat_fans as $fans) {
  127. $tagids = trim(str_replace(','.$tagid.',', ',', $fans['groupid']), ',');
  128. if ($tagids == ',') {
  129. $tagids = '';
  130. }
  131. $sql .= 'UPDATE ' . tablename('mc_mapping_fans') . " SET `groupid`='" . $tagids . "' WHERE `fanid`={$fans['fanid']};";
  132. }
  133. pdo_query($sql); }
  134. }
  135. pdo_delete('mc_fans_tag_mapping', array('tagid' => $tagid));
  136. iajax(0, 'success', '');
  137. } else {
  138. iajax(-1, $tags['message'], '');
  139. }
  140. }
  141. if ($do == 'edit_tagname') {
  142. $tag = intval($_GPC['tag']);
  143. if (empty($tag)) {
  144. iajax(1, '标签id为空', '');
  145. }
  146. $tag_name = trim($_GPC['tag_name']);
  147. if (empty($tag_name)) {
  148. iajax(1, '标签名为空', '');
  149. }
  150. $account_api = WeAccount::createByUniacid();
  151. $result = $account_api->fansTagEdit($tag, $tag_name);
  152. if (is_error($result)) {
  153. iajax(1, $result);
  154. } else {
  155. iajax(0, '');
  156. }
  157. }
  158. if ($do == 'edit_fans_tag') {
  159. $fanid = intval($_GPC['fanid']);
  160. $tags = $_GPC['tags'];
  161. $openid = pdo_getcolumn('mc_mapping_fans', array('uniacid' => $_W['uniacid'], 'fanid' => $fanid), 'openid');
  162. $account_api = WeAccount::createByUniacid();
  163. if (empty($tags) || !is_array($tags)) {
  164. $fans_tags =pdo_getall('mc_fans_tag_mapping', array('fanid' => $fanid), array(), 'tagid');
  165. if (!empty($fans_tags)) {
  166. foreach ($fans_tags as $tag) {
  167. $result = $account_api->fansTagBatchUntagging(array($openid), $tag['tagid']);
  168. }
  169. } else {
  170. iajax(0);
  171. }
  172. } else {
  173. $result = $account_api->fansTagTagging($openid, $tags);
  174. }
  175. if (!is_error($result)) {
  176. pdo_delete('mc_fans_tag_mapping', array('fanid' => $fanid));
  177. if (!empty($tags)) {
  178. foreach ($tags as $tag) {
  179. pdo_insert('mc_fans_tag_mapping', array('fanid' => $fanid, 'tagid' => $tag));
  180. }
  181. $tags = implode(',', $tags);
  182. }
  183. pdo_update('mc_mapping_fans', array('groupid' => $tags), array('fanid' => $fanid));
  184. }
  185. iajax(0, $result);
  186. }
  187. if ($do == 'batch_edit_fans_tag') {
  188. $openid_list = $_GPC['openid'];
  189. if (empty($openid_list) || !is_array($openid_list)) {
  190. iajax(1, '请选择粉丝', '');
  191. }
  192. $tags = $_GPC['tag'];
  193. if (empty($tags) || !is_array($tags)) {
  194. iajax(1, '请选择标签', '');
  195. }
  196. $account_api = WeAccount::createByUniacid();
  197. foreach ($tags as $tag) {
  198. $result = $account_api->fansTagBatchTagging($openid_list, $tag);
  199. if (is_error($result)) {
  200. iajax(-1, $result);
  201. }
  202. foreach ($openid_list as $openid) {
  203. $fan_info = pdo_get('mc_mapping_fans', array('uniacid' => $_W['uniacid'], 'openid' => $openid));
  204. pdo_insert('mc_fans_tag_mapping', array('fanid' => $fan_info['fanid'], 'tagid' => $tag), true);
  205. $groupid = $fan_info['group'].",".$tag;
  206. pdo_update('mc_mapping_fans', array('groupid' => $groupid), array('uniacid' => $_W['uniacid'], 'openid' => $openid));
  207. }
  208. }
  209. iajax(0, '');
  210. }
  211. if ($do == 'download_fans') {
  212. $next_openid = $_GPC['next_openid'];
  213. if (empty($next_openid)) {
  214. pdo_update('mc_mapping_fans', array('follow' => 0), array('uniacid' => $_W['uniacid']));
  215. }
  216. $account_api = WeAccount::createByUniacid();
  217. $wechat_fans_list = $account_api->fansAll();
  218. if (!empty($account_api->same_account_exist)) {
  219. pdo_update('mc_mapping_fans', array('uniacid' => $_W['uniacid'], 'acid' => $_W['acid']), array('uniacid' => array_keys($account_api->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 = uni_setting_load('sync_member');
  252. $force_init_member = empty($sync_member['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), $force_init_member);
  261. }
  262. if ($total == $pageindex) {
  263. setcookie(cache_system_key('sync_fans_pindex', array('uniacid' => $_W['uniacid'])), '', -1);
  264. } else {
  265. setcookie(cache_system_key('sync_fans_pindex', array('uniacid' => $_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. permission_check_account_user('mc_fans_fans_sync_set');
  286. $operate = $_GPC['operate'];
  287. if ($operate == 'save_setting') {
  288. uni_setting_save('sync', intval($_GPC['setting']));
  289. iajax(0, '');
  290. }
  291. $setting = uni_setting($_W['uniacid'], array('sync'));
  292. $sync_setting = $setting['sync'];
  293. }
  294. if ($do == 'register') {
  295. $open_id = trim($_GPC['openid']);
  296. $password = trim($_GPC['password']);
  297. $repassword = trim($_GPC['repassword']);
  298. if (empty($open_id) || empty($password) || empty($repassword)) {
  299. iajax('-1', '参数错误', url('mc/fans/display'));
  300. }
  301. if ($password != $repassword) {
  302. iajax('-1', '密码不一致', url('mc/fans/display'));
  303. }
  304. $member_info = mc_init_fans_info($open_id, true);
  305. $member_salt = pdo_getcolumn('mc_members', array('uid' => $member_info['uid']), 'salt');
  306. $password = md5($password . $member_salt . $_W['config']['setting']['authkey']);
  307. pdo_update('mc_members', array('password' => $password), array('uid' => $uid));
  308. iajax('0', '注册成功', url('mc/member/base_information', array('uid' => $member_info['uid'])));
  309. }
  310. if ($do == 'sync_member') {
  311. $sync_member = $_GPC['sync_member'] == 1 ? 1 : 0;
  312. uni_setting_save('sync_member', $sync_member);
  313. iajax(0, $sync_member);
  314. }
  315. template('mc/fans');