PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/web/source/mc/fans.ctrl.php

https://gitlab.com/llqbaimao/o2o
PHP | 346 lines | 328 code | 14 blank | 4 comment | 114 complexity | e8379a6ab5abfe715663560e493f2c19 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  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()->model('mc');
  8. $dos = array('display', 'view', 'initsync', 'updategroup', 'sms');
  9. $do = in_array($do, $dos) ? $do : 'display';
  10. if($do == 'display') {
  11. $_W['page']['title'] = '粉丝列表 - 粉丝 - 会员中心';
  12. if(checksubmit('submit')) {
  13. if (!empty($_GPC['delete'])) {
  14. $fanids = array();
  15. foreach($_GPC['delete'] as $v) {
  16. $fanids[] = intval($v);
  17. }
  18. pdo_query("DELETE FROM " . tablename('mc_mapping_fans') . " WHERE uniacid = :uniacid AND fanid IN ('" . implode("','", $fanids) . "')",array(':uniacid' => $_W['uniacid']));
  19. message('粉丝删除成功!', url('mc/fans/', array('type' => $_GPC['type'], 'acid' => $_GPC['acid'])), 'success');
  20. }
  21. }
  22. $accounts = uni_accounts();
  23. if(empty($accounts) || !is_array($accounts) || count($accounts) == 0){
  24. message('请指定公众号');
  25. }
  26. if(!isset($_GPC['acid'])){
  27. $account = current($accounts);
  28. if($account !== false){
  29. $acid = intval($account['acid']);
  30. }
  31. } else {
  32. $acid = intval($_GPC['acid']);
  33. if(!empty($acid) && !empty($accounts[$acid])) {
  34. $account = $accounts[$acid];
  35. }
  36. }
  37. reset($accounts);
  38. if($_W['isajax']) {
  39. $post = $_GPC['__input'];
  40. if($post['method'] == 'sync') {
  41. if(is_array($post['fanids'])) {
  42. $fanids = array();
  43. foreach($post['fanids'] as $fanid) {
  44. $fanid = intval($fanid);
  45. $fanids[] = $fanid;
  46. }
  47. $fanids = implode(',', $fanids);
  48. $sql = 'SELECT `fanid`,`uid`,`openid` FROM ' . tablename('mc_mapping_fans') . " WHERE `acid`='{$acid}' AND `fanid` IN ({$fanids})";
  49. $ds = pdo_fetchall($sql);
  50. $acc = WeAccount::create($acid);
  51. foreach($ds as $row) {
  52. $fan = $acc->fansQueryInfo($row['openid'], true);
  53. if(!is_error($fan)) {
  54. $group = $acc->fetchFansGroupid($row['openid']);
  55. $record = array();
  56. if(!is_error($group)) {
  57. $record['groupid'] = $group['groupid'];
  58. }
  59. $record['updatetime'] = TIMESTAMP;
  60. $record['followtime'] = $fan['subscribe_time'];
  61. $fan['nickname'] = stripcslashes($fan['nickname']);
  62. $record['nickname'] = stripslashes($fan['nickname']);
  63. $record['tag'] = iserializer($fan);
  64. $record['tag'] = base64_encode($record['tag']);
  65. pdo_update('mc_mapping_fans', $record, array('fanid' => $row['fanid']));
  66. if(!empty($row['uid'])) {
  67. $user = mc_fetch($row['uid'], array('nickname', 'gender', 'residecity', 'resideprovince', 'nationality', 'avatar'));
  68. $rec = array();
  69. if(empty($user['nickname']) && !empty($fan['nickname'])) {
  70. $rec['nickname'] = stripslashes($fan['nickname']);
  71. }
  72. if(empty($user['gender']) && !empty($fan['sex'])) {
  73. $rec['gender'] = $fan['sex'];
  74. }
  75. if(empty($user['residecity']) && !empty($fan['city'])) {
  76. $rec['residecity'] = $fan['city'] . '市';
  77. }
  78. if(empty($user['resideprovince']) && !empty($fan['province'])) {
  79. $rec['resideprovince'] = $fan['province'] . '省';
  80. }
  81. if(empty($user['nationality']) && !empty($fan['country'])) {
  82. $rec['nationality'] = $fan['country'];
  83. }
  84. if(empty($user['avatar']) && !empty($fan['headimgurl'])) {
  85. $rec['avatar'] = rtrim($fan['headimgurl'], '0') . 132;
  86. }
  87. if(!empty($rec)) {
  88. pdo_update('mc_members', $rec, array('uid' => $row['uid']));
  89. }
  90. }
  91. }
  92. }
  93. }
  94. exit('success');
  95. }
  96. if($post['method'] == 'download') {
  97. $acc = WeAccount::create($acid);
  98. if(!empty($post['next'])) {
  99. $_GPC['next_openid'] = $post['next'];
  100. }
  101. $fans = $acc->fansAll();
  102. if(!is_error($fans) && is_array($fans['fans'])) {
  103. $count = count($fans['fans']);
  104. $buffSize = ceil($count / 500);
  105. for($i = 0; $i < $buffSize; $i++) {
  106. $buffer = array_slice($fans['fans'], $i * 500, 500);
  107. $openids = implode("','", $buffer);
  108. $openids = "'{$openids}'";
  109. $sql = 'SELECT `openid` FROM ' . tablename('mc_mapping_fans') . " WHERE `acid`={$acid} AND `openid` IN ({$openids})";
  110. $ds = pdo_fetchall($sql);
  111. $exists = array();
  112. foreach($ds as $row) {
  113. $exists[] = $row['openid'];
  114. }
  115. $sql = '';
  116. foreach($buffer as $openid) {
  117. if(!empty($exists) && in_array($openid, $exists)) {
  118. continue;
  119. }
  120. $salt = random(8);
  121. $sql .= "('{$acid}', '{$_W['uniacid']}', 0, '{$openid}', '{$salt}', 1, 0, ''),";
  122. }
  123. if(!empty($sql)) {
  124. $sql = rtrim($sql, ',');
  125. $sql = 'INSERT INTO ' . tablename('mc_mapping_fans') . ' (`acid`, `uniacid`, `uid`, `openid`, `salt`, `follow`, `followtime`, `tag`) VALUES ' . $sql;
  126. pdo_query($sql);
  127. }
  128. }
  129. $ret = array();
  130. $ret['total'] = $fans['total'];
  131. $ret['count'] = count($fans['fans']) + 2;
  132. if(!empty($fans['next'])) {
  133. $ret['next'] = $fans['next'];
  134. }
  135. exit(json_encode($ret));
  136. } else {
  137. exit(json_encode($fans));
  138. }
  139. }
  140. }
  141. $pindex = max(1, intval($_GPC['page']));
  142. $psize = 20;
  143. $condition = ' WHERE `uniacid`=:uniacid';
  144. $pars = array();
  145. $pars[':uniacid'] = $_W['uniacid'];
  146. if(!empty($acid)) {
  147. $condition .= ' AND `acid`=:acid';
  148. $pars[':acid'] = $acid;
  149. }
  150. if($_GPC['type'] == 'bind') {
  151. $condition .= ' AND `uid`>0';
  152. $type = 'bind';
  153. }
  154. if($_GPC['type'] == 'unbind') {
  155. $condition .= ' AND `uid`=0';
  156. $type = 'unbind';
  157. }
  158. $nickname = trim($_GPC['nickname']);
  159. if(!empty($nickname)) {
  160. $condition .= " AND nickname LIKE '%{$nickname}%'";
  161. }
  162. $groups_data = pdo_fetchall('SELECT * FROM ' . tablename('mc_fans_groups') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
  163. if(!empty($groups_data)) {
  164. $groups = array();
  165. foreach($groups_data as $gr) {
  166. $groups[$gr['acid']] = iunserializer($gr['groups']);
  167. }
  168. }
  169. $total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('mc_mapping_fans').$condition, $pars);
  170. $list = pdo_fetchall("SELECT * FROM ".tablename('mc_mapping_fans') . $condition ." ORDER BY `fanid` DESC LIMIT ".($pindex - 1) * $psize.','.$psize, $pars);
  171. if(!empty($list)) {
  172. foreach($list as &$v) {
  173. if(!empty($v['uid'])) {
  174. $user = mc_fetch($v['uid'], array('realname', 'nickname', 'mobile', 'email', 'avatar'));
  175. if(!empty($user['avatar'])){
  176. $user['avatar'] = tomedia($user['avatar']);
  177. }
  178. }
  179. if (!empty($v['tag']) && is_string($v['tag'])) {
  180. if (is_base64($v['tag'])){
  181. $v['tag'] = base64_decode($v['tag']);
  182. }
  183. if (is_serialized($v['tag'])) {
  184. $v['tag'] = @iunserializer($v['tag']);
  185. }
  186. if(!empty($v['tag']['headimgurl'])) {
  187. $v['tag']['avatar'] = tomedia($v['tag']['headimgurl']);
  188. unset($v['tag']['headimgurl']);
  189. }
  190. }
  191. if(empty($v['tag'])) {
  192. $v['tag'] = array();
  193. }
  194. if(!empty($user)) {
  195. $niemmo = $user['realname'];
  196. if(empty($niemmo)) {
  197. $niemmo = $user['nickname'];
  198. }
  199. if(empty($niemmo)) {
  200. $niemmo = $user['mobile'];
  201. }
  202. if(empty($niemmo)) {
  203. $niemmo = $user['email'];
  204. }
  205. if(empty($niemmo) || (!empty($niemmo) && substr($niemmo, -6) == 'we7.cc' && strlen($niemmo) == 39)) {
  206. $niemmo_effective = 0;
  207. } else {
  208. $niemmo_effective = 1;
  209. }
  210. $v['user'] = array('niemmo_effective' => $niemmo_effective, 'niemmo' => $niemmo, 'nickname' => $user['nickname']);
  211. }
  212. if(empty($v['user']['nickname']) && !empty($v['tag']['nickname'])){
  213. $v['user']['nickname'] = $v['tag']['nickname'];
  214. }
  215. if(empty($v['user']['avatar']) && !empty($v['tag']['avatar'])){
  216. $v['user']['avatar'] = $v['tag']['avatar'];
  217. }
  218. $v['account'] = $accounts[$v['acid']]['name'];
  219. unset($user,$niemmo,$niemmo_effective);
  220. }
  221. }
  222. $pager = pagination($total, $pindex, $psize);
  223. }
  224. if($do == 'view') {
  225. $_W['page']['title'] = '粉丝详情 - 粉丝 - 会员中心';
  226. $fanid = intval($_GPC['id']);
  227. if(empty($fanid)) {
  228. message('访问错误.');
  229. }
  230. $row = pdo_fetch("SELECT * FROM ".tablename('mc_mapping_fans')." WHERE fanid = :fanid AND uniacid = :uniacid LIMIT 1", array(':fanid' => $fanid,':uniacid' => $_W['uniacid']));
  231. $account = WeAccount::create($row['acid']);
  232. $accountInfo = $account->fetchAccountInfo();
  233. $row['account'] = $accountInfo['name'];
  234. if(!empty($row['uid'])) {
  235. $user = mc_fetch($row['uid'], array('nickname', 'mobile', 'email'));
  236. $row['user'] = $user['nickname'];
  237. if(empty($row['user'])) {
  238. $row['user'] = $user['mobile'];
  239. }
  240. if(empty($row['user'])) {
  241. $row['user'] = $user['email'];
  242. }
  243. } else {
  244. $row['user'] = '还未登记为会员';
  245. }
  246. }
  247. if($do == 'initsync') {
  248. $acid = intval($_GPC['acid']);
  249. if(intval($_GPC['page']) == 0) {
  250. message('正在更新粉丝数据,请不要关闭浏览器', url('mc/fans/initsync', array('page' => 1, 'acid' => $acid)), 'success');
  251. }
  252. $pindex = max(1, intval($_GPC['page']));
  253. $psize = 50;
  254. $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('mc_mapping_fans') . " WHERE uniacid = :uniacid AND acid = :acid", array(':uniacid' => $_W['uniacid'], ':acid' => $acid));
  255. $total_page = ceil($total / $psize);
  256. $ds = pdo_fetchall("SELECT * FROM ".tablename('mc_mapping_fans') ." WHERE uniacid = :uniacid AND acid = :acid ORDER BY `fanid` DESC LIMIT ".($pindex - 1) * $psize.','.$psize, array(':uniacid' => $_W['uniacid'], ':acid' => $acid));
  257. $acc = WeAccount::create($acid);
  258. if(!empty($ds)) {
  259. foreach($ds as $row) {
  260. if(!empty($row['tag'])) {
  261. continue;
  262. }
  263. $fan = $acc->fansQueryInfo($row['openid'], true);
  264. if(!is_error($fan)) {
  265. $group = $acc->fetchFansGroupid($row['openid']);
  266. $record = array();
  267. if(!is_error($group)) {
  268. $record['groupid'] = $group['groupid'];
  269. }
  270. $record['updatetime'] = TIMESTAMP;
  271. $record['followtime'] = $fan['subscribe_time'];
  272. $fan['nickname'] = stripcslashes($fan['nickname']);
  273. $record['nickname'] = stripslashes($fan['nickname']);
  274. $record['tag'] = iserializer($fan);
  275. $record['tag'] = base64_encode($record['tag']);
  276. pdo_update('mc_mapping_fans', $record, array('fanid' => $row['fanid']));
  277. if(!empty($row['uid'])) {
  278. $user = mc_fetch($row['uid'], array('nickname', 'gender', 'residecity', 'resideprovince', 'nationality', 'avatar'));
  279. $rec = array();
  280. if(empty($user['nickname']) && !empty($fan['nickname'])) {
  281. $rec['nickname'] = stripslashes($fan['nickname']);
  282. }
  283. if(empty($user['gender']) && !empty($fan['sex'])) {
  284. $rec['gender'] = $fan['sex'];
  285. }
  286. if(empty($user['residecity']) && !empty($fan['city'])) {
  287. $rec['residecity'] = $fan['city'] . '市';
  288. }
  289. if(empty($user['resideprovince']) && !empty($fan['province'])) {
  290. $rec['resideprovince'] = $fan['province'] . '省';
  291. }
  292. if(empty($user['nationality']) && !empty($fan['country'])) {
  293. $rec['nationality'] = $fan['country'];
  294. }
  295. if(empty($user['avatar']) && !empty($fan['headimgurl'])) {
  296. $rec['avatar'] = rtrim($fan['headimgurl'], '0') . 132;
  297. }
  298. if(!empty($rec)) {
  299. pdo_update('mc_members', $rec, array('uid' => $row['uid']));
  300. }
  301. }
  302. }
  303. }
  304. }
  305. $pindex++;
  306. $log = ($pindex - 1) * $psize;
  307. if($pindex > $total_page) {
  308. message('粉丝数据更新完成', url('mc/fans'), 'success');
  309. } else {
  310. message('正在更新粉丝数据,请不要关闭浏览器,已完成更新 ' . $log . ' 条数据。', url('mc/fans/initsync', array('page' => $pindex, 'acid' => $acid)));
  311. }
  312. }
  313. if($do == 'updategroup') {
  314. if($_W['isajax']) {
  315. $acid = intval($_GPC['acid']);
  316. $groupid = intval($_GPC['groupid']);
  317. $openid = trim($_GPC['openid']);
  318. if($acid > 0 && !empty($openid)) {
  319. $acc = WeAccount::create($acid);
  320. $data = $acc->updateFansGroupid($openid, $groupid);
  321. if(is_error($data)) {
  322. exit(json_encode(array('status' => 'error', 'mess' => $data['message'])));
  323. } else {
  324. pdo_update('mc_mapping_fans', array('groupid' => $groupid), array('uniacid' => $_W['uniacid'], 'openid' => $openid));
  325. exit(json_encode(array('status' => 'success')));
  326. }
  327. } else {
  328. exit(json_encode(array('status' => 'error', 'mess' => '公众号信息和粉丝openid错误')));
  329. }
  330. }
  331. }
  332. template('mc/fans');