人人商城

stat.ctrl.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. $dos = array('keyword', 'rule', 'history', 'trend', 'del', 'setting', 'browser');
  8. $do = !empty($_GPC['do']) && in_array($do, $dos) ? $do : 'keyword';
  9. if ($do == 'history') {
  10. $_W['page']['title'] = '聊天记录 - 数据统计';
  11. $where = '';
  12. $starttime = empty($_GPC['time']['start']) ? TIMESTAMP - 86400 * 60 : strtotime($_GPC['time']['start']);
  13. $endtime = empty($_GPC['time']['end']) ? TIMESTAMP : strtotime($_GPC['time']['end']) + 86399;
  14. $where .= " AND createtime >= '$starttime' AND createtime < '$endtime'";
  15. !empty($_GPC['keyword']) && $where .= " AND message LIKE '%{$_GPC['keyword']}%'";
  16. switch ($_GPC['searchtype']) {
  17. case 'default':
  18. $where .= " AND module = 'default'";
  19. break;
  20. case 'rule':
  21. default:
  22. $where .= " AND module <> 'default'";
  23. break;
  24. }
  25. $pindex = max(1, intval($_GPC['page']));
  26. $psize = 30;
  27. $list = pdo_fetchall("SELECT * FROM ".tablename('stat_msg_history')." WHERE uniacid = '{$_W['uniacid']}' $where ORDER BY createtime DESC LIMIT ".($pindex - 1) * $psize.','. $psize);
  28. if (!empty($_GPC['export'])) {
  29. $list = pdo_fetchall("SELECT * FROM ".tablename('stat_msg_history')." WHERE uniacid = '{$_W['uniacid']}' $where ORDER BY createtime DESC");
  30. }
  31. if (!empty($list)) {
  32. foreach ($list as $index => &$history) {
  33. if (!empty($history['from_user'])) {
  34. $tag = pdo_fetchcolumn('SELECT tag FROM ' . tablename('mc_mapping_fans') . ' WHERE uniacid = :id AND openid = :oid', array(':id' => $_W['uniacid'], ':oid' => $history['from_user']));
  35. if (is_base64($tag)){
  36. $tag = base64_decode($tag);
  37. }
  38. if (is_serialized($tag)) {
  39. $tag = @iunserializer($tag);
  40. }
  41. $tag = (array)$tag;
  42. $history['from_user'] = $tag['nickname'] ? $tag['nickname'] : $history['from_user'];
  43. }
  44. if ($history['type'] == 'text') {
  45. $history['message'] = iunserializer($history['message']);
  46. $history['module'] = empty($history['message']['source']) ? $history['module'] : $history['message']['source'];
  47. $history['message'] = emotion($history['message']['original']) ? emotion($history['message']['original']) : emotion($history['message']['content']);
  48. } elseif ($history['type'] == 'link') {
  49. $history['message'] = iunserializer($history['message']);
  50. $history['module'] = empty($history['message']['source']) ? $history['message']['content'] : $history['message']['source'];
  51. $history['message'] = '<a href="'.$history['message']['url'].'" target="_blank" title="'.$history['message']['description'].'">'.$history['message']['title'].'</a>';
  52. } elseif ($history['type'] == 'image') {
  53. $history['message'] = '<a href="'.url('platform/stat/browser', array('attach' => $history['message'])).'" class="btn btn-success btn-sm" target="_blank">查看图片</a>';
  54. } elseif ($history['type'] == 'location') {
  55. $history['message'] = iunserializer($history['message']);
  56. $history['message'] = '<a href="http://st.map.soso.com/api?size=800*600&center='.$history['message']['y'].','.$history['message']['x'].'&zoom=16&markers='.$history['message']['y'].','.$history['message']['x'].',1" target="_blank">查看方位</a>';
  57. } elseif ($history['type'] == 'click') {
  58. $history['message'] = emotion($history['message']);
  59. } elseif ($history['type'] == 'view') {
  60. $history['message'] = '<a href="'.$history['message']['url'].'" target="_blank">跳转链接</a>';
  61. } else {
  62. $history['message'] = emotion($history['message']);
  63. }
  64. if (!empty($history['rid'])) {
  65. $rids[$history['rid']] = $history['rid'];
  66. }
  67. }
  68. }
  69. if (!empty($rids)) {
  70. $rules = pdo_fetchall("SELECT name, id, module FROM ".tablename('rule')." WHERE id IN (".implode(',', $rids).")", array(), 'id');
  71. foreach ($rules as $key => &$li) {
  72. if ($li['module'] == 'cover') {
  73. $cover_reply = pdo_fetch('SELECT module,do FROM ' . tablename('cover_reply') . ' WHERE rid = :rid', array(':rid' => $key));
  74. if (!in_array($cover_reply['module'], array('mc', 'site', 'card'))) {
  75. $eid = pdo_fetchcolumn('SELECT eid FROM ' . tablename('modules_bindings') . ' WHERE module = :m AND do = :do AND entry = :entry', array(':m' => $cover_reply['module'], ':do' => $cover_reply['do'], ':entry' => 'cover'));
  76. $li['url'] = url('platform/cover/', array('eid' => $eid));
  77. } else {
  78. $li['url'] = url('platform/cover/' . $cover_reply['module']);
  79. }
  80. $li['module'] = 'cover' . ($cover_reply['module']?'->'.$cover_reply['module']:'');
  81. } else {
  82. $li['url'] = url('platform/reply/post', array('m' => $rules[$key]['module'], 'rid' => $key));
  83. }
  84. }
  85. }
  86. $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('stat_msg_history') . " WHERE uniacid = '{$_W['uniacid']}' $where");
  87. $pager = pagination($total, $pindex, $psize);
  88. if (!empty($_GPC['export'])) {
  89. $header = array(
  90. 'name' => '用户',
  91. 'realname' => '内容',
  92. 'groupid' => '规则',
  93. 'mobile' => '模块',
  94. 'email' => '时间'
  95. );
  96. $keys = array_keys($header);
  97. $html = "\xEF\xBB\xBF";
  98. foreach ($header as $head) {
  99. $html .= $head . "\t ,";
  100. }
  101. $html .= "\n";
  102. if (!empty($list)) {
  103. foreach ($list as $row) {
  104. $nickname = pdo_fetchcolumn("SELECT nickname FROM ". tablename('mc_mapping_fans')." WHERE openid = :openid", array(':openid' => $row['from_user']));
  105. $html .= $nickname. "\t ,";
  106. if ($row['type'] == 'text') {
  107. $row['message'] = iunserializer($row['message']);
  108. } elseif ($row['type'] == 'link') {
  109. $row['message'] = '链接';
  110. } elseif ($row['type'] == 'image') {
  111. $row['message'] = '图片';
  112. } elseif ($row['type'] == 'location') {
  113. $row['message'] = '位置信息';
  114. } elseif ($row['type'] == 'click') {
  115. $row['message'] = '';
  116. } elseif ($row['type'] == 'view') {
  117. $row['message'] = '语音';
  118. } else {
  119. $row['message'] = '表情';
  120. }
  121. $html .= $row['message']. "\t ,";
  122. if (empty($row['rid']) && $row['module']) {
  123. $module = pdo_fetchcolumn("SELECT title from ".tablename('modules')." WHERE name = :name", array(':name' => $row['module']));
  124. $html .= $module. "\t , ";
  125. } elseif (!empty($row['rid'])) {
  126. $html .= $rules[$row['rid']]['name']. "\t , ";
  127. }
  128. if ($row['module'] == 'default') {
  129. $html .= "没有回复规则, \t, ";
  130. } else {
  131. $module = array();
  132. if ($row['module']) {
  133. $module = pdo_fetchcolumn("SELECT title from ".tablename('modules')." WHERE name = :name", array(':name' => $row['module']));
  134. if (!empty($module)) {
  135. $html .= $module. "\t, ";
  136. }
  137. }
  138. if ($rules[$row['rid']]['module'] && empty($module)) {
  139. $row['module'] = str_replace('cover->', '', $rules[$row['rid']]['module']);
  140. $module = pdo_fetchcolumn("SELECT title from ".tablename('modules')." WHERE name = :name", array(':name' => $row['module']));
  141. $html .= $module ." \t, ";
  142. } else {
  143. if (empty($module)){
  144. $html .= "未触发模块". "\t, ";
  145. }
  146. }
  147. }
  148. if ($row['module'] == 'default') {
  149. $html .= "没有回复规则, \t, ";
  150. }
  151. $html .= date('Y-m-d H:i:s', $row['createtime']). "\t , \n";
  152. }
  153. }
  154. header("Content-type:text/csv");
  155. header("Content-Disposition:attachment; filename=聊天记录.csv");
  156. echo $html;
  157. exit();
  158. }
  159. template('platform/stat-history');
  160. }
  161. if ($do == 'del') {
  162. $op = $_GPC['op'] ? trim($_GPC['op']) : itoast('非法访问', '', 'error');
  163. $id = intval($_GPC['id']);
  164. if($op == 'history') {
  165. pdo_delete('stat_msg_history', array('id' => $id, 'uniacid' => $_W['uniacid']));
  166. itoast('删除聊天数据成功', url('platform/stat/history'), 'success');
  167. }
  168. }
  169. if ($do == 'rule') {
  170. $_W['page']['title'] = '回复规则使用情况 - 数据统计';
  171. $foo = !empty($_GPC['foo']) ? $_GPC['foo'] : 'hit';
  172. $where = '';
  173. $starttime = empty($_GPC['time']['start']) ? TIMESTAMP - 86400 * 60 : strtotime($_GPC['time']['start']);
  174. $endtime = empty($_GPC['time']['end']) ? TIMESTAMP : strtotime($_GPC['time']['end']) + 86399;
  175. $where .= " AND createtime >= '$starttime' AND createtime < '$endtime'";
  176. if ($foo == 'hit') {
  177. $pindex = max(1, intval($_GPC['page']));
  178. $psize = 50;
  179. $list = pdo_fetchall("SELECT * FROM ".tablename('stat_rule')." WHERE uniacid = '{$_W['uniacid']}' $where ORDER BY hit DESC LIMIT ".($pindex - 1) * $psize.','. $psize, array(), 'rid');
  180. $rids = array_keys($list);
  181. if (!empty($rids)) {
  182. $rules = pdo_fetchall("SELECT name, id, module FROM ".tablename('rule')." WHERE id IN (".implode(',', $rids).")", array(), 'id');
  183. foreach ($rules as $key => &$li) {
  184. if ($li['module'] == 'cover') {
  185. $cover_reply = pdo_fetch('SELECT module,do FROM ' . tablename('cover_reply') . ' WHERE rid = :rid', array(':rid' => $key));
  186. if (!in_array($cover_reply['module'], array('mc', 'site', 'card'))) {
  187. $eid = pdo_fetchcolumn('SELECT eid FROM ' . tablename('modules_bindings') . ' WHERE module = :m AND do = :do AND entry = :entry', array(':m' => $cover_reply['module'], ':do' => $cover_reply['do'], ':entry' => 'cover'));
  188. $li['url'] = url('platform/cover/', array('eid' => $eid));
  189. } else {
  190. $li['url'] = url('platform/cover/' . $cover_reply['module']);
  191. }
  192. $li['module'] = 'cover' . ($cover_reply['module']?'->'.$cover_reply['module']:'');
  193. } else {
  194. $li['url'] = url('platform/reply/post', array('m' => $rules[$key]['module'], 'rid' => $key));
  195. }
  196. }
  197. }
  198. $total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('stat_rule')." WHERE uniacid = '{$_W['uniacid']}' $where");
  199. $pager = pagination($total, $pindex, $psize);
  200. template('platform/stat-rule_hit');
  201. } elseif ($foo == 'miss') {
  202. $pindex = max(1, intval($_GPC['page']));
  203. $psize = 50;
  204. $list = pdo_fetchall("SELECT name, id, module FROM ".tablename('rule')." WHERE uniacid = '{$_W['uniacid']}' AND id NOT IN (SELECT rid FROM ".tablename('stat_rule')." WHERE uniacid = '{$_W['uniacid']}' $where) LIMIT ".($pindex - 1) * $psize.','. $psize, array(), 'id');
  205. if (!empty($list)) {
  206. foreach ($list as $key => &$li) {
  207. if ($li['module'] == 'cover') {
  208. $cover_reply = pdo_fetch('SELECT module,do FROM ' . tablename('cover_reply') . ' WHERE rid = :rid', array(':rid' => $key));
  209. if (!in_array($cover_reply['module'], array('mc', 'site', 'card'))) {
  210. $eid = pdo_fetchcolumn('SELECT eid FROM ' . tablename('modules_bindings') . ' WHERE module = :m AND do = :do AND entry = :entry', array(':m' => $cover_reply['module'], ':do' => $cover_reply['do'], ':entry' => 'cover'));
  211. $li['url'] = url('platform/cover/', array('eid' => $eid));
  212. } else {
  213. $li['url'] = url('platform/cover/' . $cover_reply['module']);
  214. }
  215. $li['module'] = 'cover' . ($cover_reply['module']?'->'.$cover_reply['module']:'');
  216. } else {
  217. $li['url'] = url('platform/reply/post', array('m' => $li['module'], 'rid' => $key));
  218. }
  219. }
  220. }
  221. $total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('rule')." WHERE uniacid = '{$_W['uniacid']}' AND id NOT IN (SELECT rid FROM ".tablename('stat_rule')." WHERE uniacid = '{$_W['uniacid']}' $where)");
  222. $pager = pagination($total, $pindex, $psize);
  223. template('platform/stat-rule_miss');
  224. }
  225. }
  226. if ($do == 'keyword') {
  227. $_W['page']['title'] = '关键字命中情况 - 数据统计';
  228. $foo = !empty($_GPC['foo']) ? $_GPC['foo'] : 'hit';
  229. $where = '';
  230. $starttime = empty($_GPC['time']['start']) ? TIMESTAMP - 86400 * 60 : strtotime($_GPC['time']['start']);
  231. $endtime = empty($_GPC['time']['end']) ? TIMESTAMP : strtotime($_GPC['time']['end']) + 86399;
  232. $where .= " AND createtime >= '$starttime' AND createtime < '$endtime'";
  233. if ($foo == 'hit') {
  234. $pindex = max(1, intval($_GPC['page']));
  235. $psize = 50;
  236. $list = pdo_fetchall("SELECT * FROM ".tablename('stat_keyword')." WHERE uniacid = '{$_W['uniacid']}' $where ORDER BY hit DESC LIMIT ".($pindex - 1) * $psize.','. $psize);
  237. if (!empty($list)) {
  238. foreach ($list as $index => &$history) {
  239. if (!empty($history['rid'])) {
  240. $rids[$history['rid']] = $history['rid'];
  241. }
  242. $kids[$history['kid']] = $history['kid'];
  243. }
  244. }
  245. if (!empty($rids)) {
  246. $rules = pdo_fetchall("SELECT name, id, module FROM ".tablename('rule')." WHERE id IN (".implode(',', $rids).")", array(), 'id');
  247. foreach ($rules as $key => &$li) {
  248. if ($li['module'] == 'cover') {
  249. $cover_reply = pdo_fetch('SELECT module,do FROM ' . tablename('cover_reply') . ' WHERE rid = :rid', array(':rid' => $key));
  250. if (!in_array($cover_reply['module'], array('mc', 'site', 'card'))) {
  251. $eid = pdo_fetchcolumn('SELECT eid FROM ' . tablename('modules_bindings') . ' WHERE module = :m AND do = :do AND entry = :entry', array(':m' => $cover_reply['module'], ':do' => $cover_reply['do'], ':entry' => 'cover'));
  252. $li['url'] = url('platform/cover/', array('eid' => $eid));
  253. } else {
  254. $li['url'] = url('platform/cover/' . $cover_reply['module']);
  255. }
  256. $li['module'] = 'cover' . ($cover_reply['module']?'->'.$cover_reply['module']:'');
  257. } else {
  258. $li['url'] = url('platform/reply/post', array('m' => $rules[$key]['module'], 'rid' => $key));
  259. }
  260. }
  261. }
  262. if (!empty($kids)) {
  263. $keywords = pdo_fetchall("SELECT content, id FROM ".tablename('rule_keyword')." WHERE id IN (".implode(',', $kids).")", array(), 'id');
  264. }
  265. $total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('stat_keyword')." WHERE uniacid = '{$_W['uniacid']}' $where");
  266. $pager = pagination($total, $pindex, $psize);
  267. template('platform/stat-keyword_hit');
  268. } elseif ($foo == 'miss') {
  269. $pindex = max(1, intval($_GPC['page']));
  270. $psize = 50;
  271. $list = pdo_fetchall("SELECT content, id, module, rid FROM ".tablename('rule_keyword')." WHERE uniacid = '{$_W['uniacid']}' AND id NOT IN (SELECT kid FROM ".tablename('stat_keyword')." WHERE uniacid = '{$_W['uniacid']}' $where) LIMIT ".($pindex - 1) * $psize.','. $psize);
  272. if (!empty($list)) {
  273. foreach ($list as $index => $row) {
  274. if (!empty($row['rid'])) {
  275. $rids[$row['rid']] = $row['rid'];
  276. }
  277. }
  278. }
  279. if (!empty($rids)) {
  280. $rules = pdo_fetchall("SELECT name, id, module FROM ".tablename('rule')." WHERE id IN (".implode(',', $rids).")", array(), 'id');
  281. foreach($rules as $key => &$li) {
  282. if ($li['module'] == 'cover') {
  283. $cover_reply = pdo_fetch('SELECT module,do FROM ' . tablename('cover_reply') . ' WHERE rid = :rid', array(':rid' => $key));
  284. if (!in_array($cover_reply['module'], array('mc', 'site', 'card'))) {
  285. $eid = pdo_fetchcolumn('SELECT eid FROM ' . tablename('modules_bindings') . ' WHERE module = :m AND do = :do AND entry = :entry', array(':m' => $cover_reply['module'], ':do' => $cover_reply['do'], ':entry' => 'cover'));
  286. $li['url'] = url('platform/cover/', array('eid' => $eid));
  287. } else {
  288. $li['url'] = url('platform/cover/' . $cover_reply['module']);
  289. }
  290. $li['module'] = 'cover' . ($cover_reply['module']?'->'.$cover_reply['module']:'');
  291. } else {
  292. $li['url'] = url('platform/reply/post', array('m' => $rules[$key]['module'], 'rid' => $key));
  293. }
  294. }
  295. }
  296. $total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('rule_keyword')." WHERE uniacid = '{$_W['uniacid']}' AND id NOT IN (SELECT kid FROM ".tablename('stat_keyword')." WHERE uniacid = '{$_W['uniacid']}' $where)");
  297. $pager = pagination($total, $pindex, $psize);
  298. template('platform/stat-keyword_miss');
  299. }
  300. }
  301. if ($do == 'setting') {
  302. $_W['page']['title'] = '参数 - 数据统计';
  303. $settings = uni_setting($_W['uniacid'], array('stat'));
  304. $settings = $settings['stat'];
  305. $default = array('msg_history' => '1','msg_maxday' => '30','use_ratio' => '1');
  306. $settings = empty($settings) ? $default : $settings;
  307. if (checksubmit('submit')) {
  308. $stat = array();
  309. $stat['msg_history'] = intval($_GPC['msg_history']);
  310. $stat['msg_maxday'] = intval($_GPC['msg_maxday']);
  311. $stat['use_ratio'] = intval($_GPC['use_ratio']);
  312. $stat = iserializer($stat);
  313. pdo_update('uni_settings', array('stat' => $stat), array('uniacid' => $_W['uniacid']));
  314. cache_delete(cache_system_key('unisetting', array('uniacid' => $_W['uniacid'])));
  315. itoast('设置参数成功', 'refresh', 'success');
  316. }
  317. template('platform/stat-setting');
  318. }
  319. if ($do == 'trend') {
  320. $_W['page']['title'] = '关键指标详解 - 数据统计';
  321. $id = intval($_GPC['id']);
  322. $starttime = empty($_GPC['time']['start']) ? strtotime(date('Y-m-d')) - 7 * 86400 : strtotime($_GPC['time']['start']);
  323. $endtime = empty($_GPC['time']['end']) ? TIMESTAMP : strtotime($_GPC['time']['end']) + 86399;
  324. $list = pdo_fetchall("SELECT createtime, hit FROM " . tablename('stat_rule') . " WHERE uniacid = '{$_W['uniacid']}' AND rid = :rid AND createtime >= :createtime AND createtime <= :endtime ORDER BY createtime ASC", array(':rid' => $id, ':createtime' => $starttime, ':endtime' => $endtime));
  325. $day = $hit = array();
  326. if (!empty($list)) {
  327. foreach ($list as $row) {
  328. $day[] = date('m-d', $row['createtime']);
  329. $hit[] = intval($row['hit']);
  330. }
  331. }
  332. for ($i = 0; $i = count($hit) < 2; $i++) {
  333. $day[] = date('m-d', $endtime);
  334. $hit[] = $day[$i] == date('m-d', $endtime) ? $hit[0] : '0';
  335. }
  336. $list = pdo_fetchall("SELECT createtime, hit, rid, kid FROM " . tablename('stat_keyword') . " WHERE uniacid = '{$_W['uniacid']}' AND rid = :rid AND createtime >= :createtime AND createtime <= :endtime ORDER BY createtime ASC", array(':rid' => $id, ':createtime' => $starttime, ':endtime' => $endtime));
  337. if (!empty($list)) {
  338. foreach ($list as $row) {
  339. $keywords[$row['kid']]['hit'][] = $row['hit'];
  340. $keywords[$row['kid']]['day'][] = date('m-d', $row['createtime']);
  341. }
  342. foreach ($keywords as &$value) {
  343. if (count($value['hit']) < 2) {
  344. $value['hit'][] = $value['day'][0] == date('m-d', $endtime) ? $value['hit'][0] : '0';
  345. $value['day'][] = date('m-d', $endtime);
  346. }
  347. }
  348. $keywordnames = pdo_fetchall("SELECT content, id FROM " . tablename('rule_keyword') . " WHERE id IN (" . implode(',', array_keys($keywords)) . ")", array(), 'id');
  349. }
  350. template('platform/stat-trend');
  351. }
  352. if ($do == 'browser') {
  353. $attachment = $_GPC['attach'];
  354. load()->func('communication');
  355. $content = ihttp_request($attachment, '', array('CURLOPT_REFERER' => 'http://www.qq.com'));
  356. header('Content-Type:image/png');
  357. echo $content['content'];
  358. }