diff --git a/sql/kefudb.sql b/sql/kefudb.sql index 1e2272d..f9aba8e 100644 --- a/sql/kefudb.sql +++ b/sql/kefudb.sql @@ -42,8 +42,9 @@ CREATE TABLE `accounts` ( `accountid` varchar(60) DEFAULT '' COMMENT '账号id(channel + "_" + gameid + "_" + openid)', `channel` int(11) NOT NULL COMMENT 'channel', `gameid` int(11) NOT NULL COMMENT 'gameid', - `openid` varchar(50) NOT NULL COMMENT '平台openid', + `openid` varchar(50) NOT NULL COMMENT '平台', `awardtime` int(11) NOT NULL DEFAULT '0' COMMENT '最后领奖时间', + `activetime` int(11) NOT NULL DEFAULT '0' COMMENT '最后交互时间', PRIMARY KEY (`idx`), UNIQUE KEY `accountid` (`accountid`), UNIQUE KEY `channel_gameid_openid_index` (`channel`,`gameid`,`openid`), diff --git a/sql/kefudb_n_migrate_191023_01.sql b/sql/kefudb_n_migrate_191023_01.sql new file mode 100644 index 0000000..c90ff78 --- /dev/null +++ b/sql/kefudb_n_migrate_191023_01.sql @@ -0,0 +1,8 @@ +begin; + +alter table accounts add `activetime` int(11) NOT NULL DEFAULT '0' COMMENT '最后交互时间'; +UPDATE accounts set activetime=unix_timestamp(now()) WHERE 1=1; + +insert into version (version) values(2019102301); + +commit; diff --git a/webapp/controller/KefuController.class.php b/webapp/controller/KefuController.class.php index 6fa27e7..1f53d1f 100644 --- a/webapp/controller/KefuController.class.php +++ b/webapp/controller/KefuController.class.php @@ -275,7 +275,7 @@ class KefuController { $last_idx = 0; $share_conf = require('../config/kefu.share.config.php'); while (true) { - $rows = $conn->execQuery('SELECT idx, accountid, openid, awardtime, gameid ' . + $rows = $conn->execQuery('SELECT idx, accountid, openid, awardtime, gameid, activetime ' . ' FROM accounts WHERE idx > :last_idx LIMIT 0, 1000;', array( ':last_idx' => $last_idx, @@ -284,19 +284,21 @@ class KefuController { break; } foreach ($rows as $row) { - if (isset($share_conf[$row['gameid']])) { - $game_conf = $share_conf[$row['gameid']]; - if (!isset($media_conf[$row['gameid']])) { - $media_conf[$row['gameid']] = $this->getMediaId($row['gameid']); - } - $game_conf['thumb_media_id'] = $media_conf[$row['gameid']]; - $this->sendKefuMsg($row['accountid'], - array ( - "touser" => $row['openid'], - "msgtype" => "miniprogrampage", - "miniprogrampage" => $game_conf - )); + if (time() - $row['activetime'] < 3600) { + if (isset($share_conf[$row['gameid']])) { + $game_conf = $share_conf[$row['gameid']]; + if (!isset($media_conf[$row['gameid']])) { + $media_conf[$row['gameid']] = $this->getMediaId($row['gameid']); + } + $game_conf['thumb_media_id'] = $media_conf[$row['gameid']]; + $this->sendKefuMsg($row['accountid'], + array ( + "touser" => $row['openid'], + "msgtype" => "miniprogrampage", + "miniprogrampage" => $game_conf + )); + } } if ($row['idx'] > $last_idx) { $last_idx = $row['idx']; @@ -328,7 +330,7 @@ class KefuController { return; } $conn = $this->getMysql($accountid); - $row = $conn->execQueryOne('SELECT awardtime ' . + $row = $conn->execQueryOne('SELECT awardtime, activetime ' . ' FROM accounts WHERE accountid = :accountid;', array( ':accountid' => $accountid, @@ -352,11 +354,12 @@ class KefuController { } if ($this->sendAwardMail($accountid, $awardConfig)) { $ret = $conn->execScript('UPDATE accounts SET ' . - ' awardtime=:awardtime ' . + ' awardtime=:awardtime, activetime=:activetime ' . ' WHERE accountid=:accountid; ', array( ':accountid' => $accountid, - ':awardtime' => time() + ':awardtime' => time(), + ':activetime' => time() )); if ($ret) { $this->sendKefuMsg($accountid, array ( @@ -372,8 +375,8 @@ class KefuController { { if ($msg['Event'] == 'user_enter_tempsession') { $conn = $this->getMysql($accountid); - $ret = $conn->execScript('INSERT INTO accounts(accountid, channel, gameid, openid, awardtime) ' . - 'SELECT :accountid, :channel, :gameid, :openid, :awardtime ' . + $ret = $conn->execScript('INSERT INTO accounts(accountid, channel, gameid, openid, awardtime, activetime) ' . + 'SELECT :accountid, :channel, :gameid, :openid, :awardtime, :activetime ' . 'FROM DUAL ' . 'WHERE NOT EXISTS(SELECT accountid FROM accounts WHERE accountid=:accountid);', array( @@ -381,7 +384,8 @@ class KefuController { ':channel' => WEIXIN_CHANNEL, ':gameid' => phpcommon\extractGameId($accountid), ':openid' => $msg['FromUserName'], - ':awardtime' => 0 + ':awardtime' => 0, + ':activetime' => 0 )); if ($ret) { $this->sendKefuMsg($accountid, array(